From 6730f11ccac542192607c0c36545ea77aef4da20 Mon Sep 17 00:00:00 2001 From: mzl Date: Sat, 23 Aug 2025 16:52:08 +0800 Subject: [PATCH 1/5] tz --- app/common/utils/email/Emails.php | 2 +- app/controller/IndexController.php | 23 ++---- app/queue/redis/InquiryToCrm.php | 7 -- app/queue/redis/InquiryToCrmNew.php | 112 ++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 23 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..766344a 100644 --- a/app/controller/IndexController.php +++ b/app/controller/IndexController.php @@ -116,27 +116,20 @@ 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', 25998)->where('sync_crm', 0)->first(); - // return json($data); + var_dump($info); - // $phone = ''; - // $email = ''; - - // $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); + return json(['code' => 0, 'msg' => 'success']); } } \ No newline at end of file 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..a076f30 --- /dev/null +++ b/app/queue/redis/InquiryToCrmNew.php @@ -0,0 +1,112 @@ +where('id', $data['id'])->where('sync_crm', 0)->first(); + + + + if ($info) { + // 获取用户信息 + $recipient = 0; + $corpId = 0; + $uinfo = null; + if ($info->recipient) { + $recipient = $info->recipient; + $uinfo = User::where('id', $info->recipient)->first(); + if ($uinfo) { + $corpId = $uinfo->corp_id; + } + } + // 同步客户数据 + $crmCustomer = new CrmCustomer(); + $crmCustomer->sn = $info->order_no; + $crmCustomer->corp_abbr = $corpId; + $crmCustomer->user_id = $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 ($uinfo && $uinfo->email) { + // $options = [ + // 'id' => $info->id, + // 'email' => $uinfo->email + // ]; + // // Log::info('发送邮件消息:' . json_encode($options)); + // \Webman\RedisQueue\Redis::send('email-message', $options); + // } + + $wsdata = [ + 'sender_id' => 0, + 'sender_type' => 'system', + 'title' => '有新的客户线索,' . $info->order_no . ',请及时处理', + 'content' => '有新的客户线索,' . $info->order_no . ',请及时处理', + 'scene' => 1, + 'recipient' => [$info->recipient] + ]; + // 异步推送 + \Webman\RedisQueue\Redis::send('notice', $wsdata); + } + } catch (\Throwable $th) { + throw $th; + } + } + + // 消费失败回调 + /* + $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 From 118c8d99f2e6f97f35fe6979c8ca3b6f147bc6a8 Mon Sep 17 00:00:00 2001 From: beiman <1696136552@qq.com> Date: Mon, 25 Aug 2025 09:21:28 +0800 Subject: [PATCH 2/5] 11 --- app/controller/IndexController.php | 17 ++- app/queue/redis/InquiryToCrmNew.php | 167 ++++++++++++++++++---------- 2 files changed, 123 insertions(+), 61 deletions(-) diff --git a/app/controller/IndexController.php b/app/controller/IndexController.php index 766344a..ba87d98 100644 --- a/app/controller/IndexController.php +++ b/app/controller/IndexController.php @@ -126,9 +126,22 @@ class IndexController // \Webman\RedisQueue\Redis::send('notice', $data); - $info = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('id', 25998)->where('sync_crm', 0)->first(); + $info = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('id', 1)->where('sync_crm', 0)->first(); - var_dump($info); + var_dump(empty($info->recipient)); + + if ($info) { + var_dump(11); + } else { + var_dump(22); + } + + + if ($info !== NULL) { + var_dump(33); + } else { + var_dump(44); + } return json(['code' => 0, 'msg' => 'success']); } diff --git a/app/queue/redis/InquiryToCrmNew.php b/app/queue/redis/InquiryToCrmNew.php index a076f30..789d10d 100644 --- a/app/queue/redis/InquiryToCrmNew.php +++ b/app/queue/redis/InquiryToCrmNew.php @@ -10,8 +10,9 @@ use app\common\model\User; use app\common\utils\FeishuMessage; use support\exception\BusinessException; use support\Log; +use support\Model; -class InquiryToCrm implements Consumer +class InquiryToCrmNew implements Consumer { // 要消费的队列名 public $queue = 'inquiry-to-crm-new'; @@ -22,73 +23,121 @@ class InquiryToCrm implements Consumer // 消费 public function consume($data) { + + /* + 询单 与 CRM 之间的同步场景描述 + + 添加(询单) + 添加 不发 不分 (不同步)1 + 添加 发了 不分 (要同步)2 + 添加 不发 分了 (要同步)3 + 添加 不发 直分 (要同步)4 + + 更新(询单) + 没发 没分 更新 (不同步)5 + 没发 分过 更新 (要同步)6 + 发过 没分 更新 (要同步)7 + 发过 分过 更新 (要同步)8 + */ + try { // 获取询盘信息 - $info = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('id', $data['id'])->where('sync_crm', 0)->first(); + $info = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('id', $data['id'])->first(); - - - if ($info) { - // 获取用户信息 - $recipient = 0; - $corpId = 0; - $uinfo = null; - if ($info->recipient) { - $recipient = $info->recipient; - $uinfo = User::where('id', $info->recipient)->first(); - if ($uinfo) { - $corpId = $uinfo->corp_id; - } - } - // 同步客户数据 - $crmCustomer = new CrmCustomer(); - $crmCustomer->sn = $info->order_no; - $crmCustomer->corp_abbr = $corpId; - $crmCustomer->user_id = $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 ($uinfo && $uinfo->email) { - // $options = [ - // 'id' => $info->id, - // 'email' => $uinfo->email - // ]; - // // Log::info('发送邮件消息:' . json_encode($options)); - // \Webman\RedisQueue\Redis::send('email-message', $options); - // } - - $wsdata = [ - 'sender_id' => 0, - 'sender_type' => 'system', - 'title' => '有新的客户线索,' . $info->order_no . ',请及时处理', - 'content' => '有新的客户线索,' . $info->order_no . ',请及时处理', - 'scene' => 1, - 'recipient' => [$info->recipient] - ]; - // 异步推送 - \Webman\RedisQueue\Redis::send('notice', $wsdata); + // 无论添加 或 更新,询单必然要存在 + if (!$info) { + throw new BusinessException('询盘不存在:' . $data['id']); } + + if ($info->status == 0 && $info->recipient == 0) { + // 如果询单未发、未分,则不与crm同步,不做任何处理 + return true; + } + + // 更改库信息 + $this->syncCustomer($info); + + $wsdata = [ + 'sender_id' => 0, + 'sender_type' => 'system', + 'title' => '有新的客户线索,' . $info->order_no . ',请及时处理', + 'content' => '有新的客户线索,' . $info->order_no . ',请及时处理', + 'scene' => 1, + 'recipient' => [$info->recipient] + ]; + // 异步推送 + \Webman\RedisQueue\Redis::send('notice', $wsdata); } catch (\Throwable $th) { throw $th; } } + + public function syncCustomer(Model $info) + { + $uInfo = $this->recipientInfo($info); + $crmCustomerInfo = CrmCustomer::where('sn', $info->order_no)->first(); + if ($crmCustomerInfo) { + $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('同步失败,创建客户线索失败'); + } + } + 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 = [ From 635027b97c6cba969663f0b2f4fb2cf7d762cb75 Mon Sep 17 00:00:00 2001 From: mzl Date: Mon, 25 Aug 2025 15:41:07 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E8=AF=A2=E5=8D=95=E4=B8=8Ecrm=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/queue/redis/InquiryToCrmNew.php | 65 ++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/app/queue/redis/InquiryToCrmNew.php b/app/queue/redis/InquiryToCrmNew.php index 789d10d..420fe95 100644 --- a/app/queue/redis/InquiryToCrmNew.php +++ b/app/queue/redis/InquiryToCrmNew.php @@ -56,28 +56,54 @@ class InquiryToCrmNew implements Consumer // 更改库信息 $this->syncCustomer($info); - - $wsdata = [ - 'sender_id' => 0, - 'sender_type' => 'system', - 'title' => '有新的客户线索,' . $info->order_no . ',请及时处理', - 'content' => '有新的客户线索,' . $info->order_no . ',请及时处理', - 'scene' => 1, - 'recipient' => [$info->recipient] - ]; - // 异步推送 - \Webman\RedisQueue\Redis::send('notice', $wsdata); } 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, @@ -115,6 +141,23 @@ class InquiryToCrmNew implements Consumer 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, $recipient); + } + + if ($info->status == 2) { + // $notice = '分配'; + $title = '[询单·分配]新的客户线索【' . $info->order_no . '】,请及时处理'; + $content = '[询单·分配]新的客户线索【' . $info->order_no . '】,请及时处理'; + $recipient = [$info->recipient]; + $this->pushWs($title, $content, $recipient); + } } return true; } From adc1d9658d0eac7554e352a66ef79a68ca0d9af0 Mon Sep 17 00:00:00 2001 From: mzl Date: Mon, 25 Aug 2025 15:43:32 +0800 Subject: [PATCH 4/5] ts --- app/inquiry/service/InquiryLogService.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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; } From d6dae82157632bb4413742294ff91db8d417abd8 Mon Sep 17 00:00:00 2001 From: beiman <1696136552@qq.com> Date: Mon, 25 Aug 2025 16:03:34 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=AF=A2=E5=8D=95?= =?UTF-8?q?=E4=B8=8Ecrm=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/queue/redis/InquiryToCrmNew.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/app/queue/redis/InquiryToCrmNew.php b/app/queue/redis/InquiryToCrmNew.php index 420fe95..a350c7e 100644 --- a/app/queue/redis/InquiryToCrmNew.php +++ b/app/queue/redis/InquiryToCrmNew.php @@ -61,7 +61,7 @@ class InquiryToCrmNew implements Consumer } } - public function pushWs($title, $content, $recipient) + public function pushWs($title, $content, $recipient = []) { $wsdata = [ 'sender_id' => 0, @@ -84,21 +84,21 @@ class InquiryToCrmNew implements Consumer // 消息推送 if ($info->status == 2 && empty($crmCustomerInfo->user_id)) { // $notice = '分配'; - $title = '[询单·分配]新的客户线索【' . $info->order_no . '】,请及时处理'; - $content = '[询单·分配]新的客户线索【' . $info->order_no . '】,请及时处理'; + $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 . '有新的调整,请及时处理'; + $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 . '】,请及时处理'; + $title = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; + $content = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; $recipient = [$info->recipient]; $this->pushWs($title, $content, $recipient); } @@ -145,16 +145,16 @@ class InquiryToCrmNew implements Consumer // 消息推送 if ($info->status == 1) { // $notice = '发布'; - $title = '[询单·公海]新的客户线索【' . $info->order_no . '】发布'; - $content = '[询单·公海]新的客户线索【' . $info->order_no . '】发布'; + $title = '[询单·公海] 新的客户线索【' . $info->order_no . '】发布'; + $content = '[询单·公海] 新的客户线索【' . $info->order_no . '】发布'; $recipient = [$info->recipient]; - $this->pushWs($title, $content, $recipient); + $this->pushWs($title, $content); } if ($info->status == 2) { // $notice = '分配'; - $title = '[询单·分配]新的客户线索【' . $info->order_no . '】,请及时处理'; - $content = '[询单·分配]新的客户线索【' . $info->order_no . '】,请及时处理'; + $title = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; + $content = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; $recipient = [$info->recipient]; $this->pushWs($title, $content, $recipient); }