作者 lyh
<?php
namespace App\Console\Commands\Task;
use App\Mail\TextMail;
use App\Models\Subscribe\GroupSendTask;
use App\Models\Subscribe\GroupSendTaskLog;
use App\Models\Subscribe\Smtp;
use App\Models\Workchat\MessagePush;
use App\Services\Aside\ProjectAssociation\ProjectAssociationServices;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;
/**
*
* Class WorkchatMessageSend
* @package App\Console\Commands
* @author zbj
* @date 2023/11/28
*/
class WorkchatMessageSend extends Command
{
protected $signature = 'workchat_message_send';
protected $description = '企微消息推送任务';
public function handle()
{
while (true) {
$tasks = MessagePush::where('status', MessagePush::STATUS_PENDING)
->where(function ($query) {
$query->whereNull('send_time')->orWhere('send_time', '<=', date('Y-m-d H:i:s'));
})
->get();
if (!$tasks->count()) {
sleep(5);
}
foreach ($tasks as $task) {
$this->output('开始推送消息' . $task->id);
try {
ProjectAssociationServices::getInstance()->sendMessage($task->friend_id, $task->content, $task->content_type);
$this->output('推送消息' . $task->id . '成功');
$task->status = MessagePush::STATUS_SUCCESS;
}catch (\Exception $e){
$this->output('推送消息' . $task->id . '失败:' . $e->getMessage());
$task->status = MessagePush::STATUS_ERROR;
$task->remark = mb_substr($e->getMessage(), 0 , 200);
}
$task->save();
}
}
}
/**
* 输出处理日志
*/
public function output($message): bool
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
return true;
}
}
... ...
... ... @@ -5,6 +5,7 @@ namespace App\Console\Commands\Update;
use App\Helper\Arr;
use App\Http\Logic\Bside\News\NewsLogic;
use App\Http\Logic\Bside\Product\CategoryLogic;
use App\Jobs\SyncImageFileJob;
use App\Models\Blog\Blog;
use App\Models\Collect\CollectSource;
use App\Models\Collect\CollectTask;
... ... @@ -1062,6 +1063,9 @@ class ProjectUpdate extends Command
'updated_at' => date('Y-m-d H:i:s'),
]);
//同步到177
$new_url_name = basename($new_url);
SyncImageFileJob::dispatch(['path' => str_replace('/' . $new_url_name, '', $new_url), 'name' => $new_url_name, 'location' => 0]);
return getImageUrl($new_url);
} else {
return $url_complete;
... ...
... ... @@ -25,7 +25,7 @@ class InquiryController extends BaseController
{
public function form_list(){
$list = InquiryForm::where('is_default', 0)->get();
$list = InquiryForm::where('is_default', 0)->orderBy('title', 'desc')->get();
$data = $list->toArray();
foreach ($data as &$item){
$field_text = [];
... ...
... ... @@ -103,6 +103,11 @@ class InquiryForm extends Base
!$has_file && $model->is_default = 1;
}
//移动端询盘表单
if($sign == md5(json_encode(["__amp_source_origin", "message", "name", "phone"]))){
$model->title = '移动端询盘';
}
$model->save();
}
return $model->id;
... ...
... ... @@ -89,7 +89,7 @@ class InquiryFormData extends Base
LogUtils::info('询盘提交', $data, $res?: []);
}
}
return true;
return $model->id;
}
public function setDataAttribute($value)
... ...
<?php
namespace App\Models\Workchat;
use App\Helper\Arr;
use App\Models\Base;
use App\Models\Inquiry\InquiryFormData;
use App\Models\ProjectAssociation\ProjectAssociation;
use App\Services\ProjectServer;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
/**
* Class MessagePush
* @package App\Models\Workchat
* @author zbj
* @date 2024/9/7
*/
class MessagePush extends Base
{
use SoftDeletes;
const STATUS_PENDING = 0;
const STATUS_SUCCESS = 1;
const STATUS_ERROR = 9;
const TYPE_INQUIRY = 'inquiry';
//设置关联表名
/**
* @var mixed
*/
protected $table = 'gl_workchat_message_push';
public static function addInquiryMessage($id, $project_id, $country, $submit_at){
if(!ProjectServer::useProject($project_id)){
return false;
}
//项目是否有绑定群
$friend_id = ProjectAssociation::where('project_id', $project_id)
->where('status', ProjectAssociation::STATUS_NORMAL)
->where('binding_app', ProjectAssociation::ENTERPRISE_WECHAT)
->value('friend_id');
if(!$friend_id){
return false;
}
//9-21 点,每条消息及时通知
//21-第二天 9 点,整合一起通知
$hour = date('H', strtotime($submit_at));
if($hour >= 9 && $hour < 21) {
$model = new self();
$model->project_id = $project_id;
$model->friend_id = $friend_id;
$model->type = self::TYPE_INQUIRY;
$model->ref_ids = $id;
$model->content = '[' . date('H:i', strtotime($submit_at)) . '] 您的站点收到来自【' . $country . '】的询盘信息,请登录全球搜后台进行查看!';
}else{
//定时发送时间
$send_time = $hour >= 9 ? date('Y-m-d 09:00:00', strtotime('+1 day')) : date('Y-m-d 09:00:00');
$model = self::where('project_id', $project_id)->where('type', self::TYPE_INQUIRY)->where('send_time', $send_time)->first();
if(!$model){
$model = new self();
$model->project_id = $project_id;
$model->friend_id = $friend_id;
$model->type = self::TYPE_INQUIRY;
$model->ref_ids = $id;
$model->send_time = $send_time;
$model->content = '[09:00] 您的站点收到来自【' . $country . '】的询盘信息,请登录全球搜后台进行查看!';
}else{
$ref_ids = explode(',', $model->ref_ids);
$ref_ids[] = $id;
$model->ref_ids = implode(',', $ref_ids);
$countries = InquiryFormData::whereIn('id', $ref_ids)->pluck('country')->toArray();
$count = count($countries);
$countries = array_unique($countries);
if(count($countries) > 3){
$country = implode(',', array_slice($countries, 0, 3)) . '...';
}else{
$country = implode(',', $countries);
}
$model->content = '[09:00] 您的站点收到来自【' . $country . '】'.$count.'条询盘信息,请登录全球搜后台进行查看!';
}
}
$model->save();
}
}
... ...
... ... @@ -176,6 +176,27 @@ class ProjectAssociationServices extends BaseService
}
/**
* 获取AI客服列表
* @throws \Exception
* @author zbj
* @date 2024/9/7
*/
public function sendMessage($friend_id, $content, $type = 'Text'){
$param = [
'friend_id' => $friend_id,
'content' => $content,
'type' => $type,
];
$param['sign'] = $this->getSign($param);
$url = 'https://hub.ai.cc/api/globalso_ai_customer_service/send_msg';
$result = Http::withoutVerifying()->post($url, $param)->json();
if(empty($result) || $result['status'] != 200){
throw new \Exception($result['message'] ?? '');
}
return true;
}
/**
* @param $data
* @return string
*/
... ...
... ... @@ -11,6 +11,7 @@ use App\Models\Project\Project;
use App\Models\Subscribe\Email;
use App\Models\SyncSubmitTask\SyncSubmitTask;
use App\Models\Visit\Visit;
use App\Models\Workchat\MessagePush;
use App\Utils\LogUtils;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\URL;
... ... @@ -143,11 +144,19 @@ class SyncSubmitTaskService
$data['referer'] = $this->handle_referer($data['referer']);
InquiryFormData::saveData($form_id, $data['domain'], $data['ip'], $data['country'], $data['referer'], $data['user_agent'], $data['submit_at'], $data['data']);
$id = InquiryFormData::saveData($form_id, $data['domain'], $data['ip'], $data['country'], $data['referer'], $data['user_agent'], $data['submit_at'], $data['data']);
//转化询盘
Visit::isInquiry($data['ip']);
//推送企微消息
try {
MessagePush::addInquiryMessage($id, $data['project_id'], $data['country'], $data['submit_at']);
}catch (\Exception $e){
LogUtils::error('询盘消息'.$id.'写入企微消息队列失败' . $e->getMessage());
}
return true;
}
... ...