| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- import axios from 'axios';
- import { WorkOrder, WorkOrderSettings, WorkOrderStatus, WorkOrderPriority } from '../../share/monitorTypes.ts'
- // 分类数据缓存
- let categoriesCache: string[] | null = null;
- let cacheExpireTime = 0;
- const CACHE_EXPIRE_DURATION = 5 * 60 * 1000; // 5分钟缓存
- interface WorkOrderListParams {
- page?: number;
- pageSize?: number;
- status?: WorkOrderStatus;
- priority?: WorkOrderPriority;
- device_id?: number;
- creator_id?: number;
- }
- interface WorkOrderListResponse {
- data: WorkOrder[];
- pagination: {
- total: number;
- current: number;
- pageSize: number;
- totalPages: number;
- };
- }
- interface WorkOrderDetailResponse {
- data: WorkOrder;
- message?: string;
- }
- interface WorkOrderCreateResponse {
- message: string;
- data: WorkOrder;
- }
- interface WorkOrderUpdateResponse {
- message: string;
- data: WorkOrder;
- }
- interface WorkOrderStatusChangeResponse {
- message: string;
- status: string;
- history_id?: string;
- }
- interface WorkOrderDeadlineResponse {
- data: {
- deadline: string;
- remaining_hours: number;
- is_overdue: boolean;
- };
- }
- interface WorkOrderHistoryItem {
- id: string;
- status_from: string;
- status_to: string;
- operator: string;
- comment?: string;
- created_at: string;
- }
- interface WorkOrderHistoryResponse {
- data: WorkOrderHistoryItem[];
- pagination: {
- total: number;
- current: number;
- pageSize: number;
- };
- }
- interface WorkOrderAssignResponse {
- message: string;
- assignee: string;
- }
- interface WorkOrderSettingsResponse {
- data: WorkOrderSettings;
- message?: string;
- }
- interface WorkOrderCategoryResponse {
- data: string[];
- message?: string;
- }
- interface WorkOrderAttachmentResponse {
- message: string;
- data: {
- id: string;
- url: string;
- name: string;
- };
- }
- interface WorkOrderCommentResponse {
- message: string;
- data: {
- id: string;
- content: string;
- created_at: string;
- author: string;
- }[];
- }
- export const WorkOrderAPI = {
- getList: async (params?: WorkOrderListParams): Promise<WorkOrderListResponse> => {
- try {
- const response = await axios.get('/work-orders', { params });
- return response.data;
- } catch (error) {
- if (axios.isAxiosError(error)) {
- const errorMessage = error.response?.data?.message || error.message;
- throw new Error(`获取工单列表失败: ${errorMessage}`);
- }
- throw new Error('获取工单列表失败: 未知错误');
- }
- },
-
- create: async (data: {
- title: string;
- device_id?: number;
- problem_desc: string;
- problem_type: string;
- priority: WorkOrderPriority;
- deadline?: Date;
- attachments?: Array<{
- id: string;
- url: string;
- name: string;
- }>;
- }): Promise<WorkOrderCreateResponse> => {
- try {
- const response = await axios.post('/work-orders', data);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- getDetail: async (id: string): Promise<WorkOrderDetailResponse> => {
- try {
- const response = await axios.get(`/work-orders/${id}`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- update: async (id: string, data: {
- status?: WorkOrderStatus;
- priority?: WorkOrderPriority;
- feedback?: string;
- assignee_id?: number;
- deadline?: Date;
- }): Promise<WorkOrderUpdateResponse> => {
- try {
- const response = await axios.put(`/work-orders/${id}`, data);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- changeStatus: async (id: string, status: string, operator: string, comment?: string): Promise<WorkOrderStatusChangeResponse> => {
- try {
- const response = await axios.post(`/work-orders/${id}/status`, {
- status,
- operator,
- comment
- });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- getDeadline: async (id: string): Promise<WorkOrderDeadlineResponse> => {
- try {
- const response = await axios.get(`/work-orders/${id}/deadline`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- getHistory: async (id: string, page = 1, pageSize = 10): Promise<WorkOrderHistoryResponse> => {
- try {
- const response = await axios.get(`/work-orders/${id}/history`, {
- params: { page, pageSize }
- });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- getStatusHistory: async (id: string): Promise<WorkOrderHistoryResponse> => {
- try {
- const response = await axios.get(`/work-orders/${id}/status-history`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- assign: async (id: string, assignee: string): Promise<WorkOrderAssignResponse> => {
- try {
- const response = await axios.post(`/work-orders/${id}/assign`, { assignee });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- getSettings: async (): Promise<WorkOrderSettingsResponse> => {
- try {
- const response = await axios.get('/work-orders/settings');
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- updateSettings: async (data: WorkOrderSettings): Promise<WorkOrderSettingsResponse> => {
- try {
- const response = await axios.post('/work-orders/settings', data);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 新增分类相关API
- getCategories: async (): Promise<WorkOrderCategoryResponse> => {
- // 检查缓存是否有效
- if (categoriesCache && Date.now() < cacheExpireTime) {
- return { data: categoriesCache };
- }
-
- try {
- const response = await axios.get('/work-orders/categories');
- categoriesCache = response.data.data;
- cacheExpireTime = Date.now() + CACHE_EXPIRE_DURATION;
- return response.data;
- } catch (error) {
- if (axios.isAxiosError(error)) {
- const errorMessage = error.response?.data?.message || error.message;
- throw new Error(`获取分类失败: ${errorMessage}`);
- }
- throw new Error('获取分类失败: 未知错误');
- }
- },
- // 新增附件相关API
- uploadAttachment: async (id: string, file: File): Promise<WorkOrderAttachmentResponse> => {
- try {
- const formData = new FormData();
- formData.append('file', file);
- const response = await axios.post(`/work-orders/${id}/attachments`, formData, {
- headers: {
- 'Content-Type': 'multipart/form-data'
- }
- });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 新增评论相关API
- getComments: async (id: string): Promise<WorkOrderCommentResponse> => {
- try {
- const response = await axios.get(`/work-orders/${id}/comments`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- addComment: async (id: string, content: string): Promise<WorkOrderCommentResponse> => {
- try {
- const response = await axios.post(`/work-orders/${id}/comments`, { content });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- exportList: async (params?: WorkOrderListParams): Promise<Blob> => {
- try {
- const response = await axios.get('/work-orders/export', {
- params,
- responseType: 'blob'
- });
- return response.data;
- } catch (error) {
- if (axios.isAxiosError(error)) {
- const errorMessage = error.response?.data?.message || error.message;
- throw new Error(`导出工单失败: ${errorMessage}`);
- }
- throw new Error('导出工单失败: 未知错误');
- }
- },
- };
|