通知商家发码-ok
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import { Router } from 'express'
|
||||
|
||||
import { createRateLimitMiddleware } from '../middleware/rate-limit.js'
|
||||
import { handleSendCode } from '../services/platforms/kuaishou-industry/send-code-service.js'
|
||||
import { handleDestroyCode } from '../services/platforms/kuaishou-industry/destroy-code-service.js'
|
||||
import { handleQueryCode } from '../services/platforms/kuaishou-industry/query-code-service.js'
|
||||
import { buildIndustryErrorResponse } from '../services/platforms/kuaishou-industry/response.js'
|
||||
import { createRequestId, logIntegration } from '../utils/logger.js'
|
||||
|
||||
const router = Router()
|
||||
|
||||
const industryRateLimit = createRateLimitMiddleware({
|
||||
scope: 'kuaishouIndustry',
|
||||
windowMs: 60_000,
|
||||
max: 120,
|
||||
onLimit: (_req, res) => {
|
||||
res.status(200).json(buildIndustryErrorResponse(4010003, '请求过于频繁,请稍后再试'))
|
||||
},
|
||||
})
|
||||
|
||||
router.post('/send-code', industryRateLimit, async (req, res) => {
|
||||
const requestId = createRequestId('ksind')
|
||||
const startedAt = Date.now()
|
||||
|
||||
logIntegration('[kuaishou-industry/send-code]', '收到快手行业电子凭证发码请求', {
|
||||
requestId,
|
||||
method: req.method,
|
||||
originalUrl: req.originalUrl,
|
||||
ip: req.ip,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
})
|
||||
|
||||
try {
|
||||
const params = mergeRequestParams(req)
|
||||
const result = await handleSendCode(params)
|
||||
logIntegration('[kuaishou-industry/send-code]', '发码处理完成', {
|
||||
requestId,
|
||||
durationMs: Date.now() - startedAt,
|
||||
result,
|
||||
})
|
||||
res.status(200).json(result)
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : '系统异常'
|
||||
logIntegration('[kuaishou-industry/send-code]', '发码处理失败', {
|
||||
requestId,
|
||||
durationMs: Date.now() - startedAt,
|
||||
error,
|
||||
}, { level: 'error' })
|
||||
res.status(200).json(buildIndustryErrorResponse(4010003, message))
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/destroy-code', industryRateLimit, async (req, res) => {
|
||||
const requestId = createRequestId('ksind')
|
||||
const startedAt = Date.now()
|
||||
|
||||
logIntegration('[kuaishou-industry/destroy-code]', '收到快手行业电子凭证销毁请求', {
|
||||
requestId,
|
||||
method: req.method,
|
||||
originalUrl: req.originalUrl,
|
||||
ip: req.ip,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
})
|
||||
|
||||
try {
|
||||
const params = mergeRequestParams(req)
|
||||
const result = await handleDestroyCode(params)
|
||||
logIntegration('[kuaishou-industry/destroy-code]', '销毁处理完成', {
|
||||
requestId,
|
||||
durationMs: Date.now() - startedAt,
|
||||
result,
|
||||
})
|
||||
res.status(200).json(result)
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : '系统异常'
|
||||
logIntegration('[kuaishou-industry/destroy-code]', '销毁处理失败', {
|
||||
requestId,
|
||||
durationMs: Date.now() - startedAt,
|
||||
error,
|
||||
}, { level: 'error' })
|
||||
res.status(200).json(buildIndustryErrorResponse(4010003, message))
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/query-code', industryRateLimit, async (req, res) => {
|
||||
const requestId = createRequestId('ksind')
|
||||
const startedAt = Date.now()
|
||||
|
||||
logIntegration('[kuaishou-industry/query-code]', '收到快手行业电子凭证查询请求', {
|
||||
requestId,
|
||||
method: req.method,
|
||||
originalUrl: req.originalUrl,
|
||||
ip: req.ip,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
})
|
||||
|
||||
try {
|
||||
const params = mergeRequestParams(req)
|
||||
const result = await handleQueryCode(params)
|
||||
logIntegration('[kuaishou-industry/query-code]', '查询处理完成', {
|
||||
requestId,
|
||||
durationMs: Date.now() - startedAt,
|
||||
result,
|
||||
})
|
||||
res.status(200).json(result)
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : '系统异常'
|
||||
logIntegration('[kuaishou-industry/query-code]', '查询处理失败', {
|
||||
requestId,
|
||||
durationMs: Date.now() - startedAt,
|
||||
error,
|
||||
}, { level: 'error' })
|
||||
res.status(200).json(buildIndustryErrorResponse(4010003, message))
|
||||
}
|
||||
})
|
||||
|
||||
function mergeRequestParams(req: any): Record<string, any> {
|
||||
const query = req.query || {}
|
||||
const body = req.body || {}
|
||||
const params: Record<string, any> = { ...query }
|
||||
|
||||
for (const [key, value] of Object.entries(body)) {
|
||||
if (params[key] === undefined && key !== 'param') {
|
||||
params[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
if (body.param && !params.param) {
|
||||
params.param = body.param
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user