module.exports = (vm) => {
// 初始化请求配置
uni.$u.http.setConfig((config) => {
// #ifdef MP-WEIXIN
config.baseURL = 'http://uat.banlu.xuexiluxian.cn'; /* 根域名 */
// #endif
return config
})
// 请求拦截
uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
//获取token==>在header中把token给后端携带过去
let token = uni.getStorageSync('token');
if( token ){
config.header['Authorization'] = token;
}
return config
}, config => { // 可使用async await 做异步操作
return Promise.reject(config)
})
// 响应拦截
uni.$u.http.interceptors.response.use((response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
const data = response.data
return data;
}, (response) => {
// 对响应错误做点什么 (statusCode !== 200)
return Promise.reject(response)
})
}