index_code.js
6.89 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import store from "@/store"
import axios from "axios"
import { Message } from "element-ui"
import util from "@/libs/util"
import router from "@/router"
import { baseConfig } from "@/main"
import Qs from "qs"
axios.defaults.withCredentials = true
// const expiresMinute = 60 // 过期时间(分钟)
// 创建一个错误
export function errorCreate(msg) {
const error = new Error(msg)
errorLog(error)
throw error
}
// 记录和显示错误
function errorLog(error) {
// 添加到日志
store.dispatch("d2admin/log/push", {
message: "数据请求异常",
type: "danger",
meta: {
error
}
})
// 打印到控制台
if (process.env.NODE_ENV === "development") {
util.log.danger(">>>>>> Error >>>>>>")
console.log(error)
}
// 显示提示
Message({
message: error.message,
type: "error",
duration: 5 * 1000
})
}
// 网络请求公用地址 http://192.168.3.171:7300/mock/5d638b3b6fbe9a0ac59d872e/tFIMS
// VUE_APP_API=http://192.168.3.191:8944/router
// 创建一个 axios 实例
const service = axios.create({
// baseURL: baseURL,
timeout: 100000, // 请求超时时间
transformRequest: [
function (data) {
return Qs.stringify(data)
}
]
})
// 请求拦截器
service.interceptors.request.use(
(config) => {
return new Promise(async (resolve) => {
await store.commit("d2admin/loading/set", true)
// const uuid = util.cookies.get('uuid')
// 如果请求链接不在排除列表 (排除列表: 登录 基础数据) 并且cookie 过期(判断是否长时间未操作)
// if (!LogExternals.some(u => u === url || (params && u === params.method)) && !uuid) {
// await store.dispatch('d2admin/account/logout')
// router.replace('/login')
// }
// if (uuid) { // 判断 cookie 是否存在 存在则更新时间 避免登出时 记录空值
// const inExpiresMinutes = new Date(new Date().getTime() + expiresMinute * 60 * 1000)
// util.cookies.set('uuid', uuid, {
// expires: inExpiresMinutes
// })
// }
// config.url = baseConfig.axios.baseURL + config.url
// console.log(config.data.method.includes('dj.api.common.uploadImg'))
// console.log(config, "config")
// console.log(Object.prototype.toString.call(config.data))
// console.log(config.data.method)
if (process.env.NODE_ENV == "development") {
// 处理图片开发环境没用的问题
const type = Object.prototype.toString.call(config.data)
const methodList = [
"dj.api.common.uploadImg",
"dj.api.common.ocr.recognition",
"dj.api.smartcarlife.sms.sendSmsCode"
]
if (
type == "[object Object]" &&
methodList.includes(config.data.method)
) {
config.url = "http://dj.test.glsx.com.cn/rop-dj-smartcarlife/router"
return resolve(config)
}
}
// if (config.isPoint) {
// config.url = baseConfig.axios2.baseURL
// } else if (config.isCommon) {
// config.url = baseConfig.common.baseURL
// } else {
// config.url = baseConfig.axios.baseURL
// }
// console.log(config)
resolve(config)
})
},
(error) => {
store.commit("d2admin/loading/set", false)
// 发送失败
console.log(error)
Promise.reject(error)
}
)
// 响应拦截器
service.interceptors.response.use(
(response) => {
// console.log(response)
store.commit("d2admin/loading/set", false)
// dataAxios 是 axios 返回数据中的 data
// const dataAxios = {
// code: "20",
// message: "缺少sessionId参数",
// solution: "将添加会话ID请求参数",
// subErrors: null
// }
const dataAxios = response.data
const ruleResult = baseConfig.axios.result
const errCodeMsg = baseConfig.errCode
const errCodes = Object.keys(errCodeMsg)
// console.log("baseConfig", baseConfig)
// console.log("response", response)
// console.log("errCodes", errCodes)
// 这个状态码是和后端约定的
// 有 code 代表这是一个后端接口 可以进行进一步的判断
var goBackArr = ["20", "21"]
// console.log(dataAxios[ruleResult.code_key])
if (goBackArr.includes(dataAxios[ruleResult.code_key])) {
// await store.dispatch('d2admin/account/logout')
store.dispatch("d2admin/user/clear")
util.cookies.remove("token")
router.replace("/login")
// return Promise.reject(new Error("登录超时,请重新登录"))
return errorCreate("登录超时,请重新登录")
// return Promise.reject(new Error(response.message || 'Error'))
}
const codeValue = dataAxios[ruleResult.code_key] || dataAxios.returnCode
switch (+codeValue) {
case ruleResult.code_success_value:
// [ 示例 ] code === 0 代表没有错误
return dataAxios[ruleResult.data_key]
case ruleResult.code_error_value:
// [ 示例 ] code === -1 代表需将返回的message弹窗展示
return errorCreate(`${dataAxios[ruleResult.message_key]}`)
default:
// 不是正确的 code
// console.log(dataAxios)
// errorCreate(`${dataAxios[ruleResult.message_key]}`)
if (errCodes.includes(dataAxios[ruleResult.code_key])) {
return errorCreate(`${errCodeMsg[dataAxios[ruleResult.code_key]]}`)
} else {
// 返回约定定义之外的错误码直接取message
return errorCreate(`${dataAxios[ruleResult.message_key]}`)
}
break
}
},
(error) => {
if (error && error.response) {
switch (error.response.status) {
case 400:
error.message = "请求错误"
break
case 401:
error.message = "未授权,请登录"
break
case 403:
error.message = "拒绝访问"
break
case 404:
error.message = `请求地址出错: ${error.response.config.url}`
break
case 408:
error.message = "请求超时"
break
case 500:
error.message = "服务器内部错误"
break
case 501:
error.message = "服务未实现"
break
case 502:
error.message = "网关错误"
break
case 503:
error.message = "服务不可用"
break
case 504:
error.message = "网关超时"
break
case 505:
error.message = "HTTP版本不受支持"
break
default:
break
}
}
if (error && error?.stack?.indexOf("timeout") > -1) {
if (error.stack.indexOf("5000ms")) {
error.message = "查询超时,请刷新页面重试!"
}
// console.log('超时')
// console.log(error)
}
store.commit("d2admin/loading/set", false)
if (!error.__CANCEL__) errorLog(error) // 取消请求 不做错误提示
return Promise.reject(error)
}
)
export default service