Merge branch 'develop'

This commit is contained in:
mzl
2025-07-29 09:00:04 +08:00
parent a551507e4d
commit d74b374198
106 changed files with 6754 additions and 1346 deletions
+127
View File
@@ -0,0 +1,127 @@
<?php
namespace app\queue\redis;
use app\common\model\CorpFeishuApp;
use app\common\model\CrmCustomer;
use Webman\RedisQueue\Consumer;
use app\common\model\User;
use app\common\utils\FeishuMessage as FeishuMsg;
use support\exception\BusinessException;
use app\inquiry\service\InquiryLogService;
class FeishuMessage implements Consumer
{
// 要消费的队列名
public $queue = 'feishu-message';
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
public $connection = 'default';
// 消费
/**
* @param string $category 分类:crm | inquiry
* @param string $scene 场景:group-send | group-update | person-send | person-update
* @param string $id 数据ID:询单ID | 客户ID
* @param string $receive_type 接收者类型:group | person
*/
public function consume($data)
{
try {
if ($data['category'] === 'crm') {
// 客户线索
$crmCustomer = new CrmCustomer();
$info = $crmCustomer->where('id', $data['id'])->first();
// 跟单人
$reInfo = User::where('id', $info->user_id)->first();
// 飞书应用
$feishuApp = new CorpFeishuApp();
$feishuAppInfo = $feishuApp->where('corp_id', $reInfo->corp_id)->where('system_id', 1)->first();
// 推送数据
$options = [
'contacts' => $info->contacts,
'follow_last' => $info->follow_last ? date('Y-m-d H:i', $info->follow_last) : '',
'follow_next' => $info->follow_next ? date('Y-m-d H:i', $info->follow_next) : '',
'app_id' => $feishuAppInfo->app_id,
'app_secret' => $feishuAppInfo->app_secret,
'msg_type' => 'interactive',
'receive_id_type' => 'open_id',
'receive_id' => $reInfo->open_id
];
}
if ($data['category'] === 'inquiry') {
// 询单信息
$inquiryLogService = new InquiryLogService();
$info = $inquiryLogService->getInquiryLogBy($data['id']);
// 接受者处理
if ($data['receive_type'] == 'group') {
$appId = 'cli_a72f13d6c022500d';
$appSecret = 'Kxtbq70OQWrEhpltrufRZc76z7kjDqPT';
$receiveId = 'oc_4f574e6f8bc5b8f3e5175e67ebc5725c';
$receiveIdType = 'chat_id';
} else {
// 跟单人
$reInfo = User::where('id', $info->recipient)->first();
// 飞书应用
$feishuApp = new CorpFeishuApp();
$feishuAppInfo = $feishuApp->where('corp_id', $reInfo->corp_id)->where('system_id', 1)->first();
// 推送信息
$appId = $feishuAppInfo->app_id;
$appSecret = $feishuAppInfo->app_secret;
$receiveId = $reInfo->open_id;
$receiveIdType = 'open_id';
}
// 推送数据
$options = [
'order_no' => $info->order_no,
'quality' => $inquiryLogService->qualityName($info->quality),
'channel' => $info->channelInfo ? $info->channelInfo->name : '',
'country' => $info->countryInfo ? $info->countryInfo->cn_addr : '',
'enter_user' => $info->enter ? $info->enter->name : '',
'r_time' => $info->r_time ? date('Y-m-d H:i:s', $info->r_time) : '',
't_time' => $info->t_time ? date('Y-m-d H:i:s', $info->t_time) : '',
'app_id' => $appId,
'app_secret' => $appSecret,
'msg_type' => 'interactive',
'receive_id_type' => $receiveIdType,
'receive_id' => $receiveId
];
}
$feishuMsg = new FeishuMsg();
$feishuMsg->messageSend($data['scene'], $options);
} 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);
}
}
+101
View File
@@ -0,0 +1,101 @@
<?php
namespace app\queue\redis;
use app\inquiry\service\InquiryLogService;
use app\common\model\CrmCustomer;
use app\common\model\InquiryLog;
use Webman\RedisQueue\Consumer;
use app\common\model\User;
use app\common\utils\FeishuMessage;
use support\exception\BusinessException;
class InquiryToCrm implements Consumer
{
// 要消费的队列名
public $queue = 'inquiry-to-crm';
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
public $connection = 'default';
// 消费
public function consume($data)
{
try {
// 获取询盘信息
$info = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('id', $data['id'])->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;
}
// 消息推送参数 分配
$scene = 'inquiry-person-allocation';
$receiveType = 'person';
} else {
// 消息推送参数 发布
$scene = 'inquiry-group-send';
$receiveType = 'group';
}
// 同步客户数据
$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('同步失败~');
}
// 发送飞书消息
$options = [
'id' => $info->id,
'category' => 'inquiry',
'scene' => $scene,
'receive_type' => $receiveType
];
\Webman\RedisQueue\Redis::send('feishu-message', $options);
}
} 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);
}
}