123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace App\Admin\Actions\Community\Notice;
- use App\Models\OrderPatient;
- use App\Models\Patient;
- use App\User;
- use EasyWeChat\Factory;
- use Encore\Admin\Actions\BatchAction;
- use Illuminate\Database\Eloquent\Collection;
- class sendNotice extends BatchAction
- {
- public $name = '群发通知';
- public function handle(Collection $collection)
- {
- $info = $collection->toArray();
- $ids = array_unique(array_column($info,'id'));
- foreach ($ids as $id) {
- $user_id = Patient::where('id',$id)->value('user_id');
- if(empty($user_id)) continue;
- $openid = User::where('id',$user_id)->value('openid');
- $content = request('content');
- $remark= request('remark');
- $stime= request('stime');
- $type= request('type',1);
- // $openid = 'oYmUA5A1OIqtpA1XSrw35tbjtv1w';
- $service_arr = [1=>'儿保服务',2=>'疫苗接种服务'];
- $service_name = $service_arr[$type];
- //没有openid 直接不发送
- if(empty($openid)) continue;
- $template_arr = [
- 1=>'CP3AxrgS-cbW1da8QlIDFcxd-H0RStMEuXdqNRePLoc',
- 2=>'xpcaMFXI0Kc9U12o3D6CGPa7ASTpOZJwXJm2mlip6Zo'
- ];
- $tempId = $template_arr[$type];
- $msg['content'] = $content;
- $msg['service_date'] = $stime;
- $msg['remark'] = $remark;
- $msg['service_name'] = $service_name;
- $res = $this->send($openid,$tempId,$msg);
- }
- return $this->response()->success('发送成功')->refresh();
- }
- public function form()
- {
- // xpcaMFXI0Kc9U12o3D6CGPa7ASTpOZJwXJm2mlip6Zo 接种服务提醒
- // first,keyword1,keword2,remark;
- // CP3AxrgS-cbW1da8QlIDFcxd-H0RStMEuXdqNRePLoc 儿保服务提醒
- // 服务项目:XX疫苗接种
- // 服务日期:后台设置的服务时间
- $this->textarea('content','内容')->rows(4);
- $this->select('type','服务类型')->options([1=>'儿保',2=>'计免']);
- $this->date('stime','服务时间');
- $this->textarea('remark','备注')->rows(3);
- }
- public function send($open_id,$tempId,$msg)
- {
- $template = [
- 'touser' => $open_id,
- 'mp_template_msg' => [
- 'appid' => env('OFFICIAL_WECHAT_APPID'),
- 'template_id' => $tempId,
- 'url' => '',
- 'miniprogram' => [
- 'appid' => env('WECHAT_APPID', 'wxd41dd232837996c4'),
- 'pagepath' => '',
- ],
- 'data' => [
- 'first' => [
- 'value' => $msg['content'],
- ],
- 'keyword1' => [
- 'value' => $msg['service_name'],
- ],
- 'keyword2' => [
- 'value' => $msg['service_date'],
- ],
- 'remark' => [
- 'value' => $msg['remark'],
- ],
- ],
- ],
- ];
- $app = Factory::miniProgram(config('config.wechat_small_program'));
- $app->uniform_message->send($template);
- return true;
- }
- public function send2($user_ids,$number,$content){
- if(!is_array($user_ids)) {
- $user_ids = [$user_ids];
- }
- $app = app('wechat.official_account');
- foreach($user_ids as $user_id) {
- $user = (new User())->find($user_id);
- if(empty($user) || empty($user['open_id'])) continue;
- $official_open_id = $user->getOfficialOpenId($user['union_id']);
- if(empty($official_open_id)) continue;
- $result=$app->template_message->send([
- 'touser' => $official_open_id,
- 'template_id' => '4x80cGpV2boFdY7fm4QpsPBNo-FLytKjQGJKtZq_nr4',
- 'miniprogram' => [
- 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
- 'pagepath' => 'pages/index/index',
- ],
- 'data' => [
- 'first' => '作业许可状态改变',
- 'keyword1' => $number,
- 'keyword2' => $content,
- 'keyword3' => $user->name,
- 'remark' => '备注'
- ],
- ]);
- var_dump($result);
- }
- return true;
- }
- }
|