new file request + router

This commit is contained in:
may_zhouwei 2020-09-09 14:17:09 +08:00
parent a12a2375e7
commit e424f42f34
2 changed files with 40 additions and 42 deletions

View File

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

View File

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