front/src/utils/resolveOrderDownstreamCredentials.ts
zzh 5a26ae0048 feat: 小红书模块迁移、登录重构与 xhs-agent-desktop 桌面 Agent
- 新增小红书前端模块(启动服务、内容分析、爆款创作、发布记录、技能卡片、拟人设置、AI 助手)

- 新增 xhs-agent-desktop 独立桌面 Agent,对接 OMS 任务调度

- 登录页重构,整合微信登录与 OMS JWT 认证

- 订单、渠道、用户中心等页面与 API 增强

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-30 16:13:13 +08:00

108 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { getServiceChannelGroupList, getServiceChannelList } from '@/api/channel/serviceChannel'
import { getAppGetInfo } from '@/api/ApplicationServices/apps'
import { getServiceProviderGetServiceProvider } from '@/api/interface-serviceProvider/serviceProvider'
function pick(raw: Record<string, any> | undefined, ...keys: string[]) {
if (!raw) return undefined
for (const k of keys) {
const v = raw[k]
if (v !== undefined && v !== null && v !== '') return v
}
return undefined
}
function unwrapPayload(res: any): any {
if (res == null) return res
if (typeof res !== 'object') return res
return res.data ?? res.Data ?? res
}
export type OrderDownstreamCredentials = {
appKey: string
appSecret: string
supplierCode: string
groupName?: string
}
/** 从订单绑定字段或渠道树解析 itfx 应用密钥与 API 服务商编码(supplierCode) */
export async function resolveOrderDownstreamCredentials(
order: Record<string, any>
): Promise<OrderDownstreamCredentials> {
const supplierCode = String(
pick(order, 'downstreamSupplierCode', 'DownstreamSupplierCode') ?? ''
).trim()
const appKey = String(pick(order, 'downstreamAppKey', 'DownstreamAppKey') ?? '').trim()
const appId = Number(pick(order, 'downstreamAppId', 'DownstreamAppId') ?? 0)
if (supplierCode && appKey && Number.isFinite(appId) && appId > 0) {
const appRaw = unwrapPayload(await getAppGetInfo({ id: appId }))
const appSecret = String(
pick(appRaw, 'appSecret', 'AppSecret', 'secret', 'Secret') ?? ''
).trim()
if (!appSecret) {
throw new Error('应用 AppSecret 未配置')
}
return { appKey, appSecret, supplierCode }
}
const channelCode = String(pick(order, 'channelCode', 'ChannelCode') ?? '').trim()
if (!channelCode) {
throw new Error('订单未绑定服务渠道,无法预报下游')
}
const { items } = await getServiceChannelList({ page: 1, pageSize: 500, keyword: channelCode })
const channel = (items || []).find(
(row: any) =>
String(row.channelCode ?? row.ChannelCode ?? '')
.trim()
.toLowerCase() === channelCode.toLowerCase()
)
if (!channel) {
throw new Error(`未找到渠道「${channelCode}」`)
}
const groupId = Number(channel.groupId ?? channel.GroupId ?? 0)
if (!Number.isFinite(groupId) || groupId <= 0) {
throw new Error(`渠道「${channelCode}」未关联服务商分组`)
}
const groups = await getServiceChannelGroupList()
const group = (groups || []).find((g: any) => Number(g.id ?? g.Id) === groupId)
if (!group) {
throw new Error('服务商分组不存在或已删除')
}
const resolvedAppId = Number(group.appId ?? group.AppId ?? 0)
const serviceProviderId = Number(group.apiServiceProviderId ?? group.ApiServiceProviderId ?? 0)
if (!Number.isFinite(resolvedAppId) || resolvedAppId <= 0) {
throw new Error(`服务商「${group.name ?? group.Name ?? groupId}」未配置应用`)
}
if (!Number.isFinite(serviceProviderId) || serviceProviderId <= 0) {
throw new Error(`服务商「${group.name ?? group.Name ?? groupId}」未配置 API 服务商`)
}
const appRaw = unwrapPayload(await getAppGetInfo({ id: resolvedAppId }))
const resolvedAppKey = String(pick(appRaw, 'appKey', 'AppKey') ?? '').trim()
const appSecret = String(pick(appRaw, 'appSecret', 'AppSecret', 'secret', 'Secret') ?? '').trim()
if (!resolvedAppKey || !appSecret) {
throw new Error('应用 AppKey / AppSecret 未配置')
}
const spRaw = unwrapPayload(
await getServiceProviderGetServiceProvider({ serviceProviderId })
)
const resolvedSupplierCode = String(
pick(spRaw, 'code', 'Code', 'serviceProviderCode', 'ServiceProviderCode') ?? ''
).trim()
if (!resolvedSupplierCode) {
throw new Error('API 服务商编码(supplierCode)未配置')
}
return {
appKey: resolvedAppKey,
appSecret,
supplierCode: resolvedSupplierCode,
groupName: String(group.name ?? group.Name ?? '').trim() || undefined,
}
}