Commit 88994ab9 by 胡悦

修复

1 parent 66d740ee
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Toast } from 'vant';
import router, { resetRoute } from '../router'
import qs from 'qs'
import { createSign } from './util'
import $mockConfig from './config'
import { useUserStore } from '../store/user'
const { response = {}, request = {}, headers = {}, alert, sign, signWay = 'sha1', secret } = $mockConfig
// 配置新建一个axios 实例
const service: AxiosInstance = axios.create({
baseURL: request.baseURL,
// baseURL: PATH_URL,
timeout: 50000,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
})
const customer = {
response,
alert,
sign,
secret,
signWay
}
// 添加请求拦截器
service.interceptors.request.use(
(config: AxiosRequestConfig) => {
console.log(config)
let { data = {}, sign = true, noSignAttr = []} = config
if (sign) {
let _configSign = customer.sign || {}
data = createSign(Object.assign({}, _configSign, data), customer.secret, noSignAttr, customer.signWay)
}
const method = config.method?.toLowerCase()
if (method == 'get') {
config.params = data
} else if (method == 'post') {
config.data = qs.stringify(data)
}
// if (getSession('token')) {
// config.headers.common['Authorization'] = `${getSession('token')}`
// }
return config
},
(error:AxiosError) => {
return Promise.reject(error)
}
)
// 添加响应拦截器
service.interceptors.response.use(
({ data: res, config: { returnAll } }) => {
console.log(returnAll)
const {
response: { code_key, errId = 'errId', code_success_value = '0', data_key = 'data', errId_logout_value, logout_function = 'logout' }
} = customer
if (returnAll) {
return res
}
let codekey = Array.isArray(code_key) ? code_key.find(key => res.hasOwnProperty(key)) : code_key
let code = res[codekey]
let errValue = res[errId]
console.log(code, errValue)
// 特殊的code处理,可以增加
switch (code + '') {
case code_success_value + '':
break
case errId_logout_value + '':
// logout(logout_function)
console.log(11)
break
default:
if (errValue && errValue == errId_logout_value) {
// return logout(logout_function)
console.log(2)
localStorage.removeItem('sessionId')
router.push('/login')
console.log('session失效,登出',useUserStore().getSession)
}
Toast(res.message || res.msg || '出错了,请联系管理员')
break
}
return res[data_key]
},
error => {
// const { alert } = customer
Toast && Toast(error.message || '出错了,请联系管理员')
// 返回接口返回的错误信息
return Promise.reject(error)
}
)
export default service
\ No newline at end of file \ No newline at end of file
...@@ -7,12 +7,13 @@ const req: any = (url: string, data: any = {}, returnAll = false, method = 'POST ...@@ -7,12 +7,13 @@ const req: any = (url: string, data: any = {}, returnAll = false, method = 'POST
// let sessionId = '2dfdf8ff-fe50-4b07-bfd4-bdaec96ad717' // let sessionId = '2dfdf8ff-fe50-4b07-bfd4-bdaec96ad717'
if (sessionId) data.loginSessionId = sessionId if (sessionId) data.loginSessionId = sessionId
data.method = url data.method = url
return http({ const options = {
url: PATH, url: PATH,
method, method,
returnAll, data,
data returnAll
}) }
return http(options)
} }
export default req export default req
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!