c1b532ad
梁保满
权限配置,路由基础设置
|
1
2
3
4
|
import axios from "axios"
import Cookies from "js-cookie"
import NProgress from "nprogress"
import { Message } from "element-ui"
|
13b58a42
梁保满
备题组卷部分前端页面基本完成
|
5
6
7
|
import router from "@/router/index"
import store from "@/store"
import conf from "../config/index"; // 路径配置
|
c1b532ad
梁保满
权限配置,路由基础设置
|
8
|
// axios默认配置
|
65f592b6
梁保满
答题卡列表页
|
9
|
axios.defaults.timeout = 1000000000 // 超时时间
|
13b58a42
梁保满
备题组卷部分前端页面基本完成
|
10
|
axios.defaults.baseURL = conf.baseURL
|
c1b532ad
梁保满
权限配置,路由基础设置
|
11
12
13
14
15
|
// http request 拦截器
axios.interceptors.request.use(config => {
NProgress.start()
config.headers["Content-Type"] = "application/json;charset=UTF-8"
|
65f592b6
梁保满
答题卡列表页
|
16
17
18
19
20
21
22
|
const source = axios.CancelToken.source();
store.commit('setTokenSources', [source.token, source.cancel])
config.cancelToken = source.token;
// if (Cookies.get("access_token")) {
// config.headers.Authorization = "Bearer" + Cookies.get("access_token")
// }
|
c1b532ad
梁保满
权限配置,路由基础设置
|
23
24
|
return config
},
|
65f592b6
梁保满
答题卡列表页
|
25
26
27
|
error => {
return Promise.reject(error.response)
})
|
c1b532ad
梁保满
权限配置,路由基础设置
|
28
29
30
31
|
// http response 拦截器
axios.interceptors.response.use(
response => {
|
13b58a42
梁保满
备题组卷部分前端页面基本完成
|
32
|
const res = respones.data;
|
c1b532ad
梁保满
权限配置,路由基础设置
|
33
|
NProgress.done()
|
65f592b6
梁保满
答题卡列表页
|
34
35
36
|
if (respones.config.cancelToken) {
store.commit('delTokenSources', respones.config.cancelToken)
}
|
13b58a42
梁保满
备题组卷部分前端页面基本完成
|
37
|
if (respones.status == 200) {
|
c1b532ad
梁保满
权限配置,路由基础设置
|
38
|
Cookies.set("access_token", response.data.message, { expires: 1 / 12 })
|
13b58a42
梁保满
备题组卷部分前端页面基本完成
|
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
// console.log(respones.status)
if (res.code == 999) {
if (!location.href.includes('localhost')) {
if (res.data) {
window.location.href = res.data
} else {
router.push({ path: '/login' })
if (res.message.includes('不存在')) {
Message({
message: res.message,
type: 'error',
duration: 3 * 1000
})
}
}
}
return
} else {
Cookies.set("access_token", response.data.message, { expires: 1 / 12 })
}
|
c1b532ad
梁保满
权限配置,路由基础设置
|
59
|
}
|
13b58a42
梁保满
备题组卷部分前端页面基本完成
|
60
|
return Promise.resolve(response)
|
c1b532ad
梁保满
权限配置,路由基础设置
|
61
62
|
},
error => {
|
13b58a42
梁保满
备题组卷部分前端页面基本完成
|
63
64
65
66
67
68
69
70
71
72
73
74
75
|
Message({
message: error.message,
type: 'error',
duration: 3 * 1000
})
if (error.response == undefined) {
return Promise.reject(error);
}
Message.closeAll()
const {
status
} = error.response
if (status === 403 || status === 401) {
|
c1b532ad
梁保满
权限配置,路由基础设置
|
76
|
Message({
|
13b58a42
梁保满
备题组卷部分前端页面基本完成
|
77
78
79
|
message: "未登录或登录超时,即将跳转到登录页面",
type: 'error',
duration: 3 * 1000
|
c1b532ad
梁保满
权限配置,路由基础设置
|
80
|
})
|
13b58a42
梁保满
备题组卷部分前端页面基本完成
|
81
82
83
84
85
|
router.push({
path: '/login',
query: {
url: window.location.href
}
|
c1b532ad
梁保满
权限配置,路由基础设置
|
86
|
})
|
13b58a42
梁保满
备题组卷部分前端页面基本完成
|
87
|
return
|
c1b532ad
梁保满
权限配置,路由基础设置
|
88
89
90
91
|
}
return Promise.reject(error.response) // 返回接口返回的错误信息
})
export default axios
|