106 lines
3.5 KiB
JavaScript
106 lines
3.5 KiB
JavaScript
/*
|
||
* @Descripttion:
|
||
* @Author: lip
|
||
* @Date: 2023-05-12 08:44:49
|
||
* @LastEditors: lip
|
||
*/
|
||
import store from './store/index.js'
|
||
import configRc from './config.js'
|
||
import { getToken } from '@/utilsRc/auth'
|
||
import errorCode from '@/utilsRc/errorCode'
|
||
import { toast, showConfirm, tansParams } from '@/utilsRc/common'
|
||
|
||
let timeout = 10000
|
||
const baseUrl = configRc.baseUrl
|
||
|
||
const request = config => {
|
||
// 是否需要设置 token
|
||
const isToken = (config.headers || {}).isToken === false
|
||
config.header = config.header || {}
|
||
if (getToken() && !isToken) {
|
||
config.header['Authorization'] = 'Bearer ' + getToken()
|
||
}
|
||
// config.header['Authorization'] = 'Bearer ' + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJzeXNfdXNlcjoxIiwicm5TdHIiOiJGUVl3YmRUalAzQ1BMNGFVc0RxeGJmdjAyT3JMZllDVSIsInVzZXJJZCI6MX0.kSOXY2QJQPbfjE0Yx2R3S8yQciA33OZBS9xJtr7cQ1A'
|
||
// get请求映射params参数
|
||
if (config.params) {
|
||
let url = config.url + '?' + tansParams(config.params)
|
||
url = url.slice(0, -1)
|
||
config.url = url
|
||
}
|
||
return new Promise((resolve, reject) => {
|
||
uni.request({
|
||
method: config.method || 'get',
|
||
timeout: config.timeout || timeout,
|
||
url: baseUrl + config.url,
|
||
// url: 'https://gccrcdh.sd-talent.cn:80/zhq' + config.url,
|
||
data: config.data,
|
||
header: config.header,
|
||
dataType: 'json'
|
||
}).then(response => {
|
||
let res = response.data
|
||
let error = response.errMsg!='request:ok'
|
||
if (error) {
|
||
toast('网络出小差,请稍后再试')
|
||
reject('网络出小差,请稍后再试')
|
||
return
|
||
}
|
||
const code = res.code || 200
|
||
const msg = errorCode[code] || res.msg || errorCode['default']
|
||
const isShowModel = getApp().globalData.isShowModel
|
||
if (code == 200) {
|
||
resolve(res)
|
||
}else if (code === 401) {
|
||
if(isShowModel === false) {
|
||
getApp().globalData.isShowModel = true
|
||
showConfirm('您好!登录后,您才可以继续操作。', '取消', '去登录').then(res => {
|
||
if (res.confirm) {
|
||
getApp().globalData.isShowModel = false
|
||
store.dispatch('LogOut').then(res => {
|
||
uni.reLaunch({ url: '/pages/login/login-one' })
|
||
})
|
||
} else if(res.cancel) {
|
||
getApp().globalData.isShowModel = false
|
||
uni.navigateBack()
|
||
}
|
||
|
||
})
|
||
}
|
||
// reject(code)
|
||
}
|
||
// if (code === 401) {
|
||
// showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
|
||
// if (res.confirm) {
|
||
// store.dispatch('LogOut').then(res => {
|
||
// uni.reLaunch({ url: '/page_other/login/index' })
|
||
// })
|
||
// }
|
||
// })
|
||
// reject('无效的会话,或者会话已过期,请重新登录。')
|
||
// } else
|
||
else if (code === 500) {
|
||
toast(msg)
|
||
reject('500')
|
||
} else if (code !== 200 && code !== 9999) {
|
||
// 9999是正常的业务状态码,不应该被当作错误处理
|
||
toast(msg)
|
||
reject(code)
|
||
}
|
||
resolve(res.data)
|
||
})
|
||
.catch(error => {
|
||
let message = error.errMsg
|
||
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) + '异常'
|
||
}
|
||
toast(message)
|
||
reject(error)
|
||
})
|
||
})
|
||
}
|
||
|
||
export default request
|