From bd49332f640c1d35cfce625cfd437f687d578321 Mon Sep 17 00:00:00 2001 From: wucongxing <815046773@qq.com> Date: Thu, 9 Mar 2023 18:35:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=97=A0=E5=8C=BB=E7=94=9F?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Amqp/Consumer/AssignDoctorConsumer.php | 62 ++++++++++- app/Common/Common.php | 17 +++ app/Controller/CallBackController.php | 80 ++++++++++---- app/Controller/UserController.php | 32 +++--- app/Services/ImService.php | 123 +++++++++++++++++++++ app/Services/PatientDoctorService.php | 4 +- extend/TencentIm/Base.php | 2 +- extend/TencentIm/Message.php | 15 ++- extend/TencentIm/Profile.php | 45 +++++++- 9 files changed, 335 insertions(+), 45 deletions(-) create mode 100644 app/Services/ImService.php diff --git a/app/Amqp/Consumer/AssignDoctorConsumer.php b/app/Amqp/Consumer/AssignDoctorConsumer.php index ae155de..8a4406b 100644 --- a/app/Amqp/Consumer/AssignDoctorConsumer.php +++ b/app/Amqp/Consumer/AssignDoctorConsumer.php @@ -5,10 +5,14 @@ declare(strict_types=1); namespace App\Amqp\Consumer; use App\Model\OrderInquiry; +use App\Model\UserDoctor; +use App\Services\ImService; use App\Services\InquiryService; use App\Services\UserDoctorService; use App\Utils\Log; +use Extend\TencentIm\Message; use Extend\Wechat\WechatPay; +use GuzzleHttp\Exception\GuzzleException; use Hyperf\Amqp\Result; use Hyperf\Amqp\Annotation\Consumer; use Hyperf\Amqp\Message\ConsumerMessage; @@ -29,6 +33,7 @@ class AssignDoctorConsumer extends ConsumerMessage /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface + * @throws GuzzleException */ public function consumeMessage($data, AMQPMessage $message): string { @@ -166,12 +171,65 @@ class AssignDoctorConsumer extends ConsumerMessage Db::commit(); Log::getInstance()->error("分配医生 队列执行成功"); - // 加入消息通知队列 - return Result::ACK; } catch (\Exception $e) { Db::rollBack(); Log::getInstance()->error("队列执行失败原因:" . $e->getMessage()); return Result::REQUEUE; // 重回队列 } + + try { + // 专家-公益 + $ImService = new ImService(); + + // 检测并创建患者账号 + $ImService->createAccount($order_inquiry['user_id']); + + // 获取订单医生数据 + $params = array(); + $params['doctor_id'] = $doctor_id; + $user_doctor = UserDoctor::getOne($params); + if (empty($user_doctor)){ + Log::getInstance()->info("医生数据错误"); + return Result::ACK; + } + + // 检测并创建医生账号 + $ImService->createAccount($doctor_id); + + // 医生给患者发送消息 + $message = new Message(); + + $arg = array(); + $arg['From_Account'] = $user_doctor['user_id']; // 发送方user_id 如系统发送,无需填写 + $arg['To_Account'] = $order_inquiry['user_id']; // 接收方user_id + + $arg['MsgBody'] = [ + [ + "MsgType" => "TIMTextElem", + "MsgContent" => [ + "Text" => "等待医生接诊", + ], + ] + ]; + + // 自定义消息 + $cloud_custom_data = array(); + $cloud_custom_data['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; + $cloud_custom_data['inquiry_type'] = $order_inquiry['inquiry_type']; + $cloud_custom_data['message_type'] = 1;//1:系统发送 其余:用户发送 + + $arg['CloudCustomData'] = json_encode($cloud_custom_data,JSON_UNESCAPED_UNICODE); + + $message->sendMessage($arg); + + }catch (\Exception $e) { + // 验证失败 + Log::getInstance()->error("分配医生成功,发送系统问诊消息失败:" . $e->getMessage()); + // 加入消息通知队列 + return Result::ACK; + } + + // 加入消息通知队列 + return Result::ACK; } } diff --git a/app/Common/Common.php b/app/Common/Common.php index 7b99f58..daa418e 100644 --- a/app/Common/Common.php +++ b/app/Common/Common.php @@ -135,4 +135,21 @@ function addAliyunOssWebsite($path): string return ""; } return config('alibaba.oss.custom_domain_name') . '/' . $path; +} + +/** + * 转换性别至im识别的性别 + * @param string|int $sex + * @return string + */ +function sexToImSex(string|int $sex): string +{ + $result = "Gender_Type_Unknown"; + if ($sex == 1){ + $result = "Gender_Type_Male"; + }elseif ($sex == 2){ + $result = "Gender_Type_Female"; + } + + return $result; } \ No newline at end of file diff --git a/app/Controller/CallBackController.php b/app/Controller/CallBackController.php index 8d9ab44..3f4ae1a 100644 --- a/app/Controller/CallBackController.php +++ b/app/Controller/CallBackController.php @@ -3,11 +3,19 @@ namespace App\Controller; use App\Amqp\Producer\AssignDoctorProducer; +use App\Constants\DoctorTitleCode; use App\Constants\HttpEnumCode; use App\Exception\BusinessException; +use App\Model\Hospital; use App\Model\OrderInquiry; +use App\Model\UserDoctor; +use App\Model\UserPatient; use App\Services\BaseService; +use App\Services\ImService; use App\Utils\Log; +use Extend\TencentIm\Account; +use Extend\TencentIm\Message; +use Extend\TencentIm\Profile; use Extend\Wechat\WechatPay; use Hyperf\Amqp\Producer; use Hyperf\DbConnection\Db; @@ -25,8 +33,6 @@ class CallBackController extends AbstractController */ public function wxPaySuccessCallBack(): ResponseInterface { - Db::beginTransaction(); - try { // 处理支付结果事件 $WechatPay = new WechatPay(1); @@ -35,7 +41,6 @@ class CallBackController extends AbstractController $message = $server->getRequestMessage(); if (empty($message)){ - Db::rollBack(); return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "回调数据为空"], JSON_UNESCAPED_UNICODE)))); } @@ -45,7 +50,6 @@ class CallBackController extends AbstractController Log::getInstance()->info("微信支付回调数据:" . json_encode($message->toArray(),JSON_UNESCAPED_UNICODE)); if (empty($message['out_trade_no'])){ - Db::rollBack(); Log::getInstance()->info("微信支付回调数据错误"); return $server->serve(); } @@ -55,7 +59,6 @@ class CallBackController extends AbstractController $params['inquiry_no'] = $message['out_trade_no']; $order_inquiry = OrderInquiry::getOne($params); if (empty($order_inquiry)){ - Db::rollBack(); Log::getInstance()->info("非法订单"); return $server->serve(); } @@ -63,7 +66,6 @@ class CallBackController extends AbstractController // 验证订单状态 if ($order_inquiry['inquiry_status'] != 1){ // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消) - Db::rollBack(); Log::getInstance()->info("订单状态错误:当前为" . $order_inquiry['inquiry_status']); return $server->serve(); } @@ -103,8 +105,13 @@ class CallBackController extends AbstractController $params = array(); $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; OrderInquiry::edit($params,$data); + }catch (\Exception $e) { + // 验证失败 + Log::getInstance()->error("微信支付回调数据验证失败:" . $e->getMessage()); + return $this->wxPayErrorReturn($e->getMessage()); + } - // 处理分配医生问题 + try { if ($message['trade_state'] == "SUCCESS"){ if ($order_inquiry['inquiry_type'] == 2 || $order_inquiry['inquiry_type'] == 4){ // 快速-购药 @@ -116,27 +123,62 @@ class CallBackController extends AbstractController $producer = ApplicationContext::getContainer()->get(Producer::class); $result = $producer->produce($message); if (!$result) { - Db::rollBack(); Log::getInstance()->info("加入分配医生队列失败"); - return $this->wxPayErrorReturn("加入分配医生队列失败"); + return $server->serve(); } }elseif ($order_inquiry['inquiry_type'] == 1 || $order_inquiry['inquiry_type'] == 3){ // 专家-公益 - // 创建患者im账号 - // 检测医生im账号 - // 患者给医生发送im问诊消息 - // 消息订阅通知队列 + $ImService = new ImService(); + + // 检测并创建患者账号 + $ImService->createAccount($order_inquiry['user_id']); + + // 获取订单医生数据 + $params = array(); + $params['doctor_id'] = $order_inquiry['doctor_id']; + $user_doctor = UserDoctor::getOne($params); + if (empty($user_doctor)){ + Log::getInstance()->info("医生数据错误"); + return $server->serve(); + } + + // 检测并创建医生账号 + $ImService->createAccount($user_doctor['user_id']); + + // 医生给患者发送消息 + $message = new Message(); + + $arg = array(); + $arg['From_Account'] = $user_doctor['user_id']; // 发送方user_id 如系统发送,无需填写 + $arg['To_Account'] = $order_inquiry['user_id']; // 接收方user_id + + $arg['MsgBody'] = [ + [ + "MsgType" => "TIMTextElem", + "MsgContent" => [ + "Text" => "等待医生接诊", + ], + ] + ]; + + // 自定义消息 + $cloud_custom_data = array(); + $cloud_custom_data['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; + $cloud_custom_data['inquiry_type'] = $order_inquiry['inquiry_type']; + $cloud_custom_data['message_type'] = 1;//1:系统发送 其余:用户发送 + + $arg['CloudCustomData'] = json_encode($cloud_custom_data,JSON_UNESCAPED_UNICODE); + + $message->sendMessage($arg); } } - - Db::commit(); - return $server->serve(); }catch (\Exception $e) { // 验证失败 - Db::rollBack(); - Log::getInstance()->error("微信支付回调数据验证失败:" . $e->getMessage()); - return $this->wxPayErrorReturn($e->getMessage()); + Log::getInstance()->error("微信支付回调数据处理成功,发送系统问诊消息失败:" . $e->getMessage()); + return $server->serve(); } + + return $server->serve(); } public function wxPayRefundCallBack(): ResponseInterface diff --git a/app/Controller/UserController.php b/app/Controller/UserController.php index 8376800..2062bb9 100644 --- a/app/Controller/UserController.php +++ b/app/Controller/UserController.php @@ -125,7 +125,7 @@ class UserController extends AbstractController $group = new Group(); $friend = new Friend(); $message = new Message(); -// $result = $account->createAccount(1234567,'测试患者',"https://img.applets.igandanyiyuan.com/applet/doctor/avatar/03dc8df2-e7c0-4ad9-b87e-5133e40e0f76.jpg"); + $result = $account->createAccount(10009,'测试患者9',"https://img.applets.igandanyiyuan.com/applet/doctor/avatar/03dc8df2-e7c0-4ad9-b87e-5133e40e0f76.jpg"); // 查询账号导入状态 // $result = $account->checkAccountStatus("123456"); @@ -148,21 +148,23 @@ class UserController extends AbstractController // $result = $friend->addFriend("123456",'1234567',"快速问诊"); // 发送消息 - $arg = array(); - $arg['From_Account'] = "1234567"; // 发送方user_id 如系统发送,无需填写 - $arg['To_Account'] = "123456"; // 接收方user_id - $arg['ForbidCallbackControl'] = ['ForbidBeforeSendMsgCallback']; +// $arg = array(); +// $arg['From_Account'] = "1234567"; // 发送方user_id 如系统发送,无需填写 +// $arg['To_Account'] = "123456"; // 接收方user_id +// $arg['ForbidCallbackControl'] = ['ForbidBeforeSendMsgCallback']; +// +// $arg['MsgBody'] = [ +// [ +// "MsgType" => "TIMTextElem", +// "MsgContent" => [ +// "Text" => "测试消息1", +// ], +// ] +// ]; +// +// $result = $message->sendMessage($arg); - $arg['MsgBody'] = [ - [ - "MsgType" => "TIMTextElem", - "MsgContent" => [ - "Text" => "测试消息1", - ], - ] - ]; - - $result = $message->sendMessage($arg); + $result = $profile->getOneAccountPortraitList("123456"); dump($result); diff --git a/app/Services/ImService.php b/app/Services/ImService.php new file mode 100644 index 0000000..a590d69 --- /dev/null +++ b/app/Services/ImService.php @@ -0,0 +1,123 @@ +checkAccountStatus($user_patient['user_id']); + if (!$res){ + + // 创建单个账号 + $account->createAccount($user_id,$user_name,$avatar); + } + + // 医生检测并设置资料 + if ($user['user_type'] == 2){ + // 检测用户资料 + $profile = new Profile(); + $result = $profile->getOneAccountPortraitList("123456"); + if (!empty($result)){ + // 获取订单医生医院 + $params = array(); + $params['hospital_id'] = $user_doctor['hospital_id']; + $hospital = Hospital::getOne($params); + if (empty($hospital)){ + throw new BusinessException("医生医院数据错误"); + } + + // 设置用户资料 + $arg = array(); + // 性别 + if (in_array('Tag_Profile_IM_Gender',$result)){ + $arg['Tag_Profile_IM_Gender'] = sexToImSex($user_doctor['sex']); + } + + // 医院 + if (in_array('Tag_Profile_Custom_Hname',$result)){ + $arg['Tag_Profile_Custom_Hname'] = $hospital['hospital']; + } + + // 职称 + if (in_array('Tag_Profile_Custom_Hname',$result)){ + $arg['Tag_Profile_Custom_Title'] = $user_doctor['doctor_title'] ?? DoctorTitleCode::getMessage($user_doctor['doctor_title']); + } + + if (!empty($arg)){ + $profile->setProfile($user_doctor['user_id'],$arg); + } + } + } + + return true; + } catch (\Exception $e) { + throw new BusinessException($e->getMessage()); + } + + } + +} \ No newline at end of file diff --git a/app/Services/PatientDoctorService.php b/app/Services/PatientDoctorService.php index cdfda8d..efaf78b 100644 --- a/app/Services/PatientDoctorService.php +++ b/app/Services/PatientDoctorService.php @@ -493,8 +493,8 @@ class PatientDoctorService extends BaseService $data['is_online'] = $recommend_doctor['is_online']; $data['is_img_welfare_reception'] = $recommend_doctor['is_img_welfare_reception']; $data['be_good_at'] = $recommend_doctor['be_good_at']; - $data['hospital_name'] = $recommend_doctor['Hospital']['hospital_name']; - $data['hospital_level_name'] = $recommend_doctor['Hospital']['hospital_level_name']; + $data['hospital_name'] = $recommend_doctor['Hospital']['hospital_name'] ?? ""; + $data['hospital_level_name'] = $recommend_doctor['Hospital']['hospital_level_name'] ?? ""; // 处理接诊价格 $data['price'] = 0; diff --git a/extend/TencentIm/Base.php b/extend/TencentIm/Base.php index 6fbd47b..8dc1741 100644 --- a/extend/TencentIm/Base.php +++ b/extend/TencentIm/Base.php @@ -94,7 +94,7 @@ class Base } $content = json_decode($response->getBody(),true); - dump($content); + if (empty($content)){ throw new BusinessException("请求失败"); } diff --git a/extend/TencentIm/Message.php b/extend/TencentIm/Message.php index 4d4af0a..0eb821c 100644 --- a/extend/TencentIm/Message.php +++ b/extend/TencentIm/Message.php @@ -4,6 +4,7 @@ namespace Extend\TencentIm; use App\Constants\HttpEnumCode; use App\Exception\BusinessException; +use GuzzleHttp\Exception\GuzzleException; use Hyperf\Snowflake\IdGeneratorInterface; use Hyperf\Utils\ApplicationContext; @@ -69,6 +70,7 @@ class Message extends Base * 如聊天类型。json格式 * order_inquiry_id * inquiry_type + * message_type 消息类型(1:系统发送 其余:用户发送) */ 'CloudCustomData' => "", @@ -76,7 +78,14 @@ class Message extends Base } - public function sendMessage(array $arg){ + /** + * 发送消息 + * @param array $arg + * @return bool + * @throws GuzzleException + */ + public function sendMessage(array $arg): bool + { try { // 合并发送参数 @@ -86,9 +95,9 @@ class Message extends Base "json"=> $arg ]; - dump($options); $path = $this->config['base_url'] . $this->version . "/openim/sendmsg?" . $this->buildRequestParams(); - return $this->postRequest($path,$options); + $this->postRequest($path,$options); + return true; }catch (\Exception $e) { throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR); } diff --git a/extend/TencentIm/Profile.php b/extend/TencentIm/Profile.php index dd2f153..65dde6b 100644 --- a/extend/TencentIm/Profile.php +++ b/extend/TencentIm/Profile.php @@ -21,10 +21,10 @@ class Profile extends Base * 自定义字段:控制台->应用配置>功能配置->新增 * Tag_Profile_Custom_Hname:医院名称 * Tag_Profile_Custom_Title:医生职称 - * @return array + * @return bool * @throws GuzzleException */ - public function setProfile(string $user_id,array $arg): array + public function setProfile(string $user_id,array $arg): bool { try { $profileItem = []; @@ -43,7 +43,46 @@ class Profile extends Base ]; $path = $this->config['base_url'] . $this->version . "/profile/portrait_set?" . $this->buildRequestParams(); - return $this->postRequest($path,$options); + $this->postRequest($path,$options); + + return true; + }catch (\Exception $e) { + throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR); + } + } + + /** + * 拉取一个账户的多个资料 + * @param string $user_id + * @param array $arg + * @return array|string[] + * @throws GuzzleException + */ + public function getOneAccountPortraitList(string $user_id,array $arg = ['Tag_Profile_IM_Gender','Tag_Profile_Custom_Hname','Tag_Profile_Custom_Title']): array + { + try { + $options = [ + "json"=> [ + "To_Account" => [$user_id], + "TagList" => $arg, + ] + ]; + + $path = $this->config['base_url'] . $this->version . "/profile/portrait_get?" . $this->buildRequestParams(); + $result = $this->postRequest($path,$options); + if (!empty($result['UserProfileItem'])){ + foreach ($result['UserProfileItem'] as $user_profile_item){ + if(!empty($user_profile_item['ProfileItem'])){ + $result_arg = array_column($user_profile_item['ProfileItem'],"Tag"); + } + } + } + + if (empty($result_arg)){ + return $arg; + } + + return array_diff($arg,$result_arg); }catch (\Exception $e) { throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR); }