import { getServiceChannelGroupList, getServiceChannelList } from '@/api/channel/serviceChannel' import { getAppGetInfo } from '@/api/ApplicationServices/apps' import { getServiceProviderGetServiceProvider } from '@/api/interface-serviceProvider/serviceProvider' function pick(raw: Record | 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 ): Promise { 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, } }