Prechádzať zdrojové kódy

chore(api): 封装接口请求函数

ccnnde 2 mesiacov pred
rodič
commit
7caf8406cc
2 zmenil súbory, kde vykonal 114 pridanie a 58 odobranie
  1. 9 0
      src/env.d.ts
  2. 105 58
      src/http.ts

+ 9 - 0
src/env.d.ts

@@ -6,3 +6,12 @@ declare module '*.vue' {
   const component: DefineComponent<{}, {}, any>;
   export default component;
 }
+
+interface ImportMetaEnv {
+  readonly VITE_BASE_API: string;
+  readonly VITE_TEMP_TOKEN: string;
+}
+
+interface ImportMeta {
+  readonly env: ImportMetaEnv;
+}

+ 105 - 58
src/http.ts

@@ -9,7 +9,9 @@ import { axiosRequest, axiosResponse} from '@le5le/auth-token';
 // axios 配置
 axios.defaults.timeout = 60000;
 axios.defaults.withCredentials = false;
-axiosRequest(axios);
+axios.defaults.baseURL = `${import.meta.env.VITE_BASE_API}/api-dashboard`
+
+// axiosRequest(axios);
 // http request 拦截器
 // axios.interceptors.request.use(
 //   (config: any) => {
@@ -22,73 +24,118 @@ axiosRequest(axios);
 // );
 
 // http response 拦截器
-axiosResponse(axios);
+// axiosResponse(axios);
+// axios.interceptors.response.use(
+//   (response: any) => {
+//     if (response && response.data && response.data.error) {
+//       MessagePlugin.error(response.data.error);
+//       return;
+//     }
+//     if (response) {
+//       if (response.status == undefined) {
+//         return response;
+//       }
+//       return response.data;
+//     }
+//     return;
+//   },
+//   (error: any) => {
+//     if (error && !error.response) {
+//       return;
+//     }
+//     if (error && error.response) {
+//       if (
+//         error.response.config.url === '/api/account/profile' ||
+//         error.response.data.error === $t('此为付费数据,请购买后访问')
+//       ) {
+//         return;
+//       }
+
+//       if (
+//         error.response.status !== 401 &&
+//         error.response &&
+//         error.response.data &&
+//         error.response.data.error
+//       ) {
+//         MessagePlugin.error(error.response.data.error);
+//         return;
+//       }
+
+//       switch (error.response.status) {
+//         case 401:
+//           if (error.response.config.url==='/api/account/statistics') {
+//             break;
+//           }
+//           sessionStorage.setItem('cb', encodeURIComponent(location.href));
+//           MessagePlugin.error($t('请先登录!'));
+//           // router.replace({ path: '/login' });
+//           break;
+//         case 403:
+//           MessagePlugin.error($t('请求错误,不合法的请求!'));
+
+//           break;
+//         case 404:
+//           if (error.response.config.url.indexOf('/data/') !== 0) {
+//             MessagePlugin.error($t('访问数据不存在,请检查后重试!'));
+//           }
+//           break;
+//         case 500:
+//           MessagePlugin.error($t('请求服务错误,请稍后重试!'));
+//           break;
+//         case 504:
+//           MessagePlugin.error($t('网络超时,请检测你的网络!'));
+//           break;
+//         default:
+//           MessagePlugin.error($t('未知网络错误!'));
+//           break;
+//       }
+//     }
+//     // return error.response ? error.response.data : error;
+//   }
+// );
+
+axios.interceptors.request.use(
+  (config: any) => {
+    config.headers.Authorization = 'Bearer ' + import.meta.env.VITE_TEMP_TOKEN;
+    return config;
+  },
+  (err: any) => Promise.reject(err)
+);
+
 axios.interceptors.response.use(
   (response: any) => {
-    if (response && response.data && response.data.error) {
-      MessagePlugin.error(response.data.error);
+    if (!response || !response.data) {
+      console.error('Empty Response')
       return;
     }
-    if (response) {
-      if (response.status == undefined) {
-        return response;
-      }
-      return response.data;
+
+    const { data, code, msg } = response.data;
+
+    if (code !== 0) {
+      MessagePlugin.error(msg);
+      return;
     }
-    return;
+
+    if (data === null || data === undefined) {
+      MessagePlugin.error($t("请求数据为空"));
+      return;
+    }
+
+    return data;
   },
   (error: any) => {
-    if (error && !error.response) {
+    if (!error?.response) {
       return;
     }
-    if (error && error.response) {
-      if (
-        error.response.config.url === '/api/account/profile' ||
-        error.response.data.error === $t('此为付费数据,请购买后访问')
-      ) {
-        return;
-      }
-
-      if (
-        error.response.status !== 401 &&
-        error.response &&
-        error.response.data &&
-        error.response.data.error
-      ) {
-        MessagePlugin.error(error.response.data.error);
-        return;
-      }
-
-      switch (error.response.status) {
-        case 401:
-          if (error.response.config.url==='/api/account/statistics') {
-            break;
-          }
-          sessionStorage.setItem('cb', encodeURIComponent(location.href));
-          MessagePlugin.error($t('请先登录!'));
-          // router.replace({ path: '/login' });
-          break;
-        case 403:
-          MessagePlugin.error($t('请求错误,不合法的请求!'));
-
-          break;
-        case 404:
-          if (error.response.config.url.indexOf('/data/') !== 0) {
-            MessagePlugin.error($t('访问数据不存在,请检查后重试!'));
-          }
-          break;
-        case 500:
-          MessagePlugin.error($t('请求服务错误,请稍后重试!'));
-          break;
-        case 504:
-          MessagePlugin.error($t('网络超时,请检测你的网络!'));
-          break;
-        default:
-          MessagePlugin.error($t('未知网络错误!'));
-          break;
-      }
+
+    const { status, data } = error.response
+
+    if (status === 401) {
+      MessagePlugin.error($t('请先登录!'));
+    } else {
+      const errMsg: string = data.msg || data.error || '';
+      MessagePlugin.error(`HTTP Error: ${status} ${errMsg}`);
     }
-    // return error.response ? error.response.data : error;
   }
 );