|
@@ -1,111 +1,102 @@
|
|
|
+import axios from "axios";
|
|
|
+import Router from "@/router";
|
|
|
+import { Message, Notification } from "@arco-design/web-vue";
|
|
|
+import { useSystemStore } from "@/store/modules/systemStore";
|
|
|
|
|
|
-import axios from 'axios'
|
|
|
-import Router from '@/router'
|
|
|
-import { Message, Notification } from '@arco-design/web-vue'
|
|
|
-import { useSystemStore } from '@/store/modules/systemStore'
|
|
|
-
|
|
|
-
|
|
|
-import { fn_logout } from '@/utils'
|
|
|
-
|
|
|
+import { fn_logout } from "@/utils";
|
|
|
|
|
|
// console.log(import.meta.env.BASE_URL)
|
|
|
const axiosInstance = axios.create({
|
|
|
// baseURL: `${import.meta.env.PROD ? import.meta.env.VITE_PRO_PATH : ''}/api`,
|
|
|
baseURL: import.meta.env.BASE_URL + "api",
|
|
|
timeout: 300000,
|
|
|
-})
|
|
|
-
|
|
|
+});
|
|
|
|
|
|
const requestState = {
|
|
|
// 得到正确响应
|
|
|
- success: [ 200],
|
|
|
+ success: [200],
|
|
|
// token 跳转
|
|
|
beOverdue: [886],
|
|
|
// 没有访问权限
|
|
|
NotAccessRight: [500],
|
|
|
// 异常code
|
|
|
- exception: [400]
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
+ exception: [400],
|
|
|
+};
|
|
|
|
|
|
-const pathArr = ['/api/user/login', '/api/logout']
|
|
|
+const pathArr = ["/api/user/login", "/api/logout"];
|
|
|
axiosInstance.interceptors.request.use(
|
|
|
(config) => {
|
|
|
- const systemStore = useSystemStore()
|
|
|
- systemStore.localLoading(true)
|
|
|
-
|
|
|
+ const systemStore = useSystemStore();
|
|
|
+ systemStore.localLoading(true);
|
|
|
+
|
|
|
// 在发送请求之前做些什么
|
|
|
if (!pathArr.includes(config.url)) {
|
|
|
- const token = localStorage.getItem('token')
|
|
|
+ const token = localStorage.getItem("token");
|
|
|
if (token && config.headers) {
|
|
|
- config.headers['Authorization'] = token
|
|
|
+ config.headers["Authorization"] = token;
|
|
|
}
|
|
|
}
|
|
|
- return config
|
|
|
- },(err) => {
|
|
|
- const systemStore = useSystemStore()
|
|
|
- systemStore.localLoading()
|
|
|
- Promise.reject(err)
|
|
|
+ return config;
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ const systemStore = useSystemStore();
|
|
|
+ systemStore.localLoading();
|
|
|
+ Promise.reject(err);
|
|
|
}
|
|
|
-)
|
|
|
+);
|
|
|
|
|
|
// 响应拦截器
|
|
|
axiosInstance.interceptors.response.use(
|
|
|
(res) => {
|
|
|
- const systemStore = useSystemStore()
|
|
|
- systemStore.localLoading()
|
|
|
- const { code, data, message:msg } = res.data
|
|
|
- // 成功
|
|
|
- if (code === 200) {
|
|
|
- return Promise.resolve(res.data)
|
|
|
- }
|
|
|
-
|
|
|
- // 服务端错误信息
|
|
|
- if(requestState.NotAccessRight.includes(code)){
|
|
|
- Notification.warning({
|
|
|
- title: '系统信息',
|
|
|
- content: msg,
|
|
|
- })
|
|
|
- return Promise.reject(msg);
|
|
|
- }
|
|
|
+ const systemStore = useSystemStore();
|
|
|
+ systemStore.localLoading();
|
|
|
+ const { code, data, message: msg } = res.data;
|
|
|
+ // 成功
|
|
|
+ if (code === 200) {
|
|
|
+ return Promise.resolve(res.data);
|
|
|
+ }
|
|
|
|
|
|
- // 异常code
|
|
|
- if(requestState.exception.includes(code)){
|
|
|
- Notification.warning({
|
|
|
- title: '系统提示',
|
|
|
- content: msg,
|
|
|
- })
|
|
|
- return Promise.reject(msg);
|
|
|
- }
|
|
|
- //token失效
|
|
|
- if(requestState.beOverdue.includes(code)){
|
|
|
- fn_logout(Router)
|
|
|
- Notification.warning({
|
|
|
- title: '系统信息',
|
|
|
- content: msg,
|
|
|
- duration: 2000,
|
|
|
- })
|
|
|
- return Promise.reject(msg);
|
|
|
- }
|
|
|
+ // 服务端错误信息
|
|
|
+ if (requestState.NotAccessRight.includes(code)) {
|
|
|
+ Notification.warning({
|
|
|
+ title: "系统信息",
|
|
|
+ content: msg,
|
|
|
+ });
|
|
|
+ return Promise.reject(msg);
|
|
|
+ }
|
|
|
|
|
|
- return Promise.resolve(data)
|
|
|
- },(err) => {
|
|
|
- const systemStore = useSystemStore()
|
|
|
- systemStore.localLoading()
|
|
|
- const res = err['response']
|
|
|
- if(err.code === "ERR_CANCELED") {
|
|
|
- console.log('请求中断')
|
|
|
- return
|
|
|
+ // 异常code
|
|
|
+ if (requestState.exception.includes(code)) {
|
|
|
+ Notification.warning({
|
|
|
+ title: "系统提示",
|
|
|
+ content: msg,
|
|
|
+ });
|
|
|
+ return Promise.reject(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ return Promise.resolve(data);
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ const systemStore = useSystemStore();
|
|
|
+ systemStore.localLoading();
|
|
|
+ const res = err["response"];
|
|
|
+ if (err.code === "ERR_CANCELED") {
|
|
|
+ console.log("请求中断");
|
|
|
+ return;
|
|
|
}
|
|
|
- console.log("err.response-",err.response)
|
|
|
+ const msg = err.response.data ? err.response.data.message : ""
|
|
|
Notification.warning({
|
|
|
- title: '系统信息',
|
|
|
- content: err.response.data ? err.response.data.message : '',
|
|
|
- })
|
|
|
- Promise.reject(err)
|
|
|
+ title: "系统信息",
|
|
|
+ content: msg
|
|
|
+ });
|
|
|
+ //token失效
|
|
|
+ if (requestState.beOverdue.includes(err.status)) {
|
|
|
+ fn_logout(Router);
|
|
|
+ // 直接终止请求
|
|
|
+ return undefined;
|
|
|
+ }
|
|
|
+ Promise.reject(err);
|
|
|
}
|
|
|
-)
|
|
|
-
|
|
|
-export default axiosInstance
|
|
|
+);
|
|
|
|
|
|
+export default axiosInstance;
|