chuangte_bike_newxcx/common/http.interceptor.js

55 lines
2.0 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-10-25 15:52:11 +08:00
// baseUrl: 'http://192.168.1.2:4101', //键辉本地
// baseUrl: 'http://192.168.2.221:4101', //景森本地
baseUrl: 'https://ele.ccttiot.com/prod-api', //线上 小鹿appid wx8a05cf95418a6859
2025-06-06 11:32:12 +08:00
// baseUrl: 'https://cc.ccttiot.com/prod-api', //叉车线上
2025-10-25 15:52:11 +08:00
// baseUrl:'https://ysd.chuantewulian.cn/prod-api', //嵛山岛线上api 嵛山岛appidwx4d178f8c80348214
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) => {
2025-10-25 15:52:11 +08:00
const token = uni.getStorageSync('token')
config.header.Authorization = token
2025-04-01 21:35:30 +08:00
// config.header.Tenant-Id=1
// #ifdef H5
2025-10-25 15:52:11 +08:00
config.header.Authorization = 'Bearer ' + token
2025-04-01 21:35:30 +08:00
// #endif
// 可以对某个url进行特别处理此url参数为this.$u.get(url)中的url值
2025-10-25 15:52:11 +08:00
if (config.url == '/user/login') config.header.noToken = true
return config
2025-04-01 21:35:30 +08:00
}
// 响应拦截部分
Vue.prototype.$u.http.interceptor.response = (res) => {
2025-10-25 15:52:11 +08:00
return res
2025-04-01 21:35:30 +08:00
}
// 手动实现超时控制
2025-10-25 15:52:11 +08:00
const originalRequest = Vue.prototype.$u.http.request
2025-04-01 21:35:30 +08:00
Vue.prototype.$u.http.request = (options) => {
return new Promise((resolve, reject) => {
const timeout = 20000; // 10秒超时
const timer = setTimeout(() => {
2025-10-25 15:52:11 +08:00
resolve( { code: 500, msg: '请求超时',} )
}, timeout)
2025-04-01 21:35:30 +08:00
// 执行原始请求
originalRequest.call(Vue.prototype.$u.http, options).then(response => {
2025-10-25 15:52:11 +08:00
clearTimeout(timer)
resolve(response)
2025-04-01 21:35:30 +08:00
}).catch(error => {
2025-10-25 15:52:11 +08:00
clearTimeout(timer)
reject(error)
})
})
}
2023-12-05 16:45:28 +08:00
}
export default {
2025-04-01 21:35:30 +08:00
install
}