Files
fdesk/app/common/controller/NotifyController.php
T
2025-07-29 10:19:15 +08:00

136 lines
3.8 KiB
PHP

<?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());
}
}
}