소스 검색

new file request + router

may_zhouwei 4 년 전
부모
커밋
e424f42f34
2개의 변경된 파일40개의 추가작업 그리고 42개의 파일을 삭제
  1. 33 25
      src/store/modules/routes.js
  2. 7 17
      src/utils/request.js

+ 33 - 25
src/store/modules/routes.js

@@ -1,10 +1,10 @@
 /**
- * @copyright chuzhixin 1204505056@qq.com
+ * @author chuzhixin 1204505056@qq.com
  * @description 路由拦截状态管理,目前两种模式:all模式与intelligence模式,其中partialRoutes是菜单暂未使用
  */
 import { asyncRoutes, constantRoutes } from "@/router";
 import { getRouterList } from "@/api/router";
-import { filterAllRoutes, filterAsyncRoutes } from "@/utils/handleRoutes";
+import { convertRouter, filterRoutes } from "@/utils/routes";
 
 const state = { routes: [], partialRoutes: [] };
 const getters = {
@@ -13,38 +13,46 @@ const getters = {
 };
 const mutations = {
   setRoutes(state, routes) {
-    state.routes = constantRoutes.concat(routes);
-  },
-  setAllRoutes(state, routes) {
-    state.routes = constantRoutes.concat(routes);
+    state.routes = routes;
   },
   setPartialRoutes(state, routes) {
-    state.partialRoutes = constantRoutes.concat(routes);
+    state.partialRoutes = routes;
   },
 };
 const actions = {
-  async setRoutes({ commit }, permissions) {
-    //防止污染路由
-    const baseRoutes = [...asyncRoutes];
-    let accessedRoutes = [];
-    if (permissions.includes("admin")) {
-      accessedRoutes = baseRoutes;
-    } else {
-      accessedRoutes = await filterAsyncRoutes(baseRoutes, permissions);
-    }
-    commit("setRoutes", accessedRoutes);
-    return accessedRoutes;
+  /**
+   * @author chuzhixin 1204505056@qq.com
+   * @description intelligence模式设置路由
+   * @param {*} { commit }
+   * @returns
+   */
+  async setRoutes({ commit }) {
+    const finallyRoutes = filterRoutes([...constantRoutes, ...asyncRoutes]);
+    commit("setRoutes", finallyRoutes);
+    return [...asyncRoutes];
   },
+  /**
+   * @author chuzhixin 1204505056@qq.com
+   * @description all模式设置路由
+   * @param {*} { commit }
+   * @returns
+   */
   async setAllRoutes({ commit }) {
     let { data } = await getRouterList();
-    data.push({ path: "*", redirect: "/404", hidden: true });
-    let accessRoutes = filterAllRoutes(data);
-    commit("setAllRoutes", accessRoutes);
-    return accessRoutes;
+    if (data[data.length - 1].path !== "*")
+      data.push({ path: "*", redirect: "/404", hidden: true });
+    const finallyRoutes = filterRoutes(convertRouter(data));
+    commit("setRoutes", finallyRoutes);
+    return finallyRoutes;
   },
-  setPartialRoutes({ commit }, accessRoutes) {
-    commit("setPartialRoutes", accessRoutes);
-    return accessRoutes;
+  /**
+   * @author chuzhixin 1204505056@qq.com
+   * @description 画廊布局、综合布局设置路由
+   * @param {*} { commit }
+   * @param accessedRoutes 画廊布局、综合布局设置路由
+   */
+  setPartialRoutes({ commit }, accessedRoutes) {
+    commit("setPartialRoutes", accessedRoutes);
   },
 };
 export default { state, getters, mutations, actions };

+ 7 - 17
src/utils/request.js

@@ -5,11 +5,10 @@ import {
   contentType,
   debounce,
   invalidCode,
-  noPermissionCode,
+  noRoleCode,
   requestTimeout,
   successCode,
   tokenName,
-  loginInterception,
 } from "@/config/settings";
 import store from "@/store";
 import qs from "qs";
@@ -19,7 +18,7 @@ import { isArray } from "@/utils/validate";
 let loadingInstance;
 
 /**
- * @copyright chuzhixin 1204505056@qq.com
+ * @author chuzhixin 1204505056@qq.com
  * @description 判断当前url是否需要加loading
  * @param {*} config
  * @returns
@@ -35,7 +34,7 @@ const needLoading = (config) => {
 };
 
 /**
- * @copyright chuzhixin 1204505056@qq.com
+ * @author chuzhixin 1204505056@qq.com
  * @description 处理code异常
  * @param {*} code
  * @param {*} msg
@@ -44,13 +43,9 @@ const handleCode = (code, msg) => {
   switch (code) {
     case invalidCode:
       Vue.prototype.$baseMessage(msg || `后端接口${code}异常`, "error");
-      store.dispatch("user/resetAccessToken").catch(() => {});
-      //开启登录拦截才需要刷新,不然死循环
-      if (loginInterception) {
-        location.reload();
-      }
+      store.dispatch("user/resetAll").catch(() => {});
       break;
-    case noPermissionCode:
+    case noRoleCode:
       router.push({ path: "/401" }).catch(() => {});
       break;
     default:
@@ -65,6 +60,7 @@ const instance = axios.create({
   headers: {
     "Content-Type": contentType,
   },
+  //withCredentials: true,
 });
 
 instance.interceptors.request.use(
@@ -72,12 +68,6 @@ instance.interceptors.request.use(
     if (store.getters["user/accessToken"]) {
       config.headers[tokenName] = store.getters["user/accessToken"];
     }
-    //这里会过滤所有为空、0、false的key,如果不需要请自行注释
-    if (config.data)
-      config.data = Vue.prototype.$baseLodash.pickBy(
-        config.data,
-        Vue.prototype.$baseLodash.identity
-      );
 
     if (
       contentType === "application/x-www-form-urlencoded;charset=UTF-8" &&
@@ -100,7 +90,7 @@ instance.interceptors.response.use(
   (response) => {
     if (loadingInstance) loadingInstance.close();
 
-    const { status, data, config } = response;
+    const { data, config } = response;
     const { code, msg } = data;
     // 操作正常Code数组
     const codeVerificationArray = isArray(successCode)