query(); $user = Admin::user(); $patient_ids = Order::where('organization_id', $user['org_id'])->pluck('patient_id')->toArray(); $builder = Patient::whereIn('id', $patient_ids)->orderBy('id', 'desc'); if (!empty($req['user'])) { $userIds = User::where('nickname', 'like', '%'.$req['user'].'%')->pluck('id')->toArray(); $builder->whereIn('user_id', $userIds); } if (!empty($req['name'])) { $builder->where('name', 'like', '%'.$req['name'].'%'); } if (!empty($req['guardian_name'])) { $builder->where('guardian_name', 'like', '%'.$req['guardian_name'].'%'); } if (!empty($req['phone'])) { $builder->where('phone', $req['phone']); } if (!empty($req['product_type'])) { $user_ids = Order::where('product_type', $req['product_type'])->distinct('user_id')->pluck('user_id'); $builder->whereIn('user_id', $user_ids); } $list = $builder->paginate(); $data['req'] = $req; $data['list'] = $list; $data['ids'] = Cache::get('notice-'.$user['id']) ?? []; $content = new Content(); Admin::disablePjax(); return $content->title('群发通知')->view('cdms/notice_list', $data); } public function saveIds() { $req = request()->post(); $user = Admin::user(); $arr = Cache::get($req['type'].'-'.$user['id']) ?? []; foreach ($req['ids'] as $k => $v) { if (isset($arr[$v])) { unset($arr[$v]); } else { $arr[$v] = $v; } } Cache::set($req['type'].'-'.$user['id'], $arr, 300); return out(); } public function sendNotice() { $req = request()->post(); admin_validate($req, [ 'content|内容' => 'required', 'type|服务类型' => 'required', 'stime|服务时间' => 'required', 'remark|备注' => 'required', ]); $user = Admin::user(); $arr = Cache::get('notice-'.$user['id']) ?? []; $ids = array_values(array_unique($arr)); if (empty($ids)) { return out(null, 10001, '请勾选要发送通知的用户'); } Cache::delete('notice-'.$user['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'); if(empty($openid)) continue; $content = $req['content']; $remark= $req['remark']; $stime= $req['stime']; $type= $req['type']; $service_arr = [4 => '疫苗接种服务', 5 => '儿保服务']; $service_name = $service_arr[$type]; $template_arr = [ 4 => 'xpcaMFXI0Kc9U12o3D6CGPa7ASTpOZJwXJm2mlip6Zo', 5 => 'CP3AxrgS-cbW1da8QlIDFcxd-H0RStMEuXdqNRePLoc', ]; $tempId = $template_arr[$type]; $msg['content'] = $content; $msg['service_date'] = $stime; $msg['remark'] = $remark; $msg['service_name'] = $service_name; $this->send($openid, $tempId, $msg); } return out(); } public function getCacheUsers() { $req = request()->post(); $user = Admin::user(); $ids = Cache::get($req['type'].'-'.$user['id']) ?? []; $nameText = ''; if (!empty($ids)) { $nameArr = Patient::whereIn('id', $ids)->pluck('name')->toArray(); $nameText = implode(';', $nameArr); } return out($nameText); } private function send($open_id, $tempId, $msg) { try { $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')); $ret = $app->uniform_message->send($template); if (isset($ret['errcode']) && $ret['errcode'] != 0) { trace(['后台发送消息通知失败,请求参数' => $template, '返回数据' => $ret], 'error'); } } catch (Exception $e) { trace(['后台发送消息通知失败' => $e->getMessage(), '请求参数' => $template ?? '', '返回数据' => $ret ?? ''], 'error'); } return true; } }