规范code 403代表无权限 401代表登录失效

This commit is contained in:
chuzhixin 2020-10-07 11:24:50 +08:00
parent 402131dd8a
commit af8afbc638
7 changed files with 25 additions and 29 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 147 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 KiB

View File

@ -34,7 +34,7 @@ const setting = {
//路由模式,可选值为 history 或 hash //路由模式,可选值为 history 或 hash
routerMode: "hash", routerMode: "hash",
//不经过token校验的路由 //不经过token校验的路由
routesWhiteList: ["/login", "/register", "/callback", "/404", "/401"], routesWhiteList: ["/login", "/register", "/callback", "/404", "/403"],
//加载时显示文字 //加载时显示文字
loadingText: "正在加载中...", loadingText: "正在加载中...",
//token名称 //token名称

View File

@ -8,9 +8,9 @@ export const constantRoutes = [
hidden: true, hidden: true,
}, },
{ {
path: "/401", path: "/403",
name: "401", name: "403",
component: () => import("@/views/401"), component: () => import("@/views/403"),
hidden: true, hidden: true,
}, },
{ {
@ -88,18 +88,18 @@ export const asyncRoutes = [
path: "/error", path: "/error",
name: "Error", name: "Error",
component: Layout, component: Layout,
redirect: "/error/401", redirect: "/error/403",
meta: { meta: {
title: "错误页", title: "错误页",
icon: "error-warning-line", icon: "error-warning-line",
}, },
children: [ children: [
{ {
path: "401", path: "403",
name: "Error401", name: "Error403",
component: () => import("@/views/401"), component: () => import("@/views/403"),
meta: { meta: {
title: "401", title: "403",
icon: "error-warning-line", icon: "error-warning-line",
}, },
}, },

View File

@ -1,10 +1,9 @@
import Vue from "vue";
import axios from "axios"; import axios from "axios";
import { import {
baseURL, baseURL,
contentType, contentType,
debounce, debounce,
invalidCode,
noRoleCode,
requestTimeout, requestTimeout,
successCode, successCode,
tokenName, tokenName,
@ -13,33 +12,33 @@ import store from "@/store";
import qs from "qs"; import qs from "qs";
import router from "@/router"; import router from "@/router";
import { isArray } from "@/utils/validate"; import { isArray } from "@/utils/validate";
import { message } from "ant-design-vue";
let loadingInstance; let loadingInstance;
/** /**
* @author chuzhixin 1204505056@qq.com
* @description 处理code异常 * @description 处理code异常
* @param {*} code * @param {*} code
* @param {*} msg * @param {*} msg
*/ */
const handleCode = (code, msg) => { const handleCode = (code, msg) => {
switch (code) { switch (code) {
case invalidCode: case 401:
message.error(msg || `后端接口${code}异常`); Vue.prototype.$baseMessage(msg || `无权限访问`, "error");
store.dispatch("user/resetAll").catch(() => {}); store.dispatch("user/resetAll").catch(() => {});
break; break;
case noRoleCode: case 403:
router.push({ path: "/401" }).catch(() => {}); router.push({ path: "/403" }).catch(() => {});
break;
case undefined:
Vue.prototype.$baseMessage(`后端接口未返回code`, "error");
break; break;
default: default:
message.error(msg || `后端接口${code}异常`); Vue.prototype.$baseMessage(msg || `后端接口${code}异常`, "error");
break; break;
} }
}; };
/** /**
* @author chuzhixin 1204505056@qq.com
* @description axios初始化 * @description axios初始化
*/ */
const instance = axios.create({ const instance = axios.create({
@ -51,7 +50,6 @@ const instance = axios.create({
}); });
/** /**
* @author chuzhixin 1204505056@qq.com
* @description axios请求拦截器 * @description axios请求拦截器
*/ */
instance.interceptors.request.use( instance.interceptors.request.use(
@ -64,9 +62,8 @@ instance.interceptors.request.use(
"application/x-www-form-urlencoded;charset=UTF-8" "application/x-www-form-urlencoded;charset=UTF-8"
) )
config.data = qs.stringify(config.data); config.data = qs.stringify(config.data);
if (debounce.some((item) => config.url.includes(item))) { if (debounce.some((item) => config.url.includes(item)))
//这里写加载动画 loadingInstance = Vue.prototype.$baseLoading();
}
return config; return config;
}, },
(error) => { (error) => {
@ -75,7 +72,6 @@ instance.interceptors.request.use(
); );
/** /**
* @author chuzhixin 1204505056@qq.com
* @description axios响应拦截器 * @description axios响应拦截器
*/ */
instance.interceptors.response.use( instance.interceptors.response.use(
@ -94,7 +90,7 @@ instance.interceptors.response.use(
} else { } else {
handleCode(code, msg); handleCode(code, msg);
return Promise.reject( return Promise.reject(
"vue-admin-beautiful请求异常拦截:" + "vue-admin-beautiful-pro请求异常拦截:" +
JSON.stringify({ url: config.url, code, msg }) || "Error" JSON.stringify({ url: config.url, code, msg }) || "Error"
); );
} }
@ -118,7 +114,7 @@ instance.interceptors.response.use(
const code = message.substr(message.length - 3); const code = message.substr(message.length - 3);
message = "后端接口" + code + "异常"; message = "后端接口" + code + "异常";
} }
message.error(message || `后端接口未知异常`); Vue.prototype.$baseMessage(message || `后端接口未知异常`, "error");
return Promise.reject(error); return Promise.reject(error);
} }
} }

View File

@ -4,7 +4,7 @@
<a-row :gutter="20"> <a-row :gutter="20">
<a-col :lg="12" :md="12" :sm="24" :xl="12" :xs="24"> <a-col :lg="12" :md="12" :sm="24" :xl="12" :xs="24">
<div class="pic-error"> <div class="pic-error">
<img class="pic-error-parent" src="@/assets/error_images/401.png" /> <img class="pic-error-parent" src="@/assets/error_images/403.png" />
<img <img
class="pic-error-child left" class="pic-error-child left"
src="@/assets/error_images/cloud.png" src="@/assets/error_images/cloud.png"
@ -31,7 +31,7 @@
import { mapActions } from "vuex"; import { mapActions } from "vuex";
export default { export default {
name: "Page401", name: "Page403",
data() { data() {
return { return {
jumpTime: 5, jumpTime: 5,

View File

@ -95,7 +95,7 @@
login: "user/login", login: "user/login",
}), }),
handleRoute() { handleRoute() {
return this.redirect === "/404" || this.redirect === "/401" return this.redirect === "/404" || this.redirect === "/403"
? "/" ? "/"
: this.redirect; : this.redirect;
}, },