作者 赵彬吉

update

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Task;
  4 +
  5 +
  6 +use App\Mail\TextMail;
  7 +use App\Models\Subscribe\GroupSendTask;
  8 +use App\Models\Subscribe\GroupSendTaskLog;
  9 +use App\Models\Subscribe\Smtp;
  10 +use App\Models\Workchat\MessagePush;
  11 +use App\Services\Aside\ProjectAssociation\ProjectAssociationServices;
  12 +use Illuminate\Console\Command;
  13 +use Illuminate\Support\Facades\Config;
  14 +use Illuminate\Support\Facades\Mail;
  15 +
  16 +/**
  17 + *
  18 + * Class WorkchatMessageSend
  19 + * @package App\Console\Commands
  20 + * @author zbj
  21 + * @date 2023/11/28
  22 + */
  23 +class WorkchatMessageSend extends Command
  24 +{
  25 +
  26 + protected $signature = 'workchat_message_send';
  27 + protected $description = '企微消息推送任务';
  28 +
  29 + public function handle()
  30 + {
  31 + while (true) {
  32 + $tasks = MessagePush::where('status', MessagePush::STATUS_PENDING)
  33 + ->where(function ($query) {
  34 + $query->whereNull('send_time')->orWhere('send_time', '<=', date('Y-m-d H:i:s'));
  35 + })
  36 + ->get();
  37 + if (!$tasks->count()) {
  38 + sleep(5);
  39 + }
  40 + foreach ($tasks as $task) {
  41 + $this->output('开始推送消息' . $task->id);
  42 + try {
  43 + ProjectAssociationServices::getInstance()->sendMessage($task->friend_id, $task->content, $task->content_type);
  44 + $this->output('推送消息' . $task->id . '成功');
  45 + $task->status = MessagePush::STATUS_SUCCESS;
  46 + }catch (\Exception $e){
  47 + $this->output('推送消息' . $task->id . '失败:' . $e->getMessage());
  48 + $task->status = MessagePush::STATUS_ERROR;
  49 + $task->remark = mb_substr($e->getMessage(), 0 , 200);
  50 + }
  51 + $task->save();
  52 + }
  53 + }
  54 + }
  55 +
  56 + /**
  57 + * 输出处理日志
  58 + */
  59 + public function output($message): bool
  60 + {
  61 + echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
  62 + return true;
  63 + }
  64 +}
@@ -89,7 +89,7 @@ class InquiryFormData extends Base @@ -89,7 +89,7 @@ class InquiryFormData extends Base
89 LogUtils::info('询盘提交', $data, $res?: []); 89 LogUtils::info('询盘提交', $data, $res?: []);
90 } 90 }
91 } 91 }
92 - return true; 92 + return $model->id;
93 } 93 }
94 94
95 public function setDataAttribute($value) 95 public function setDataAttribute($value)
  1 +<?php
  2 +
  3 +namespace App\Models\Workchat;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Models\Base;
  7 +use App\Models\Inquiry\InquiryFormData;
  8 +use App\Models\ProjectAssociation\ProjectAssociation;
  9 +use App\Services\ProjectServer;
  10 +use Illuminate\Database\Eloquent\SoftDeletes;
  11 +use Illuminate\Support\Facades\Cache;
  12 +
  13 +/**
  14 + * Class MessagePush
  15 + * @package App\Models\Workchat
  16 + * @author zbj
  17 + * @date 2024/9/7
  18 + */
  19 +class MessagePush extends Base
  20 +{
  21 + use SoftDeletes;
  22 +
  23 + const STATUS_PENDING = 0;
  24 + const STATUS_SUCCESS = 1;
  25 + const STATUS_ERROR = 9;
  26 +
  27 + const TYPE_INQUIRY = 'inquiry';
  28 +
  29 + //设置关联表名
  30 + /**
  31 + * @var mixed
  32 + */
  33 + protected $table = 'gl_workchat_message_push';
  34 +
  35 +
  36 + public static function addInquiryMessage($id, $project_id, $country, $submit_at){
  37 + if(!ProjectServer::useProject($project_id)){
  38 + return false;
  39 + }
  40 +
  41 + //项目是否有绑定群
  42 + $friend_id = ProjectAssociation::where('project_id', $project_id)
  43 + ->where('status', ProjectAssociation::STATUS_NORMAL)
  44 + ->where('binding_app', ProjectAssociation::ENTERPRISE_WECHAT)
  45 + ->value('friend_id');
  46 + if(!$friend_id){
  47 + return false;
  48 + }
  49 +
  50 + //9-21 点,每条消息及时通知
  51 + //21-第二天 9 点,整合一起通知
  52 + $hour = date('H', strtotime($submit_at));
  53 + if($hour >= 9 && $hour < 21) {
  54 + $model = new self();
  55 + $model->project_id = $project_id;
  56 + $model->friend_id = $friend_id;
  57 + $model->type = self::TYPE_INQUIRY;
  58 + $model->ref_ids = $id;
  59 + $model->content = '[' . date('H:i', strtotime($submit_at)) . '] 您的站点收到来自【' . $country . '】的询盘信息,请登录全球搜后台进行查看!';
  60 + }else{
  61 + //定时发送时间
  62 + $send_time = $hour >= 9 ? date('Y-m-d 09:00:00', strtotime('+1 day')) : date('Y-m-d 09:00:00');
  63 +
  64 + $model = self::where('project_id', $project_id)->where('type', self::TYPE_INQUIRY)->where('send_time', $send_time)->first();
  65 + if(!$model){
  66 + $model = new self();
  67 + $model->project_id = $project_id;
  68 + $model->friend_id = $friend_id;
  69 + $model->type = self::TYPE_INQUIRY;
  70 + $model->ref_ids = $id;
  71 + $model->send_time = $send_time;
  72 + $model->content = '[09:00] 您的站点收到来自【' . $country . '】的询盘信息,请登录全球搜后台进行查看!';
  73 + }else{
  74 + $ref_ids = explode(',', $model->ref_ids);
  75 + $ref_ids[] = $id;
  76 + $model->ref_ids = implode(',', $ref_ids);
  77 + $countries = InquiryFormData::whereIn('id', $ref_ids)->pluck('country')->toArray();
  78 + $count = count($countries);
  79 + $countries = array_unique($countries);
  80 + if(count($countries) > 3){
  81 + $country = implode(',', array_slice($countries, 0, 3)) . '...';
  82 + }else{
  83 + $country = implode(',', $countries);
  84 + }
  85 + $model->content = '[09:00] 您的站点收到来自【' . $country . '】'.$count.'条询盘信息,请登录全球搜后台进行查看!';
  86 + }
  87 + }
  88 + $model->save();
  89 + }
  90 +}
@@ -176,6 +176,27 @@ class ProjectAssociationServices extends BaseService @@ -176,6 +176,27 @@ class ProjectAssociationServices extends BaseService
176 } 176 }
177 177
178 /** 178 /**
  179 + * 获取AI客服列表
  180 + * @throws \Exception
  181 + * @author zbj
  182 + * @date 2024/9/7
  183 + */
  184 + public function sendMessage($friend_id, $content, $type = 'Text'){
  185 + $param = [
  186 + 'friend_id' => $friend_id,
  187 + 'content' => $content,
  188 + 'type' => $type,
  189 + ];
  190 + $param['sign'] = $this->getSign($param);
  191 + $url = 'http://aicc-pro.local/api/globalso_ai_customer_service/send_msg';
  192 + $result = Http::withoutVerifying()->post($url, $param)->json();
  193 + if(empty($result) || $result['status'] != 200){
  194 + throw new \Exception($result['message'] ?? '');
  195 + }
  196 + return true;
  197 + }
  198 +
  199 + /**
179 * @param $data 200 * @param $data
180 * @return string 201 * @return string
181 */ 202 */
@@ -11,6 +11,7 @@ use App\Models\Project\Project; @@ -11,6 +11,7 @@ use App\Models\Project\Project;
11 use App\Models\Subscribe\Email; 11 use App\Models\Subscribe\Email;
12 use App\Models\SyncSubmitTask\SyncSubmitTask; 12 use App\Models\SyncSubmitTask\SyncSubmitTask;
13 use App\Models\Visit\Visit; 13 use App\Models\Visit\Visit;
  14 +use App\Models\Workchat\MessagePush;
14 use App\Utils\LogUtils; 15 use App\Utils\LogUtils;
15 use Illuminate\Support\Facades\Http; 16 use Illuminate\Support\Facades\Http;
16 use Illuminate\Support\Facades\URL; 17 use Illuminate\Support\Facades\URL;
@@ -143,11 +144,19 @@ class SyncSubmitTaskService @@ -143,11 +144,19 @@ class SyncSubmitTaskService
143 144
144 $data['referer'] = $this->handle_referer($data['referer']); 145 $data['referer'] = $this->handle_referer($data['referer']);
145 146
146 - InquiryFormData::saveData($form_id, $data['domain'], $data['ip'], $data['country'], $data['referer'], $data['user_agent'], $data['submit_at'], $data['data']); 147 + $id = InquiryFormData::saveData($form_id, $data['domain'], $data['ip'], $data['country'], $data['referer'], $data['user_agent'], $data['submit_at'], $data['data']);
147 148
148 //转化询盘 149 //转化询盘
149 Visit::isInquiry($data['ip']); 150 Visit::isInquiry($data['ip']);
150 151
  152 + //推送企微消息
  153 + try {
  154 + MessagePush::addInquiryMessage($id, $data['project_id'], $data['country'], $data['submit_at']);
  155 + }catch (\Exception $e){
  156 + LogUtils::error('询盘消息'.$id.'写入企微消息队列失败' . $e->getMessage());
  157 + }
  158 +
  159 +
151 return true; 160 return true;
152 } 161 }
153 162