56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
const install = (Vue, vm) => {
|
||
uni.setStorageSync('deptId', 100);
|
||
Vue.prototype.$u.http.setConfig({
|
||
// baseUrl: 'http://192.168.2.46:4101', //键辉本地
|
||
// baseUrl: 'http://192.168.0.102:4101', //景森本地
|
||
baseUrl: 'https://ele.ccttiot.com/prod-api', //线上
|
||
loadingText: '努力加载中~',
|
||
loadingTime: 10000,
|
||
// 设置自定义头部content-type
|
||
header: {
|
||
'content-type': 'application/json;charset=UTF-8',
|
||
},
|
||
})
|
||
// 请求拦截部分
|
||
Vue.prototype.$u.http.interceptor.request = (config) => {
|
||
const token = uni.getStorageSync('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;
|
||
return config;
|
||
}
|
||
|
||
// 响应拦截部分
|
||
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
|
||
}
|