123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\SourceQrcodes;
- use App\Models\SourceQrcodeWelcomeMsg;
- use App\Models\WelcomeMsg;
- use App\RedisModel;
- use App\Service\CustomerTagService;
- use App\Service\ExternalContactService;
- use App\Service\MaterialService;
- use App\Service\WelcomeMsgSendService;
- use App\Support\EmailQueue;
- use Illuminate\Console\Command;
- class WelcomeMsgSend extends Command
- {
- protected $signature = 'WelcomeMsgSend';
- protected $description = '发送新客户欢迎语';
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $beginTime = time();
- $this->info(date('m-d H:i:s') . ' 开始整理');
- while(true) {
- $result = $this->getMsg();
- if($result === false) {
- sleep(2);
- }
- $now = time();
- // 超过10分钟,主动停止循环
- if ($now - $beginTime > 600) {
- break;
- }
- }
- $this->info(date('Y-m-d H:i:s') . ' 整理结束');
- }
- private function getMsg()
- {
- try {
- # 取出新客户数据
- $dataJson = RedisModel::rPop(WelcomeMsg::WELCOME_USER_RDS);
- if(empty($dataJson))
- return false;
- $userInfo = json_decode($dataJson, true);
- Log::logInfo('此次取出的新客户数据', ['user_info' => $userInfo], 'WelcomeCodeSendTrace');
- $result = $this->send($userInfo);
- } catch (\Exception $e) {
- Log::logError('发送新客户欢迎语流程出现异常', [
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage()
- ], 'WelcomeMsgSend');
- return false;
- }
- return $result;
- }
- private function send($userInfo)
- {
- $corpid = isset($userInfo['corpid']) ? $userInfo['corpid'] : '';
- $userId = isset($userInfo['user_id']) ? $userInfo['user_id'] : '';
- $externalUserId = isset($userInfo['external_userid']) ? $userInfo['external_userid'] : '';
- $welcomeCode = isset($userInfo['welcome_code']) ? $userInfo['welcome_code'] : '';
- $createTime = time();
- if(!$corpid || !$userId || !$welcomeCode) {
- Log::logError('待发送欢迎语客户信息缺失', ['user_info' => $userInfo], 'WelcomeMsgSend');
- return false;
- }
- $state = $userInfo['state'] ?? null;
- # 查询满足条件的消息体和附件
- $defMsgSend = true; // 是否使用默认
- if (!is_null($state)) {
- // 是否符合渠道
- $sqId = SourceQrcodes::query()
- ->where('corpid', $corpid)
- ->where('state', $state)
- ->where('enable', 1)
- ->value('id');
- if (!empty($sqId)) {
- list($content, $attachments, $ruleId) = SourceQrcodeWelcomeMsg::getMatchSqMsg($sqId, $createTime);
- if (!empty($content) || !empty($attachments)) {
- $defMsgSend = false;
- }
- }
- }
- if ($defMsgSend) {
- // 使用默认
- list($content, $attachments, $ruleId) = WelcomeMsgSendService::getMatchMsg($corpid, $userId, $createTime);
- if(empty($content) && empty($attachments)) {
- return false;
- }
- }
- # 替换文本消息中的用户昵称占位符
- if(!empty($content) && (strstr($content, '%NICKNAME%') !== false))
- $content = WelcomeMsgSendService::nicknameReplace($corpid, $externalUserId, $content);
- Log::logInfo('发送的消息体和附件的处理结果', [
- 'content' => $content,
- 'attachment' => $attachments,
- 'user_info' => $userInfo
- ], 'WelcomeCodeSendTrace');
- # 处理附件内容中的素材ID
- $attachments = json_decode($attachments, true);
- $attachments = MaterialService::mediaIdReplace($attachments, $corpid);
- Log::logInfo('附件内容:', ['attachment' => $attachments, 'user_info' => $userInfo], 'WelcomeCodeSendTrace');
- # 处理附件中的雷达信息
- $channel = $defMsgSend ? 3 : 4;
- $attachments = MaterialService::radarAttachment($attachments, $corpid, $userId, $channel, $ruleId, 2);
- Log::logInfo('【雷达信息处理后】附件内容:', ['attachment' => $attachments, 'user_info' => $userInfo], 'WelcomeCodeSendTrace');
- # 处理附件中的小程序链接
- $attachments = WelcomeMsgSendService::MiniProgramPathDeal($corpid, $userId, $attachments, $externalUserId);
- Log::logInfo('【小程序跳转链接处理后】附件内容:', ['attachment' => $attachments, 'user_info' => $userInfo], 'WelcomeCodeSendTrace');
- # 发送客户欢迎语
- $responseData = WelcomeMsgSendService::sendMsg($corpid, $welcomeCode, $content, $attachments);
- Log::logInfo('欢迎语发送响应结果:', ['response' => $responseData, 'user_info' => $userInfo], 'WelcomeCodeSendTrace');
- # 将数据放入队列,处理剧集标签信息
- # 2024-01-16日注释,因为仅可处理柚子嘉书迈步三个平台的推广链接
- // RedisModel::lPush(CustomerTagService::CUSTOMER_PLAYLET_TAG_LIST, json_encode([
- // 'corpid' => $corpid,
- // 'user_id' => $userId,
- // 'external_userid' => $externalUserId,
- // 'attachments' => $attachments,
- // ]));
- if(isset($responseData['errcode']) && $responseData['errcode']) {
- if(!in_array($responseData['errcode'], [41051, 41096])) {
- # 获取入队列时间
- $pushTime = $userInfo['push_time'] ?? null;
- if(!$pushTime || (time() - strtotime($pushTime)) > 20) {
- EmailQueue::rPush(date('H:i:s') . '新用户欢迎语发送失败', $pushTime . $welcomeCode . json_encode($responseData, JSON_UNESCAPED_UNICODE), ['xiaohua.hou@kuxuan-inc.com'], '新用户欢迎语发送失败');
- }
- }
- Log::logError('新用户欢迎语发送失败', [
- 'code' => $responseData['errcode'],
- 'msg' => $responseData['errmsg'],
- 'corpid' => $corpid,
- 'userId' => $userId,
- 'push_time' => $userInfo['push_time'] ?? null,
- 'welcome_code' => $welcomeCode
- ], 'WelcomeMsgSend');
- return false;
- }
- return true;
- }
- }
|