|
1
|
import axios from 'axios'
|
|
2
3
4
5
6
|
import {
Notification,
MessageBox,
Message
} from 'element-ui'
|
|
7
|
import store from '@/store'
|
|
8
9
10
|
import {
getToken
} from '@/utils/auth'
|
|
11
|
import errorCode from '@/utils/errorCode'
|
|
12
13
14
|
import {
tansParams
} from "@/utils/huaheng";
|
|
15
16
17
18
19
20
21
|
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例
const service = axios.create({
// axios中请求配置有baseURL选项,表示请求URL公共部分
baseURL: process.env.VUE_APP_BASE_API,
// 超时
|
|
22
|
timeout: 15000
|
|
23
24
25
26
27
28
29
30
31
32
33
|
})
// request拦截器
service.interceptors.request.use(config => {
// 是否需要设置 token
const isToken = (config.headers || {}).isToken === false
if (getToken() && !isToken) {
config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际
}
return config
}, error => {
|
|
34
35
|
console.log(error)
Promise.reject(error)
|
|
36
37
38
39
40
41
42
43
44
45
46
|
})
// 响应拦截器
service.interceptors.response.use(res => {
// 未设置状态码则默认成功状态
const code = res.data.code || 200;
// 获取错误信息
const msg = errorCode[code] || res.data.msg || errorCode['default']
if (code === 401) {
MessageBox.confirm(
'登录状态已过期,您可以继续留在该页面,或者重新登录',
|
|
47
|
'系统提示', {
|
|
48
49
50
51
52
|
confirmButtonText: '重新登录',
cancelButtonText: '取消',
type: 'warning'
}
).then(() => {
|
|
53
|
store.dispatch('user/LogOut').then(() => {
|
|
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
location.reload() // 为了重新实例化vue-router对象 避免bug
})
})
} else if (code === 500) {
Message({
message: msg,
type: 'error'
})
return Promise.reject(new Error(msg))
} else if (code !== 200) {
Notification.error({
title: msg
})
return Promise.reject('error')
} else {
return res.data
}
},
error => {
console.log('err' + error)
|
|
74
75
76
|
let {
message
} = error;
|
|
77
78
|
if (message == "Network Error") {
message = "后端接口连接异常";
|
|
79
|
} else if (message.includes("timeout")) {
|
|
80
|
message = "系统接口请求超时";
|
|
81
|
} else if (message.includes("Request failed with status code")) {
|
|
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
message = "系统接口" + message.substr(message.length - 3) + "异常";
}
Message({
message: message,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error)
}
)
// 通用下载方法
export function download(url, params, filename) {
return service.post(url, params, {
transformRequest: [(params) => {
return tansParams(params)
}],
headers: {
|
|
100
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
},
responseType: 'blob'
}).then((data) => {
const content = data
const blob = new Blob([content])
if ('download' in document.createElement('a')) {
const elink = document.createElement('a')
elink.download = filename
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href)
document.body.removeChild(elink)
} else {
navigator.msSaveBlob(blob, filename)
}
|
|
118
|
this.exportLoading = false;
|
|
119
120
|
}).catch((r) => {
console.error(r)
|
|
121
|
this.exportLoading = false;
|
|
122
123
124
125
|
})
}
export default service
|