request.js
1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import axios from axios
import config from "@/config"
function createAxios(baseUrl) {
const instance = axios.create({
baseUrl,
headers: {
Auth_token: ""
},
timeout: 3000,
})
// 请求拦截
instance.interceptors.request.use(config => {
// console.log('request config: ' + JSON.stringify(config))
return config
}, error => {
return Promise.reject(error)
})
// 响应拦截
instance.interceptors.response.use(res => {
// console.log('res: ' + res)
const { data, status } = res
// code 0:成功,-1/其它:错误
if (status === 200 && data.code === 0) {
return JSON.stringify(data.result) ? data.result : data.data
} else if (data.code === 201) {
// Message.error({
// content: `产品已存在`,
// duration: 3
// })
} else {
addErrorLog(res)
}
if (process.env.NODE_ENV === 'development') {
return Promise.reject(res) // 这样控制台会显示报错日志
} else {
return new Promise(() => { }) // 中断promise
}
}, error => {
if (error.response) {
addErrorLog(error.response)
} else {
// Message.error({
// content: `服务器未启动或连接超时`,
// duration: 3
// })
console.error("服务器未启动或连接超时")
}
return Promise.reject(error)
})
// 错误日志
const addErrorLog = errorInfo => {
const { data, statusText, status, request: { responseText, responseURL } } = errorInfo
// let info = {
// type: 'ajax',
// code: status,
// mes: statusText,
// url: responseURL
// }
// if (!responseURL.includes('save_error_logger')) store.dispatch('addErrorLog', info)
process.env.NODE_ENV === 'development'
? console.error(`错误: 路径: ${responseURL}, 返回值 : ${responseText}`,)
: console.error(`${JSON.parse(responseText).message}`,)
}
return instance
}
// const httpRequest = createAxios()
export default createAxios(config.domain)