zhangwei
4 天以前 33c5adad9cec6581ba4c0db54959d0fec0ec9756
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { service, cancelRequest } from '/@/utils/request';
import {AxiosRequestConfig, AxiosResponse} from "axios";
 
// 接口基类
export const useBaseApi = (module: string) => {
    const baseUrl = `/api/${module}/`;
    const request = <T>(config: AxiosRequestConfig<T>, cancel: boolean = false) => {
        if (cancel) {
            cancelRequest(config.url || "");
            return Promise.resolve({} as AxiosResponse<any, any>);
        }
        return service(config);
    }
    return {
        baseUrl: baseUrl,
        request: request,
        page: function (data: any, cancel: boolean = false) {
            return request({
                url: baseUrl + "page",
                method: 'post',
                data,
            }, cancel);
        },
        detail: function (id: any, cancel: boolean = false) {
            return request({
                url: baseUrl + "detail",
                method: 'get',
                data: { id },
            }, cancel);
        },
        dropdownData: function (data: any, cancel: boolean = false) {
            return request({
                url: baseUrl + "dropdownData",
                method: 'post',
                data,
            }, cancel);
        },
        add: function (data: any, cancel: boolean = false) {
            return request({
                url: baseUrl + 'add',
                method: 'post',
                data
            }, cancel);
        },
        update: function (data: any, cancel: boolean = false) {
            return request({
                url: baseUrl + 'update',
                method: 'post',
                data
            }, cancel);
        },
        setStatus: function (data: any, cancel: boolean = false) {
            return request({
                url: baseUrl + 'setStatus',
                method: 'post',
                data
            }, cancel);
        },
        delete: function (data: any, cancel: boolean = false) {
            return request({
                url: baseUrl + 'delete',
                method: 'post',
                data
            }, cancel);
        },
        batchDelete: function (data: any, cancel: boolean = false) {
            return request({
                url: baseUrl + 'batchDelete',
                method: 'post',
                data
            }, cancel);
        },
        exportData: function (data: any, cancel: boolean = false) {
            return request({
                responseType: 'arraybuffer',
                url: baseUrl + 'export',
                method: 'post',
                data
            }, cancel);
        },
        downloadTemplate: function (cancel: boolean = false) {
            return request({
                responseType: 'arraybuffer',
                url: baseUrl + 'import',
                method: 'get',
            }, cancel);
        },
        importData: function (file: any, cancel: boolean = false) {
            const formData = new FormData();
            formData.append('file', file);
            return request({
                headers: { 'Content-Type': 'multipart/form-data;charset=UTF-8' },
                responseType: 'arraybuffer',
                url: baseUrl + 'import',
                method: 'post',
                data: formData,
            }, cancel);
        },
        uploadFile: function (params: any, action: string, cancel: boolean = false) {
            const formData = new FormData();
            formData.append('file', params.file);
            // 自定义参数
            if (params.data) {
                Object.keys(params.data).forEach((key) => {
                    const value = params.data![key];
                    if (Array.isArray(value)) {
                        value.forEach((item) => formData.append(`${key}[]`, item));
                        return;
                    }
                    formData.append(key, params.data![key]);
                });
            }
            return request({
                url: baseUrl + action,
                method: 'POST',
                data: formData,
                headers: {
                    'Content-Type': 'multipart/form-data;charset=UTF-8',
                    ignoreCancelToken: true,
                },
            }, cancel);
        }
    }
}