master
lion 6 days ago
parent cec50e36b7
commit 7f6c54efa1

@ -170,28 +170,44 @@ class VisitAuditController extends CommonController
}
}
if ($auditResult) {
// 发通知订阅消息
// 发通知订阅消息通过与驳回共用「审核结果」模板Caa0…
// 驳回若单独使用另一模板,易出现后台字段不一致或用户未授权该模板导致发送失败。
$subMsgTemplateId = 'Caa0O6pPtQDHQMPsqE6PHyzueeI11JvlpenbgwkYC0s';
$templateData = [
'date1' => [
'value' => $visit->date
],
'name3' => [
'value' => $acceptAdmin->name,
'value' => $acceptAdmin->name ?? '',
],
'thing4' => [
'value' => $visit->name
'value' => $this->clipSubscribeThing($visit->name)
],
'thing13' => [
'value' => $auditResult,
'value' => $this->clipSubscribeThing($auditResult, 20),
],
'thing9' => [
'value' => $visit->company_name
'value' => $this->clipSubscribeThing($visit->company_name ?? '-')
],
];
$subMsgTemplateId = $auditResult === '驳回'
? 'JDIyQ6yY0y-xdgd2z1gTSo6nBMG84VT1Bu3kcpeStSw'
: 'Caa0O6pPtQDHQMPsqE6PHyzueeI11JvlpenbgwkYC0s';
Visit::subMsg($subMsgTemplateId, $user->openid, $templateData, $all['visit_id']);
if ($user && !empty($user->openid)) {
try {
$sendResult = Visit::subMsg($subMsgTemplateId, $user->openid, $templateData, $all['visit_id']);
if (is_array($sendResult) && isset($sendResult['errcode']) && (int) $sendResult['errcode'] !== 0) {
Log::warning('visit audit subscribe message send failed', [
'visit_id' => $all['visit_id'],
'audit_result' => $auditResult,
'errcode' => $sendResult['errcode'] ?? null,
'errmsg' => $sendResult['errmsg'] ?? null,
]);
}
} catch (\Throwable $e) {
Log::warning('visit audit subscribe message exception', [
'visit_id' => $all['visit_id'],
'message' => $e->getMessage(),
]);
}
}
// 发通知短信消息
$vars = ['result' => $auditResult];
$template_id = 'G5W6Y3';
@ -238,4 +254,24 @@ class VisitAuditController extends CommonController
VisitAudit::where('id', $all['id'])->delete();
return $this->success('删除成功');
}
/**
* 订阅消息 thing 类字段长度限制较严,避免微信接口因超长拒绝发送。
*/
private function clipSubscribeThing(?string $text, int $maxLen = 20): string
{
$text = trim((string) $text);
if ($text === '') {
return '-';
}
if (function_exists('mb_strlen') && function_exists('mb_substr')) {
if (mb_strlen($text) > $maxLen) {
return mb_substr($text, 0, $maxLen - 1) . '…';
}
} elseif (strlen($text) > $maxLen) {
return substr($text, 0, $maxLen - 1) . '…';
}
return $text;
}
}

Loading…
Cancel
Save