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.
35 lines
1.2 KiB
35 lines
1.2 KiB
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))
|
|
}
|
|
|