Browse Source

feat: api完成

xyh 2 years atrás
parent
commit
780b20af2f

+ 0 - 5
packages/app/src/apis/.gitkeep

@@ -1,5 +0,0 @@
-# To ensure that empty folders can also be submitted
-
-# You can delete this file
-
-# This type of file has been entered in .gitignore, and this type of file will not be submitted to git

+ 60 - 0
packages/app/src/apis/network.ts

@@ -0,0 +1,60 @@
+import {BaseResultContent} from '@models';
+import {NETWORK_ERROR_TIPS, NETWORK_URL} from '@utils';
+import {message} from 'antd';
+import axios from 'axios';
+
+// 普通请求
+const http = axios.create({
+  baseURL: NETWORK_URL,
+  headers: {
+    'Content-Type': 'application/json',
+    'Cache-Control': 'no-cache',
+  },
+});
+
+const exportReg = /export|excel/i;
+
+http.interceptors.request.use(function(config) {
+  if (config?.url) {
+    const isExport = exportReg.test(config.url);
+    isExport && (config.responseType = 'blob');
+  }
+
+  return config;
+});
+
+export async function request<T, R extends BaseResultContent<any>>(options: {
+  method: 'POST' | 'GET' | 'PUT' | 'DELETE';
+  url: string;
+  data?: T;
+  skipError?: boolean;
+  useBody?: boolean;
+  signal?: AbortSignal;
+}) {
+  const {data, skipError, method, url, useBody, signal} = options;
+
+  let res: BaseResultContent<any>;
+  const useData = useBody ? useBody : method !== 'GET' && method !== 'DELETE';
+
+  try {
+    const result = await http.request<R>({
+      method,
+      url,
+      data: useData ? data : void 0,
+      params: !useData ? data : void 0,
+      signal,
+    });
+
+    res = result.data;
+
+    if (res.msg !== '200' && !skipError && !exportReg.test(url))
+      message.error(res.errMsg);
+  } catch (error: any) {
+    res = {msg: '510', errMsg: NETWORK_ERROR_TIPS};
+
+    if (!skipError && error.code !== 'ERR_CANCELED' && !exportReg.test(url))
+      message.error(res.errMsg);
+  }
+
+  return res;
+}

+ 2 - 0
packages/app/src/utils/constants.ts

@@ -10,3 +10,5 @@ export const PAGE_SIZE_LIST = [
   '80',
   '100',
 ];
+/** 接口请求地址 */
+export const NETWORK_URL = '';