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.
 
 
 
 
 
 

130 lines
3.3 KiB

import conf from "@/common/config.js"
/**
* 响应拦截
* @param {Object} http
*/
module.exports = (vm) => {
uni.$u.http.interceptors.response.use((response) => {
/* 对响应成功做点什么 可使用async await 做异步操作*/
// 自定义参数
let _cus = {
showLoading: true, // 是否显示加载等待框
loadingTitle: '加载中', // 加载等待框的提示文字
showFailMessage: true, // 返回失败信息是否显示
catchError: true, // 是否集中管理Catch,
}
if (response.config && response.config.custom)
Object.assign(_cus, response.config.custom)
if (200 == response.statusCode) {
let data = response.data
if (_cus.catchError) {
if (data.success) {
return data.data
} else {
let loginTimeoutCode = "5000"
let loginTimeoutPage = "/pages/login/index"
if (conf.loginTimeoutCode) {
loginTimeoutCode = "" + conf.loginTimeoutCode
}
if (conf.loginTimeoutPage) {
loginTimeoutPage = conf.loginTimeoutPage
}
if (loginTimeoutCode === data.code) {
uni.showModal({
title: '提示',
content: '登录过期或失效,请重新登录!',
showCancel: false,
success: function(res) {
uni.reLaunch({
url: loginTimeoutPage
})
}
});
} else {
if (_cus.showFailMessage) {
uni.showToast({
title: data.msg,
icon: 'none',
duration: 2000,
})
}
}
return Promise.reject(data)
}
} else {
return data
}
} else {
console.log('response.statusCode--', response)
let em = {
"code": "" + response.statusCode,
"data": "",
"msg": "网络请求失败~",
"success": false,
"timestamp": new Date().getTime()
}
if (res.data) {
em.msg = res.data
}
if (_cus.showFailMessage) {
uni.showToast({
title: em.msg,
icon: 'none',
duration: 2000,
})
}
return Promise.reject(em)
}
// const data = response.data
// if (data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
// // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
// if (custom.toast !== false) {
// uni.$u.toast(data.message)
// }
// // 如果需要catch返回,则进行reject
// if (custom?.catch) {
// return Promise.reject(data)
// } else {
// // 否则返回一个pending中的promise
// return new Promise(() => {})
// }
// }
// return data.data || {}
}, (error) => {
console.log('request-fail', error)
let _opts = {
showLoading: true, // 是否显示加载等待框
loadingTitle: '加载中', // 加载等待框的提示文字
showFailMessage: true, // 返回失败信息是否显示
catchError: true, // 是否集中管理Catch,
}
if (error.config && error.config.custom)
Object.assign(_opts, response.config.custom)
let em = {
"code": "600",
"data": "",
"msg": "网络请求失败~",
"success": false,
"timestamp": new Date().getTime()
}
if (404 == error.statusCode) {
em.code = "404"
em.msg = "404,请求地址错误"
}
if (500 == error.statusCode) {
em.code = "500"
em.msg = "500,内部服务错误"
}
if (_opts.showFailMessage) {
uni.showToast({
title: em.msg,
icon: 'none',
duration: 2000,
})
}
return Promise.reject(em)
})
}