From 77dd5c933145ead16df8376a67e73ba3438f0dcc Mon Sep 17 00:00:00 2001 From: wucongxing <815046773@qq.com> Date: Mon, 25 Sep 2023 10:36:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eim=E6=B6=88=E6=81=AF=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E6=97=B6=E6=8E=A8=E9=80=81=E8=AE=A2=E9=98=85=E6=B6=88?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CallBackController.php | 280 ++------------------------ app/Services/MessagePush.php | 117 +++++++++++ app/Services/UserService.php | 75 ++++++- 3 files changed, 209 insertions(+), 263 deletions(-) diff --git a/app/Controller/CallBackController.php b/app/Controller/CallBackController.php index 5c70361..54e5c9d 100644 --- a/app/Controller/CallBackController.php +++ b/app/Controller/CallBackController.php @@ -29,14 +29,17 @@ use App\Model\OrderProductRefund; use App\Model\PatientFamily; use App\Model\Product; use App\Model\ProductPlatformAmount; +use App\Model\User; use App\Model\UserCoupon; use App\Model\UserDoctor; use App\Model\UserPatient; +use App\Model\UserSystem; use App\Services\BaseService; use App\Services\ImService; use App\Services\InquiryService; use App\Services\MessagePush; use App\Services\OrderPrescriptionService; +use App\Services\UserService; use App\Utils\Log; use Extend\Alibaba\Oss; use Extend\TencentIm\Account; @@ -634,10 +637,9 @@ class CallBackController extends AbstractController { $request_params = $this->request->all(); try { - Log::getInstance()->info("Im回调数据:" . json_encode($request_params, JSON_UNESCAPED_UNICODE)); + Log::getInstance("CallBackController-imCallBack")->info("Im回调数据:" . json_encode($request_params, JSON_UNESCAPED_UNICODE)); if (empty($request_params['RequestTime']) || empty($request_params['Sign'])) { - Log::getInstance()->error("Im回调数据处理失败:缺少时间时间戳/签名字段"); return $this->ImErrorReturn("缺少时间时间戳/签名字段"); } @@ -645,37 +647,31 @@ class CallBackController extends AbstractController $imService = new ImService(); $result = $imService->validateSign($request_params['RequestTime'], $request_params['Sign']); if (!$result) { - Log::getInstance()->error("Im回调数据处理失败:回调签名不匹配"); return $this->ImErrorReturn("回调签名不匹配"); } // 验证消息内容 if (empty($request_params['MsgBody'])) { - Log::getInstance()->error("Im回调数据处理失败:消息内容错误"); return $this->ImErrorReturn("消息内容错误,缺少MsgBody"); } // 验证消息内容类型 if (empty($request_params['MsgBody'][0]['MsgType'])) { - Log::getInstance()->error("Im回调数据处理失败:缺少MsgType"); return $this->ImErrorReturn("消息内容错误,缺少MsgType"); } // 验证消息内容详情 if (empty($request_params['MsgBody'][0]['MsgContent'])) { - Log::getInstance()->error("Im回调数据处理失败:缺少MsgContent"); return $this->ImErrorReturn("消息内容错误,缺少MsgContent"); } // 验证接收方user_id if (empty($request_params['To_Account'])) { - Log::getInstance()->error("Im回调数据处理失败:接收用户错误"); return $this->ImErrorReturn("消息内容错误,接收用户错误"); } // 验证消息唯一id if (empty($request_params['MsgKey'])) { - Log::getInstance()->error("Im回调数据处理失败:消息唯一标识错误"); return $this->ImErrorReturn("消息内容错误,消息唯一标识错误"); } @@ -685,7 +681,7 @@ class CallBackController extends AbstractController $message = MessageIm::getExists($params); if ($message) { // 消息重复 - Log::getInstance()->info("Im回调数据处理失败:消息重复"); + Log::getInstance("CallBackController-imCallBack")->info("消息重复"); return $this->ImSuccessReturn(); } @@ -706,7 +702,6 @@ class CallBackController extends AbstractController $params['order_inquiry_id'] = $cloud_custom_data['order_inquiry_id']; $order_inquiry = OrderInquiry::getOne($params); if (empty($order_inquiry)) { - Log::getInstance()->error("Im回调数据处理失败:非法订单"); return $this->ImErrorReturn("消息内容错误,非法订单"); } @@ -745,13 +740,21 @@ class CallBackController extends AbstractController $data['message_custom_content'] = $request_params['CloudCustomData'] ?? ""; $message = MessageIm::addMessage($data); if (empty($message)) { - Log::getInstance()->error("Im回调数据处理失败:存储数据库失败"); - return $this->wxPayErrorReturn("存储数据库失败"); + return $this->ImErrorReturn("存储数据库失败"); + } + + // im消息通知 + if ($is_system == 0 && isset($message_send_result) && isset($order_inquiry_id)){ + try { + $UserService = new UserService(); + $UserService->userImMessageNotice($request_params['To_Account'],$order_inquiry_id,$request_params['MsgBody']); + }catch (\Throwable $e){ + Log::getInstance("CallBackController-imCallBack")->error("im消息通知失败"); + } } } catch (\Exception $e) { // 验证失败 - Log::getInstance()->error("Im回调数据处理失败:" . $e->getMessage()); - return $this->wxPayErrorReturn($e->getMessage()); + return $this->ImErrorReturn($e->getMessage()); } Log::getInstance()->info("Im回调数据处理成功"); @@ -765,6 +768,7 @@ class CallBackController extends AbstractController */ protected function ImErrorReturn(string $message): ResponseInterface { + Log::getInstance("CallBackController-imCallBack")->error("Im回调数据处理失败:接收用户错误"); return $this->response ->withStatus(200) ->withBody( @@ -1334,254 +1338,6 @@ class CallBackController extends AbstractController return $server->serve(); } -// /** -// * 检测所结果回调 -// * @return ResponseInterface -// */ -// public function DetectionResultCallBack1(): ResponseInterface -// { -// $request_params = $this->request->all(); -// $auth = $this->request->header("Authorization"); -// if (empty($auth)){ -// return $this->detectionResultFailReturn("非法请求"); -// } -// -// Log::getInstance("CallBackController-DetectionResultCallBack")->info("处理检测所结果"); -// -// try { -// // 检测参数 -// if (!isset($request_params['appId'])){ -// return $this->detectionResultFailReturn("缺少appId参数"); -// } -// -// if (!isset($request_params['orderCode'])){ -// return $this->detectionResultFailReturn("缺少orderCode参数"); -// } -// -// if (!isset($request_params['detectBarcode'])){ -// return $this->detectionResultFailReturn("缺少detectBarcode参数"); -// } -// -// if (!isset($request_params['pdfReport'])){ -// return $this->detectionResultFailReturn("缺少pdfReport参数"); -// } -// -// // 检测签名 -// $params = array(); -// $params['app_id'] = $request_params['appId']; -// $basic_detection_organ = BasicDetectionOrgan::getOne($params); -// if (empty($basic_detection_organ)){ -// return $this->detectionResultFailReturn("非法appId"); -// } -// -// $time = date('Ymd'); -// $sign = md5($basic_detection_organ['app_id'] . $basic_detection_organ['app_secret'] . $time); -// if ($auth != $sign){ -// return $this->detectionResultFailReturn("签名错误"); -// } -// -// // 检测pdf文件 -// $pdfData = base64_decode($request_params['pdfReport']); -// if (!$pdfData){ -// return $this->detectionResultFailReturn("文件错误"); -// } -// -// // 获取检测订单数据 -// $params = array(); -// $params['detection_no'] = $request_params['orderCode']; -// $order_detection = OrderDetection::getOne($params); -// if (empty($order_detection)){ -// return $this->detectionResultFailReturn("非法订单"); -// } -// -// // 检测订单状态 -// if ($order_detection['detection_status'] != 3){ -// return $this->detectionResultFailReturn("订单状态错误,无法处理"); -// } -// -// if (!empty($order_detection['detection_result_pdf'])){ -// return $this->detectionResultSuccessReturn(); -// } -// -// if ($order_detection['detection_bar_code'] != $request_params['detectBarcode']){ -// return $this->detectionResultFailReturn("检测条码无法对应"); -// } -// -// // 获取医生数据 -// $params = array(); -// $params['doctor_id'] = $order_detection['doctor_id']; -// $user_doctor = UserDoctor::getOne($params); -// if (empty($user_doctor)){ -// return $this->detectionResultFailReturn("医生数据错误"); -// } -// -// // 获取检测病例数据 -// $params = array(); -// $params['order_detection_id'] = $order_detection['order_detection_id']; -// $order_detection_case = OrderDetectionCase::getOne($params); -// if (empty($order_detection_case)){ -// return $this->detectionResultFailReturn("订单数据错误"); -// } -// -// // 获取检测项目数据 -// $params = array(); -// $params['detection_project_id'] = $order_detection['detection_project_id']; -// $detection_project = DetectionProject::getOne($params); -// if (empty($detection_project)){ -// return $this->detectionResultFailReturn("订单数据错误"); -// } -// -// // 检测家庭成员是否存在 -// $params = array(); -// $params['family_id'] = $order_detection['family_id']; -// $params['patient_id'] = $order_detection['patient_id']; -// $patient_family = PatientFamily::getOne($params); -// if (empty($patient_family)) { -// return $this->detectionResultFailReturn("患者信息错误"); -// } -// -// // 上传处方图片至oss -// $oss = new Oss(); -// $detection_result_pdf_name = "applet/patient/detection/pdf/" . $order_detection['order_detection_id'] . '.' . 'pdf'; -// $detection_result_pdf = $oss->putObject($detection_result_pdf_name, $pdfData); -// $detection_result_pdf = '/' . $detection_result_pdf; -// -// Db::beginTransaction(); -// -// try { -// $generator = $this->container->get(IdGeneratorInterface::class); -// -// // 创建问诊订单 -// $data = array(); -// $data['user_id'] = $order_detection['user_id']; -// $data['patient_id'] = $order_detection['patient_id']; -// $data['doctor_id'] = $order_detection['doctor_id']; -// $data['family_id'] = $order_detection['family_id']; -// $data['inquiry_type'] = 5; // 订单类型(1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药 5:检测) -// $data['inquiry_mode'] = 1; // 订单问诊方式(1:图文 2:视频 3:语音 4:电话 5:会员) -// $data['inquiry_status'] = 4; // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消) -// $data['inquiry_pay_channel'] = 3; // 支付渠道(1:小程序支付 2:微信扫码支付 3:模拟支付) -// $data['inquiry_pay_status'] = 2; // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) -// $data['inquiry_no'] = $generator->generate();// 订单编号 -// $data['escrow_trade_no'] = "GD" . $generator->generate(); // 第三方支付流水号 -// $data['amount_total'] = 0;// 订单金额 -// $data['coupon_amount_total'] = 0;// 优惠卷总金额 -// $data['payment_amount_total'] = 0;// 实际付款金额 -// $data['pay_time'] = date('Y-m-d H:i:s', time());// 支付时间 -// $data['reception_time'] = date('Y-m-d H:i:s', time());// 接诊时间 -// $data['patient_name'] = $patient_family['card_name'];// 患者姓名-就诊人 -// $data['patient_name_mask'] = $patient_family['card_name_mask'];// 患者姓名-就诊人(掩码) -// $data['patient_sex'] = $patient_family['sex'];// 患者性别-就诊人(0:未知 1:男 2:女) -// $data['patient_age'] = $patient_family['age'];// 患者年龄-就诊人 -// $order_inquiry = OrderInquiry::addOrderInquiry($data); -// if (empty($order_inquiry)) { -// Db::rollBack(); -// return $this->detectionResultFailReturn("问诊订单创建失败"); -// } -// -// // 回填问诊订单id -// $order_detection['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; -// -// // 增加患者问诊病例 -// $data = array(); -// $data['user_id'] = $order_detection['user_id']; -// $data['patient_id'] = $order_detection['patient_id']; -// $data['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];// 订单-问诊id -// $data['family_id'] = $patient_family['family_id']; // 家庭成员id -// $data['relation'] = $patient_family['relation']; // 与患者关系(1:本人 2:父母 3:爱人 4:子女 5:亲戚 6:其他 ) -// $data['name'] = $patient_family['card_name']; // 患者名称 -// $data['sex'] = $patient_family['sex'] ?? 0; // 患者性别(0:未知 1:男 2:女) -// $data['age'] = $patient_family['age'] ?? null; // 患者年龄 -// $data['height'] = $patient_family['height'] ?: null; // 身高(cm) -// $data['weight'] = $patient_family['weight'] ?: null;; // 体重(kg) -// $order_inquiry_case = OrderInquiryCase::addOrderInquiryCase($data); -// if (empty($order_inquiry_case)) { -// Db::rollBack(); -// return $this->detectionResultFailReturn("问诊订单病例创建失败"); -// } -// -// // 修改检测订单 -// $data = array(); -// $data['detection_status'] = 4; -// $data['detection_result_pdf'] = $detection_result_pdf; -// $data['detection_result_date'] = date('Y-m-d H:i:s',time()); -// $data['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];// 订单-问诊id -// -// $params = array(); -// $params['order_detection_id'] = $order_detection['order_detection_id']; -// OrderDetection::editOrderDetection($params,$data); -// -// // 回填检测结果字段 -// $order_detection['detection_result_pdf'] = $detection_result_pdf; -// $order_detection['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; -// -// // 添加自动完成队列 -// $time = 1000 * 60 * 60 * 24 * 3; -// -// $data = array(); -// $data['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; -// -// $message = new AutoCompleteInquiryDelayDirectProducer($data); -// $message->setDelayMs($time); -// $producer = $this->container->get(Producer::class); -// $res = $producer->produce($message); -// if (!$res) { -// Db::rollBack(); -// return $this->detectionResultFailReturn("添加自动完成队列失败"); -// } -// -// // 发送im消息 -// $imService = new ImService(); -// -// // 患者病例 -// $imService->patientCase( -// $order_inquiry, -// $user_doctor['user_id'], -// $order_inquiry_case['disease_desc'] -// ); -// -// // 发送IM消息-检测报告结果 -// $imService->detectionTestReport( -// $order_detection, -// $user_doctor['user_id'], -// $order_detection['user_id'], -// $order_detection_case['detection_disease_class_names'], -// $detection_project['detection_project_name'] -// ); -// -// Db::commit(); -// } catch (\Throwable $e) { -// Db::rollBack(); -// return $this->detectionResultFailReturn($e->getMessage()); -// } -// -// try { -// // 发送IM消息-检测报告结果-文字 -// $imService->detectionTestReportStr( -// $order_detection, -// $user_doctor['user_name'], -// $detection_project['detection_project_name'], -// $user_doctor['user_id'], -// $order_detection['user_id'] -// ); -// -// // 患者-新报告生成通知 -// $MessagePush = new MessagePush($order_detection['user_id']); -// $MessagePush->patientDetectionResultNotice($order_detection['order_detection_id']); -// -// // 医生-通知医生患者检测报告已生成 -// $MessagePush = new MessagePush($user_doctor['user_id']); -// $MessagePush->doctorDetectionResultNotice($order_detection['order_detection_id']); -// }catch (\Throwable $e){ -// Log::getInstance("detectionResult")->error($e->getMessage()); -// } -// -// return $this->detectionResultSuccessReturn(); -// } catch (\Throwable $e) { -// return $this->detectionResultFailReturn("异常:" . $e->getMessage()); -// } -// } - /** * 检测所结果回调 * @return ResponseInterface diff --git a/app/Services/MessagePush.php b/app/Services/MessagePush.php index 48066c9..ccde9f2 100644 --- a/app/Services/MessagePush.php +++ b/app/Services/MessagePush.php @@ -2416,4 +2416,121 @@ class MessagePush extends BaseService throw new BusinessException("加入推送队列失败" . json_encode($data, JSON_UNESCAPED_UNICODE)); } } + + /** + * 患者-im消息通知 + * 订阅 + * @param string $data 消息内容 + * @return void + */ + public function patientImMessageNotice(string $msg): void + { + try { + // 获取医生数据 + $params = array(); + $params['doctor_id'] = $this->order_inquiry['doctor_id']; + $user_doctor = UserDoctor::getOne($params); + if (empty($user_doctor)) { + Log::getInstance("MessagePush")->error("错误:医生数据为空"); + return; + } + + // im内容 + if (strlen($msg) > 15) { + $msg = mb_substr($msg, 0, 15); + if ($msg) { + $msg = $msg . "..."; + } + } + + // 订阅 + $sub_data = array(); + $sub_data['push_user_id'] = $this->user['user_id']; + $sub_data['wx_template_id'] = "9v6dZhjg09CttLd3W9nEUV_-eshNc4BYYNy59jglvZE";// 问诊提醒 + $sub_data['params']['page'] = "pages/message/message"; + $sub_data['params']['data'] = [ + "thing1" => (string)"医生回复了您的消息,点击查看详情",// 问诊内容 + "thing2" => (string)$msg,// 提醒内容 + "name3" => (string)$user_doctor['user_name'],// 问诊医生 + "thing4" => "如有困扰,前往个人中心的设置中关闭",// 提示说明 + ]; + + $data = array(); + $data['sub_data'] = $sub_data; + $data['sms_data'] = array();; + + $message = new SendSubMessageProducer($data); + $producer = ApplicationContext::getContainer()->get(Producer::class); + $result = $producer->produce($message); + if (!$result) { + Log::getInstance("MessagePush-patientImMessageNotice")->error(json_encode($data, JSON_UNESCAPED_UNICODE)); + } + } catch (\Throwable $e) { + Log::getInstance("MessagePush-patientImMessageNotice")->error($e->getMessage()); + } + } + + /** + * 医生-im消息通知 + * 订阅 + * @param string $im_data + * @return void + */ + public function doctorImMessageNotice(string $msg): void + { + try { + // 获取问诊订单关联病例 + $params = array(); + $params['order_inquiry_id'] = $this->order_inquiry['order_inquiry_id']; + $order_inquiry_case = OrderInquiryCase::getOne($params); + if (empty($order_inquiry_case)) { + Log::getInstance("MessagePush")->error("错误:病例数据为空"); + return; + } + + // im内容 + if (strlen($msg) > 15) { + $msg = mb_substr($msg, 0, 15); + if ($msg) { + $msg = $msg . "..."; + } + } + + // 问诊内容-病情主诉 + $disease_desc = $order_inquiry_case['disease_desc']; + if (!empty($disease_desc)) { + if (strlen($disease_desc) > 15) { + $disease_desc = mb_substr($disease_desc, 0, 15); + if ($disease_desc) { + $disease_desc = $disease_desc . "..."; + } + } + } + + // 订阅 + $sub_data = array(); + $sub_data['push_user_id'] = $this->user['user_id']; + $sub_data['wx_template_id'] = "G1RIs0RYqsTQ2CuPQWalIMyb6_deuEEbJfajfhGvNzc";// 问诊提醒 + $sub_data['params']['page'] = "Pages/yishi/wenzhen_v2/wenzhen"; + $sub_data['params']['data'] = [ + "thing1" => $msg,// 提醒内容 + "name2" => (string)$this->order_inquiry['patient_name'],// 患者姓名 + "thing4" => (string)$disease_desc,// 病情描述 + "thing6" => "患者发来了最新消息,点击查看详情",// 提示说明 + ]; + + $data = array(); + $data['sub_data'] = $sub_data; + $data['sms_data'] = array();; + + $message = new SendSubMessageProducer($data); + $producer = ApplicationContext::getContainer()->get(Producer::class); + $result = $producer->produce($message); + if (!$result) { + Log::getInstance("MessagePush-patientImMessageNotice")->error(json_encode($data, JSON_UNESCAPED_UNICODE)); + } + } catch (\Throwable $e) { + Log::getInstance("MessagePush-patientImMessageNotice")->error($e->getMessage()); + } + } } \ No newline at end of file diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 209362b..f632526 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -755,7 +755,6 @@ class UserService extends BaseService return success(); } - /** * 通过user_id获取用户openid * @param string|int $user_id @@ -835,4 +834,78 @@ class UserService extends BaseService return $avatar; } + /** + * 用户im消息通知 + * @param int|string $user_id + * @param string|int $order_inquiry_id + * @param array $msg_body + * @return bool + */ + public function userImMessageNotice(int|string $user_id,string|int $order_inquiry_id,array $msg_body): bool + { + try { + // 获取用户数据 + $params = array(); + $params['user_id'] = $user_id; + $user = User::getOne($params); + if (empty($user)){ + return false; + } + + // 获取用户配置 + $params = array(); + $params['user_id'] = $user['user_id']; + $user_system = UserSystem::getOne($params); + if (empty($user_system)){ + return false; + } + + $message = "您有一条新的问诊消息"; + // 判断消息类型 + if (isset($msg_body[0]['MsgType'])){ + switch ($msg_body[0]['MsgType']) { + case 'TIMLocationElem': + $message = "地理位置"; + break; + case 'TIMFaceElem': + $message = "表情消息"; + break; + + case 'TIMSoundElem': + $message = "语音消息"; + break; + case 'TIMImageElem': + $message = "图像消息"; + break; + case 'TIMFileElem': + $message = "文件消息"; + break; + case 'TIMTextElem': + if (isset($msg_body[0]['MsgContent']['Text'])){ + $message = $msg_body[0]['MsgContent']['Text']; + } + break; + default: + // code... + break; + } + } + + $MessagePush = new MessagePush($user_id, $order_inquiry_id); + + if ($user['user_type'] == 1){ + // 患者 + $MessagePush->patientImMessageNotice($message); + } elseif ($user['user_type'] == 2){ + // 医师 + $MessagePush->doctorImMessageNotice($message); + }else{ + return false; + } + }catch (\Throwable $e){ + return false; + } + + return true; + } } \ No newline at end of file