You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.6 KiB
106 lines
3.6 KiB
import store from '@/store'
|
|
import { getToken } from '@/utils/token'
|
|
import errorCode from '@/utils/errorCode'
|
|
import { getSessionCache, setSessionCache } from '@/utils/cache'
|
|
import { toast, showConfirm, tansParams } from '@/utils/requestTool'
|
|
|
|
let timeout = 10000
|
|
const baseUrl = import.meta.env.VITE_APP_BASE_URL
|
|
|
|
const request = config => {
|
|
// 是否需要设置 token
|
|
const isToken = (config.headers || {}).isToken === false
|
|
// 是否需要防止数据重复提交
|
|
const isRepeatSubmit = (config.headers || {}).repeatSubmit === false
|
|
config.header = config.header || {}
|
|
if (getToken() && !isToken) {
|
|
config.header['Authorization'] = 'Vrapile ' + getToken()
|
|
}
|
|
// get请求映射params参数
|
|
if (config.params) {
|
|
let url = config.url + '?' + tansParams(config.params)
|
|
url = url.slice(0, -1)
|
|
config.url = url
|
|
}
|
|
if (!isRepeatSubmit) {
|
|
const requestObj = {
|
|
url: config.url,
|
|
data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data,
|
|
time: new Date().getTime()
|
|
}
|
|
const sessionObj = getSessionCache()
|
|
if (sessionObj === undefined || sessionObj === null || sessionObj === '') {
|
|
setSessionCache(requestObj)
|
|
} else {
|
|
const s_url = sessionObj.url; // 请求地址
|
|
const s_data = sessionObj.data; // 请求数据
|
|
const s_time = sessionObj.time; // 请求时间
|
|
const interval = 1000; // 间隔时间(ms),小于此时间视为重复提交
|
|
if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) {
|
|
const message = '数据正在处理,请勿重复提交:' + s_url;
|
|
console.warn(`[${s_url}]: ` + message)
|
|
return Promise.reject(new Error(message))
|
|
} else {
|
|
setSessionCache(requestObj)
|
|
}
|
|
}
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
method: config.method || 'get',
|
|
timeout: config.timeout || timeout,
|
|
url: config.baseUrl || baseUrl + config.url,
|
|
data: config.data,
|
|
header: config.header,
|
|
dataType: 'json'
|
|
}).then(res => {
|
|
const code = res.data.code || res.data.status || 200
|
|
const msg = errorCode[code] || res.data.msg || errorCode['default']
|
|
|
|
if (code === 401) {
|
|
// 密码登录的,实现自动重新登录
|
|
let loginInfo = store.state.user.loginInfo;
|
|
if(loginInfo && loginInfo.loginType == 0){
|
|
store.dispatch('Login', {
|
|
loginType: loginInfo.loginType,
|
|
userName: loginInfo.userName,
|
|
password: loginInfo.password,
|
|
registerFlag: "N"
|
|
}).then(() => {
|
|
store.dispatch('GetInfo');
|
|
})
|
|
reject('无效的会话,或者会话已过期,已重新登录。')
|
|
}else{
|
|
showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
|
|
if (res.confirm) {
|
|
uni.reLaunch({ url: '/pages/user/login' })
|
|
}
|
|
})
|
|
reject('无效的会话,或者会话已过期,请重新登录。')
|
|
}
|
|
} else if (code === 500) {
|
|
toast(msg)
|
|
reject('500')
|
|
} else if (code !== 200) {
|
|
toast(msg)
|
|
reject(code)
|
|
}
|
|
resolve(res.data)
|
|
}).catch(error => {
|
|
let { message } = error
|
|
if (message && message === 'Network Error') {
|
|
message = '后端接口连接异常'
|
|
} else if (message && message.includes('timeout')) {
|
|
message = '系统接口请求超时'
|
|
} else if (message && message.includes('Request failed with status code')) {
|
|
message = '系统接口' + message.substr(message.length - 3) + '异常'
|
|
}else{
|
|
message = '系统繁忙'
|
|
}
|
|
toast(message)
|
|
reject(error)
|
|
})
|
|
})
|
|
}
|
|
|
|
export default request
|