53 lines
1.2 KiB
JavaScript
53 lines
1.2 KiB
JavaScript
import Request from 'luch-request' // https://www.quanzhan.co/luch-request/guide/3.x/#npm
|
|
import {
|
|
baseUrl
|
|
} from './baseUrl.js';
|
|
const http = new Request();
|
|
|
|
http.setConfig((config) => {
|
|
console.log(12313123123);
|
|
/* config 为默认全局配置*/
|
|
config.baseURL = baseUrl; /* 根域名 */
|
|
config.timeout = 100000;
|
|
// config.header['Access-Control-Allow-Origin'] = "*"
|
|
// config.header['Origin']='*'
|
|
return config
|
|
})
|
|
|
|
// 在请求之前拦截
|
|
http.interceptors.request.use((config) => {
|
|
console.log(config);
|
|
config.header = {
|
|
token:uni.getStorageSync('token'),
|
|
...config.header,
|
|
}
|
|
|
|
return config
|
|
}, config => { // 可使用async await 做异步操作
|
|
console.log(config);
|
|
return Promise.reject(config)
|
|
})
|
|
|
|
// 在请求之后拦截
|
|
http.interceptors.response.use((response) => {
|
|
/* 对响应成功做点什么 可使用async await 做异步操作*/
|
|
|
|
return response
|
|
}, (response) => {
|
|
console.log(response,'1111111111');
|
|
if(response.data?.code===401){
|
|
uni.navigateTo({
|
|
url:'pages/login/login'
|
|
})
|
|
}
|
|
uni.showToast({
|
|
title:'请求错误',
|
|
icon:'error'
|
|
})
|
|
// uni.showToast({title:response.data?.message||response.errMsg})
|
|
/* 对响应错误做点什么*/
|
|
// console.log(response)
|
|
return Promise.reject(response)
|
|
})
|
|
export default http;
|