From d0d1e7494da409204b8dfef9c5aa5e76a6847a97 Mon Sep 17 00:00:00 2001 From: wucongxing <815046773@qq.com> Date: Tue, 28 Mar 2023 15:56:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=97=AE=E8=AF=8A=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Amqp/Consumer/AssignDoctorConsumer.php | 5 + ...ncelUnInquiryOrdersDelayDirectConsumer.php | 4 +- .../Consumer/SendStationMessageConsumer.php | 3 +- app/Amqp/Consumer/SendSubMessageConsumer.php | 40 ++-- app/Common/Common.php | 44 ++++ app/Controller/CallBackController.php | 3 + app/Controller/TestController.php | 15 +- app/Model/MessageNotice.php | 2 +- app/Request/InquiryRequest.php | 2 - app/Services/ImService.php | 39 ---- app/Services/InquiryService.php | 16 +- app/Services/MessagePush.php | 189 ++++++++++++++++++ app/Services/PatientOrderService.php | 4 +- extend/Wechat/Wechat.php | 4 +- 14 files changed, 288 insertions(+), 82 deletions(-) create mode 100644 app/Services/MessagePush.php diff --git a/app/Amqp/Consumer/AssignDoctorConsumer.php b/app/Amqp/Consumer/AssignDoctorConsumer.php index 78c0bec..8814c29 100644 --- a/app/Amqp/Consumer/AssignDoctorConsumer.php +++ b/app/Amqp/Consumer/AssignDoctorConsumer.php @@ -123,6 +123,7 @@ class AssignDoctorConsumer extends ConsumerMessage $InquiryService->inquiryRefund($order_inquiry['order_inquiry_id'], "暂无医生接诊"); + Db::commit(); return Result::ACK; } @@ -192,6 +193,10 @@ class AssignDoctorConsumer extends ConsumerMessage $cloud_custom_data['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; $cloud_custom_data['is_system'] = 1; $cloud_custom_data['inquiry_type'] = $order_inquiry['inquiry_type']; + $cloud_custom_data['message_rounds'] = 0; + $cloud_custom_data['patient_family_data']['patient_name'] = $order_inquiry['patient_name']; + $cloud_custom_data['patient_family_data']['patient_sex'] = $order_inquiry['patient_sex']; + $cloud_custom_data['patient_family_data']['patient_age'] = $order_inquiry['patient_age']; // 消息内容 $message_content_data = array(); diff --git a/app/Amqp/Consumer/CancelUnInquiryOrdersDelayDirectConsumer.php b/app/Amqp/Consumer/CancelUnInquiryOrdersDelayDirectConsumer.php index d2b194c..8c48a53 100644 --- a/app/Amqp/Consumer/CancelUnInquiryOrdersDelayDirectConsumer.php +++ b/app/Amqp/Consumer/CancelUnInquiryOrdersDelayDirectConsumer.php @@ -132,7 +132,7 @@ class CancelUnInquiryOrdersDelayDirectConsumer extends ConsumerMessage } $InquiryService->inquiryRefund($order_inquiry['order_inquiry_id'], "医生未接诊"); - + Db::commit(); return Result::ACK; } @@ -143,8 +143,8 @@ class CancelUnInquiryOrdersDelayDirectConsumer extends ConsumerMessage return Result::REQUEUE; } - Log::getInstance()->info("取消未接诊问诊订单成功"); + return Result::ACK; } } diff --git a/app/Amqp/Consumer/SendStationMessageConsumer.php b/app/Amqp/Consumer/SendStationMessageConsumer.php index 1718826..a099648 100644 --- a/app/Amqp/Consumer/SendStationMessageConsumer.php +++ b/app/Amqp/Consumer/SendStationMessageConsumer.php @@ -73,6 +73,7 @@ class SendStationMessageConsumer extends ConsumerMessage $cloud_custom_data['is_system'] = 1; $cloud_custom_data['inquiry_type'] = ""; $cloud_custom_data['message_rounds'] = 0; + $cloud_custom_data['patient_family_data'] = []; // 消息内容 $message_content_data = array(); @@ -89,7 +90,7 @@ class SendStationMessageConsumer extends ConsumerMessage $this->saveMessageNotice($user->toArray(),$data); Log::getInstance()->info("站内消息推送成功"); - $this->saveSuccessPushLog($user['user_type'],$data['sub_data']); + $this->saveSuccessPushLog($user['user_type'],$data); } catch (\Exception $e) { Log::getInstance()->error("站内消息推送执行失败:" . $e->getMessage()); diff --git a/app/Amqp/Consumer/SendSubMessageConsumer.php b/app/Amqp/Consumer/SendSubMessageConsumer.php index 30b1779..58a17cf 100644 --- a/app/Amqp/Consumer/SendSubMessageConsumer.php +++ b/app/Amqp/Consumer/SendSubMessageConsumer.php @@ -52,16 +52,16 @@ class SendSubMessageConsumer extends ConsumerMessage { Log::getInstance()->info("开始执行 订阅消息推送 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE)); - try { - // 获取被推送用户信息 - $params = array(); - $params['user_id'] = $data['push_user_id']; - $user = User::getOne($params); - if (empty($user)){ - Log::getInstance()->error("订阅消息推送执行失败:未查询到被推送用户信息"); - return Result::ACK; - } + // 获取被推送用户信息 + $params = array(); + $params['user_id'] = $data['sub_data']['push_user_id']; + $user = User::getOne($params); + if (empty($user)){ + Log::getInstance()->error("订阅消息推送执行失败:未查询到被推送用户信息"); + return Result::ACK; + } + try { // 验证发送参数 if (empty($data['sub_data']['params']['data'])){ Log::getInstance()->error("订阅消息推送执行失败:无推送数据"); @@ -74,9 +74,7 @@ class SendSubMessageConsumer extends ConsumerMessage $send_data = array(); foreach ($data['sub_data']['params']['data'] as $key => $item){ $send_data[$key] = [ - "value" => [ - $item - ] + "value" => $item ]; } @@ -87,7 +85,6 @@ class SendSubMessageConsumer extends ConsumerMessage return Result::DROP; } - // 获取open_id $UserService = new UserService(); $open_id = $UserService->getOpenIdWithUserId($user['user_id'],$user['user_type']); @@ -103,7 +100,7 @@ class SendSubMessageConsumer extends ConsumerMessage // 获取消息模版 $sub_template = $this->getSubTemplate($user['user_type'],$data['sub_data']['template_title']); - if (empty($template_title)){ + if (empty($sub_template)){ Log::getInstance()->error("订阅消息推送执行失败:未获取到需要发送的消息模版数据"); $this->saveErrorPushLog($user['user_type'],$data['sub_data'],"未获取到需要发送的消息模版数据"); @@ -115,6 +112,7 @@ class SendSubMessageConsumer extends ConsumerMessage // 处理发送环境 $miniprogram_state = "developer"; +// $miniprogram_state = "trial"; if (env("APP_ENV") == "prod"){ $miniprogram_state = "formal"; } @@ -128,6 +126,8 @@ class SendSubMessageConsumer extends ConsumerMessage "lang" => "zh_CN", ]; + dump($options); + // 发起推送 $Wechat = new Wechat($user['user_type']); $result = $Wechat->sendSubscribeMessage($options); @@ -270,13 +270,13 @@ class SendSubMessageConsumer extends ConsumerMessage // 下载消息模版数据 Log::getInstance()->info("消息模版:" . $template_title . " 在数据库中为空"); $this->downSubTemplate($user_type); - } - // 重新获取订阅消息模版 - $params = array(); - $params['client_type'] = $user_type; // 客户端类型(1:患者端 2:医师端 3:药师端) - $params['template_title'] = $template_title; - $sub_template = SubTemplate::getOne($params); + // 重新获取订阅消息模版 + $params = array(); + $params['client_type'] = $user_type; // 客户端类型(1:患者端 2:医师端 3:药师端) + $params['template_title'] = $template_title; + $sub_template = SubTemplate::getOne($params); + } } catch (\Exception $e) { Log::getInstance()->error("获取数据库消息订阅模版失败:" . $e->getMessage()); } diff --git a/app/Common/Common.php b/app/Common/Common.php index 6dc14b9..aa399a3 100644 --- a/app/Common/Common.php +++ b/app/Common/Common.php @@ -152,5 +152,49 @@ function sexToImSex(string|int $sex): string $result = "Gender_Type_Female"; } + return $result; +} + +/** + * 转换订单类型-字符串 + * @param int|string $inquiry_type 订单类型(1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药) + * @return string + */ +function inquiryTypeToString(int|string $inquiry_type): string +{ + if ($inquiry_type == 1) { + $result = "专家问诊"; + } elseif ($inquiry_type == 2) { + $result = "快速问诊"; + } elseif ($inquiry_type == 3) { + $result = "公益问诊"; + } elseif ($inquiry_type == 4) { + $result = "问诊购药"; + } else { + $result = "未知"; + } + return $result; +} + +/** + * 转换取消订单原因-字符串 + * @param int|string $cancel_reason 取消订单原因(1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时) + * @return string + */ +function cancelReasonToString(int|string $cancel_reason): string +{ + if ($cancel_reason == 1) { + $result = "医生未接诊"; + } elseif ($cancel_reason == 2) { + $result = "主动取消"; + } elseif ($cancel_reason == 3) { + $result = "无可分配医生"; + } elseif ($cancel_reason == 4) { + $result = "客服取消"; + } elseif ($cancel_reason == 5) { + $result = "支付超时"; + } else { + $result = "未知"; + } return $result; } \ No newline at end of file diff --git a/app/Controller/CallBackController.php b/app/Controller/CallBackController.php index c6ac8ce..425c399 100644 --- a/app/Controller/CallBackController.php +++ b/app/Controller/CallBackController.php @@ -167,6 +167,9 @@ class CallBackController extends AbstractController $cloud_custom_data['is_system'] = 1; $cloud_custom_data['inquiry_type'] = $order_inquiry['inquiry_type']; $cloud_custom_data['message_rounds'] = 0; + $cloud_custom_data['patient_family_data']['patient_name'] = $order_inquiry['patient_name']; + $cloud_custom_data['patient_family_data']['patient_sex'] = $order_inquiry['patient_sex']; + $cloud_custom_data['patient_family_data']['patient_age'] = $order_inquiry['patient_age']; // 消息内容 $message_content_data = array(); diff --git a/app/Controller/TestController.php b/app/Controller/TestController.php index 7b6521d..7a5d226 100644 --- a/app/Controller/TestController.php +++ b/app/Controller/TestController.php @@ -8,6 +8,7 @@ use App\Model\DoctorInquiryTime; use App\Model\DoctorPharmacistCert; use App\Model\UserDoctor; use App\Services\ImService; +use App\Services\MessagePush; use App\Utils\Log; use Extend\Alibaba\Oss; use Extend\Ca\Ca; @@ -435,17 +436,11 @@ class TestController extends AbstractController } public function test_10(){ - $data = array(); - $data['order_inquiry_id'] = "499163900703698944"; - $message = new CancelUnpayOrdersDelayDirectProducer($data); - $message->setDelayMs(1000 * 5); - $producer = $this->container->get(Producer::class); - $res = $producer->produce($message); - if (!$res) { - Db::rollBack(); - return fail(HttpEnumCode::SERVER_ERROR, "订单创建失败"); - } + $MessagePush = new MessagePush(2,"491923510680424449","499172549291458560"); + $result = $MessagePush->noInquiry(); +// $MessagePush = new MessagePush(2,"491923510680424449","499176434366210048"); +// $result = $MessagePush->patientReplyNotice("你好1"); } } \ No newline at end of file diff --git a/app/Model/MessageNotice.php b/app/Model/MessageNotice.php index 61f9012..80e6d4a 100644 --- a/app/Model/MessageNotice.php +++ b/app/Model/MessageNotice.php @@ -45,7 +45,7 @@ class MessageNotice extends Model */ protected array $fillable = ['notice_id', 'user_id', 'user_type', 'notice_type', 'notice_system_type', 'read_status', 'inquiry_type', 'from_name', 'notice_title', 'notice_send_time', 'notice_content', 'send_status', 'send_fail_reason', 'button_type', 'link_type', 'link_params', 'show_type', 'show_params', 'created_at', 'updated_at']; - protected string $primaryKey = "message_id"; + protected string $primaryKey = "notice_id"; /** * 获取是否存在 diff --git a/app/Request/InquiryRequest.php b/app/Request/InquiryRequest.php index 43dbe54..7e85fa0 100644 --- a/app/Request/InquiryRequest.php +++ b/app/Request/InquiryRequest.php @@ -22,8 +22,6 @@ class InquiryRequest extends FormRequest 'family_history', 'is_pregnant',// 备孕、妊娠、哺乳期 'pregnant', - 'product.product_ids', // 商品id - 'product.product_num', // 药品数量 'height', 'weight', 'inquiry_type', // 订单类型(1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药) diff --git a/app/Services/ImService.php b/app/Services/ImService.php index aa732d2..701951d 100644 --- a/app/Services/ImService.php +++ b/app/Services/ImService.php @@ -276,43 +276,4 @@ class ImService extends BaseService $message->sendMessage($arg); } - - // 发送自定义消息 - public function sendCustomMessage(string $from_user_id,string $to_user_id,string $content,string $order_inquiry_id,int $inquiry_type, array $cloud_custom_data): void - { - if (!empty($from_user_id)){ - // 检测并创建发送者资料 - $this->setUserProfile($from_user_id); - } - - // 检测并创建接收者资料 - $this->setUserProfile($to_user_id); - - $message = new Message(); - - $arg = array(); - if (!empty($from_user_id)){ - $arg['From_Account'] = $from_user_id; // 发送方user_id 如系统发送,无需填写 - } - $arg['To_Account'] = $to_user_id; // 接收方user_id - $arg['ForbidCallbackControl'] = ['ForbidBeforeSendMsgCallback','ForbidAfterSendMsgCallback']; - $arg['SendMsgControl'] = ['NoUnread']; - - $arg['MsgBody'] = [ - [ - "MsgType" => "TIMCustomElem", - "MsgContent" => [ - "Data" => $content, - ], - ] - ]; - - // 自定义消息 - $arg['CloudCustomData'] = ""; - if (!empty($cloud_custom_data)){ - $arg['CloudCustomData'] = json_encode($cloud_custom_data,JSON_UNESCAPED_UNICODE); - } - - $message->sendMessage($arg); - } } \ No newline at end of file diff --git a/app/Services/InquiryService.php b/app/Services/InquiryService.php index 986c169..35dec35 100644 --- a/app/Services/InquiryService.php +++ b/app/Services/InquiryService.php @@ -278,7 +278,7 @@ class InquiryService extends BaseService $data = array(); $data['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; $message = new CancelUnInquiryOrdersDelayDirectProducer($data); - $message->setDelayMs(1000 * 60 * 5); + $message->setDelayMs(1000 * 60 * 60 * 24); $producer = $this->container->get(Producer::class); $res = $producer->produce($message); if (!$res) { @@ -604,11 +604,19 @@ class InquiryService extends BaseService $params['order_inquiry_id'] = $order_inquiry_id; $order_inquiry = OrderInquiry::getOne($params); if (empty($order_inquiry)) { - return fail(); + return fail(HttpEnumCode::HTTP_ERROR,"订单错误"); } - if ($order_inquiry['doctor_id'] != $user_info['client_user_id'] && $order_inquiry['patient_id'] != $user_info['client_user_id']){ - return fail(); + if ($user_info['user_type'] == 1){ + if ($order_inquiry['patient_id'] != $user_info['client_user_id']){ + return fail(HttpEnumCode::SERVER_ERROR); + } + } + + if ($user_info['user_type'] == 2){ + if ($order_inquiry['doctor_id'] != $user_info['client_user_id']){ + return fail(HttpEnumCode::SERVER_ERROR); + } } $params = array(); diff --git a/app/Services/MessagePush.php b/app/Services/MessagePush.php new file mode 100644 index 0000000..a605463 --- /dev/null +++ b/app/Services/MessagePush.php @@ -0,0 +1,189 @@ +push_type = $push_type; + + if (!empty($user_id)) { + $params = array(); + $params['user_id'] = $user_id; + $user = User::getOne($params); + if (!empty($user)) { + $this->user = $user->toArray(); + } + } + + if (!empty($order_inquiry_id)) { + $params = array(); + $params['order_inquiry_id'] = $order_inquiry_id; + $order_inquiry = OrderInquiry::getOne($params); + if (!empty($order_inquiry)) { + $this->order_inquiry = $order_inquiry->toArray(); + } + } + } + + /** + * 快速/购药的服务5分钟未接诊 + * 公益/专家24小时未接诊 + * @return bool + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function noInquiry(): bool + { + try { + if ($this->push_type == 1) { + // 站内 + // 服务消息 + $data = array(); + $data['user_id'] = $this->user['user_id']; + $data['notice_type'] = 3; + $data['notice_system_type'] = 1; + $data['from_name'] = "肝胆小秘书"; + if ($this->order_inquiry['inquiry_type'] == 1 || $this->order_inquiry['inquiry_type'] == 3) { + // 专家-公益 + $inquiry_type_string = inquiryTypeToString($this->order_inquiry['inquiry_type']); + $data['notice_title'] = "【{$inquiry_type_string}】医生未接诊:您咨询的医生因工作繁忙没有时间接诊,点击查看详情。"; + $data['notice_content'] = "【{$inquiry_type_string}】未接诊:您咨询的服务当前排队人较多,暂无空闲医生接诊。给您带来不便敬请谅解,平台会在24小时内进行退款,您可以点击订单详情查看退款详情。"; + + } elseif ($this->order_inquiry['inquiry_type'] == 1 || $this->order_inquiry['inquiry_type'] == 4) { + // 快速-购药 + $inquiry_type_string = inquiryTypeToString($this->order_inquiry['inquiry_type']); + $data['notice_title'] = "【{$inquiry_type_string}】未接诊:您咨询的服务暂无医生接诊,点击查看详情。"; + $data['notice_content'] = "【{$inquiry_type_string}】医生未接诊:您咨询的医生因工作繁忙,暂无空闲时间接诊。给您带来不便敬请谅解,平台会在24小时内进行退款,您可以点击订单详情查看退款详情。"; + } + + $data['link_type'] = 10;// 问诊订单详情 + + $link_params = array(); + $link_params['order_inquiry_id'] = $this->order_inquiry['order_inquiry_id']; + $data['link_params'] = json_encode($link_params,JSON_UNESCAPED_UNICODE);// 跳转参数 + + $message = new SendStationMessageProducer($data); + $producer = ApplicationContext::getContainer()->get(Producer::class); + $result = $producer->produce($message); + if (!$result) { + throw new BusinessException("加入推送队列失败" . json_encode($data,JSON_UNESCAPED_UNICODE)); + } + } elseif ($this->push_type == 2) { + // 订阅 + // 获取医生数据 + $params = array(); + $params['doctor_id'] = $this->order_inquiry['doctor_id']; + $user_doctor = UserDoctor::getOne($params); + if (empty($user_doctor)){ + throw new BusinessException("加入推送队列失败:医生数据为空"); + } + + $sub_data = array(); + $sub_data['push_user_id'] = $this->user['user_id']; + $sub_data['template_title'] = "问诊异常通知"; + $sub_data['params']['page'] = "pages/orderDetail/orderDetail"; + $sub_data['params']['data'] = [ + "character_string1" => $this->order_inquiry['inquiry_no'],// 订单ID + "name2" => (string)$user_doctor['user_name'],// 问诊医生 + "date3" => $this->order_inquiry['created_at'],// 问诊时间 + "thing4" => cancelReasonToString($this->order_inquiry['cancel_reason']),// 取消原因 + "thing5" => "点击详情更换医生问诊",// 提示说明 + ]; + + + $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) { + throw new BusinessException("加入推送队列失败" . json_encode($data,JSON_UNESCAPED_UNICODE)); + } + + } elseif ($this->push_type == 3) { + // 短信 无需接入 + + } else { + + } + } catch (\Exception $e) { + throw new BusinessException("加入推送队列失败" . $e->getMessage()); + } + + return true; + } + + /** + * 患者回复通知 + * @param string $content 回复内容 + * @return bool + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function patientReplyNotice(string $content): bool + { + try { + // 只有订阅消息 + if ($this->push_type == 2){ + $sub_data = array(); + $sub_data['push_user_id'] = $this->user['user_id']; + $sub_data['template_title'] = "患者回复通知"; + $sub_data['params']['page'] = "Pages/yishi/chat/index?from_account={$this->user['user_id']}&order_inquiry_id={$this->order_inquiry['order_inquiry_id']}&inquiry_type={$this->order_inquiry['inquiry_type']}"; + $sub_data['params']['data'] = [ + "thing1" => $this->user['user_name'],// 患者姓名 + "thing2" => $content,// 回复内容 + ]; + + $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) { + throw new BusinessException("加入推送队列失败" . json_encode($data,JSON_UNESCAPED_UNICODE)); + } + } + } catch (\Exception $e) { + throw new BusinessException("加入推送队列失败" . $e->getMessage()); + } + + return true; + } +} \ No newline at end of file diff --git a/app/Services/PatientOrderService.php b/app/Services/PatientOrderService.php index bda30f7..9dc9f02 100644 --- a/app/Services/PatientOrderService.php +++ b/app/Services/PatientOrderService.php @@ -3,7 +3,6 @@ namespace App\Services; use App\Amqp\Producer\AssignDoctorProducer; -use App\Amqp\Producer\CancelUnPayInquiryOrderDelayProducer; use App\Amqp\Producer\CancelUnpayOrdersDelayDirectProducer; use App\Constants\DoctorTitleCode; use App\Constants\HttpEnumCode; @@ -810,6 +809,9 @@ class PatientOrderService extends BaseService $cloud_custom_data['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; $cloud_custom_data['is_system'] = 1; $cloud_custom_data['inquiry_type'] = $order_inquiry['inquiry_type']; + $cloud_custom_data['patient_family_data']['patient_name'] = $order_inquiry['patient_name']; + $cloud_custom_data['patient_family_data']['patient_sex'] = $order_inquiry['patient_sex']; + $cloud_custom_data['patient_family_data']['patient_age'] = $order_inquiry['patient_age']; // 消息内容 $message_content_data = array(); diff --git a/extend/Wechat/Wechat.php b/extend/Wechat/Wechat.php index 38e2e40..2f825ee 100644 --- a/extend/Wechat/Wechat.php +++ b/extend/Wechat/Wechat.php @@ -29,9 +29,9 @@ class Wechat protected array $config;// 系统配置 /** - * @param string $user_type + * @param string|int $user_type */ - public function __construct(string $user_type) + public function __construct(string|int $user_type) { if ($user_type == 1){ $this->config = config("we_chat.applets.patient");