38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
uni.$u.http.setConfig((config) => {
|
|
/* config 为默认全局配置*/
|
|
config.baseURL = 'https://wx.beishaoyuan.com/';
|
|
return config
|
|
})
|
|
|
|
// 设置请求拦截器
|
|
uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
|
|
// 携带token
|
|
const storeToken = wx.getStorageSync('userToken')
|
|
console.log(storeToken)
|
|
if(storeToken){
|
|
config.header.token = storeToken
|
|
}
|
|
// uni.showLoading();
|
|
return config
|
|
}, config => { // 可使用async await 做异步操作
|
|
return Promise.reject(config)
|
|
})
|
|
|
|
// 设置响应拦截器
|
|
uni.$u.http.interceptors.response.use((response) => {
|
|
const data = response.data
|
|
if(data.code==401){
|
|
setTimeout(()=>{
|
|
uni.navigateTo({
|
|
url: '/pages/login/index'
|
|
})
|
|
},2000)
|
|
}
|
|
// uni.hideLoading();
|
|
return response
|
|
}, (response) => {
|
|
// console.log(response,'responseresponse');
|
|
return Promise.reject(response)
|
|
});
|
|
|
|
export default uni.$u.http |