youyawu 5 years ago
parent
commit
a9c3c52071
3 changed files with 33 additions and 30 deletions
  1. 4 3
      package.json
  2. 2 2
      src/types/global.d.ts
  3. 27 25
      src/utils/request.ts

+ 4 - 3
package.json

@@ -15,14 +15,15 @@
     "js-md5": "^0.7.3",
     "vue": "^2.6.10",
     "vue-class-component": "^7.0.2",
+    "vue-i18n": "^8.15.3",
     "vue-property-decorator": "^8.3.0",
     "vue-router": "^3.1.3",
     "vuex": "^3.1.2",
-    "vuex-module-decorators": "^0.11.0",
-    "vue-i18n": "^8.15.3"
+    "vuex-module-decorators": "^0.11.0"
   },
   "devDependencies": {
     "@types/js-cookie": "^2.2.4",
+    "@types/js-md5": "^0.4.2",
     "@types/qs": "^6.9.0",
     "@vue/cli-plugin-babel": "^4.1.0",
     "@vue/cli-plugin-eslint": "^4.1.0",
@@ -41,4 +42,4 @@
     "typescript": "~3.5.3",
     "vue-template-compiler": "^2.6.10"
   }
-}
+}

+ 2 - 2
src/types/global.d.ts

@@ -10,8 +10,8 @@ interface IAny {
   [index: string]: any;
 }
 interface IError {
-  errmsg: string;
-  errno: number;
+  msg: string;
+  code: number;
 }
 interface IBaseResult<T = any> extends IError {
   data: T;

+ 27 - 25
src/utils/request.ts

@@ -1,45 +1,47 @@
 import Vue from "vue";
 import axios from "axios";
-import qs from "qs";
 import user from "@/store/modules/user";
+import { Message } from "@/utils/message";
+import md5 from "js-md5";
 const instance = axios.create({
   baseURL: process.env.VUE_APP_BASE_API,
-  timeout: 5000,
-  headers: {
-    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
-  },
-  transformRequest: [
-    data => {
-      if (typeof data !== "object") return data;
-      let newData: IAny = {};
-      Object.keys(data).forEach(key => {
-        if ([null, undefined, ""].includes(data[key])) return;
-        if (typeof data[key] === "object") {
-          data[key] = JSON.stringify(data[key]);
-        }
-        newData[key] = data[key];
-      });
-      return qs.stringify(newData);
-    }
-  ]
+  timeout: 5000
 });
+const appKey = "cd72c223-923f-44a3-aede-b9f07dcd56b8";
+const plat = "steelfurniture";
+const v = "1.0";
+instance.interceptors.request.use(
+  ({ headers: { timestamp = Date.now(), ...h }, ...x }) => ({
+    headers: {
+      timestamp,
+      appKey,
+      plat,
+      v,
+      sign: md5(`timestamp${timestamp}plat${plat}v${v}appKey${appKey}`),
+      ...h
+    },
+    ...x
+  })
+);
+
 instance.interceptors.response.use(
   x => x,
   err => ({
     data: {
-      errno: -1,
-      errmsg: "网络请求失败",
+      code: -1,
+      msg: "网络请求失败",
       data: err
     }
   })
 );
 const getResult: <T>(x: IBaseResult) => IResult<T> = <T>({
   data,
-  errmsg,
-  errno
+  msg,
+  code
 }: IBaseResult<T>) => {
-  if (errno === 0) return [null, data];
-  return [{ errno, errmsg }, (data || {}) as T];
+  if (code === 0) return [null, data];
+  Message.error(msg || "发生未知错误");
+  return [{ msg, code }, (data || {}) as T];
 };
 export const post: IRequest = async <T>(url: string, params: any) => {
   const { data } = await instance.post<IBaseResult<T>>(url, params);