77 lines
2.5 KiB
JavaScript
77 lines
2.5 KiB
JavaScript
const install = (Vue, vm) => {
|
||
|
||
|
||
uni.setStorageSync('deptId', 100);
|
||
Vue.prototype.$u.http.setConfig({
|
||
// baseUrl: 'http://61.174.243.28:15861',
|
||
// baseUrl: 'http://192.168.2.46:8080',
|
||
// baseUrl: 'https://che.chuangtewl.com/prod-api',
|
||
// 测试环境
|
||
// baseUrl: 'https://dianche.chuantewulian.cn/prod-api',
|
||
// 俞山岛
|
||
baseUrl: 'https://dche.ccttiot.com/prod-api',
|
||
// 创特
|
||
loadingText: '努力加载中~',
|
||
loadingTime: 10000,
|
||
// 设置自定义头部content-type
|
||
header: {
|
||
'content-type': 'application/json;charset=UTF-8',
|
||
},
|
||
});
|
||
// 创特:wx3428c498d5061192
|
||
// 俞山岛: wx4d178f8c80348214
|
||
// 请求拦截部分
|
||
Vue.prototype.$u.http.interceptor.request = (config) => {
|
||
|
||
const token = uni.getStorageSync('token');
|
||
|
||
// const token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjQ0ZDljYzBlLThjZjEtNGFkOC05OGFjLThlNThjMWIxYjQ3OSJ9.v-aMkp9t7Z_QfjfsZy6d_1Ng76hPYa0A--SWScMJY9to7UlNv9IxHQnTJylLKdYKGrr8fRZ47Bu12UPm1DgMQg"
|
||
|
||
// console.log("我是token", token)
|
||
config.header.Authorization = token;
|
||
// config.header.Tenant-Id=1
|
||
// #ifdef H5
|
||
config.header.Authorization = 'Bearer ' + token;
|
||
// #endif
|
||
// 可以对某个url进行特别处理,此url参数为this.$u.get(url)中的url值
|
||
if (config.url == '/user/login') config.header.noToken = true;
|
||
|
||
|
||
// 最后需要将config进行return
|
||
return config;
|
||
|
||
// 如果return一个false值,则会取消本次请求
|
||
// if(config.url == '/user/rest') return false; // 取消某次请求
|
||
}
|
||
|
||
// 响应拦截部分
|
||
Vue.prototype.$u.http.interceptor.response = (res) => {
|
||
|
||
return res;
|
||
}
|
||
|
||
// 手动实现超时控制
|
||
const originalRequest = Vue.prototype.$u.http.request;
|
||
Vue.prototype.$u.http.request = (options) => {
|
||
return new Promise((resolve, reject) => {
|
||
const timeout = 20000; // 10秒超时
|
||
const timer = setTimeout(() => {
|
||
resolve( { code: 500, msg: '请求超时' } );
|
||
}, timeout);
|
||
|
||
// 执行原始请求
|
||
originalRequest.call(Vue.prototype.$u.http, options).then(response => {
|
||
clearTimeout(timer);
|
||
resolve(response);
|
||
}).catch(error => {
|
||
clearTimeout(timer);
|
||
reject(error);
|
||
});
|
||
});
|
||
};
|
||
}
|
||
|
||
export default {
|
||
install
|
||
}
|