增强行业凭证发货回调日志

This commit is contained in:
yml2213
2026-07-08 12:16:32 +08:00
parent 24a1ade83f
commit b9d1f57e3d
2 changed files with 70 additions and 20 deletions
@@ -1,6 +1,6 @@
import crypto from 'node:crypto'
import { logInfo, logWarn } from '../../../utils/logger.js'
import { logIntegration } from '../../../utils/logger.js'
import { getKuaishouIndustryConfig } from './config.js'
import { ensureKuaishouIndustryAccessToken } from './token-service.js'
@@ -32,7 +32,7 @@ export async function sendCallback(input: SendCallbackInput): Promise<{ success:
let config = getKuaishouIndustryConfig()
if (!config.enabled) {
logInfo('[kuaishou-industry/send-callback]', '行业电子凭证配置未启用,跳过发回调')
logIntegration('[kuaishou-industry/send-callback]', '行业电子凭证配置未启用,跳过发回调')
return { success: true }
}
@@ -45,15 +45,15 @@ export async function sendCallback(input: SendCallbackInput): Promise<{ success:
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
logWarn('[kuaishou-industry/send-callback]', 'accessToken 刷新失败,无法发起发回调', {
logIntegration('[kuaishou-industry/send-callback]', 'accessToken 刷新失败,无法发起发回调', {
error: message,
})
}, { level: 'warn' })
return { success: false, error: message }
}
if (!config.accessToken) {
const message = 'accessToken 未配置,无法发起发回调'
logWarn('[kuaishou-industry/send-callback]', message)
const message = 'accessToken 未配置,无法发起发回调'
logIntegration('[kuaishou-industry/send-callback]', message, undefined, { level: 'warn' })
return { success: false, error: message }
}
@@ -102,13 +102,14 @@ export async function sendCallback(input: SendCallbackInput): Promise<{ success:
}
const url = `${resolveKuaishouOpenApiBaseUrl(config.baseUrl)}/integration/callback/virtual/eticket/send`
logInfo('[kuaishou-industry/send-callback]', `发起发码回调 oid=${input.oid}`, {
const requestLog = buildSendCallbackRequestLog({
url,
oid: input.oid,
sendNum: input.sendNum,
signParams,
bizParams,
})
logIntegration('[kuaishou-industry/send-callback]', `发起电子凭证发货回调 oid=${input.oid}`, requestLog)
try {
const startedAt = Date.now()
const res = await fetch(url, {
@@ -124,26 +125,56 @@ export async function sendCallback(input: SendCallbackInput): Promise<{ success:
const ok = res.ok && Number(json.result) === 1
if (ok) {
logInfo('[kuaishou-industry/send-callback]', `发码回调成功 oid=${input.oid}`, {
logIntegration('[kuaishou-industry/send-callback]', `电子凭证发货回调成功 oid=${input.oid}`, {
durationMs,
status: res.status,
request: requestLog,
response: json,
})
} else {
logWarn('[kuaishou-industry/send-callback]', `发码回调失败 oid=${input.oid}`, {
logIntegration('[kuaishou-industry/send-callback]', `电子凭证发货回调失败 oid=${input.oid}`, {
durationMs,
status: res.status,
request: requestLog,
response: json,
})
}, { level: 'warn' })
}
return { success: ok, response: json }
} catch (err) {
const message = err instanceof Error ? err.message : String(err)
logWarn('[kuaishou-industry/send-callback]', `发码回调异常 oid=${input.oid}`, resolveCallbackErrorDetail(err))
logIntegration('[kuaishou-industry/send-callback]', `电子凭证发货回调异常 oid=${input.oid}`, {
...resolveCallbackErrorDetail(err),
request: requestLog,
}, { level: 'warn' })
return { success: false, error: message }
}
}
function buildSendCallbackRequestLog({
url,
signParams,
bizParams,
}: {
url: string
signParams: JsonObject
bizParams: JsonObject
}): JsonObject {
return {
url,
method: 'POST',
contentType: 'application/x-www-form-urlencoded',
apiMethod: signParams.method,
appkey: signParams.appkey,
access_token: signParams.access_token,
version: signParams.version,
timestamp: signParams.timestamp,
signMethod: signParams.signMethod,
sign: signParams.sign,
param: bizParams,
}
}
function signPayload(params: JsonObject, signSecret: string): string {
const entries = Object.entries(params)
.filter(([key]) => key !== 'sign' && key !== 'signSecret')
@@ -3,7 +3,7 @@ import { listTasksByOrderId } from '../../../repositories/task-repo.js'
import { upsertKuaishouIndustryVoucher } from '../../../repositories/kuaishou-industry-voucher-repo.js'
import { OPEN_91_PLATFORM, OPEN_91_PROVIDER } from '../../open-91/config.js'
import { normalizeTimestampIso } from '../../../utils/time.js'
import { logWarn } from '../../../utils/logger.js'
import { logIntegration } from '../../../utils/logger.js'
import {
getKuaishouIndustryConfig,
assertMatchingAppKey,
@@ -119,28 +119,47 @@ function fireSendCallback(input: {
num: Number(e.num) || 1,
validStartTime: Number(e.validStartTime) || 0,
validEndTime: Number(e.validEndTime) || 0,
goodsValue: Number(e.goodsValue) || 0,
}))
const sendNum = eticketItems.reduce((sum, e) => sum + e.num, 0)
const totalGoodsValue = eticketItems.reduce((sum, e) => sum + e.goodsValue, 0)
logIntegration('[kuaishou-industry/send-code]', '电子凭证发货回调已调度', {
oid: input.oid,
sendType: input.sendType,
sendNum,
totalGoodsValue,
etickets: eticketItems,
eticketType: input.eticketType || '',
})
sendCallback({
oid: input.oid,
sendType: input.sendType,
etickets: eticketItems,
sendNum: eticketItems.reduce((sum, e) => sum + e.num, 0),
sendNum,
totalGoodsValue,
token: input.token,
...(input.eticketType ? { eticketType: input.eticketType } : {}),
...(input.ext ? { ext: input.ext } : {}),
}).then((result) => {
if (!result.success) {
logWarn('[kuaishou-industry/send-code]', '发码回调异步执行失败', {
logIntegration('[kuaishou-industry/send-code]', '电子凭证发货回调异步执行失败', {
oid: input.oid,
error: result.error || '',
response: result.response || null,
})
}, { level: 'warn' })
return
}
logIntegration('[kuaishou-industry/send-code]', '电子凭证发货回调异步执行完成', {
oid: input.oid,
response: result.response || null,
})
}).catch((err) => {
logWarn('[kuaishou-industry/send-code]', '发码回调异步执行异常', {
logIntegration('[kuaishou-industry/send-code]', '电子凭证发货回调异步执行异常', {
oid: input.oid,
error: err instanceof Error ? err.message : String(err),
})
}, { level: 'warn' })
})
}