chuangte_bike_newxcx/common/http.interceptor.js

56 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-12-05 16:45:28 +08:00
const install = (Vue, vm) => {
2025-04-01 21:35:30 +08:00
uni.setStorageSync('deptId', 100);
Vue.prototype.$u.http.setConfig({
2025-04-11 18:23:16 +08:00
// baseUrl: 'http://192.168.2.46:4101', //键辉本地
2025-04-10 08:57:21 +08:00
// baseUrl: 'http://192.168.0.102:4101', //景森本地
baseUrl: 'https://ele.ccttiot.com/prod-api', //线上
2025-04-01 21:35:30 +08:00
loadingText: '努力加载中~',
loadingTime: 10000,
// 设置自定义头部content-type
header: {
'content-type': 'application/json;charset=UTF-8',
},
2025-04-10 08:57:21 +08:00
})
2025-04-01 21:35:30 +08:00
// 请求拦截部分
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;
}
2023-12-07 00:04:36 +08:00
2025-04-01 21:35:30 +08:00
// 响应拦截部分
Vue.prototype.$u.http.interceptor.response = (res) => {
return res;
}
2023-12-07 00:04:36 +08:00
2025-04-01 21:35:30 +08:00
// 手动实现超时控制
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);
});
});
};
2023-12-05 16:45:28 +08:00
}
export default {
2025-04-01 21:35:30 +08:00
install
}