2023-11-28
This commit is contained in:
13
utils/request/index.js
Normal file
13
utils/request/index.js
Normal file
@@ -0,0 +1,13 @@
|
||||
// 引入配置
|
||||
import config from '@/common/config'
|
||||
// 初始化请求配置
|
||||
uni.$u.http.setConfig((defaultConfig) => {
|
||||
/* defaultConfig 为默认全局配置 */
|
||||
defaultConfig.baseURL = config.baseUrl /* 根域名 */
|
||||
return defaultConfig
|
||||
})
|
||||
|
||||
module.exports = (vm) => {
|
||||
require('./requestInterceptors')(vm)
|
||||
require('./responseInterceptors')(vm)
|
||||
}
|
||||
35
utils/request/requestInterceptors.js
Normal file
35
utils/request/requestInterceptors.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import conf from "@/common/config.js"
|
||||
import token from '@/utils/auth.token.js'
|
||||
/**
|
||||
* 请求拦截
|
||||
* @param {Object} http
|
||||
*/
|
||||
module.exports = (vm) => {
|
||||
uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
|
||||
// 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
|
||||
config.data = config.data || {}
|
||||
// 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
|
||||
// console.log(vm.$store.state);
|
||||
|
||||
// 设置请求头中token的名字,以及token的值
|
||||
let _token = token.getToken()
|
||||
let _tokenName = 'Authorization'
|
||||
if (conf.tokenName) {
|
||||
_tokenName = conf.tokenName
|
||||
}
|
||||
config.header[_tokenName] = _token
|
||||
|
||||
// 设置针对响应结果的处理方式
|
||||
let _cus = {
|
||||
showLoading: true, // 是否显示加载等待框
|
||||
loadingTitle: '加载中', // 加载等待框的提示文字
|
||||
showFailMessage: true, // 返回失败信息是否显示
|
||||
catchError: true, // 是否集中管理Catch,
|
||||
}
|
||||
Object.assign(_cus, config.custom)
|
||||
config.custom = _cus
|
||||
|
||||
return config
|
||||
}, (config) => // 可使用async await 做异步操作
|
||||
Promise.reject(config))
|
||||
}
|
||||
130
utils/request/responseInterceptors.js
Normal file
130
utils/request/responseInterceptors.js
Normal file
@@ -0,0 +1,130 @@
|
||||
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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user