diff --git a/app/common/model/CrmCustomer.php b/app/common/model/CrmCustomer.php new file mode 100644 index 0000000..e5a40b7 --- /dev/null +++ b/app/common/model/CrmCustomer.php @@ -0,0 +1,49 @@ +orWhere('phone', $account) ->orWhere('email', $account); } -} +} \ No newline at end of file diff --git a/app/controller/IndexController.php b/app/controller/IndexController.php index 48977c2..c2a23df 100644 --- a/app/controller/IndexController.php +++ b/app/controller/IndexController.php @@ -13,9 +13,9 @@ use support\Cache; class IndexController { - protected $noNeedLogin = ['index', 'view', 'perm', 'createResource']; + protected $noNeedLogin = ['index', 'view', 'perm', 'createResource', 'test']; - protected $noNeedHeaderData = ['index', 'view', 'perm', 'createResource']; + protected $noNeedHeaderData = ['index', 'view', 'perm', 'createResource', 'test']; public function index(Request $request) { @@ -76,7 +76,7 @@ class IndexController // ], 'model' => [ 'module' => 'common', - 'name' => 'inquiry_channel' + 'name' => 'crm_customer' ], // 'service' => [ // 'module' => 'inquiry', @@ -90,10 +90,18 @@ class IndexController // 'module' => 'inquiry', // 'name' => 'country' // ], - 'table' => 'inquiry_channel' + 'table' => 'crm_customer' ]; $result = (new Generate($options))->doGenerate(); return $result; } + + + public function test() + { + $res = ['id'=>25714]; + \Webman\RedisQueue\Redis::send('inquiry-to-crm', $res); + return 'success'; + } } \ No newline at end of file diff --git a/app/inquiry/service/InquiryLogService.php b/app/inquiry/service/InquiryLogService.php index 05b371d..d8be346 100644 --- a/app/inquiry/service/InquiryLogService.php +++ b/app/inquiry/service/InquiryLogService.php @@ -7,6 +7,7 @@ use app\common\traits\crud\CrudServiceTrait; use app\common\model\InquiryLog; use support\exception\BusinessException; use support\Request; +use Webman\RedisQueue\Redis as RedisQueueRedis; class InquiryLogService { @@ -39,7 +40,7 @@ class InquiryLogService $data = $this->preProcess('Release', $data, $request); $config = $this->buildQueryConfig($request, 'release'); $updated = $this->logicUpdateBy($config, $data); - return $this->postProcess('Release', $updated); + return $this->postProcess('Release', $data); } /** @@ -50,7 +51,7 @@ class InquiryLogService $data = $this->preProcess('Allocation', $data, $request); $config = $this->buildQueryConfig($request, 'allocation'); $updated = $this->logicUpdateBy($config, $data); - return $this->postProcess('Allocation', $updated); + return $this->postProcess('Allocation', $data); } /** @@ -139,7 +140,7 @@ class InquiryLogService throw new BusinessException('数据不存在'); } if ($info->status != 0) { - throw new BusinessException('询单已发布'); + throw new BusinessException('询单已被发布'); } $data['r_time'] = time(); $data['status'] = 1; @@ -158,8 +159,29 @@ class InquiryLogService if ($info->status == 2) { throw new BusinessException('询单已被分配'); } + if ($info->status == 0) { + $data['r_time'] = time(); + } $data['t_time'] = time(); $data['status'] = 2; return $data; } + + /** + * 发布后置处理 + */ + protected function afterServiceRelease($res) + { + RedisQueueRedis::send('inquiry-to-crm', $res); + return $res; + } + + /** + * 分配后置处理 + */ + protected function afterServiceAllocation($res) + { + RedisQueueRedis::send('inquiry-to-crm', $res); + return $res; + } } \ No newline at end of file diff --git a/app/queue/redis/InquiryToCrm.php b/app/queue/redis/InquiryToCrm.php new file mode 100644 index 0000000..36e70dc --- /dev/null +++ b/app/queue/redis/InquiryToCrm.php @@ -0,0 +1,83 @@ +where('sync_crm', 0)->first(); + if ($info) { + // 获取用户信息 + $recipient = 0; + $corpId = 0; + 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 - 1; + $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; + // 修改同步CRM状态 + $info->sync_crm = 1; + // 保存 + if (!$crmCustomer->save() || !$info->save()) { + throw new BusinessException('同步失败~'); + } + } + } 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 diff --git a/config/plugin/webman/redis-queue/process.php b/config/plugin/webman/redis-queue/process.php index 9fac733..8474aee 100644 --- a/config/plugin/webman/redis-queue/process.php +++ b/config/plugin/webman/redis-queue/process.php @@ -5,7 +5,7 @@ return [ 'count' => getenv('REDIS_QUEUE_PROCESS_COUNT'), // 可以设置多进程同时消费 'constructor' => [ // 消费者类目录 - // 'consumer_dir' => app_path() . '/queue/redis' + 'consumer_dir' => app_path() . '/queue/redis', 'consumer_dir' => app_path() . getenv('REDIS_QUEUE_CONSUMER_DIR') ] ]