feat(miniapp): 拍照巡检页(拍照上传 + AI 结果 + 历史记录,#8)
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
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 });
|
||||
Reference in New Issue
Block a user