import type { AllocateBatchPrepareParams, AllocateBatchRes, AllocateBatchSubmitParams, AllocateTargetListParams, AllocateTargetListRes, BindCardParams, CardLifecycleParams, CardLifecycleRes, CardLifecycleStatusParams, CardListParams, CardListRes, CardQueryRes, CardRecognitionRes, ChangePrepareParams, ChangePrepareRes, ChangeSubmitParams, ChangeSubmitRes, PackageDetailRes, PackageListParams, PackageListRes, PublicPackageDetailParams, PublicQueryCardParams, RenewBatchPrepareParams, RenewBatchSubmitParams, RenewPrepareParams, RenewPrepareRes, RenewSubmitParams, RenewSubmitRes, RestartCardParams, RestartCardRes, } from './types/card' import { http } from '@/http/http' import { getEnvBaseUrl } from '@/utils/index' const CARD_REQUEST_OPTIONS = { timeout: 15000, hideErrorToast: true, } as const export type { AllocateBatchRes, AllocateTarget, AppCard, AppPackage, AppRenewOption, CardLifecycleRes, ChangePackageOption, ChangePrepareRes, ChangeSubmitRes, RenewPackageOption, RenewPrepareRes, RenewSubmitRes, } from './types/card' export function publicQueryCard(data: PublicQueryCardParams) { return http.post('/card/public-query', data, undefined, undefined, CARD_REQUEST_OPTIONS) } interface CardRecognitionPayload { code?: number message?: string data?: CardRecognitionRes } function parseCardRecognitionPayload(data: unknown): CardRecognitionPayload { if (data && typeof data === 'object') return data as CardRecognitionPayload const text = String(data ?? '').replace(/^\uFEFF/, '').trim() return text ? JSON.parse(text) as CardRecognitionPayload : {} } export function recognizeCardImage(filePath: string) { return new Promise((resolve, reject) => { uni.uploadFile({ url: `${getEnvBaseUrl()}/card/recognize`, filePath, name: 'file', timeout: 30000, success: (response) => { let payload: CardRecognitionPayload try { payload = parseCardRecognitionPayload(response.data) } catch { const headers = (response as typeof response & { header?: Record }).header const contentType = headers?.['content-type'] || headers?.['Content-Type'] const responseType = contentType ? `,${contentType}` : '' reject(new Error(`图片识别响应格式错误(HTTP ${response.statusCode}${responseType})`)) return } if (response.statusCode < 200 || response.statusCode >= 300 || Number(payload.code) !== 0) { reject(new Error(payload.message || '图片识别失败')) return } resolve(payload.data || { code: 'RECOGNITION_FAILED', message: '图片识别结果为空', cardType: 'iccid', cardValue: '', confidence: 0, candidates: [], }) }, fail: (error) => { reject(new Error(error.errMsg || '图片上传失败,请检查网络后重试')) }, }) }) } export function getCardList(params: CardListParams) { return http.get('/card/list', params, undefined, CARD_REQUEST_OPTIONS) } export function getPackageList(params: PackageListParams) { return http.get('/card/package-list', params, undefined, CARD_REQUEST_OPTIONS) } export function publicPackageDetail(data: PublicPackageDetailParams) { return http.post( '/card/public-package-detail', data, undefined, undefined, CARD_REQUEST_OPTIONS, ) } export function getCardDetail(cardValue: string) { return http.get('/card/detail', { cardValue }, undefined, CARD_REQUEST_OPTIONS) } export function bindCard(data: BindCardParams) { return http.post('/card/bind', data, undefined, undefined, CARD_REQUEST_OPTIONS) } export function restartCard(data: RestartCardParams) { return http.post('/card/restart', data, undefined, undefined, CARD_REQUEST_OPTIONS) } export function stopCard(data: RestartCardParams) { return http.post('/card/stop', data, undefined, undefined, CARD_REQUEST_OPTIONS) } export function startCard(data: RestartCardParams) { return http.post('/card/start', data, undefined, undefined, CARD_REQUEST_OPTIONS) } export function startDataService(data: RestartCardParams) { return lifecycleBatchSubmit({ cardValue: data.cardValue, operation: 'dataStart', remark: data.remark }) } export function stopDataService(data: RestartCardParams) { return lifecycleBatchSubmit({ cardValue: data.cardValue, operation: 'dataStop', remark: data.remark }) } export function lifecycleBatchSubmit(data: CardLifecycleParams) { return http.post( '/card/lifecycle-batch-submit', data, undefined, undefined, CARD_REQUEST_OPTIONS, ) } export function lifecycleBatchStatus(data: CardLifecycleStatusParams) { return http.get('/card/lifecycle-batch-status', data, undefined, CARD_REQUEST_OPTIONS) } export function renewPrepare(data: RenewPrepareParams) { return http.post('/card/renew-prepare', data, undefined, undefined, CARD_REQUEST_OPTIONS) } export function renewBatchPrepare(data: RenewBatchPrepareParams) { return http.post('/card/renew-batch-prepare', data, undefined, undefined, CARD_REQUEST_OPTIONS) } export function renewSubmit(data: RenewSubmitParams) { return http.post('/card/renew-submit', data, undefined, undefined, CARD_REQUEST_OPTIONS) } export function renewBatchSubmit(data: RenewBatchSubmitParams) { return http.post('/card/renew-batch-submit', data, undefined, undefined, CARD_REQUEST_OPTIONS) } export function getAllocateTargets(params: AllocateTargetListParams) { return http.get('/card/allocate-targets', params, undefined, CARD_REQUEST_OPTIONS) } export function allocateBatchPrepare(data: AllocateBatchPrepareParams) { return http.post( '/card/allocate-batch-prepare', data, undefined, undefined, CARD_REQUEST_OPTIONS, ) } export function allocateBatchSubmit(data: AllocateBatchSubmitParams) { return http.post( '/card/allocate-batch-submit', data, undefined, undefined, CARD_REQUEST_OPTIONS, ) } export function changePrepare(data: ChangePrepareParams) { return http.post('/card/change-prepare', data, undefined, undefined, CARD_REQUEST_OPTIONS) } export function changeSubmit(data: ChangeSubmitParams) { return http.post('/card/change-submit', data, undefined, undefined, CARD_REQUEST_OPTIONS) }