飞书消息推送

This commit is contained in:
mzl
2025-07-25 13:39:23 +08:00
parent 9fdf2dabde
commit 2b052ec2b4
10 changed files with 904 additions and 22 deletions
+136
View File
@@ -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());
}
}
}
+1 -1
View File
@@ -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'];
/**
* 不允许自动填充模型字段
+2 -2
View File
@@ -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');
}
+555
View File
@@ -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": "&emsp;**'.$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": "&emsp;**'.$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": "&emsp;**'.$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;
}
}