飞书消息推送
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\controller;
|
||||
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use app\common\utils\FeishuMessage;
|
||||
use app\inquiry\service\InquiryLogService;
|
||||
use app\common\model\CrmCustomer;
|
||||
use app\common\model\InquiryLog;
|
||||
use app\common\model\User;
|
||||
use support\exception\BusinessException;
|
||||
|
||||
class NotifyController
|
||||
{
|
||||
|
||||
/**
|
||||
* 不用头部数据
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedHeaderData = ['feishuCallback', 'feishuEvent'];
|
||||
|
||||
/**
|
||||
* 不登录不鉴权
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedLogin = ['feishuCallback', 'feishuEvent'];
|
||||
|
||||
/**
|
||||
* 飞书回调
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function feishuCallback(Request $request)
|
||||
{
|
||||
try {
|
||||
$param = $request->post();
|
||||
|
||||
// 解析数据
|
||||
// $param = json_decode($param['event'], true);
|
||||
$orderSn = $param['event']['action']['value'];
|
||||
if (strpos($orderSn, '"') !== false) {
|
||||
$orderSn = str_replace('"', '', $orderSn);
|
||||
}
|
||||
|
||||
// 处理询单&客户线索信息
|
||||
$openId = $param['event']['operator']['open_id'];
|
||||
|
||||
// 获取询单
|
||||
$order = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('order_no', $orderSn)->first();
|
||||
if (!$order) {
|
||||
return $this->error('未找到询单记录');
|
||||
}
|
||||
|
||||
// 获取客户xians
|
||||
$customer = CrmCustomer::where('sn', $orderSn)->first();
|
||||
if (!$customer) {
|
||||
return $this->error('未找到客户线索');
|
||||
}
|
||||
|
||||
// 获取用户
|
||||
$uinfo = User::where('open_id', $openId)->first();
|
||||
|
||||
// 更新客户线索
|
||||
$customer->user_id = $uinfo->id;
|
||||
|
||||
// 更新询单数据
|
||||
$order->recipient = $uinfo->id;
|
||||
$order->status = 2;
|
||||
$order->t_time = time();
|
||||
|
||||
// 保存
|
||||
if (!$order->save() || !$customer->save()) {
|
||||
return $this->error('跟踪失败');
|
||||
}
|
||||
|
||||
// 获取飞书消息模板
|
||||
$feishuMessage = new FeishuMessage();
|
||||
$content = $feishuMessage->groupUpdateTemplates(
|
||||
(new InquiryLogService())->qualityName($order->quality),
|
||||
$order->order_no,
|
||||
$order->channelInfo ? $order->channelInfo->name : '',
|
||||
$order->countryInfo ? $order->countryInfo->cn_addr : '',
|
||||
$order->enter ? $order->enter->name : '',
|
||||
$order->r_time ? date('Y-m-d H:i:s', $order->r_time) : '',
|
||||
$order->t_time ? date('Y-m-d H:i:s', $order->t_time) : ''
|
||||
);
|
||||
|
||||
// 组装飞书消息模板
|
||||
$data = '{
|
||||
"toast": {
|
||||
"type": "success",
|
||||
"content": "操作成功"
|
||||
},
|
||||
"card": {
|
||||
"type": "raw",
|
||||
"data": '.$content.'
|
||||
}
|
||||
}';
|
||||
|
||||
// 响应消息
|
||||
return new Response(200, ['Content-Type' => 'application/json'], $data);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function error($msg = '')
|
||||
{
|
||||
$data = '{
|
||||
"toast": {
|
||||
"type": "error",
|
||||
"content": "'.$msg.'"
|
||||
}
|
||||
}';
|
||||
return new Response(200, ['Content-Type' => 'application/json'], $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 飞书事件
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function feishuEvent(Request $request)
|
||||
{
|
||||
try {
|
||||
$data = $request->post();
|
||||
|
||||
var_dump($data);
|
||||
|
||||
return json($data);
|
||||
} catch (\Throwable $e) {
|
||||
return json($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,7 @@ class CrmCustomer extends Model
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'sn', 'corp_abbr', 'user_id', 'user_sn', 'type', 'grade', 'sex', 'channel_id', 'country_id', 'car_type_id', 'magnitude', 'email', 'phone', 'WhatsApp', 'contacts', 'describe', 'desc_collect', 'address', 'lat', 'lng', 'follow_last', 'follow_next', 'warn', 'follow_remark', 'created_at', 'updated_at', 'deleted_at'];
|
||||
protected $fillable = ['id', 'sn', 'corp_abbr', 'user_id', 'user_sn', 'type', 'grade', 'sex', 'channel_id', 'country_id', 'car_type_id', 'magnitude', 'email', 'phone', 'WhatsApp', 'contacts', 'describe', 'desc_collect', 'address', 'lat', 'lng', 'follow_last', 'follow_next', 'warn', 'follow_remark', 'enter_user_id', 'created_at', 'updated_at', 'deleted_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
|
||||
@@ -65,12 +65,12 @@ class InquiryLog extends Model
|
||||
return $this->hasOne(User::class, 'id', 'recipient');
|
||||
}
|
||||
|
||||
public function channel()
|
||||
public function channelInfo()
|
||||
{
|
||||
return $this->hasOne(InquiryChannel::class, 'id', 'channel');
|
||||
}
|
||||
|
||||
public function country()
|
||||
public function countryInfo()
|
||||
{
|
||||
return $this->hasOne(Country::class, 'id', 'country');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,555 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\utils;
|
||||
|
||||
use Webman\Exception\BusinessException;
|
||||
|
||||
class FeishuMessage
|
||||
{
|
||||
|
||||
protected $app_id = 'cli_a72f13d6c022500d';
|
||||
protected $app_secret = 'Kxtbq70OQWrEhpltrufRZc76z7kjDqPT';
|
||||
protected $class = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->class = new Feishu($this->app_id, $this->app_secret);
|
||||
}
|
||||
|
||||
/**
|
||||
* 飞书消息模板
|
||||
* @param string $scene
|
||||
* @param array $options
|
||||
*/
|
||||
public function messageTemplates(string $scene = 'group-send', array $options = [])
|
||||
{
|
||||
try {
|
||||
$template = match ($scene) {
|
||||
'inquiry-group-send' => $this->groupNewTemplates(
|
||||
$options['quality'],
|
||||
$options['order_no'],
|
||||
$options['channel'],
|
||||
$options['country'],
|
||||
$options['enter_user'],
|
||||
$options['r_time']
|
||||
),
|
||||
'inquiry-group-update' => $this->groupUpdateTemplates(
|
||||
$options['quality'],
|
||||
$options['order_no'],
|
||||
$options['channel'],
|
||||
$options['country'],
|
||||
$options['enter_user'],
|
||||
$options['r_time'],
|
||||
$options['t_time']
|
||||
),
|
||||
'inquiry-person-allocation' => $this->personAllocationTemplates(
|
||||
$options['quality'],
|
||||
$options['order_no'],
|
||||
$options['channel'],
|
||||
$options['country'],
|
||||
$options['enter_user'],
|
||||
$options['r_time'],
|
||||
$options['t_time']
|
||||
),
|
||||
'crm-person-follow-up' => $this->personFollowUpTemplates(
|
||||
$options['contacts'],
|
||||
$options['follow_next'],
|
||||
$options['follow_last']
|
||||
)
|
||||
};
|
||||
} catch (\UnhandledMatchError $e) {
|
||||
throw new BusinessException($e->getMessage());
|
||||
}
|
||||
return $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送飞书消息
|
||||
* @param array $options
|
||||
*/
|
||||
public function messageSend(string $scene = 'group-send', array $options = [])
|
||||
{
|
||||
$template = $this->messageTemplates($scene, $options);
|
||||
$result = $this->class->sendMessages($options['receive_id'], $template, $options['msg_type'], $options['receive_id_type']);
|
||||
if (!isset($result['code']) || $result['code'] !== 0) {
|
||||
throw new BusinessException('发送飞书消息失败');
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 线索消息发送
|
||||
*/
|
||||
public function groupNewTemplates($quality = '💛 银牌商机', $order_no = 'SN2025072365640', $channel = '谷歌', $country = '白俄罗斯', $enter_user = '木子李', $r_time = '2025-07-24 09:41:23')
|
||||
{
|
||||
$content = '{
|
||||
"schema": "2.0",
|
||||
"config": {
|
||||
"update_multi": true,
|
||||
"style": {
|
||||
"text_size": {
|
||||
"normal_v2": {
|
||||
"default": "normal",
|
||||
"pc": "normal",
|
||||
"mobile": "heading"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"body": {
|
||||
"direction": "vertical",
|
||||
"horizontal_spacing": "8px",
|
||||
"vertical_spacing": "16px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top",
|
||||
"padding": "0px 20px 20px 20px",
|
||||
"elements": [
|
||||
{
|
||||
"tag": "hr",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
},
|
||||
{
|
||||
"tag": "column_set",
|
||||
"horizontal_align": "left",
|
||||
"columns": [
|
||||
{
|
||||
"tag": "column",
|
||||
"width": "weighted",
|
||||
"elements": [
|
||||
{
|
||||
"tag": "markdown",
|
||||
"content": " **'.$quality.'**\n- **编号:** '.$order_no.'\n- **渠道:** '.$channel.'\n- **国家:** '.$country.'\n- **发布人:** '.$enter_user.'\n- **发布时间:** '.$r_time.'",
|
||||
"text_align": "left",
|
||||
"text_size": "normal_v2",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
}
|
||||
],
|
||||
"vertical_spacing": "8px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top",
|
||||
"weight": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tag": "form",
|
||||
"elements": [
|
||||
{
|
||||
"tag": "hr",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
},
|
||||
{
|
||||
"tag": "column_set",
|
||||
"flex_mode": "stretch",
|
||||
"horizontal_spacing": "8px",
|
||||
"horizontal_align": "left",
|
||||
"columns": [
|
||||
{
|
||||
"tag": "column",
|
||||
"width": "auto",
|
||||
"elements": [
|
||||
{
|
||||
"tag": "button",
|
||||
"text": {
|
||||
"tag": "plain_text",
|
||||
"content": "立即跟踪"
|
||||
},
|
||||
"type": "primary_filled",
|
||||
"width": "fill",
|
||||
"size": "medium",
|
||||
"icon": {
|
||||
"tag": "standard_icon",
|
||||
"token": "list-check-bold_outlined"
|
||||
},
|
||||
"confirm": {
|
||||
"title": {
|
||||
"tag": "plain_text",
|
||||
"content": "确认跟踪?"
|
||||
},
|
||||
"text": {
|
||||
"tag": "plain_text",
|
||||
"content": "客户线索:'.$order_no.'"
|
||||
}
|
||||
},
|
||||
"behaviors": [
|
||||
{
|
||||
"type": "callback",
|
||||
"value": "'.$order_no.'"
|
||||
}
|
||||
],
|
||||
"form_action_type": "submit",
|
||||
"name": "Button_m7d0zqa2",
|
||||
"element_id": "track"
|
||||
}
|
||||
],
|
||||
"direction": "vertical",
|
||||
"horizontal_spacing": "8px",
|
||||
"vertical_spacing": "8px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top"
|
||||
},
|
||||
{
|
||||
"tag": "column",
|
||||
"width": "auto",
|
||||
"elements": [],
|
||||
"direction": "vertical",
|
||||
"horizontal_spacing": "8px",
|
||||
"vertical_spacing": "8px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top"
|
||||
},
|
||||
{
|
||||
"tag": "column",
|
||||
"width": "auto",
|
||||
"elements": [],
|
||||
"padding": "0px 0px 0px 0px",
|
||||
"direction": "vertical",
|
||||
"horizontal_spacing": "8px",
|
||||
"vertical_spacing": "8px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
}
|
||||
],
|
||||
"margin": "4px 0px 0px 0px"
|
||||
}
|
||||
],
|
||||
"direction": "vertical",
|
||||
"horizontal_spacing": "8px",
|
||||
"vertical_spacing": "12px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top",
|
||||
"padding": "0px 0px 0px 0px",
|
||||
"margin": "0px 0px 0px 0px",
|
||||
"name": "Form_m7d0zqa1"
|
||||
}
|
||||
]
|
||||
},
|
||||
"header": {
|
||||
"title": {
|
||||
"tag": "plain_text",
|
||||
"content": "新的【公海】客户线索"
|
||||
},
|
||||
"subtitle": {
|
||||
"tag": "plain_text",
|
||||
"content": ""
|
||||
},
|
||||
"template": "default",
|
||||
"icon": {
|
||||
"tag": "standard_icon",
|
||||
"token": "ai-common_colorful"
|
||||
},
|
||||
"padding": "12px 12px 12px 20px"
|
||||
}
|
||||
}';
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 线索消息更新
|
||||
*/
|
||||
public function groupUpdateTemplates($quality = '💛 银牌商机', $order_no = 'SN2025072365640', $channel = '谷歌', $country = '白俄罗斯', $enter_user = '木子李', $r_time = '2025-07-24 09:41:23', $t_time = '2025-07-24 14:41:23')
|
||||
{
|
||||
$content = '{
|
||||
"schema": "2.0",
|
||||
"config": {
|
||||
"update_multi": true,
|
||||
"style": {
|
||||
"text_size": {
|
||||
"normal_v2": {
|
||||
"default": "normal",
|
||||
"pc": "normal",
|
||||
"mobile": "heading"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"body": {
|
||||
"direction": "vertical",
|
||||
"horizontal_spacing": "8px",
|
||||
"vertical_spacing": "16px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top",
|
||||
"padding": "0px 20px 20px 20px",
|
||||
"elements": [
|
||||
{
|
||||
"tag": "hr",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
},
|
||||
{
|
||||
"tag": "column_set",
|
||||
"horizontal_align": "left",
|
||||
"columns": [
|
||||
{
|
||||
"tag": "column",
|
||||
"width": "weighted",
|
||||
"elements": [
|
||||
{
|
||||
"tag": "markdown",
|
||||
"content": " **'.$quality.'**\n- **编号:** '.$order_no.'\n- **渠道:** '.$channel.'\n- **国家:** '.$country.'\n- **发布人:** '.$enter_user.'\n- **发布时间:** '.$r_time.'",
|
||||
"text_align": "left",
|
||||
"text_size": "normal_v2",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
}
|
||||
],
|
||||
"vertical_spacing": "8px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top",
|
||||
"weight": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tag": "hr",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
},
|
||||
{
|
||||
"tag": "div",
|
||||
"text": {
|
||||
"tag": "plain_text",
|
||||
"content": "跟踪时间:'.$t_time.'",
|
||||
"text_size": "notation",
|
||||
"text_align": "left",
|
||||
"text_color": "green"
|
||||
},
|
||||
"icon": {
|
||||
"tag": "standard_icon",
|
||||
"token": "chat-done_outlined",
|
||||
"color": "green"
|
||||
},
|
||||
"element_id": "track_time"
|
||||
}
|
||||
]
|
||||
},
|
||||
"header": {
|
||||
"title": {
|
||||
"tag": "plain_text",
|
||||
"content": "【公海】客户线索"
|
||||
},
|
||||
"subtitle": {
|
||||
"tag": "plain_text",
|
||||
"content": ""
|
||||
},
|
||||
"text_tag_list": [
|
||||
{
|
||||
"tag": "text_tag",
|
||||
"text": {
|
||||
"tag": "plain_text",
|
||||
"content": "已跟踪"
|
||||
},
|
||||
"color": "orange"
|
||||
}
|
||||
],
|
||||
"template": "green",
|
||||
"icon": {
|
||||
"tag": "standard_icon",
|
||||
"token": "ai-common_colorful"
|
||||
},
|
||||
"padding": "12px 12px 12px 20px"
|
||||
}
|
||||
}';
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 线索分配消息
|
||||
*/
|
||||
public function personAllocationTemplates($quality = '💛 银牌商机', $order_no = 'SN2025072365640', $channel = '谷歌', $country = '白俄罗斯', $enter_user = '木子李', $r_time = '2025-07-24 09:41:23', $t_time = '2025-07-24 14:41:23')
|
||||
{
|
||||
$content = '{
|
||||
"schema": "2.0",
|
||||
"config": {
|
||||
"update_multi": true,
|
||||
"style": {
|
||||
"text_size": {
|
||||
"normal_v2": {
|
||||
"default": "normal",
|
||||
"pc": "normal",
|
||||
"mobile": "heading"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"body": {
|
||||
"direction": "vertical",
|
||||
"horizontal_spacing": "8px",
|
||||
"vertical_spacing": "16px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top",
|
||||
"padding": "0px 20px 20px 20px",
|
||||
"elements": [
|
||||
{
|
||||
"tag": "hr",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
},
|
||||
{
|
||||
"tag": "column_set",
|
||||
"horizontal_align": "left",
|
||||
"columns": [
|
||||
{
|
||||
"tag": "column",
|
||||
"width": "weighted",
|
||||
"elements": [
|
||||
{
|
||||
"tag": "markdown",
|
||||
"content": " **'.$quality.'**\n- **编号:** '.$order_no.'\n- **渠道:** '.$channel.'\n- **国家:** '.$country.'\n- **录单人:** '.$enter_user.'\n- **录入时间:** '.$r_time.'",
|
||||
"text_align": "left",
|
||||
"text_size": "normal_v2",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
}
|
||||
],
|
||||
"vertical_spacing": "8px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top",
|
||||
"weight": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tag": "hr",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
},
|
||||
{
|
||||
"tag": "div",
|
||||
"text": {
|
||||
"tag": "plain_text",
|
||||
"content": "分配时间:'.$t_time.'",
|
||||
"text_size": "notation",
|
||||
"text_align": "left",
|
||||
"text_color": "green"
|
||||
},
|
||||
"icon": {
|
||||
"tag": "standard_icon",
|
||||
"token": "chat-done_outlined",
|
||||
"color": "green"
|
||||
},
|
||||
"element_id": "allocation_time"
|
||||
}
|
||||
]
|
||||
},
|
||||
"header": {
|
||||
"title": {
|
||||
"tag": "plain_text",
|
||||
"content": "新的客户线索"
|
||||
},
|
||||
"subtitle": {
|
||||
"tag": "plain_text",
|
||||
"content": ""
|
||||
},
|
||||
"text_tag_list": [
|
||||
{
|
||||
"tag": "text_tag",
|
||||
"text": {
|
||||
"tag": "plain_text",
|
||||
"content": "指定"
|
||||
},
|
||||
"color": "yellow"
|
||||
}
|
||||
],
|
||||
"template": "blue",
|
||||
"icon": {
|
||||
"tag": "standard_icon",
|
||||
"token": "todo_colorful"
|
||||
},
|
||||
"padding": "12px 12px 12px 20px"
|
||||
}
|
||||
}';
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 跟进提醒消息
|
||||
*/
|
||||
public function personFollowUpTemplates($contacts = '木子李', $follow_next = '2025-07-24 14:41:23', $follow_last = '2025-07-24 14:41:23')
|
||||
{
|
||||
$content = '{
|
||||
"schema": "2.0",
|
||||
"config": {
|
||||
"update_multi": true,
|
||||
"style": {
|
||||
"text_size": {
|
||||
"normal_v2": {
|
||||
"default": "normal",
|
||||
"pc": "normal",
|
||||
"mobile": "heading"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"body": {
|
||||
"direction": "vertical",
|
||||
"horizontal_spacing": "8px",
|
||||
"vertical_spacing": "16px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top",
|
||||
"padding": "0px 20px 20px 20px",
|
||||
"elements": [
|
||||
{
|
||||
"tag": "hr",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
},
|
||||
{
|
||||
"tag": "column_set",
|
||||
"horizontal_align": "left",
|
||||
"columns": [
|
||||
{
|
||||
"tag": "column",
|
||||
"width": "weighted",
|
||||
"elements": [
|
||||
{
|
||||
"tag": "markdown",
|
||||
"content": "- **客户名称:** '.$contacts.'\n- **跟进时间:** '.$follow_next.'",
|
||||
"text_align": "left",
|
||||
"text_size": "normal_v2",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
}
|
||||
],
|
||||
"vertical_spacing": "8px",
|
||||
"horizontal_align": "left",
|
||||
"vertical_align": "top",
|
||||
"weight": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tag": "hr",
|
||||
"margin": "0px 0px 0px 0px"
|
||||
},
|
||||
{
|
||||
"tag": "div",
|
||||
"text": {
|
||||
"tag": "plain_text",
|
||||
"content": "最近跟进:'.$follow_last.'",
|
||||
"text_size": "notation",
|
||||
"text_align": "left",
|
||||
"text_color": "grey"
|
||||
},
|
||||
"icon": {
|
||||
"tag": "standard_icon",
|
||||
"token": "chat_outlined",
|
||||
"color": "grey"
|
||||
},
|
||||
"element_id": "track_time"
|
||||
}
|
||||
]
|
||||
},
|
||||
"header": {
|
||||
"title": {
|
||||
"tag": "plain_text",
|
||||
"content": "跟进提醒"
|
||||
},
|
||||
"subtitle": {
|
||||
"tag": "plain_text",
|
||||
"content": ""
|
||||
},
|
||||
"template": "green",
|
||||
"icon": {
|
||||
"tag": "standard_icon",
|
||||
"token": "calendar_colorful"
|
||||
},
|
||||
"padding": "12px 12px 12px 20px"
|
||||
}
|
||||
}';
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use app\common\utils\scaffold\Generate;
|
||||
use support\Request;
|
||||
use support\Db;
|
||||
use support\Cache;
|
||||
use app\inquiry\service\InquiryLogService;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
@@ -100,8 +101,23 @@ class IndexController
|
||||
|
||||
public function test()
|
||||
{
|
||||
$res = ['id'=>25714];
|
||||
\Webman\RedisQueue\Redis::send('inquiry-to-crm', $res);
|
||||
// $res = ['id'=>25714];
|
||||
// \Webman\RedisQueue\Redis::send('inquiry-to-crm', $res);
|
||||
|
||||
// $res = [
|
||||
// 'id'=>25717,
|
||||
// 'category'=>'inquiry',
|
||||
// 'scene'=>'inquiry-person-allocation',
|
||||
// 'receive_type'=>'person'
|
||||
// ];
|
||||
|
||||
// $res = [
|
||||
// 'id'=>1,
|
||||
// 'category'=>'crm',
|
||||
// 'scene'=>'crm-person-follow-up',
|
||||
// 'receive_type'=>'person'
|
||||
// ];
|
||||
// \Webman\RedisQueue\Redis::send('feishu-message', $res);
|
||||
return 'success';
|
||||
}
|
||||
}
|
||||
@@ -97,11 +97,11 @@ class InquiryLogService
|
||||
}
|
||||
|
||||
// ====== ====== 关联 ====== ======
|
||||
$config['with']['country'][] = 'cn_addr';
|
||||
$config['with']['country'][] = 'steering';
|
||||
$config['with']['countryInfo'][] = 'cn_addr';
|
||||
$config['with']['countryInfo'][] = 'steering';
|
||||
$config['with']['enter'][] = 'name';
|
||||
$config['with']['recipient'][] = 'name';
|
||||
$config['with']['channel'][] = 'name';
|
||||
$config['with']['channelInfo'][] = 'name';
|
||||
break;
|
||||
case 'show':
|
||||
case 'update':
|
||||
@@ -184,4 +184,43 @@ class InquiryLogService
|
||||
RedisQueueRedis::send('inquiry-to-crm', $res);
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取询单ById
|
||||
* @param int|string $id
|
||||
* @param string $orderSn
|
||||
* @return Model|null
|
||||
*/
|
||||
public function getInquiryLogBy(int|string $id)
|
||||
{
|
||||
$config = [];
|
||||
$config['model'] = $this->modelClass;
|
||||
$config['with']['countryInfo'][] = 'cn_addr';
|
||||
$config['with']['countryInfo'][] = 'steering';
|
||||
$config['with']['enter'][] = 'name';
|
||||
$config['with']['recipient'][] = 'name';
|
||||
$config['with']['channelInfo'][] = 'name';
|
||||
$config['orWhere'][] = ['id', '=', $id];
|
||||
$config['orWhere'][] = ['order_no', '=', $id];
|
||||
return $this->logicShow($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取等级名称
|
||||
* @param $id
|
||||
* @return string
|
||||
*/
|
||||
public function qualityName($id)
|
||||
{
|
||||
if (!$id) {
|
||||
return '🤍 银牌商机';
|
||||
}
|
||||
$quality = [
|
||||
1 => '💛 金牌商机',
|
||||
2 => '🤍 银牌商机',
|
||||
3 => '🤍 银牌商机'
|
||||
];
|
||||
return $quality[$id];
|
||||
}
|
||||
}
|
||||
@@ -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)->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)->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);
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
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
|
||||
@@ -21,7 +23,7 @@ class InquiryToCrm implements Consumer
|
||||
{
|
||||
try {
|
||||
// 获取询盘信息
|
||||
$info = InquiryLog::where('id', $data['id'])->where('sync_crm', 0)->first();
|
||||
$info = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('id', $data['id'])->where('sync_crm', 0)->first();
|
||||
if ($info) {
|
||||
// 获取用户信息
|
||||
$recipient = 0;
|
||||
@@ -32,6 +34,13 @@ class InquiryToCrm implements Consumer
|
||||
if ($uinfo) {
|
||||
$corpId = $uinfo->corp_id;
|
||||
}
|
||||
// 消息推送参数 分配
|
||||
$scene = 'inquiry-person-allocation';
|
||||
$receiveType = 'person';
|
||||
} else {
|
||||
// 消息推送参数 发布
|
||||
$scene = 'inquiry-group-send';
|
||||
$receiveType = 'group';
|
||||
}
|
||||
// 同步客户数据
|
||||
$crmCustomer = new CrmCustomer();
|
||||
@@ -54,6 +63,15 @@ class InquiryToCrm implements Consumer
|
||||
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;
|
||||
|
||||
@@ -29,6 +29,10 @@ use Webman\Route;
|
||||
Route::any('/c', [app\controller\IndexController::class, 'createResource']);
|
||||
Route::any('/perm', [app\controller\IndexController::class, 'perm']);
|
||||
|
||||
// 飞书
|
||||
Route::any('/feishu/event', [app\common\controller\NotifyController::class, 'feishuEvent']);
|
||||
// 飞书回调
|
||||
Route::any('/feishu/callback', [app\common\controller\NotifyController::class, 'feishuCallback']);
|
||||
|
||||
// 公共
|
||||
Route::group('/common', function () {
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'basi_cdata' => [
|
||||
'/', // 首页
|
||||
'/index/index', // 首页
|
||||
'/index/view', // 飞书调试
|
||||
'/perm', // 调试-添加权限
|
||||
|
||||
'/common/preset/corps', // 公司列表
|
||||
'/common/preset/feishu/app', // 飞书应用信息
|
||||
]
|
||||
];
|
||||
Reference in New Issue
Block a user