快手小店-核销--ok
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"syncFromCreatedAt": "2026-05-02T10:53:28.550Z",
|
||||
"lastRunStartedAt": "2026-05-02T16:37:16.806Z",
|
||||
"lastRunFinishedAt": "2026-05-02T16:37:17.707Z",
|
||||
"lastRunStartedAt": "2026-05-03T08:34:17.211Z",
|
||||
"lastRunFinishedAt": "2026-05-03T08:34:17.844Z",
|
||||
"lastRunStatus": "success",
|
||||
"lastErrorMessage": "",
|
||||
"fetchedCount": 40,
|
||||
"syncedCount": 0,
|
||||
"ignoredCount": 40,
|
||||
"lastOrderCreatedAt": "2026-05-02T16:36:46.000Z"
|
||||
"lastOrderCreatedAt": "2026-05-03T08:33:37.000Z"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"enabled": true,
|
||||
"baseUrl": "https://s.kwaixiaodian.com",
|
||||
"shops": [
|
||||
{
|
||||
"shopId": "140000112682050",
|
||||
"kshopName": "洋洋运营",
|
||||
"cookie": "_did=web_412376696871A458; did=web_pdd5bx2mm4sdx78w7zg3oj1uya9cqs59; bUserId=1000598999789; userId=140000112682050; merchantSellerId=4269276762; sid=kshop.api; kshop.api_st=Cgxrc2hvcC5hcGkuc3QSoAEHQpSWH5NWp5ReO9nWzyqJIZ9NsB7YhqywKcjVzUAf1YBJODvE9HHSA5jNrO1kyoYtQmK7hlUmD7FCkDjvzTNoDJCd5mGiu_oFbUr-JYJ2--q_QNhHlY13he_NQSPQ7EYDf54NBMk1jKQNarlUnB-1WosXNgV7G5YNeZXr8CDMWkDwKUaDnG-2JGmPTsouINryXjteKsj4YZEzwWHR2GTZGhJex1zz60qNCRAHidwVN1PhX4wiIN3oIIsuly3Zub__D_EnvOhbCP_Ma363nNk4hmpKm_NpKAUwAQ; kshop.api_ph=fabd3673d2238257e37fc892abf2e8e73fd4; KS-CSRF-Token=179C60C8C34FB066A71221C9EE64B5CE; merchantSessionKey=1777795509898_209a40cd7ad55a7e842151ee83de41bb",
|
||||
"userAvatar": "https://p2-pro.a.yximgs.com/uhead/AB/2024/07/23/17/BMjAyNDA3MjMxNzA1MThfNDI2OTI3Njc2Ml8yX2hkNDg1XzgxNQ==_s.jpg",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"shopId": "4269276762",
|
||||
"kshopName": "稚嫩游戏交易店",
|
||||
"cookie": "_did=web_1426300268274CD9; did=web_ozkrrsevhb6yuztyk6m63vqqi5h2pzfl; __risk_web_device_id=cd52e12c1773406609757e71; sid=kuaishou.shop.b; sellerId=4269276762; bUserId=1000492186791; userId=4269276762; merchantSellerId=4269276762; merchantSessionKey=1777621574120_fbd47024d23e73fc4ad1a157ffece433; kuaishou.shop.b_st=ChJrdWFpc2hvdS5zaG9wLmIuc3QSoAFzGnewF-wCpt4rHi-D-pp8zPUp7HByKY0MlHkw-WV676zG7z09Y5i7d7URwmGC8CL13jN0fOGV7d3GfaH2XLH8d4p60z0Ms-dIX0bipkI0yUWHfgnWqv9VPa8HqVRqMLs2VGD82X9tAR3eN9PXu38AYg0a5kpawchGPb2tjcYgLgH5b5DnJgjipHqzWb8Ma8AcLOmryqCSg4ZHwO5dbVJHGhIdwFBlxRpZgyh6i0Do-DSRsroiIIayd1xA628OsvCY2qV1HjCrTvMVt-3LmXcU6rLKSzytKAUwAQ; kuaishou.shop.b_ph=38925ccb882ec5456dd1c6bcee3f87b7e740",
|
||||
"userAvatar": "https://p2-pro.a.yximgs.com/uhead/AB/2024/07/23/17/BMjAyNDA3MjMxNzA1MThfNDI2OTI3Njc2Ml8yX2hkNDg1XzgxNQ==_s.jpg",
|
||||
"enabled": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -10,20 +10,25 @@ import {
|
||||
getAdminCloudtentaclesAsset,
|
||||
getAdminCloudtentaclesBindUrl,
|
||||
getAdminCloudtentaclesCategories,
|
||||
getAdminKuaishouEticketSourceConfig,
|
||||
getAdminCloudtentaclesKnapsack,
|
||||
getAdminCloudtentaclesSourceConfig,
|
||||
getAdminCloudtentaclesSkuList,
|
||||
getAdminAgisoShopConfigs,
|
||||
getAdminFulfillmentBindingConfigs,
|
||||
getAdminKhhaoSourceConfig,
|
||||
consumeAdminKuaishouEticket,
|
||||
listAdminCloudtentaclesVirtualNumbers,
|
||||
lookupAdminFulfillmentBindingOrder,
|
||||
queryAdminKuaishouEticketShopInfo,
|
||||
queryAdminKuaishouEticketDetail,
|
||||
queryAdminKhhaoOrders,
|
||||
runAdminCloudtentaclesFullFlow,
|
||||
sendAdminCloudtentaclesSmsCode,
|
||||
syncAdminKhhaoOrders,
|
||||
testAdminCloudtentaclesLogin,
|
||||
testAdminKhhaoLogin,
|
||||
updateAdminKuaishouEticketSourceConfig,
|
||||
updateAdminCloudtentaclesSourceConfig,
|
||||
updateAdminAgisoShopConfigs,
|
||||
updateAdminKhhaoSourceConfig,
|
||||
@@ -39,6 +44,10 @@ import { createJsonHandler, requireAdminRoles } from './shared.js'
|
||||
/** @typedef {import('../../types/admin-route-inputs.js').AdminKhhaoOrderQueryRouteBody} AdminKhhaoOrderQueryRouteBody */
|
||||
/** @typedef {import('../../types/admin-route-inputs.js').AdminKhhaoSourceConfigRouteBody} AdminKhhaoSourceConfigRouteBody */
|
||||
/** @typedef {import('../../types/admin-route-inputs.js').AdminKhhaoTestLoginRouteBody} AdminKhhaoTestLoginRouteBody */
|
||||
/** @typedef {import('../../types/admin-route-inputs.js').AdminKuaishouEticketSourceConfigRouteBody} AdminKuaishouEticketSourceConfigRouteBody */
|
||||
/** @typedef {import('../../types/admin-route-inputs.js').AdminKuaishouEticketDetailQueryRouteBody} AdminKuaishouEticketDetailQueryRouteBody */
|
||||
/** @typedef {import('../../types/admin-route-inputs.js').AdminKuaishouEticketConsumeRouteBody} AdminKuaishouEticketConsumeRouteBody */
|
||||
/** @typedef {import('../../types/admin-route-inputs.js').AdminKuaishouEticketShopInfoRouteBody} AdminKuaishouEticketShopInfoRouteBody */
|
||||
/** @typedef {import('../../types/admin-route-inputs.js').AdminCloudtentaclesSourceConfigRouteBody} AdminCloudtentaclesSourceConfigRouteBody */
|
||||
/** @typedef {import('../../types/admin-route-inputs.js').AdminCloudtentaclesSendSmsCodeRouteBody} AdminCloudtentaclesSendSmsCodeRouteBody */
|
||||
/** @typedef {import('../../types/admin-route-inputs.js').AdminCloudtentaclesTestLoginRouteBody} AdminCloudtentaclesTestLoginRouteBody */
|
||||
@@ -83,6 +92,37 @@ router.post('/platform-config/agiso-shops', createJsonHandler(
|
||||
},
|
||||
))
|
||||
|
||||
router.get('/platform-config/kuaishou-eticket-source', createJsonHandler(
|
||||
() => getAdminKuaishouEticketSourceConfig(),
|
||||
{
|
||||
successMessage: 'ok',
|
||||
errorMessage: '读取快手小店核销配置失败',
|
||||
scope: '[admin/platform-config/kuaishou-eticket-source]',
|
||||
},
|
||||
))
|
||||
|
||||
router.post('/platform-config/kuaishou-eticket-source', createJsonHandler(
|
||||
(req) => updateAdminKuaishouEticketSourceConfig(/** @type {AdminKuaishouEticketSourceConfigRouteBody} */ (req.body)),
|
||||
{
|
||||
successMessage: '快手小店核销配置已保存',
|
||||
errorMessage: '保存快手小店核销配置失败',
|
||||
scope: '[admin/platform-config/kuaishou-eticket-source]',
|
||||
audit: (_req, data) => {
|
||||
const result = /** @type {{ filePath?: string, source?: { enabled?: boolean, shops?: unknown[] } }} */ (data)
|
||||
return {
|
||||
action: 'platform_kuaishou_eticket_source_updated',
|
||||
targetType: 'platform_config',
|
||||
targetId: 'kuaishou_eticket_source',
|
||||
data: {
|
||||
filePath: String(result.filePath || '').trim(),
|
||||
enabled: Boolean(result.source?.enabled),
|
||||
shopCount: Array.isArray(result.source?.shops) ? result.source.shops.length : 0,
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
))
|
||||
|
||||
router.get('/platform-config/khhao-source', createJsonHandler(
|
||||
() => getAdminKhhaoSourceConfig(),
|
||||
{
|
||||
@@ -188,6 +228,75 @@ router.post('/platform-config/khhao/sync-orders', createJsonHandler(
|
||||
},
|
||||
))
|
||||
|
||||
router.post('/platform-config/kuaishou-eticket/query-detail', createJsonHandler(
|
||||
(req) => queryAdminKuaishouEticketDetail(/** @type {AdminKuaishouEticketDetailQueryRouteBody} */ (req.body)),
|
||||
{
|
||||
successMessage: '快手小店核销信息查询成功',
|
||||
errorMessage: '查询快手小店核销信息失败',
|
||||
scope: '[admin/platform-config/kuaishou-eticket/query-detail]',
|
||||
audit: (req, data) => {
|
||||
const body = /** @type {AdminKuaishouEticketDetailQueryRouteBody} */ (req.body)
|
||||
const result = /** @type {{ eTicketId?: string, result?: number, ok?: boolean, alreadyConsumed?: boolean }} */ (data)
|
||||
return {
|
||||
action: 'platform_kuaishou_eticket_query_detail',
|
||||
targetType: 'platform_config',
|
||||
targetId: String(result.eTicketId || body.eTicketId || '').trim() || 'kuaishou_eticket',
|
||||
data: {
|
||||
result: Number(result.result || 0),
|
||||
ok: Boolean(result.ok),
|
||||
alreadyConsumed: Boolean(result.alreadyConsumed),
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
))
|
||||
|
||||
router.post('/platform-config/kuaishou-eticket/query-shop-info', createJsonHandler(
|
||||
(req) => queryAdminKuaishouEticketShopInfo(/** @type {AdminKuaishouEticketShopInfoRouteBody} */ (req.body)),
|
||||
{
|
||||
successMessage: '快手小店店铺信息查询成功',
|
||||
errorMessage: '查询快手小店店铺信息失败',
|
||||
scope: '[admin/platform-config/kuaishou-eticket/query-shop-info]',
|
||||
audit: (req, data) => {
|
||||
const body = /** @type {AdminKuaishouEticketShopInfoRouteBody} */ (req.body)
|
||||
const result = /** @type {{ shop?: { shopId?: string, kshopName?: string }, result?: number, ok?: boolean }} */ (data)
|
||||
return {
|
||||
action: 'platform_kuaishou_eticket_query_shop_info',
|
||||
targetType: 'platform_config',
|
||||
targetId: String(result.shop?.shopId || body.shopId || '').trim() || 'kuaishou_eticket_shop_info',
|
||||
data: {
|
||||
result: Number(result.result || 0),
|
||||
ok: Boolean(result.ok),
|
||||
kshopName: String(result.shop?.kshopName || '').trim(),
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
))
|
||||
|
||||
router.post('/platform-config/kuaishou-eticket/consume', createJsonHandler(
|
||||
(req) => consumeAdminKuaishouEticket(/** @type {AdminKuaishouEticketConsumeRouteBody} */ (req.body)),
|
||||
{
|
||||
successMessage: '快手小店核销请求已执行',
|
||||
errorMessage: '执行快手小店核销失败',
|
||||
scope: '[admin/platform-config/kuaishou-eticket/consume]',
|
||||
audit: (req, data) => {
|
||||
const body = /** @type {AdminKuaishouEticketConsumeRouteBody} */ (req.body)
|
||||
const result = /** @type {{ eTicketId?: string, result?: number, consumed?: boolean, alreadyConsumed?: boolean }} */ (data)
|
||||
return {
|
||||
action: 'platform_kuaishou_eticket_consume',
|
||||
targetType: 'platform_config',
|
||||
targetId: String(result.eTicketId || body.eTicketId || '').trim() || 'kuaishou_eticket',
|
||||
data: {
|
||||
result: Number(result.result || 0),
|
||||
consumed: Boolean(result.consumed),
|
||||
alreadyConsumed: Boolean(result.alreadyConsumed),
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
))
|
||||
|
||||
router.get('/platform-config/cloudtentacles-source', createJsonHandler(
|
||||
() => getAdminCloudtentaclesSourceConfig(),
|
||||
{
|
||||
|
||||
@@ -15,6 +15,19 @@ import { syncKhhaoOrders } from '../platforms/khhao/order-sync-service.js'
|
||||
import { getKhhaoSourceConfig, getKhhaoSourcesFilePath, saveKhhaoSourceConfig } from '../platforms/khhao/source-config-service.js'
|
||||
import { ensureKhhaoSyncBaseline, getKhhaoSyncState, getKhhaoSyncStateFilePath } from '../platforms/khhao/sync-state-service.js'
|
||||
import { loginKhhaoSession } from '../platforms/khhao/session-service.js'
|
||||
import {
|
||||
consumeKuaishouEticket,
|
||||
queryKuaishouEticketConsumeDetail,
|
||||
} from '../platforms/kuaishou-eticket/consume-service.js'
|
||||
import {
|
||||
findKuaishouEticketShopConfig,
|
||||
getFirstAvailableKuaishouEticketShop,
|
||||
getKuaishouEticketSourceConfig,
|
||||
getKuaishouEticketSourceFilePath,
|
||||
listKuaishouEticketShopConfigs,
|
||||
saveKuaishouEticketSourceConfig,
|
||||
} from '../platforms/kuaishou-eticket/source-config-service.js'
|
||||
import { queryKuaishouEticketStableInfo } from '../platforms/kuaishou-eticket/info-service.js'
|
||||
import {
|
||||
getCloudtentaclesSourceConfig,
|
||||
getCloudtentaclesSourcesFilePath,
|
||||
@@ -62,6 +75,10 @@ import { resolveDisplayShopName } from './admin-read-shared-helpers.js'
|
||||
|
||||
/** @typedef {import('../../types/admin-write-inputs.js').AdminAgisoShopConfigSaveInput} AdminAgisoShopConfigSaveInput */
|
||||
/** @typedef {import('../../types/admin-write-inputs.js').AdminKhhaoSourceConfigInput} AdminKhhaoSourceConfigInput */
|
||||
/** @typedef {import('../../types/admin-write-inputs.js').AdminKuaishouEticketSourceConfigInput} AdminKuaishouEticketSourceConfigInput */
|
||||
/** @typedef {import('../../types/admin-write-inputs.js').AdminKuaishouEticketShopInfoInput} AdminKuaishouEticketShopInfoInput */
|
||||
/** @typedef {import('../../types/admin-write-inputs.js').AdminKuaishouEticketDetailQueryInput} AdminKuaishouEticketDetailQueryInput */
|
||||
/** @typedef {import('../../types/admin-write-inputs.js').AdminKuaishouEticketConsumeInput} AdminKuaishouEticketConsumeInput */
|
||||
/** @typedef {import('../../types/admin-write-inputs.js').AdminKhhaoOrderQueryInput} AdminKhhaoOrderQueryInput */
|
||||
/** @typedef {import('../../types/admin-write-inputs.js').AdminKhhaoTestLoginInput} AdminKhhaoTestLoginInput */
|
||||
/** @typedef {import('../../types/admin-write-inputs.js').AdminCloudtentaclesSourceConfigInput} AdminCloudtentaclesSourceConfigInput */
|
||||
@@ -258,6 +275,37 @@ export function getAdminKhhaoSourceConfig() {
|
||||
}
|
||||
}
|
||||
|
||||
export function getAdminKuaishouEticketSourceConfig() {
|
||||
const config = getKuaishouEticketSourceConfig()
|
||||
|
||||
return {
|
||||
filePath: getKuaishouEticketSourceFilePath(),
|
||||
source: {
|
||||
enabled: config.enabled !== false,
|
||||
baseUrl: String(config.baseUrl || '').trim(),
|
||||
shops: listKuaishouEticketShopConfigs(config).map((item) => mapAdminKuaishouEticketShopItem(item)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** @param {AdminKuaishouEticketSourceConfigInput} [payload] */
|
||||
export function updateAdminKuaishouEticketSourceConfig(payload = /** @type {AdminKuaishouEticketSourceConfigInput} */ ({})) {
|
||||
const saved = saveKuaishouEticketSourceConfig({
|
||||
enabled: payload.enabled !== false,
|
||||
baseUrl: String(payload.baseUrl || '').trim() || 'https://s.kwaixiaodian.com',
|
||||
shops: Array.isArray(payload.shops) ? payload.shops : [],
|
||||
})
|
||||
|
||||
return {
|
||||
filePath: getKuaishouEticketSourceFilePath(),
|
||||
source: {
|
||||
enabled: saved.enabled !== false,
|
||||
baseUrl: String(saved.baseUrl || '').trim(),
|
||||
shops: listKuaishouEticketShopConfigs(saved).map((item) => mapAdminKuaishouEticketShopItem(item)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** @param {AdminKhhaoSourceConfigInput} [payload] */
|
||||
export function updateAdminKhhaoSourceConfig(payload = /** @type {AdminKhhaoSourceConfigInput} */ ({})) {
|
||||
const saved = saveKhhaoSourceConfig({
|
||||
@@ -376,6 +424,41 @@ export async function syncAdminKhhaoOrders(payload = /** @type {AdminKhhaoOrderQ
|
||||
})
|
||||
}
|
||||
|
||||
/** @param {AdminKuaishouEticketDetailQueryInput} [payload] */
|
||||
export async function queryAdminKuaishouEticketDetail(payload = /** @type {AdminKuaishouEticketDetailQueryInput} */ ({})) {
|
||||
const context = resolveKuaishouEticketAdminContext(payload)
|
||||
|
||||
return queryKuaishouEticketConsumeDetail({
|
||||
baseUrl: context.baseUrl,
|
||||
cookie: context.cookie,
|
||||
eTicketId: String(payload.eTicketId || '').trim(),
|
||||
})
|
||||
}
|
||||
|
||||
/** @param {AdminKuaishouEticketShopInfoInput} [payload] */
|
||||
export async function queryAdminKuaishouEticketShopInfo(payload = /** @type {AdminKuaishouEticketShopInfoInput} */ ({})) {
|
||||
const context = resolveKuaishouEticketAdminContext(payload)
|
||||
return queryKuaishouEticketStableInfo({
|
||||
baseUrl: context.baseUrl,
|
||||
cookie: context.cookie,
|
||||
})
|
||||
}
|
||||
|
||||
/** @param {AdminKuaishouEticketConsumeInput} [payload] */
|
||||
export async function consumeAdminKuaishouEticket(payload = /** @type {AdminKuaishouEticketConsumeInput} */ ({})) {
|
||||
const context = resolveKuaishouEticketAdminContext(payload)
|
||||
|
||||
return consumeKuaishouEticket({
|
||||
baseUrl: context.baseUrl,
|
||||
cookie: context.cookie,
|
||||
eTicketId: String(payload.eTicketId || '').trim(),
|
||||
oid: String(payload.oid || '').trim(),
|
||||
formToken: String(payload.formToken || '').trim(),
|
||||
num: payload.num,
|
||||
storeId: String(payload.storeId || '').trim(),
|
||||
})
|
||||
}
|
||||
|
||||
export function getAdminCloudtentaclesSourceConfig() {
|
||||
const config = getCloudtentaclesSourceConfig()
|
||||
const session = getCloudtentaclesSessionState()
|
||||
@@ -984,6 +1067,33 @@ function maskPhone(value) {
|
||||
return `${normalized.slice(0, 3)}****${normalized.slice(-4)}`
|
||||
}
|
||||
|
||||
function mapAdminKuaishouEticketShopItem(item = {}) {
|
||||
return {
|
||||
shopId: String(item.shopId || '').trim(),
|
||||
kshopName: String(item.kshopName || '').trim(),
|
||||
cookie: String(item.cookie || '').trim(),
|
||||
cookieMasked: maskSecret(item.cookie),
|
||||
hasCookie: Boolean(String(item.cookie || '').trim()),
|
||||
userAvatar: String(item.userAvatar || '').trim(),
|
||||
enabled: item.enabled !== false,
|
||||
}
|
||||
}
|
||||
|
||||
function resolveKuaishouEticketAdminContext(payload = {}) {
|
||||
const savedSource = getKuaishouEticketSourceConfig()
|
||||
const requestedShopId = String(payload.shopId || '').trim()
|
||||
const configuredShop = requestedShopId
|
||||
? findKuaishouEticketShopConfig(requestedShopId, savedSource)
|
||||
: getFirstAvailableKuaishouEticketShop(savedSource)
|
||||
|
||||
return {
|
||||
baseUrl: pickFirstNonEmpty([payload.baseUrl, savedSource.baseUrl, 'https://s.kwaixiaodian.com']),
|
||||
cookie: pickFirstNonEmpty([payload.cookie, configuredShop?.cookie]),
|
||||
shopId: pickFirstNonEmpty([requestedShopId, configuredShop?.shopId]),
|
||||
shop: configuredShop,
|
||||
}
|
||||
}
|
||||
|
||||
function resolveCloudtentaclesAdminContext(payload = {}) {
|
||||
const savedSource = getCloudtentaclesSourceConfig()
|
||||
const persistedSession = getCloudtentaclesSessionState()
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
// @ts-check
|
||||
|
||||
import { createHttpError } from '../../../utils/http.js'
|
||||
import { kuaishouEticketRequest } from './http-client.js'
|
||||
import { resolveKuaishouEticketConfig } from './shared.js'
|
||||
|
||||
export async function queryKuaishouEticketConsumeDetail(payload = {}) {
|
||||
const context = resolveKuaishouEticketActionContext(payload)
|
||||
const response = await kuaishouEticketRequest(context.detailPath, {
|
||||
baseUrl: context.baseUrl,
|
||||
cookie: context.cookie,
|
||||
timeoutMs: context.timeoutMs,
|
||||
body: {
|
||||
eTicketId: context.eTicketId,
|
||||
},
|
||||
})
|
||||
|
||||
return mapKuaishouEticketDetailResult(response.payload, {
|
||||
baseUrl: context.baseUrl,
|
||||
eTicketId: context.eTicketId,
|
||||
})
|
||||
}
|
||||
|
||||
export async function consumeKuaishouEticket(payload = {}) {
|
||||
const context = resolveKuaishouEticketActionContext(payload)
|
||||
const detailResult = shouldResolveConsumeDetail(payload)
|
||||
? await queryKuaishouEticketConsumeDetail({
|
||||
baseUrl: context.baseUrl,
|
||||
cookie: context.cookie,
|
||||
eTicketId: context.eTicketId,
|
||||
timeoutMs: context.timeoutMs,
|
||||
})
|
||||
: null
|
||||
const consumePayload = resolveKuaishouEticketConsumePayload(payload, detailResult)
|
||||
|
||||
const response = await kuaishouEticketRequest(context.consumePath, {
|
||||
baseUrl: context.baseUrl,
|
||||
cookie: context.cookie,
|
||||
timeoutMs: context.timeoutMs,
|
||||
body: consumePayload,
|
||||
})
|
||||
|
||||
return mapKuaishouEticketConsumeResult(response.payload, {
|
||||
baseUrl: context.baseUrl,
|
||||
eTicketId: consumePayload.eTicketId,
|
||||
oid: consumePayload.oid,
|
||||
formToken: consumePayload.formToken,
|
||||
num: consumePayload.num,
|
||||
storeId: consumePayload.storeId,
|
||||
detailResult,
|
||||
})
|
||||
}
|
||||
|
||||
export function mapKuaishouEticketDetailResult(payload = {}, context = {}) {
|
||||
const normalized = normalizeKuaishouEticketApiResult(payload)
|
||||
const detail = mapKuaishouEticketDetailPayload(payload?.data?.eTicket)
|
||||
const goods = mapKuaishouEticketGoodsPayload(payload?.data?.goods)
|
||||
|
||||
return {
|
||||
baseUrl: String(context.baseUrl || '').trim(),
|
||||
eTicketId: String(context.eTicketId || detail?.eTicketId || '').trim(),
|
||||
ok: normalized.ok,
|
||||
alreadyConsumed: normalized.result === 21,
|
||||
result: normalized.result,
|
||||
errorMessage: normalized.errorMessage,
|
||||
requestId: normalized.requestId,
|
||||
serverTimestamp: normalized.serverTimestamp,
|
||||
detail,
|
||||
goods,
|
||||
raw: payload && typeof payload === 'object' ? payload : {},
|
||||
}
|
||||
}
|
||||
|
||||
export function mapKuaishouEticketConsumeResult(payload = {}, context = {}) {
|
||||
const normalized = normalizeKuaishouEticketApiResult(payload)
|
||||
|
||||
return {
|
||||
baseUrl: String(context.baseUrl || '').trim(),
|
||||
eTicketId: String(context.eTicketId || '').trim(),
|
||||
oid: String(context.oid || '').trim(),
|
||||
formToken: String(context.formToken || '').trim(),
|
||||
num: normalizePositiveInteger(context.num, 1),
|
||||
storeId: String(context.storeId || '0').trim() || '0',
|
||||
ok: normalized.ok,
|
||||
consumed: normalized.result === 1 || normalized.result === 21,
|
||||
alreadyConsumed: normalized.result === 21,
|
||||
result: normalized.result,
|
||||
errorMessage: normalized.errorMessage,
|
||||
requestId: normalized.requestId,
|
||||
serverTimestamp: normalized.serverTimestamp,
|
||||
detail: context.detailResult?.detail || null,
|
||||
raw: payload && typeof payload === 'object' ? payload : {},
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveKuaishouEticketConsumePayload(payload = {}, detailResult = null) {
|
||||
const detail = detailResult?.detail || null
|
||||
const eTicketId = pickFirstNonEmpty([payload.eTicketId, detail?.eTicketId])
|
||||
const oid = pickFirstNonEmpty([payload.oid, detail?.oid])
|
||||
const formToken = pickFirstNonEmpty([payload.formToken, detail?.formToken])
|
||||
const storeId = pickFirstNonEmpty([payload.storeId, '0'])
|
||||
const num = normalizePositiveInteger(payload.num, normalizePositiveInteger(detail?.leftCount, 1))
|
||||
|
||||
if (!eTicketId) {
|
||||
throw createHttpError('请先填写 eTicketId', {
|
||||
statusCode: 400,
|
||||
errorCode: 'kuaishou_eticket_missing_eticket_id',
|
||||
})
|
||||
}
|
||||
|
||||
if (!oid) {
|
||||
throw createHttpError('缺少核销所需 oid,请先查询核销信息', {
|
||||
statusCode: 400,
|
||||
errorCode: 'kuaishou_eticket_missing_oid',
|
||||
})
|
||||
}
|
||||
|
||||
if (!formToken) {
|
||||
throw createHttpError('缺少核销所需 formToken,请先查询核销信息', {
|
||||
statusCode: 400,
|
||||
errorCode: 'kuaishou_eticket_missing_form_token',
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
eTicketId,
|
||||
num,
|
||||
storeId,
|
||||
oid,
|
||||
formToken,
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizeKuaishouEticketApiResult(payload = {}) {
|
||||
const result = Number(payload?.result || 0)
|
||||
|
||||
return {
|
||||
ok: result === 1,
|
||||
result,
|
||||
errorMessage: String(payload?.error_msg || '').trim(),
|
||||
requestId: String(payload?.requestId || '').trim(),
|
||||
serverTimestamp: String(payload?.serverTimestamp || '').trim(),
|
||||
}
|
||||
}
|
||||
|
||||
export function mapKuaishouEticketDetailPayload(value = {}) {
|
||||
if (!isPlainObject(value)) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
uid: String(value.uid || '').trim(),
|
||||
fulfillDetailId: String(value.fulfillDetailId || '').trim(),
|
||||
sellerId: String(value.sellerId || '').trim(),
|
||||
formToken: String(value.formToken || '').trim(),
|
||||
validEndTime: String(value.validEndTime || '').trim(),
|
||||
validStartTime: String(value.validStartTime || '').trim(),
|
||||
leftReverseCount: normalizeInteger(value.leftReverseCount, 0),
|
||||
eTicketId: String(value.eTicketId || '').trim(),
|
||||
oid: String(value.oid || '').trim(),
|
||||
totalCount: normalizeInteger(value.totalCount, 0),
|
||||
leftCount: normalizeInteger(value.leftCount, 0),
|
||||
status: String(value.status || '').trim(),
|
||||
}
|
||||
}
|
||||
|
||||
export function mapKuaishouEticketGoodsPayload(value = {}) {
|
||||
if (!isPlainObject(value)) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
itemId: String(value.itemId || '').trim(),
|
||||
itemPicUrl: String(value.itemPicUrl || '').trim(),
|
||||
itemTitle: String(value.itemTitle || '').trim(),
|
||||
price: String(value.price || '').trim(),
|
||||
skuDesc: String(value.skuDesc || '').trim(),
|
||||
skuId: String(value.skuId || '').trim(),
|
||||
}
|
||||
}
|
||||
|
||||
function resolveKuaishouEticketActionContext(payload = {}) {
|
||||
const config = resolveKuaishouEticketConfig(payload)
|
||||
const eTicketId = String(payload.eTicketId || '').trim()
|
||||
|
||||
if (!config.cookie) {
|
||||
throw createHttpError('请先配置快手小店 Cookie', {
|
||||
statusCode: 400,
|
||||
errorCode: 'kuaishou_eticket_missing_cookie',
|
||||
})
|
||||
}
|
||||
|
||||
if (!eTicketId) {
|
||||
throw createHttpError('请先填写 eTicketId', {
|
||||
statusCode: 400,
|
||||
errorCode: 'kuaishou_eticket_missing_eticket_id',
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
...config,
|
||||
eTicketId,
|
||||
}
|
||||
}
|
||||
|
||||
function shouldResolveConsumeDetail(payload = {}) {
|
||||
return !String(payload.oid || '').trim() || !String(payload.formToken || '').trim()
|
||||
}
|
||||
|
||||
function pickFirstNonEmpty(values) {
|
||||
for (const value of values) {
|
||||
const normalized = String(value || '').trim()
|
||||
if (normalized) {
|
||||
return normalized
|
||||
}
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
function normalizePositiveInteger(value, fallback) {
|
||||
const parsed = Number(value)
|
||||
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback
|
||||
}
|
||||
|
||||
function normalizeInteger(value, fallback) {
|
||||
const parsed = Number(value)
|
||||
return Number.isInteger(parsed) ? parsed : fallback
|
||||
}
|
||||
|
||||
function isPlainObject(value) {
|
||||
return Object.prototype.toString.call(value) === '[object Object]'
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import {
|
||||
mapKuaishouEticketDetailResult,
|
||||
normalizeKuaishouEticketApiResult,
|
||||
resolveKuaishouEticketConsumePayload,
|
||||
} from './consume-service.js'
|
||||
|
||||
test('normalizeKuaishouEticketApiResult recognizes already consumed response', () => {
|
||||
const result = normalizeKuaishouEticketApiResult({
|
||||
result: 21,
|
||||
error_msg: '券码已核销无法再次核销,请核实后重试',
|
||||
requestId: 'abc',
|
||||
})
|
||||
|
||||
assert.equal(result.ok, false)
|
||||
assert.equal(result.result, 21)
|
||||
assert.equal(result.errorMessage, '券码已核销无法再次核销,请核实后重试')
|
||||
assert.equal(result.requestId, 'abc')
|
||||
})
|
||||
|
||||
test('mapKuaishouEticketDetailResult maps detail payload fields', () => {
|
||||
const result = mapKuaishouEticketDetailResult({
|
||||
result: 1,
|
||||
error_msg: '成功',
|
||||
data: {
|
||||
eTicket: {
|
||||
eTicketId: 'F2E886B6CBF8E5F5',
|
||||
oid: '2612200246413561',
|
||||
formToken: '1777716306154',
|
||||
leftCount: 1,
|
||||
totalCount: 1,
|
||||
status: 'CONSUME_ORDER_CREATED',
|
||||
},
|
||||
goods: {
|
||||
itemTitle: '测试链接五排',
|
||||
skuDesc: '测试1',
|
||||
},
|
||||
},
|
||||
}, {
|
||||
baseUrl: 'https://s.kwaixiaodian.com',
|
||||
eTicketId: 'F2E886B6CBF8E5F5',
|
||||
})
|
||||
|
||||
assert.equal(result.ok, true)
|
||||
assert.equal(result.detail?.oid, '2612200246413561')
|
||||
assert.equal(result.detail?.formToken, '1777716306154')
|
||||
assert.equal(result.detail?.leftCount, 1)
|
||||
assert.equal(result.goods?.itemTitle, '测试链接五排')
|
||||
})
|
||||
|
||||
test('resolveKuaishouEticketConsumePayload falls back to detail result', () => {
|
||||
const payload = resolveKuaishouEticketConsumePayload({
|
||||
eTicketId: 'F2E886B6CBF8E5F5',
|
||||
}, {
|
||||
detail: {
|
||||
eTicketId: 'F2E886B6CBF8E5F5',
|
||||
oid: '2612200246413561',
|
||||
formToken: '1777716306154',
|
||||
leftCount: 2,
|
||||
},
|
||||
})
|
||||
|
||||
assert.deepEqual(payload, {
|
||||
eTicketId: 'F2E886B6CBF8E5F5',
|
||||
num: 2,
|
||||
storeId: '0',
|
||||
oid: '2612200246413561',
|
||||
formToken: '1777716306154',
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,170 @@
|
||||
// @ts-check
|
||||
|
||||
import http from 'node:http'
|
||||
import https from 'node:https'
|
||||
|
||||
import { parseJsonObject } from '../../../utils/json.js'
|
||||
import { createHttpError } from '../../../utils/http.js'
|
||||
import { logInfo } from '../../../utils/logger.js'
|
||||
import { buildKuaishouEticketHeaders, buildKuaishouEticketUrl, resolveKuaishouEticketConfig } from './shared.js'
|
||||
|
||||
export async function kuaishouEticketRequest(pathname, options = {}) {
|
||||
const config = resolveKuaishouEticketConfig(options)
|
||||
const url = buildKuaishouEticketUrl(config.baseUrl, pathname)
|
||||
const timeoutMs = Number(options.timeoutMs || config.timeoutMs || 8000)
|
||||
const controller = new AbortController()
|
||||
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
||||
const method = String(options.method || 'POST').trim().toUpperCase()
|
||||
const headers = buildKuaishouEticketHeaders({
|
||||
baseUrl: config.baseUrl,
|
||||
refererPath: config.refererPath,
|
||||
userAgent: config.userAgent,
|
||||
cookie: options.cookie ?? config.cookie,
|
||||
contentType: options.contentType === null ? '' : options.contentType || inferContentType(options.body),
|
||||
extra: options.headers,
|
||||
})
|
||||
|
||||
try {
|
||||
const response = await requestViaNodeHttp(url, {
|
||||
method,
|
||||
headers,
|
||||
body: normalizeRequestBody(options.body, headers['content-type']),
|
||||
signal: controller.signal,
|
||||
})
|
||||
|
||||
const rawText = response.bodyText
|
||||
const payload = parseJsonObject(rawText, { preserveLargeIntegers: true })
|
||||
|
||||
if (!response.ok) {
|
||||
throw createHttpError(`快手小店核销请求失败,HTTP ${response.status}`, {
|
||||
statusCode: 502,
|
||||
errorCode: 'kuaishou_eticket_http_error',
|
||||
})
|
||||
}
|
||||
|
||||
logInfo('[kuaishou-eticket/http]', '请求完成', {
|
||||
method,
|
||||
pathname,
|
||||
status: response.status,
|
||||
result: Number(payload?.result || 0),
|
||||
})
|
||||
|
||||
return {
|
||||
url: url.toString(),
|
||||
status: response.status,
|
||||
headers: response.headers,
|
||||
payload,
|
||||
rawText,
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
throw createHttpError(`快手小店核销请求超时(${timeoutMs}ms)`, {
|
||||
statusCode: 504,
|
||||
errorCode: 'kuaishou_eticket_request_timeout',
|
||||
})
|
||||
}
|
||||
throw error
|
||||
} finally {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
}
|
||||
|
||||
function inferContentType(body) {
|
||||
if (body == null) {
|
||||
return ''
|
||||
}
|
||||
|
||||
if (typeof body === 'string') {
|
||||
return 'application/json'
|
||||
}
|
||||
|
||||
return 'application/json'
|
||||
}
|
||||
|
||||
function normalizeRequestBody(body, contentType) {
|
||||
if (body == null) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
if (typeof body === 'string') {
|
||||
return body
|
||||
}
|
||||
|
||||
if (String(contentType || '').includes('application/json')) {
|
||||
return JSON.stringify(body)
|
||||
}
|
||||
|
||||
return String(body)
|
||||
}
|
||||
|
||||
async function requestViaNodeHttp(url, { method, headers, body, signal }) {
|
||||
const isHttps = url.protocol === 'https:'
|
||||
const transport = isHttps ? https : http
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = transport.request(url, {
|
||||
method,
|
||||
headers,
|
||||
rejectUnauthorized: false,
|
||||
}, (response) => {
|
||||
const chunks = []
|
||||
|
||||
response.on('data', (chunk) => {
|
||||
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
|
||||
})
|
||||
|
||||
response.on('end', () => {
|
||||
const bodyText = Buffer.concat(chunks).toString('utf8')
|
||||
resolve({
|
||||
ok: Number(response.statusCode || 0) >= 200 && Number(response.statusCode || 0) < 300,
|
||||
status: Number(response.statusCode || 0),
|
||||
headers: createHeaderAdapter(response.headers),
|
||||
bodyText,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
request.on('error', reject)
|
||||
|
||||
if (signal) {
|
||||
if (signal.aborted) {
|
||||
const error = new Error('Request aborted')
|
||||
error.name = 'AbortError'
|
||||
request.destroy(error)
|
||||
} else {
|
||||
signal.addEventListener('abort', () => {
|
||||
const error = new Error('Request aborted')
|
||||
error.name = 'AbortError'
|
||||
request.destroy(error)
|
||||
}, { once: true })
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof body !== 'undefined') {
|
||||
request.write(body)
|
||||
}
|
||||
|
||||
request.end()
|
||||
})
|
||||
}
|
||||
|
||||
function createHeaderAdapter(headers) {
|
||||
const normalized = new Map()
|
||||
|
||||
for (const [key, value] of Object.entries(headers || {})) {
|
||||
if (Array.isArray(value)) {
|
||||
normalized.set(String(key || '').toLowerCase(), value.join(', '))
|
||||
continue
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
normalized.set(String(key || '').toLowerCase(), value)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
get(name) {
|
||||
return normalized.get(String(name || '').toLowerCase()) || null
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// @ts-check
|
||||
|
||||
import { createHttpError } from '../../../utils/http.js'
|
||||
import { kuaishouEticketRequest } from './http-client.js'
|
||||
import { resolveKuaishouEticketConfig } from './shared.js'
|
||||
|
||||
export async function queryKuaishouEticketStableInfo(payload = {}) {
|
||||
const config = resolveKuaishouEticketConfig(payload)
|
||||
const cookie = String(payload.cookie || config.cookie || '').trim()
|
||||
|
||||
if (!cookie) {
|
||||
throw createHttpError('请先提供店铺 Cookie', {
|
||||
statusCode: 400,
|
||||
errorCode: 'kuaishou_eticket_missing_cookie',
|
||||
})
|
||||
}
|
||||
|
||||
const response = await kuaishouEticketRequest(config.infoPath, {
|
||||
method: 'GET',
|
||||
baseUrl: config.baseUrl,
|
||||
cookie,
|
||||
timeoutMs: config.timeoutMs,
|
||||
contentType: null,
|
||||
})
|
||||
|
||||
return mapKuaishouEticketStableInfoResult(response.payload, {
|
||||
baseUrl: config.baseUrl,
|
||||
cookie,
|
||||
})
|
||||
}
|
||||
|
||||
export function mapKuaishouEticketStableInfoResult(payload = {}, context = {}) {
|
||||
const result = Number(payload?.result || 0)
|
||||
const userInfo = isPlainObject(payload?.data?.userInfo) ? payload.data.userInfo : {}
|
||||
const bizStatus = isPlainObject(payload?.data?.bizStatus) ? payload.data.bizStatus : {}
|
||||
|
||||
return {
|
||||
baseUrl: String(context.baseUrl || '').trim(),
|
||||
ok: result === 1,
|
||||
result,
|
||||
errorMessage: String(payload?.error_msg || '').trim(),
|
||||
requestId: String(payload?.requestId || '').trim(),
|
||||
shop: {
|
||||
shopId: String(userInfo.userId || '').trim(),
|
||||
kshopName: String(userInfo.userName || '').trim(),
|
||||
userAvatar: String(userInfo.userAvatar || '').trim(),
|
||||
settleStatus: normalizeInteger(bizStatus.settleStatus, 0),
|
||||
hasCookie: Boolean(String(context.cookie || '').trim()),
|
||||
},
|
||||
raw: payload && typeof payload === 'object' ? payload : {},
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeInteger(value, fallback) {
|
||||
const parsed = Number(value)
|
||||
return Number.isInteger(parsed) ? parsed : fallback
|
||||
}
|
||||
|
||||
function isPlainObject(value) {
|
||||
return Object.prototype.toString.call(value) === '[object Object]'
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { mapKuaishouEticketStableInfoResult } from './info-service.js'
|
||||
|
||||
test('mapKuaishouEticketStableInfoResult extracts shop info from payload', () => {
|
||||
const result = mapKuaishouEticketStableInfoResult({
|
||||
result: 1,
|
||||
error_msg: '成功',
|
||||
requestId: '777795601162094230',
|
||||
data: {
|
||||
userInfo: {
|
||||
userId: 4269276762,
|
||||
userName: '稚嫩游戏交易店',
|
||||
userAvatar: 'https://example.com/avatar.jpg',
|
||||
},
|
||||
bizStatus: {
|
||||
settleStatus: 200,
|
||||
},
|
||||
},
|
||||
}, {
|
||||
baseUrl: 'https://s.kwaixiaodian.com',
|
||||
cookie: 'sid=abc',
|
||||
})
|
||||
|
||||
assert.equal(result.ok, true)
|
||||
assert.equal(result.shop.shopId, '4269276762')
|
||||
assert.equal(result.shop.kshopName, '稚嫩游戏交易店')
|
||||
assert.equal(result.shop.userAvatar, 'https://example.com/avatar.jpg')
|
||||
assert.equal(result.shop.settleStatus, 200)
|
||||
assert.equal(result.shop.hasCookie, true)
|
||||
})
|
||||
@@ -0,0 +1,75 @@
|
||||
// @ts-check
|
||||
|
||||
const DEFAULT_BASE_URL = 'https://s.kwaixiaodian.com'
|
||||
const DEFAULT_REFERER_PATH = '/zone/industry/voucher/verify-list'
|
||||
const DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36'
|
||||
|
||||
export function resolveKuaishouEticketConfig(overrides = {}) {
|
||||
return {
|
||||
enabled: overrides.enabled !== false,
|
||||
baseUrl: normalizeBaseUrl(overrides.baseUrl || DEFAULT_BASE_URL) || DEFAULT_BASE_URL,
|
||||
cookie: String(overrides.cookie || '').trim(),
|
||||
timeoutMs: normalizePositiveInteger(overrides.timeoutMs, 8000),
|
||||
infoPath: normalizePath(overrides.infoPath, '/gateway/pc/workbench/service/stable/info'),
|
||||
detailPath: normalizePath(overrides.detailPath, '/gateway/industry/eticket/consume/detail'),
|
||||
consumePath: normalizePath(overrides.consumePath, '/gateway/industry/eticket/consume'),
|
||||
refererPath: normalizePath(overrides.refererPath, DEFAULT_REFERER_PATH),
|
||||
userAgent: String(overrides.userAgent || DEFAULT_USER_AGENT).trim() || DEFAULT_USER_AGENT,
|
||||
}
|
||||
}
|
||||
|
||||
export function buildKuaishouEticketUrl(baseUrl, pathname) {
|
||||
return new URL(normalizePath(pathname, '/'), normalizeBaseUrl(baseUrl) || DEFAULT_BASE_URL)
|
||||
}
|
||||
|
||||
export function buildKuaishouEticketHeaders({
|
||||
baseUrl = DEFAULT_BASE_URL,
|
||||
refererPath = DEFAULT_REFERER_PATH,
|
||||
userAgent = DEFAULT_USER_AGENT,
|
||||
cookie = '',
|
||||
contentType = 'application/json',
|
||||
extra = {},
|
||||
} = {}) {
|
||||
return {
|
||||
accept: 'application/json',
|
||||
'accept-language': 'zh-CN,zh;q=0.9',
|
||||
...(contentType ? { 'content-type': contentType } : {}),
|
||||
kpf: 'PC_WEB',
|
||||
kpn: 'KWAIXIAODIAN',
|
||||
origin: normalizeBaseUrl(baseUrl) || DEFAULT_BASE_URL,
|
||||
referer: buildKuaishouEticketUrl(baseUrl, refererPath).toString(),
|
||||
'user-agent': String(userAgent || DEFAULT_USER_AGENT).trim() || DEFAULT_USER_AGENT,
|
||||
...(String(cookie || '').trim() ? { cookie: String(cookie || '').trim() } : {}),
|
||||
...normalizeHeaderMap(extra),
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeBaseUrl(value) {
|
||||
return String(value || '').trim().replace(/\/+$/, '')
|
||||
}
|
||||
|
||||
function normalizePath(value, fallback) {
|
||||
const normalized = String(value || '').trim()
|
||||
if (!normalized) {
|
||||
return fallback
|
||||
}
|
||||
|
||||
return normalized.startsWith('/') ? normalized : `/${normalized}`
|
||||
}
|
||||
|
||||
function normalizePositiveInteger(value, fallback) {
|
||||
const parsed = Number(value)
|
||||
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback
|
||||
}
|
||||
|
||||
function normalizeHeaderMap(extra) {
|
||||
if (!extra || typeof extra !== 'object') {
|
||||
return {}
|
||||
}
|
||||
|
||||
return Object.fromEntries(
|
||||
Object.entries(extra)
|
||||
.map(([key, value]) => [String(key || '').trim().toLowerCase(), String(value || '').trim()])
|
||||
.filter(([key, value]) => key && value),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
// @ts-check
|
||||
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
import { PROJECT_ROOT } from '../../../config/runtime.js'
|
||||
|
||||
const KUAISHOU_ETICKET_SOURCE_FILE_PATH = path.join(PROJECT_ROOT, 'data', 'kuaishou-eticket-source.json')
|
||||
const DEFAULT_BASE_URL = 'https://s.kwaixiaodian.com'
|
||||
|
||||
export function getKuaishouEticketSourceFilePath() {
|
||||
return KUAISHOU_ETICKET_SOURCE_FILE_PATH
|
||||
}
|
||||
|
||||
export function getKuaishouEticketSourceConfig() {
|
||||
return loadKuaishouEticketSourceConfigFromFile()
|
||||
}
|
||||
|
||||
export function saveKuaishouEticketSourceConfig(rawValue) {
|
||||
const normalized = normalizeKuaishouEticketSourceConfig(rawValue)
|
||||
fs.mkdirSync(path.dirname(KUAISHOU_ETICKET_SOURCE_FILE_PATH), { recursive: true })
|
||||
fs.writeFileSync(KUAISHOU_ETICKET_SOURCE_FILE_PATH, `${JSON.stringify(normalized, null, 2)}\n`, 'utf8')
|
||||
return normalized
|
||||
}
|
||||
|
||||
export function listKuaishouEticketShopConfigs(source = getKuaishouEticketSourceConfig()) {
|
||||
return Array.isArray(source?.shops) ? source.shops : []
|
||||
}
|
||||
|
||||
export function findKuaishouEticketShopConfig(shopId, source = getKuaishouEticketSourceConfig()) {
|
||||
const normalizedShopId = String(shopId || '').trim()
|
||||
if (!normalizedShopId) {
|
||||
return null
|
||||
}
|
||||
|
||||
return listKuaishouEticketShopConfigs(source).find((item) => String(item.shopId || '').trim() === normalizedShopId) || null
|
||||
}
|
||||
|
||||
export function getFirstAvailableKuaishouEticketShop(source = getKuaishouEticketSourceConfig()) {
|
||||
return listKuaishouEticketShopConfigs(source).find((item) => item.enabled !== false && String(item.cookie || '').trim()) || null
|
||||
}
|
||||
|
||||
function loadKuaishouEticketSourceConfigFromFile() {
|
||||
if (!fs.existsSync(KUAISHOU_ETICKET_SOURCE_FILE_PATH)) {
|
||||
return createDefaultKuaishouEticketSourceConfig()
|
||||
}
|
||||
|
||||
try {
|
||||
const rawText = fs.readFileSync(KUAISHOU_ETICKET_SOURCE_FILE_PATH, 'utf8')
|
||||
return normalizeKuaishouEticketSourceConfig(JSON.parse(rawText))
|
||||
} catch {
|
||||
return createDefaultKuaishouEticketSourceConfig()
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeKuaishouEticketSourceConfig(rawValue) {
|
||||
const source = isPlainObject(rawValue) ? rawValue : {}
|
||||
const shops = Array.isArray(source.shops)
|
||||
? source.shops
|
||||
: buildLegacySingleShopList(source)
|
||||
|
||||
return {
|
||||
enabled: typeof source.enabled === 'boolean' ? source.enabled : true,
|
||||
baseUrl: String(source.baseUrl || DEFAULT_BASE_URL).trim() || DEFAULT_BASE_URL,
|
||||
shops: shops
|
||||
.map((item) => normalizeKuaishouEticketShopConfig(item))
|
||||
.filter(Boolean),
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeKuaishouEticketShopConfig(rawValue) {
|
||||
if (!isPlainObject(rawValue)) {
|
||||
return null
|
||||
}
|
||||
|
||||
const shopId = String(rawValue.shopId || rawValue.userId || '').trim()
|
||||
const kshopName = String(rawValue.kshopName || rawValue.shopName || rawValue.userName || '').trim()
|
||||
const cookie = String(rawValue.cookie || '').trim()
|
||||
const userAvatar = String(rawValue.userAvatar || '').trim()
|
||||
const enabled = typeof rawValue.enabled === 'boolean' ? rawValue.enabled : true
|
||||
|
||||
if (!shopId && !kshopName && !cookie) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
shopId,
|
||||
kshopName,
|
||||
cookie,
|
||||
userAvatar,
|
||||
enabled,
|
||||
}
|
||||
}
|
||||
|
||||
function buildLegacySingleShopList(source) {
|
||||
const cookie = String(source.cookie || '').trim()
|
||||
if (!cookie) {
|
||||
return []
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
shopId: '',
|
||||
kshopName: '',
|
||||
cookie,
|
||||
userAvatar: '',
|
||||
enabled: source.enabled !== false,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
function createDefaultKuaishouEticketSourceConfig() {
|
||||
return {
|
||||
enabled: true,
|
||||
baseUrl: DEFAULT_BASE_URL,
|
||||
shops: [],
|
||||
}
|
||||
}
|
||||
|
||||
function isPlainObject(value) {
|
||||
return Object.prototype.toString.call(value) === '[object Object]'
|
||||
}
|
||||
@@ -34,10 +34,26 @@ export {}
|
||||
* @typedef {import('./admin-write-inputs.js').AdminKhhaoSourceConfigInput} AdminKhhaoSourceConfigRouteBody
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {import('./admin-write-inputs.js').AdminKuaishouEticketSourceConfigInput} AdminKuaishouEticketSourceConfigRouteBody
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {import('./admin-write-inputs.js').AdminAgisoShopConfigSaveInput} AdminAgisoShopConfigRouteBody
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {import('./admin-write-inputs.js').AdminKuaishouEticketDetailQueryInput} AdminKuaishouEticketDetailQueryRouteBody
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {import('./admin-write-inputs.js').AdminKuaishouEticketConsumeInput} AdminKuaishouEticketConsumeRouteBody
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {import('./admin-write-inputs.js').AdminKuaishouEticketShopInfoInput} AdminKuaishouEticketShopInfoRouteBody
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {import('./admin-write-inputs.js').AdminKhhaoTestLoginInput} AdminKhhaoTestLoginRouteBody
|
||||
*/
|
||||
|
||||
@@ -83,6 +83,54 @@ export {}
|
||||
* }} AdminKhhaoSourceConfigInput
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* shopId?: string
|
||||
* kshopName?: string
|
||||
* cookie?: string
|
||||
* userAvatar?: string
|
||||
* enabled?: boolean
|
||||
* }} AdminKuaishouEticketShopConfigWriteItemInput
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* enabled?: boolean
|
||||
* baseUrl?: string
|
||||
* shops?: AdminKuaishouEticketShopConfigWriteItemInput[]
|
||||
* }} AdminKuaishouEticketSourceConfigInput
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* baseUrl?: string
|
||||
* shopId?: string
|
||||
* cookie?: string
|
||||
* }} AdminKuaishouEticketShopInfoInput
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* baseUrl?: string
|
||||
* shopId?: string
|
||||
* cookie?: string
|
||||
* eTicketId?: string
|
||||
* }} AdminKuaishouEticketDetailQueryInput
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* baseUrl?: string
|
||||
* shopId?: string
|
||||
* cookie?: string
|
||||
* eTicketId?: string
|
||||
* oid?: string
|
||||
* formToken?: string
|
||||
* num?: number | string
|
||||
* storeId?: string
|
||||
* }} AdminKuaishouEticketConsumeInput
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* baseUrl?: string
|
||||
|
||||
Reference in New Issue
Block a user