import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { CourierProviderCode } from './courier.constants'; export type XiaofeixiaSignType = 'MD5' | 'HMAC-SHA256'; export interface XiaofeixiaConfig { apiUrl: string; appId?: string; mchId: string; apiKey: string; signType: XiaofeixiaSignType; } export interface CourierIntegrationConfig { provider: CourierProviderCode; xiaofeixia: XiaofeixiaConfig; } @Injectable() export class CourierConfigService { constructor(private readonly config: ConfigService) {} load(): CourierIntegrationConfig { const providerRaw = (this.config.get('COURIER_PROVIDER') ?? 'xiaofeixia').toLowerCase(); const provider = this.resolveProvider(providerRaw); return { provider, xiaofeixia: { apiUrl: this.config.get('XIAOFEIXIA_API_URL') ?? 'https://beta.51xiaoju.cn/app/api/interface.do', appId: this.config.get('XIAOFEIXIA_APP_ID') || undefined, mchId: this.config.get('XIAOFEIXIA_MCH_ID') ?? '', apiKey: this.config.get('XIAOFEIXIA_API_KEY') ?? '', signType: this.resolveSignType(this.config.get('XIAOFEIXIA_SIGN_TYPE')), }, }; } private resolveProvider(raw: string): CourierProviderCode { switch (raw) { case 'xiaofeixia': return CourierProviderCode.XIAOFEIXIA; case 'sf': return CourierProviderCode.SF; case 'jd': return CourierProviderCode.JD; default: throw new Error(`Unsupported COURIER_PROVIDER: ${raw}`); } } private resolveSignType(raw?: string): XiaofeixiaSignType { if (raw?.toUpperCase() === 'HMAC-SHA256') { return 'HMAC-SHA256'; } return 'MD5'; } }