3
0
Fork 0
web-store-retail-h5/config/request.js

93 lines
2.2 KiB
JavaScript
Raw Normal View History

2025-03-23 09:29:40 +08:00
/*
* @Descripttion:
* @version:
2025-03-23 09:29:40 +08:00
* @Author: kBank
* @Date: 2022-05-24 14:43:45
*/
import { getToken, removeToken } from '@/config/auth.js'
2025-05-28 09:16:29 +08:00
module.exports = vm => {
// 检查uni.$u.http是否可用
if (!uni || !uni.$u || !uni.$u.http) {
console.warn('uni.$u.http is not available, request configuration skipped')
return
}
// 初始化请求配置
2025-05-28 09:16:29 +08:00
uni.$u.http.setConfig(config => {
// config.baseURL = 'https://p1.hzs413.com/inter-api';
2025-04-07 11:41:04 +08:00
// 192.168.0.100:8080
2025-04-09 09:30:26 +08:00
//#ifdef DEV_SERVER
console.log('DEV_SERVER')
config.baseURL = 'http://192.168.0.104:8080'
2025-04-09 09:30:26 +08:00
//#endif
//#ifdef QA_SERVER
console.log('QA_SERVER')
2025-05-28 09:16:29 +08:00
config.baseURL = '/prod-api'
2025-04-09 09:30:26 +08:00
//#endif
2025-05-28 09:16:29 +08:00
config.timeout = 30000
2025-03-23 09:29:40 +08:00
return config
})
2025-03-23 09:29:40 +08:00
// 请求拦截
2025-05-28 09:16:29 +08:00
uni.$u.http.interceptors.request.use(
config => {
config.data = config.data || {}
config.header['Source'] = 3
let token = getToken()
let mToken = uni.getStorageSync('mToken')
let lang = uni.getStorageSync('lang')
if (lang) {
config.header['Accept-Language'] = lang
} else {
config.header['Accept-Language'] = 'zh-CN'
}
if (token) {
config.header['Authorization'] = token
}
config.header['token'] = mToken || ''
return config
},
config => {
return Promise.reject(config)
2025-03-23 09:29:40 +08:00
}
2025-05-28 09:16:29 +08:00
)
2025-03-23 09:29:40 +08:00
// 响应拦截
2025-05-28 09:16:29 +08:00
uni.$u.http.interceptors.response.use(
response => {
const data = response.data
2025-03-23 09:29:40 +08:00
// uni.$u.toast(data.msg)
2025-05-28 09:16:29 +08:00
// if (data.code == 200) {
// return data;
// } else {
// token过期,清除token重新获取
if (data.code == 400 || data.code == 401 || data.code == 402) {
// uni.showToast({
// title: '登录超时',
// icon: 'none',
// duration: 1500,
// })
removeToken()
// 跳转到登录
setTimeout(() => {
uni.reLaunch({
url: '/pages/login/index',
})
}, 1500)
} else if (data.code == 500) {
uni.$u.toast(data.msg)
return data
} else {
// uni.$u.toast(data.msg)
return data
}
},
response => {
uni.$u.toast('服务器错误,请稍后重试')
2025-03-23 09:29:40 +08:00
}
2025-05-28 09:16:29 +08:00
)
}