Files
x-server-mobile/backend/app/controller/TestController.php
T

110 lines
4.2 KiB
PHP
Raw Normal View History

<?php
namespace app\controller;
use app\service\FeishuNotificationService;
use Webman\Http\Request;
use Webman\Http\Response;
class TestController extends BaseController
{
/**
* 测试发送飞书通知
*/
public function testNotification(Request $request): Response
{
$userId = (int) ($request->get('user_id') ?? 1);
2026-02-26 19:07:09 +08:00
$orderId = (int) ($request->get('order_id') ?? 0);
$real = (int) ($request->get('real') ?? 0); // real=1 发送真实业务通知
try {
$notification = new FeishuNotificationService();
// 获取用户的company_id和feishu_app_id
$user = \app\model\User::find($userId);
if (!$user) {
return $this->error('用户不存在');
}
if (!$user->feishu_app_id) {
return $this->error('用户未绑定飞书应用');
}
$notification->setCompany($user->company_id);
$notification->setApp($user->feishu_app_id);
2026-02-26 19:07:09 +08:00
// 如果 real=1,发送真实业务通知
if ($real && $orderId) {
$order = \app\model\ApprovalOrder::find($orderId);
if (!$order) {
return $this->error('审批单不存在');
}
// 获取业务数据
$businessTrip = \app\model\BusinessTrip::find($order->business_id);
if (!$businessTrip) {
return $this->error('出差申请不存在');
}
// 构建真实业务内容
$detailUrl = "https://xsm.dev.hnxinyouhe.com/business-trip/detail/{$businessTrip->id}";
$content = "**出差原因**: {$businessTrip->reason}\n";
$content .= "**目的地**: {$businessTrip->destination}\n";
$content .= "**时间**: {$businessTrip->start_date} 至 {$businessTrip->end_date}";
$params = [
'user_id' => $userId,
'title' => '新的出差审批申请',
'applicant' => $businessTrip->user->name,
'type' => '出差申请',
'content' => $content,
'url' => $detailUrl,
'order_id' => $orderId,
];
$note = '真实业务通知(含审批按钮)';
} else {
// 发送测试消息
$params = [
'user_id' => $userId,
'title' => '测试审批通知',
'applicant' => '系统测试',
'type' => '测试申请',
'content' => '这是一条测试消息,用于验证飞书通知功能是否正常工作。如果看到"通过"和"拒绝"按钮,说明按钮功能正常。',
'url' => $orderId ? "https://xsm.dev.hnxinyouhe.com/approval/detail/{$orderId}" : 'https://xsm.dev.hnxinyouhe.com/dashboard',
];
// 如果提供了 order_id,添加审批按钮
if ($orderId) {
$params['order_id'] = $orderId;
}
$note = $orderId ? '测试消息(含审批按钮)' : '测试消息(不含审批按钮)';
}
// 发送消息
$result = $notification->sendApprovalNotification($params);
if ($result) {
// 获取用户在该应用下的feishu_user_id
$feishuUserId = $user->getFeishuUserId($user->feishu_app_id);
return $this->success([
2026-02-26 19:07:09 +08:00
'message' => '消息发送成功',
'user_id' => $userId,
'user_name' => $user->name,
'feishu_user_id' => $feishuUserId,
'feishu_app_id' => $user->feishu_app_id,
2026-02-26 19:07:09 +08:00
'has_order_id' => !empty($orderId),
'type' => $real ? '真实业务通知' : '测试消息',
'note' => $note,
]);
} else {
2026-02-26 19:07:09 +08:00
return $this->error('消息发送失败,请查看日志');
}
} catch (\Exception $e) {
return $this->error('测试失败: ' . $e->getMessage());
}
}
}