YNUtdPlatform/utils/request_new_yn.js

119 lines
3.5 KiB
JavaScript
Raw Normal View History

2024-10-21 09:24:11 +08:00
import store from '@/store'
import config from '@/config'
import { getToken, setToken } from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import { toast, showConfirm, tansParams } from '@/utils/common'
2024-10-25 18:21:10 +08:00
const token = uni.getStorageSync('wkToken') || null
2024-10-21 09:24:11 +08:00
let timeout = 60000
2024-10-25 18:21:10 +08:00
const baseUrl = config.workPlanNewYnUrl
2024-10-27 22:40:40 +08:00
console.log('token', token)
2024-10-21 09:24:11 +08:00
2024-11-07 20:21:02 +08:00
let isRequesting = false // 请求锁,防止重复请求
2024-10-21 09:24:11 +08:00
const request = config => {
2024-11-07 20:21:02 +08:00
// 如果正在请求中,直接返回一个 rejected promise
if (isRequesting) {
return uni.showToast({ title: '请求正在处理中...请稍后', icon: 'none' })
// return Promise.reject('请求正在处理中,请稍后再试')
// uni.showToast({ title: '请求正在处理中...', icon: 'none' })
}
// 设置请求正在进行
isRequesting = true
2024-10-21 09:24:11 +08:00
// 是否需要设置 token
const isToken = (config.headers || {}).isToken === false
config.header = config.header || {}
2024-10-25 18:21:10 +08:00
if (token && !isToken) {
config.header['Authorization'] = 'Bearer ' + token
2024-10-21 09:24:11 +08:00
}
2024-11-07 20:21:02 +08:00
2024-10-21 09:24:11 +08:00
// get请求映射params参数
if (config.params) {
let url = config.url + '?' + tansParams(config.params)
url = url.slice(0, -1)
config.url = url
}
if (config.method === 'post') {
config.header = {
...config.header,
'Content-Type': 'application/x-www-form-urlencoded'
}
// config.data = JSON.stringify(config.data);
}
2024-11-07 20:21:02 +08:00
2024-10-21 09:24:11 +08:00
return new Promise((resolve, reject) => {
uni
.request({
method: config.method || 'get',
timeout: config.timeout || timeout,
url: baseUrl + config.url,
data: config.data,
header: config.header,
dataType: 'json'
})
.then(response => {
let [error, res] = response
if (error) {
toast('后端接口连接异常')
2024-11-07 20:21:02 +08:00
// 请求完成,解除锁定
isRequesting = false
2024-10-21 09:24:11 +08:00
reject('后端接口连接异常')
return
}
2024-11-07 20:21:02 +08:00
2024-10-21 09:24:11 +08:00
const code = res.data.code || 200
const msg = errorCode[code] || res.data.msg || errorCode['default']
2024-11-07 20:21:02 +08:00
2024-10-21 09:24:11 +08:00
if (code === 401) {
showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
if (res.confirm) {
store.dispatch('LogOut').then(res => {
uni.reLaunch({ url: '/pages/login' })
})
}
})
2024-11-07 20:21:02 +08:00
// 请求完成,解除锁定
isRequesting = false
2024-10-21 09:24:11 +08:00
reject('无效的会话,或者会话已过期,请重新登录。')
} else if (code === 500) {
toast(msg)
2024-11-07 20:21:02 +08:00
// 请求完成,解除锁定
isRequesting = false
2024-10-21 09:24:11 +08:00
reject(msg)
} else if (code !== 200) {
toast(msg)
2024-11-07 20:21:02 +08:00
// 请求完成,解除锁定
isRequesting = false
2024-10-21 09:24:11 +08:00
reject(msg)
}
2024-11-07 20:21:02 +08:00
// 请求完成,解除锁定
isRequesting = false
2024-10-21 09:24:11 +08:00
resolve(res.data)
})
.catch(error => {
let { message } = error
2024-11-07 20:21:02 +08:00
// 请求异常处理
2024-10-21 09:24:11 +08:00
if (message === 'Network Error') {
message = '后端接口连接异常'
} else if (message.includes('timeout')) {
message = '系统接口请求超时'
} else if (message.includes('Request failed with status code')) {
message = '系统接口' + message.substr(message.length - 3) + '异常'
}
2024-11-07 20:21:02 +08:00
// 请求完成,解除锁定
isRequesting = false
2024-10-21 09:24:11 +08:00
toast(message)
reject(error)
})
})
}
export default request