|
|
@@ -1,65 +1,88 @@
|
|
|
-import {uploadFile} from '/@/api/system/uploadFile';
|
|
|
-import request from '/@/utils/request';
|
|
|
+import { service, cancelRequest } from '/@/utils/request';
|
|
|
+import { AxiosRequestConfig } from "axios";
|
|
|
|
|
|
// 接口基类
|
|
|
export const useBaseApi = (module: string) => {
|
|
|
const baseUrl = `/api/${module}/`;
|
|
|
+ const request = <T>(config: AxiosRequestConfig<T>, cancel: boolean = false) => {
|
|
|
+ if (cancel) return cancelRequest(config.url || "");
|
|
|
+ return service(config);
|
|
|
+ }
|
|
|
return {
|
|
|
- request,
|
|
|
- uploadFile,
|
|
|
baseUrl: baseUrl,
|
|
|
- page: (data: any) => request({
|
|
|
- url: baseUrl + "page",
|
|
|
- method: 'post',
|
|
|
- data,
|
|
|
- }),
|
|
|
- detail: (id: any) => request({
|
|
|
- url: baseUrl + "detail",
|
|
|
- method: 'get',
|
|
|
- data: { id },
|
|
|
- }),
|
|
|
- dropdownData: (data: any) => request({
|
|
|
- url: baseUrl + "dropdownData",
|
|
|
- method: 'post',
|
|
|
- data,
|
|
|
- }),
|
|
|
- add: (data: any) => request({
|
|
|
- url: baseUrl + 'add',
|
|
|
- method: 'post',
|
|
|
- data
|
|
|
- }),
|
|
|
- update: (data: any) => request({
|
|
|
- url: baseUrl + 'update',
|
|
|
- method: 'post',
|
|
|
- data
|
|
|
- }),
|
|
|
- setStatus: (data: any) => request({
|
|
|
- url: baseUrl + 'setStatus',
|
|
|
- method: 'post',
|
|
|
- data
|
|
|
- }),
|
|
|
- delete: (data: any) => request({
|
|
|
- url: baseUrl + 'delete',
|
|
|
- method: 'post',
|
|
|
- data
|
|
|
- }),
|
|
|
- batchDelete: (data: any) => request({
|
|
|
- url: baseUrl + 'batchDelete',
|
|
|
- method: 'post',
|
|
|
- data
|
|
|
- }),
|
|
|
- exportData: (data: any) => request({
|
|
|
- responseType: 'arraybuffer',
|
|
|
- url: baseUrl + 'export',
|
|
|
- method: 'post',
|
|
|
- data
|
|
|
- }),
|
|
|
- downloadTemplate: () => request({
|
|
|
- responseType: 'arraybuffer',
|
|
|
- url: baseUrl + 'import',
|
|
|
- method: 'get',
|
|
|
- }),
|
|
|
- importData: (file: any) => {
|
|
|
+ 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({
|
|
|
@@ -68,8 +91,31 @@ export const useBaseApi = (module: string) => {
|
|
|
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: {
|
|
|
+ ContentType: 'multipart/form-data;charset=UTF-8',
|
|
|
+ ignoreCancelToken: true,
|
|
|
+ },
|
|
|
+ }, cancel);
|
|
|
+ }
|
|
|
}
|
|
|
}
|