Quellcode durchsuchen

封装 GET params 数组问题

qukaidi vor 3 Jahren
Ursprung
Commit
aa8ecd533f
1 geänderte Dateien mit 21 neuen und 5 gelöschten Zeilen
  1. 21 5
      src/router/axios.js

+ 21 - 5
src/router/axios.js

@@ -8,13 +8,14 @@
 import axios from 'axios';
 import store from '@/store/';
 import router from '@/router/router';
-import {serialize} from '@/util/util';
-import {getToken} from '@/util/auth';
-import {Message} from 'element-ui';
+import { serialize } from '@/util/util';
+import { getToken } from '@/util/auth';
+import { Message } from 'element-ui';
 import website from '@/config/website';
-import {Base64} from 'js-base64';
+import { Base64 } from 'js-base64';
 import NProgress from 'nprogress';
 import 'nprogress/nprogress.css';
+import { array } from 'js-md5';
 
 //默认超时时间
 axios.defaults.timeout = 1200000;
@@ -47,6 +48,21 @@ axios.interceptors.request.use(config => {
   if (config.method === 'post' && meta.isSerialize === true) {
     config.data = serialize(config.data);
   }
+  if (config.method == 'get' && config.params) {
+    let url = config.url + '?';
+    for (const propName of Object.keys(config.params)) {
+      const value = config.params[propName];
+      if (value !== null && typeof (value) !== "undefined") {
+        if (value instanceof Array) {
+          for (const key of Object.keys(value)) {
+            let params = propName + '[' + key + ']';
+            delete config.params[propName]
+            config.params[params] = value[key]
+          }
+        }
+      }
+    }
+  }
   return config
 }, error => {
   return Promise.reject(error)
@@ -62,7 +78,7 @@ axios.interceptors.response.use(res => {
   //如果在白名单里则自行catch逻辑处理
   if (statusWhiteList.includes(status)) return Promise.reject(res);
   //如果是401则跳转到登录页面
-  if (status === 401) store.dispatch('FedLogOut').then(() => router.push({path: '/login'}));
+  if (status === 401) store.dispatch('FedLogOut').then(() => router.push({ path: '/login' }));
   // 如果请求为非200否者默认统一处理
   if (status !== 200) {
     Message({