Files
iotlcm/app/src/api/card.ts
T

221 lines
6.6 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 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<CardQueryRes>('/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<CardRecognitionRes>((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<string, string> }).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<CardListRes>('/card/list', params, undefined, CARD_REQUEST_OPTIONS)
}
export function getPackageList(params: PackageListParams) {
return http.get<PackageListRes>('/card/package-list', params, undefined, CARD_REQUEST_OPTIONS)
}
export function publicPackageDetail(data: PublicPackageDetailParams) {
return http.post<PackageDetailRes>(
'/card/public-package-detail',
data,
undefined,
undefined,
CARD_REQUEST_OPTIONS,
)
}
export function getCardDetail(cardValue: string) {
return http.get<CardQueryRes>('/card/detail', { cardValue }, undefined, CARD_REQUEST_OPTIONS)
}
export function bindCard(data: BindCardParams) {
return http.post<CardQueryRes>('/card/bind', data, undefined, undefined, CARD_REQUEST_OPTIONS)
}
export function restartCard(data: RestartCardParams) {
return http.post<RestartCardRes>('/card/restart', data, undefined, undefined, CARD_REQUEST_OPTIONS)
}
export function stopCard(data: RestartCardParams) {
return http.post<CardLifecycleRes>('/card/stop', data, undefined, undefined, CARD_REQUEST_OPTIONS)
}
export function startCard(data: RestartCardParams) {
return http.post<CardLifecycleRes>('/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<CardLifecycleRes>(
'/card/lifecycle-batch-submit',
data,
undefined,
undefined,
CARD_REQUEST_OPTIONS,
)
}
export function lifecycleBatchStatus(data: CardLifecycleStatusParams) {
return http.get<CardLifecycleRes>('/card/lifecycle-batch-status', data, undefined, CARD_REQUEST_OPTIONS)
}
export function renewPrepare(data: RenewPrepareParams) {
return http.post<RenewPrepareRes>('/card/renew-prepare', data, undefined, undefined, CARD_REQUEST_OPTIONS)
}
export function renewBatchPrepare(data: RenewBatchPrepareParams) {
return http.post<RenewPrepareRes>('/card/renew-batch-prepare', data, undefined, undefined, CARD_REQUEST_OPTIONS)
}
export function renewSubmit(data: RenewSubmitParams) {
return http.post<RenewSubmitRes>('/card/renew-submit', data, undefined, undefined, CARD_REQUEST_OPTIONS)
}
export function renewBatchSubmit(data: RenewBatchSubmitParams) {
return http.post<RenewSubmitRes>('/card/renew-batch-submit', data, undefined, undefined, CARD_REQUEST_OPTIONS)
}
export function getAllocateTargets(params: AllocateTargetListParams) {
return http.get<AllocateTargetListRes>('/card/allocate-targets', params, undefined, CARD_REQUEST_OPTIONS)
}
export function allocateBatchPrepare(data: AllocateBatchPrepareParams) {
return http.post<AllocateBatchRes>(
'/card/allocate-batch-prepare',
data,
undefined,
undefined,
CARD_REQUEST_OPTIONS,
)
}
export function allocateBatchSubmit(data: AllocateBatchSubmitParams) {
return http.post<AllocateBatchRes>(
'/card/allocate-batch-submit',
data,
undefined,
undefined,
CARD_REQUEST_OPTIONS,
)
}
export function changePrepare(data: ChangePrepareParams) {
return http.post<ChangePrepareRes>('/card/change-prepare', data, undefined, undefined, CARD_REQUEST_OPTIONS)
}
export function changeSubmit(data: ChangeSubmitParams) {
return http.post<ChangeSubmitRes>('/card/change-submit', data, undefined, undefined, CARD_REQUEST_OPTIONS)
}