新增(自动发货抖店): 接入异步卡密下单与回调
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import { runtimeConfig } from '../../config/runtime.js'
|
||||
import { createHttpError } from '../../utils/http.js'
|
||||
import type { RuntimeConfig } from '../../types/runtime-config.js'
|
||||
|
||||
export const AUTO_SHIP_DOUYIN_PROVIDER = 'auto_ship_douyin'
|
||||
export const AUTO_SHIP_DOUYIN_PLATFORM = 'doudian'
|
||||
export const AUTO_SHIP_DOUYIN_SUCCESS_MESSAGE = '接口调用成功'
|
||||
export const AUTO_SHIP_DOUYIN_DEFAULT_FAIL_CODE = 1204
|
||||
export const AUTO_SHIP_DOUYIN_COST_EXCEED_FAIL_CODE = 1220
|
||||
|
||||
type AutoShipDouyinRuntimeConfig = RuntimeConfig['platforms']['autoShipDouyin']
|
||||
|
||||
export function getAutoShipDouyinConfig(overrides: Partial<AutoShipDouyinRuntimeConfig> = {}) {
|
||||
const config = {
|
||||
...(runtimeConfig.platforms?.autoShipDouyin || {}),
|
||||
...overrides,
|
||||
}
|
||||
|
||||
return {
|
||||
enabled: Boolean(config.enabled),
|
||||
secret: String(config.secret || '').trim(),
|
||||
version: String(config.version || '1.0').trim() || '1.0',
|
||||
shopId: String(config.shopId || AUTO_SHIP_DOUYIN_PROVIDER).trim() || AUTO_SHIP_DOUYIN_PROVIDER,
|
||||
shopName: String(config.shopName || '自动发货抖店').trim() || '自动发货抖店',
|
||||
timestampToleranceSeconds: Number(config.timestampToleranceSeconds || 600) || 600,
|
||||
callbackAllowedOrigins: Array.isArray(config.callbackAllowedOrigins)
|
||||
? config.callbackAllowedOrigins.map((item) => String(item || '').trim()).filter(Boolean)
|
||||
: [],
|
||||
callbackTimeoutMs: Number(config.callbackTimeoutMs || 5000) || 5000,
|
||||
callbackMaxAttempts: Number(config.callbackMaxAttempts || 12) || 12,
|
||||
}
|
||||
}
|
||||
|
||||
export function assertAutoShipDouyinConfig(overrides: Partial<AutoShipDouyinRuntimeConfig> = {}) {
|
||||
const config = getAutoShipDouyinConfig(overrides)
|
||||
|
||||
if (!config.enabled) {
|
||||
throw createHttpError('自动发货抖店接口未启用', {
|
||||
statusCode: 503,
|
||||
errorCode: 'auto_ship_douyin_disabled',
|
||||
})
|
||||
}
|
||||
|
||||
if (!config.secret) {
|
||||
throw createHttpError('自动发货抖店 secret 未配置', {
|
||||
statusCode: 500,
|
||||
errorCode: 'auto_ship_douyin_missing_secret',
|
||||
})
|
||||
}
|
||||
|
||||
if (config.secret.length !== 32) {
|
||||
throw createHttpError('自动发货抖店 secret 长度必须为 32 个字符', {
|
||||
statusCode: 500,
|
||||
errorCode: 'auto_ship_douyin_invalid_secret_length',
|
||||
})
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
export function assertAutoShipDouyinCallbackUrl(
|
||||
callbackUrl: unknown,
|
||||
config = assertAutoShipDouyinConfig(),
|
||||
) {
|
||||
const raw = String(callbackUrl || '').trim()
|
||||
if (!raw) {
|
||||
throw createHttpError('缺少 callbackUrl', {
|
||||
statusCode: 400,
|
||||
errorCode: 'auto_ship_douyin_missing_callback_url',
|
||||
})
|
||||
}
|
||||
|
||||
let url: URL
|
||||
try {
|
||||
url = new URL(raw)
|
||||
} catch {
|
||||
throw createHttpError('callbackUrl 格式无效', {
|
||||
statusCode: 400,
|
||||
errorCode: 'auto_ship_douyin_invalid_callback_url',
|
||||
})
|
||||
}
|
||||
|
||||
if (url.protocol !== 'https:') {
|
||||
throw createHttpError('callbackUrl 必须使用 HTTPS', {
|
||||
statusCode: 400,
|
||||
errorCode: 'auto_ship_douyin_insecure_callback_url',
|
||||
})
|
||||
}
|
||||
|
||||
if (config.callbackAllowedOrigins.length === 0) {
|
||||
throw createHttpError('callbackUrl 域名白名单未配置', {
|
||||
statusCode: 500,
|
||||
errorCode: 'auto_ship_douyin_callback_allowlist_missing',
|
||||
})
|
||||
}
|
||||
|
||||
if (!config.callbackAllowedOrigins.includes(url.origin)) {
|
||||
throw createHttpError('callbackUrl 域名不在允许列表内', {
|
||||
statusCode: 400,
|
||||
errorCode: 'auto_ship_douyin_callback_origin_denied',
|
||||
})
|
||||
}
|
||||
|
||||
return url.toString()
|
||||
}
|
||||
Reference in New Issue
Block a user