43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import Taro from '@tarojs/taro';
|
|
import { getApiBaseUrl } from './config';
|
|
import { get } from './request';
|
|
import type { InspectionRecord } from '@/types';
|
|
|
|
export const uploadInspection = (
|
|
filePath: string,
|
|
roomId?: string,
|
|
): Promise<InspectionRecord> => {
|
|
const token = Taro.getStorageSync('token');
|
|
const idempotencyKey = `insp-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
|
const header: Record<string, string> = { 'Idempotency-Key': idempotencyKey };
|
|
if (token) {
|
|
header['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
Taro.uploadFile({
|
|
url: `${getApiBaseUrl()}/inspections`,
|
|
filePath,
|
|
name: 'file',
|
|
header,
|
|
formData: roomId ? { roomId } : {},
|
|
timeout: 60000,
|
|
success: (res) => {
|
|
try {
|
|
const body = JSON.parse(res.data || '{}');
|
|
if (res.statusCode >= 400) {
|
|
reject(new Error(body?.error || `上传失败 (${res.statusCode})`));
|
|
return;
|
|
}
|
|
resolve(body as InspectionRecord);
|
|
} catch (e) {
|
|
reject(new Error('解析巡检结果失败'));
|
|
}
|
|
},
|
|
fail: (err) => reject(new Error(err?.errMsg || '上传失败')),
|
|
});
|
|
});
|
|
};
|
|
|
|
export const listInspections = (limit = 10): Promise<InspectionRecord[]> =>
|
|
get<InspectionRecord[]>('/inspections', { limit });
|