info("开始执行 站内消息推送 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE)); // 验证推送参数 $res = $this->checkPushParams($data); if (!$res){ Log::getInstance()->error("站内消息推送失败:入参错误"); return Result::DROP; } // 验证用户信息 $params = array(); $params['user_id'] = $data['user_id']; $user = User::getOne($params); if(empty($user)){ Log::getInstance()->error("站内消息推送失败:推送用户信息错误"); return Result::DROP; } if ($data['notice_type'] == 1){ // 医生服务通知 $message_type = 3; }elseif ($data['notice_type'] == 2){ // 医生系统公告 $message_type = 4; }elseif ($data['notice_type'] == 3){ // 患者系统消息 $message_type = 5; }else{ Log::getInstance()->error("站内消息推送失败:消息类型错误"); $this->saveErrorPushLog($user['user_type'],$data,"消息类型错误"); return Result::DROP; } try { // 发送消息 $ImService = new ImService(); // 自定义消息 $cloud_custom_data = array(); $cloud_custom_data['order_inquiry_id'] = ""; $cloud_custom_data['is_system'] = 1; $cloud_custom_data['inquiry_type'] = ""; $cloud_custom_data['message_rounds'] = 0; // 消息内容 $message_content_data = array(); $message_content_data['message_type'] = $message_type; $message_content_data['content'] = $data['notice_title'] ?: "消息推送"; $message_content_data['desc'] = $data['notice_content'] ?? ""; $message_content = [ 'Data' => json_encode($message_content_data,JSON_UNESCAPED_UNICODE), ]; $ImService->sendMessage("", $user['user_id'], $message_content, "TIMCustomElem", $cloud_custom_data); // 新增通知消息表 Log::getInstance()->info("站内消息推送成功"); $this->saveSuccessPushLog($user['user_type'],$data['sub_data']); } catch (\Exception $e) { Log::getInstance()->error("站内消息推送执行失败:" . $e->getMessage()); $this->saveErrorPushLog($user['user_type'],$data,$e->getMessage()); } return Result::ACK; } /** * 验证推送参数 * @param $push_data * @return bool */ private function checkPushParams($push_data): bool { // 用户id(接受者) if (!isset($push_data['user_id'])){ return false; } // 消息类型(1:医生服务通知 2:医生系统公告 3:患者系统消息) if (!isset($push_data['notice_type'])){ return false; } // 患者系统消息存在 if ($push_data['notice_type'] == 3){ // 系统消息类型(患者端系统消息存在 1:服务消息 2:福利消息 3:退款消息 4:物流消息) if (!isset($push_data['notice_system_type'])){ return false; } } // 医生服务通知存在 if ($push_data['notice_type'] == 1){ // 问诊类型(医生端服务通知存在 1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药) if (!isset($push_data['notice_system_type'])){ return false; } } // 消息标题 if (!isset($push_data['notice_title'])){ return false; } return true; } /** * 记录成功日志 * @param string|int $user_type 用户类型 * @param array $push_data 需发送的站内消息数据 * @return void */ private function saveSuccessPushLog(string|int $user_type,array $push_data): void { $data = array(); $data['to_user_id'] = $push_data['user_id']; $data['user_type'] = $user_type; $data['type'] = 1; $data['status'] = 1; $data['content'] = json_encode($push_data,JSON_UNESCAPED_UNICODE); $log_message_push = LogMessagePush::addLogMessagePush($data); if (empty($log_message_push)){ Log::getInstance()->error("站内消息推送成功,增加推送日志失败:" . json_encode($data,JSON_UNESCAPED_UNICODE)); } } /** * 记录成功日志 * @param string|int $user_type 用户类型 * @param array $push_data 需发送的站内消息数据 * @param string $fail_reason 失败原因 * @return void */ private function saveErrorPushLog(string|int $user_type,array $push_data,string $fail_reason): void { $data = array(); $data['to_user_id'] = $push_data['user_id']; $data['user_type'] = $user_type; $data['type'] = 1; $data['status'] = 2; $data['fail_reason'] = $fail_reason ?: ""; $data['content'] = json_encode($push_data,JSON_UNESCAPED_UNICODE); $log_message_push = LogMessagePush::addLogMessagePush($data); if (empty($log_message_push)){ Log::getInstance()->error("站内消息推送成功,增加推送日志失败:" . json_encode($data,JSON_UNESCAPED_UNICODE)); } } // private function saveMessageNotice(){ // $notice_data = array(); // $notice_data['user_id'] = $user['user_id']; // $notice_data['user_type'] = $user['user_type']; // $notice_data['notice_type'] = $data['notice_type']; // if (isset($data['notice_system_type'])){ // $notice_data['notice_system_type'] = $data['notice_system_type'] ; // } // // $notice_data['read_status'] = 0; // // if (isset($data['inquiry_type'])){ // $notice_data['inquiry_type'] = $data['inquiry_type'] ; // } // // if (isset($data['from_name'])){ // $notice_data['from_name'] = $data['from_name'] ; // } // // $notice_data['notice_title'] = $data['notice_title'] ; // $notice_data['notice_send_time'] = date('Y-m-d H:i:s',time()); // // if (isset($data['notice_content'])){ // $notice_data['notice_content'] = $data['notice_content'] ; // } // // if (isset($data['notice_content'])){ // $notice_data['notice_content'] = $data['notice_content'] ; // } // } }