|
|
|
@ -24,8 +24,21 @@ uni.addInterceptor('request', {
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
function request (options, config) {
|
|
|
|
|
return checkNetworkStatus()
|
|
|
|
|
.then(res => {
|
|
|
|
|
return requestFun(options, config)
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: 'error',
|
|
|
|
|
title: error.msg
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 请求函数
|
|
|
|
|
function request ({ url, method, data = {} }, config = { loading: true }) {
|
|
|
|
|
function requestFun ({ url, method, data = {} }, config = { loading: true }) {
|
|
|
|
|
url = netConfig.baseUrl + url;
|
|
|
|
|
const header = {
|
|
|
|
|
// 'Authorization': 'Bearer ' + getToken()
|
|
|
|
@ -73,6 +86,10 @@ function request ({ url, method, data = {} }, config = { loading: true }) {
|
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
console.log('Request Error', new Error(error));
|
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: 'error',
|
|
|
|
|
title: '请求失败'
|
|
|
|
|
})
|
|
|
|
|
reject(error);
|
|
|
|
|
},
|
|
|
|
|
complete: () => {
|
|
|
|
@ -90,6 +107,8 @@ function request ({ url, method, data = {} }, config = { loading: true }) {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default request
|
|
|
|
|
|
|
|
|
|
// 处理401状态码
|
|
|
|
|
function dispose401 (response) {
|
|
|
|
|
logout().then(() => {
|
|
|
|
@ -147,4 +166,18 @@ function joinGetQuery(url, query) {
|
|
|
|
|
return queryStr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default request
|
|
|
|
|
// 检查网络状态
|
|
|
|
|
function checkNetworkStatus () {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
uni.getNetworkType({
|
|
|
|
|
success (res) {
|
|
|
|
|
const { networkType } = res;
|
|
|
|
|
networkType === 'none' ? reject({status: false, msg: '无网络连接'})
|
|
|
|
|
: resolve({ status: true, msg: '网络连接正常' })
|
|
|
|
|
},
|
|
|
|
|
fail (err) {
|
|
|
|
|
reject({ status: false, msg: err.msg || '网络错误' });
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|