feat: 自审批时发送文本消息而不是卡片消息

- FeishuNotificationService: 检测自审批场景,发送纯文本消息
- ApprovalService: 传递 applicant_id 参数
- 添加 sendTextNotification 和 sendText 方法
- 保留自审批自动通过逻辑(前端调用场景)
This commit is contained in:
root
2026-02-28 14:22:40 +08:00
parent 1b0be5918d
commit 6eed14933a
2 changed files with 161 additions and 4 deletions
@@ -143,6 +143,13 @@ class FeishuNotificationService
return false;
}
// 检查是否是申请人自己 - 发送文本消息而不是卡片
$applicantId = $params['applicant_id'] ?? null;
if ($applicantId && $applicantId == $userId) {
error_log("🔄 检测到自审批,发送文本通知而不是卡片");
return $this->sendTextNotification($feishuUserId, $params);
}
$card = $this->buildApprovalCard($params, $user->company_id);
return $this->sendMessage($feishuUserId, $card);
} catch (Exception $e) {
@@ -573,4 +580,80 @@ class FeishuNotificationService
return false;
}
}
/**
* 发送文本消息(用于自审批场景)
*/
private function sendTextNotification(string $feishuUserId, array $params): bool
{
try {
$title = $params['title'] ?? '审批申请';
$type = $params['type'] ?? '申请';
$content = $params['content'] ?? '';
$url = $params['url'] ?? '';
// 构建文本内容
$text = "【{$title}】\n";
$text .= "申请类型:{$type}\n";
if ($content) {
// 清理 markdown 格式,转为纯文本
$content = preg_replace('/\*\*(.*?)\*\*/', '$1', $content);
$content = str_replace('**', '', $content);
$text .= "\n{$content}\n";
}
if ($url) {
$text .= "\n👉 点击查看详情:{$url}";
}
// 发送文本消息
return $this->sendText($feishuUserId, $text);
} catch (Exception $e) {
error_log('❌ sendTextNotification 异常: ' . $e->getMessage());
return false;
}
}
/**
* 发送纯文本消息
*/
private function sendText(string $feishuUserId, string $text): bool
{
try {
$accessToken = $this->getTenantAccessToken();
if (!$accessToken) {
error_log('❌ sendText: 获取 tenant_access_token 失败');
return false;
}
$apiUrl = "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=user_id";
$requestData = [
'receive_id' => $feishuUserId,
'msg_type' => 'text',
'content' => json_encode(['text' => $text]),
];
error_log('📤 发送文本消息: feishu_user_id=' . $feishuUserId . ', text=' . substr($text, 0, 100));
$response = HttpClient::post($apiUrl, $requestData, [
'Authorization: Bearer ' . $accessToken,
]);
$responseData = json_decode($response, true);
$code = $responseData['code'] ?? -1;
if ($code !== 0) {
error_log('❌ 发送文本消息失败: ' . json_encode($responseData));
} else {
error_log('✅ 文本消息发送成功');
}
return $code === 0;
} catch (Exception $e) {
error_log('❌ sendText 异常: ' . $e->getMessage());
return false;
}
}
}