From b15c03f009cdcecbbc06a9b39ffd45ed731fb95e Mon Sep 17 00:00:00 2001 From: mzl Date: Mon, 25 Aug 2025 16:39:48 +0800 Subject: [PATCH] Merge branch 'develop' --- app/common/utils/email/Emails.php | 2 +- app/controller/IndexController.php | 34 ++-- app/inquiry/service/InquiryLogService.php | 10 +- app/queue/redis/InquiryToCrm.php | 7 - app/queue/redis/InquiryToCrmNew.php | 204 +++++++++++++++++++++ app/ticket/service/ConversationService.php | 43 +++-- 6 files changed, 254 insertions(+), 46 deletions(-) create mode 100644 app/queue/redis/InquiryToCrmNew.php diff --git a/app/common/utils/email/Emails.php b/app/common/utils/email/Emails.php index 6d67f1a..18b06c2 100644 --- a/app/common/utils/email/Emails.php +++ b/app/common/utils/email/Emails.php @@ -436,7 +436,7 @@ class Emails // 初始化 UID 轮询器 $uidRotator = new \app\common\utils\RedisUidRotator('email_parsed_result_20250804'); - $uidRotator->setUids([64, 63, 83, 93, 108, 118, 131, 132, 145, 150, 143, 156, 157, 158], false); + $uidRotator->setUids([64, 63, 83, 93, 108, 118, 132, 145, 150, 143, 156, 157, 158], false); EmailParsedResult::where('status', 0) ->chunkById($batchSize, function ($records) use ($delaySeconds, $throttleAfter, $throttleSeconds, $enterUser, $channelInfo, $uidRotator) { diff --git a/app/controller/IndexController.php b/app/controller/IndexController.php index 8e15d2a..ba87d98 100644 --- a/app/controller/IndexController.php +++ b/app/controller/IndexController.php @@ -116,27 +116,33 @@ class IndexController // 'recipient' => [1], // ]; - $data['recipient'] = [1, 74]; - // // 同步推送 - // (new MessageNotificationsService())->pushNotice($data); + // $data['recipient'] = [1, 74]; + // // // 同步推送 + // // (new MessageNotificationsService())->pushNotice($data); - // 异步推送 - \Webman\RedisQueue\Redis::send('notice', $data); + // // 异步推送 + // \Webman\RedisQueue\Redis::send('notice', $data); - return 'success'; + $info = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('id', 1)->where('sync_crm', 0)->first(); - // return json($data); + var_dump(empty($info->recipient)); - // $phone = ''; - // $email = ''; + if ($info) { + var_dump(11); + } else { + var_dump(22); + } - // $userByPhone = User::where('corp_id', 2)->where('phone', $phone)->where('phone', '!=', '')->first(); - // $userByEmail = User::where('corp_id', 2)->where('email', $email)->where('email', '!=', '')->first(); - // $user = $userByPhone ?: $userByEmail; - - // return json($user); + + if ($info !== NULL) { + var_dump(33); + } else { + var_dump(44); + } + + return json(['code' => 0, 'msg' => 'success']); } } \ No newline at end of file diff --git a/app/inquiry/service/InquiryLogService.php b/app/inquiry/service/InquiryLogService.php index 31c161a..8f189ea 100644 --- a/app/inquiry/service/InquiryLogService.php +++ b/app/inquiry/service/InquiryLogService.php @@ -262,7 +262,7 @@ class InquiryLogService */ protected function afterServiceRelease($res) { - RedisQueueRedis::send('inquiry-to-crm', $res); + RedisQueueRedis::send('inquiry-to-crm-new', $res); return $res; } @@ -271,7 +271,7 @@ class InquiryLogService */ protected function afterServiceAllocation($res) { - RedisQueueRedis::send('inquiry-to-crm', $res); + RedisQueueRedis::send('inquiry-to-crm-new', $res); return $res; } @@ -280,7 +280,7 @@ class InquiryLogService */ protected function afterServiceStore($res) { - RedisQueueRedis::send('inquiry-to-crm', $res); + RedisQueueRedis::send('inquiry-to-crm-new', $res); return $res; } @@ -290,7 +290,7 @@ class InquiryLogService protected function afterServiceUpdate($res) { if ($res['recipient']) { - RedisQueueRedis::send('inquiry-to-crm-update', $res); + RedisQueueRedis::send('inquiry-to-crm-new', $res); } return $res; } @@ -300,7 +300,7 @@ class InquiryLogService */ protected function afterServiceReAllocation($res) { - RedisQueueRedis::send('inquiry-to-crm-update', $res); + RedisQueueRedis::send('inquiry-to-crm-new', $res); return $res; } diff --git a/app/queue/redis/InquiryToCrm.php b/app/queue/redis/InquiryToCrm.php index 4d48d1c..4d7ca3e 100644 --- a/app/queue/redis/InquiryToCrm.php +++ b/app/queue/redis/InquiryToCrm.php @@ -37,13 +37,6 @@ class InquiryToCrm implements Consumer if ($uinfo) { $corpId = $uinfo->corp_id; } - // // 消息推送参数 分配 - // $scene = 'inquiry-person-allocation'; - // $receiveType = 'person'; - } else { - // // 消息推送参数 发布 - // $scene = 'inquiry-group-send'; - // $receiveType = 'group'; } // 同步客户数据 $crmCustomer = new CrmCustomer(); diff --git a/app/queue/redis/InquiryToCrmNew.php b/app/queue/redis/InquiryToCrmNew.php new file mode 100644 index 0000000..a350c7e --- /dev/null +++ b/app/queue/redis/InquiryToCrmNew.php @@ -0,0 +1,204 @@ +where('id', $data['id'])->first(); + + // 无论添加 或 更新,询单必然要存在 + if (!$info) { + throw new BusinessException('询盘不存在:' . $data['id']); + } + + if ($info->status == 0 && $info->recipient == 0) { + // 如果询单未发、未分,则不与crm同步,不做任何处理 + return true; + } + + // 更改库信息 + $this->syncCustomer($info); + } catch (\Throwable $th) { + throw $th; + } + } + + public function pushWs($title, $content, $recipient = []) + { + $wsdata = [ + 'sender_id' => 0, + 'sender_type' => 'system', + 'title' => $title, + 'content' => $content, + 'scene' => 1, + 'recipient' => $recipient + ]; + // 异步推送 + \Webman\RedisQueue\Redis::send('notice', $wsdata); + } + + public function syncCustomer(Model $info) + { + $uInfo = $this->recipientInfo($info); + $crmCustomerInfo = CrmCustomer::where('sn', $info->order_no)->first(); + if ($crmCustomerInfo) { + + // 消息推送 + if ($info->status == 2 && empty($crmCustomerInfo->user_id)) { + // $notice = '分配'; + $title = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; + $content = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; + $recipient = [$info->recipient]; + $this->pushWs($title, $content, $recipient); + } else { + if ($info->status == 2 && $crmCustomerInfo->user_id == $info->recipient) { + // $notice = '更新'; + $title = '[询单·更新] 客户线索' . $info->order_no . '有新的调整,请及时处理'; + $content = '[询单·更新] 客户线索' . $info->order_no . '有新的调整,请及时处理'; + $recipient = [$info->recipient]; + $this->pushWs($title, $content, $recipient); + } else { + // $notice = '重新分配'; + $title = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; + $content = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; + $recipient = [$info->recipient]; + $this->pushWs($title, $content, $recipient); + } + } + + $udata = [ + 'corp_abbr' => $uInfo['corpId'], + 'user_id' => $info->recipient, + 'channel_id' => $info->channel, + 'country_id' => $info->country, + 'magnitude' => $info->quality, + 'email' => $info->email, + 'phone' => $info->tel, + 'WhatsApp' => $info->whatsapp, + 'contacts' => $info->contacts, + 'desc_collect' => $info->content, + ]; + if (!$crmCustomerInfo->update($udata)) { + throw new BusinessException('同步失败,更新客户线索失败'); + } + } else { + $crmCustomer = new CrmCustomer(); + $crmCustomer->sn = $info->order_no; + $crmCustomer->corp_abbr = $uInfo['corpId']; + $crmCustomer->user_id = $uInfo['recipient']; + $crmCustomer->type = 1; + $crmCustomer->channel_id = $info->channel; + $crmCustomer->country_id = $info->country; + $crmCustomer->magnitude = $info->quality; + $crmCustomer->email = $info->email; + $crmCustomer->phone = $info->tel; + $crmCustomer->WhatsApp = $info->whatsapp; + $crmCustomer->contacts = $info->contacts; + $crmCustomer->desc_collect = $info->content; + $crmCustomer->enter_user_id = $info->enter_user_id; + $crmCustomer->obtain = time(); + // 修改同步CRM状态 + $info->sync_crm = 1; + // 保存 + if (!$crmCustomer->save() || !$info->save()) { + throw new BusinessException('同步失败,创建客户线索失败'); + } + + // 消息推送 + if ($info->status == 1) { + // $notice = '发布'; + $title = '[询单·公海] 新的客户线索【' . $info->order_no . '】发布'; + $content = '[询单·公海] 新的客户线索【' . $info->order_no . '】发布'; + $recipient = [$info->recipient]; + $this->pushWs($title, $content); + } + + if ($info->status == 2) { + // $notice = '分配'; + $title = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; + $content = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; + $recipient = [$info->recipient]; + $this->pushWs($title, $content, $recipient); + } + } + return true; + } + + public function recipientInfo(Model $info) + { + $recipient = 0; + $corpId = 0; + $uinfo = null; + if (!empty($info->recipient)) { + $recipient = $info->recipient; + $uinfo = User::where('id', $info->recipient)->first(); + if ($uinfo) { + $corpId = $uinfo->corp_id; + } + } + return [ + 'recipient' => $recipient, + 'corpId' => $corpId, + 'uinfo' => $uinfo + ]; + } + + // 消费失败回调 + /* + $package = [ + 'id' => 1357277951, // 消息ID + 'time' => 1709170510, // 消息时间 + 'delay' => 0, // 延迟时间 + 'attempts' => 2, // 消费次数 + 'queue' => 'send-mail', // 队列名 + 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 + 'max_attempts' => 5, // 最大重试次数 + 'error' => '错误信息' // 错误信息 + ] + */ + public function onConsumeFailure(\Throwable $e, $package) + { + echo "consume failure\n"; + echo $e->getMessage() . "\n"; + // 无需反序列化 + var_export($package); + } +} \ No newline at end of file diff --git a/app/ticket/service/ConversationService.php b/app/ticket/service/ConversationService.php index ea97cef..972938e 100644 --- a/app/ticket/service/ConversationService.php +++ b/app/ticket/service/ConversationService.php @@ -3,6 +3,7 @@ namespace app\ticket\service; use app\common\model\User; +use app\common\service\MessageNotificationsService; use app\common\service\OssService; use app\ticket\logic\CategorieLogic; use app\ticket\logic\ConversationLogic; @@ -54,43 +55,47 @@ class ConversationService $ticket = (new TicketLogic())->find($data['ticket_id']); $categorie= (new CategorieLogic())->find($ticket['categorie_id']); $categorieName = isset($categorie['translation']) ? $categorie['translation']['name'] : ''; - - $notice = [ - 'code' => $ticket['code'], - 'category' => $categorieName, - 'content' => $data['content'], - 'reply' => '', - ]; - //$emailDefault = 'app@hnxinyouhe.com'; //公共邮箱 - //$emailDefault = 'support@hnxinyouhe.com'; //公共邮箱 - $emailDefault = env('TICKET_EMAIL_DEFAULT'); - if (empty($conversation)) { - $email = $emailDefault; + + $userDefault = [1, 75, 78]; + $tiket_content = $ticket['content']; + if (empty($conversation)) { //没有沟通记录 + $title = '新工单提醒-'.$categorieName; + $notice_user_id = $userDefault; }else{ + $title = '工单回复'; $tiket = (new TicketLogic())->find($conversation['ticket_id']); if($data['user_id'] != $tiket['user_id']){ - $email = User::where('id',$tiket['user_id'])->value('email'); + $notice_user_id = [$tiket['user_id']]; }else{ $conversationLast = Conversation::where('ticket_id', '=', $conversation['ticket_id']) ->where('user_id', '!=', $data['user_id']) ->orderBy('id', 'desc') ->first(); - $email = (!empty($conversationLast['user_id']))?User::where('id',$conversationLast['user_id'])->value('email',$emailDefault):$emailDefault; + $notice_user_id = (!empty($conversationLast['user_id']))? [$conversationLast['user_id']]:$userDefault; } - $notice['content'] = $tiket['content']; + $tiket_content = $tiket['content']; $status_fanyi = array_map(fn($v) => "{{{$v}}}", array_flip(Constants::TICKET_STATUS)); $locale = locale(); $ticket_status = $locale == 'zh'? Constants::TICKET_STATUS_ZH:Constants::TICKET_STATUS_EN; $content = str_replace($status_fanyi,$ticket_status,$data['content']); - $notice['reply'] = '
  • 最新回复:' . $content . '
  • '; - } - if(!empty($emailDefault)){ - \app\common\utils\email\MailSend::send($email, '【XYH】新的工单提醒', replaceTpl($notice, 'work_order')); + $reply = $content?($content."\n".'工单内容:'):'' ; } + $notice = ['sender_id' => 0, //发送人ID 0默认系统 + 'sender_type' => 'system', //发送者类型 user admin system + 'title' => $title, //标题 + 'content' => ($reply??'') . $tiket_content, //内容 + 'scene' => 1, //场景 0系统公共 1系统通知 + 'recipient' => $notice_user_id, //接收人 空为全部 + 'push_method' => 0 //推送方式0系统推送1手动推送 + ]; + + // 异步推送 + \Webman\RedisQueue\Redis::send('notice', $notice); + unset($data['categorie_id']); unset($data['code']);