feat: 完善业务审批消息链路

修改内容:
1. TestController - 添加 real 参数支持发送真实业务通知
2. BusinessTripService.sendApprovalNotification() - 传递 order_id

业务流程:
1. 用户创建申请 → 审批人收到带按钮的通知
2. 点击"通过"/"拒绝"按钮 → 跳转到审批详情页
3. 自动弹出确认框 → 完成审批
4. 通知下一节点审批人/申请人

按钮链接:
- 通过/拒绝:/approval/detail/{order_id}?action=approve|reject
- 查看详情:/business-trip/detail/{business_id}

测试命令:
- 测试消息: /api/v1/test/notification?user_id=1&order_id=64
- 真实业务: /api/v1/test/notification?user_id=1&order_id=64&real=1

相关文件:
- backend/app/controller/TestController.php
- backend/app/service/BusinessTripService.php

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
root
2026-02-26 19:07:09 +08:00
parent 7ee92d68fe
commit 68e0c261d8
2 changed files with 75 additions and 14 deletions
+58 -11
View File
@@ -14,6 +14,8 @@ class TestController extends BaseController
public function testNotification(Request $request): Response
{
$userId = (int) ($request->get('user_id') ?? 1);
$orderId = (int) ($request->get('order_id') ?? 0);
$real = (int) ($request->get('real') ?? 0); // real=1 发送真实业务通知
try {
$notification = new FeishuNotificationService();
@@ -31,29 +33,74 @@ class TestController extends BaseController
$notification->setCompany($user->company_id);
$notification->setApp($user->feishu_app_id);
// 发送测试消息
$result = $notification->sendApprovalNotification([
'user_id' => $userId,
'title' => '测试审批通知',
'applicant' => '系统测试',
'type' => '测试申请',
'content' => '这是一条测试消息,用于验证飞书通知功能是否正常工作。',
'url' => 'https://xsm.dev.hnxinyouhe.com/dashboard',
]);
// 如果 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([
'message' => '测试消息发送成功',
'message' => '消息发送成功',
'user_id' => $userId,
'user_name' => $user->name,
'feishu_user_id' => $feishuUserId,
'feishu_app_id' => $user->feishu_app_id,
'has_order_id' => !empty($orderId),
'type' => $real ? '真实业务通知' : '测试消息',
'note' => $note,
]);
} else {
return $this->error('测试消息发送失败,请查看日志');
return $this->error('消息发送失败,请查看日志');
}
} catch (\Exception $e) {
return $this->error('测试失败: ' . $e->getMessage());
+17 -3
View File
@@ -285,7 +285,20 @@ class BusinessTripService
$notificationService = new \app\service\FeishuNotificationService();
// 构建详情链接
// 获取 approval_order_id
$approvalOrder = \app\model\ApprovalOrder::where('business_type', 'business_trip')
->where('business_id', $businessTrip->id)
->first();
if (!$approvalOrder) {
error_log("⚠️ 未找到审批单,无法发送通知");
return;
}
// 构建审批链接(使用 approval_order 的 ID)
$approvalUrl = "https://xsm.dev.hnxinyouhe.com/approval/detail/{$approvalOrder->id}";
// 构建业务详情链接(用于查看详情按钮)
$detailUrl = "https://xsm.dev.hnxinyouhe.com/business-trip/detail/{$businessTrip->id}";
// 构建内容
@@ -293,7 +306,7 @@ class BusinessTripService
$content .= "**目的地**: {$businessTrip->destination}\n";
$content .= "**时间**: {$businessTrip->start_date} 至 {$businessTrip->end_date}";
// 通知审批人
// 通知审批人(带审批按钮)
foreach ($approverIds as $approverId) {
try {
error_log("📧 通知审批人: approver_id={$approverId}");
@@ -305,6 +318,7 @@ class BusinessTripService
'type' => '出差申请',
'content' => $content,
'url' => $detailUrl,
'order_id' => $approvalOrder->id, // 添加 order_id 以显示审批按钮
]);
error_log("✅ 审批人通知成功: approver_id={$approverId}");
} catch (\Exception $e) {
@@ -312,7 +326,7 @@ class BusinessTripService
}
}
// 通知抄送人
// 通知抄送人(不带审批按钮)
foreach ($ccUserIds as $ccUserId) {
try {
error_log("📧 通知抄送人: cc_user_id={$ccUserId}");