From 68e0c261d847a3ce37e884bb059a6c6c4036fa82 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 26 Feb 2026 19:07:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E5=AE=A1=E6=89=B9=E6=B6=88=E6=81=AF=E9=93=BE=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改内容: 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 --- backend/app/controller/TestController.php | 69 +++++++++++++++++---- backend/app/service/BusinessTripService.php | 20 +++++- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/backend/app/controller/TestController.php b/backend/app/controller/TestController.php index 0c6c252..8e8cbf7 100644 --- a/backend/app/controller/TestController.php +++ b/backend/app/controller/TestController.php @@ -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()); diff --git a/backend/app/service/BusinessTripService.php b/backend/app/service/BusinessTripService.php index 2196f22..e90817d 100644 --- a/backend/app/service/BusinessTripService.php +++ b/backend/app/service/BusinessTripService.php @@ -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}");