|
|
@@ -8,13 +8,13 @@ function handleError(error) {
|
|
|
code: error?.code || error?.statusCode || -1,
|
|
|
message: error?.message || error?.errMsg || '未知错误'
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 特殊错误码处理
|
|
|
switch (err.code) {
|
|
|
case 401:
|
|
|
-
|
|
|
- uni.redirectTo({url: '/pages/system/login/index'})
|
|
|
-
|
|
|
+
|
|
|
+ uni.redirectTo({ url: '/pages/system/login/index' })
|
|
|
+ // userStore.logout();
|
|
|
break
|
|
|
case 403:
|
|
|
uni.showModal({
|
|
|
@@ -23,13 +23,13 @@ function handleError(error) {
|
|
|
showCancel: false
|
|
|
})
|
|
|
break
|
|
|
-
|
|
|
+
|
|
|
case 500:
|
|
|
uni.showToast({
|
|
|
icon: '',
|
|
|
title: err.message,
|
|
|
})
|
|
|
-
|
|
|
+
|
|
|
default:
|
|
|
// 通用错误提示(防抖处理)
|
|
|
if (!handleError.lastToastTime || Date.now() - handleError.lastToastTime > 2000) {
|
|
|
@@ -59,7 +59,7 @@ export function request(requestConfig) {
|
|
|
data: requestConfig.data,
|
|
|
params: requestConfig.params,
|
|
|
header: createPlatformHeaders(requestConfig.header),
|
|
|
-
|
|
|
+
|
|
|
success(res) {
|
|
|
// HTTP状态码处理
|
|
|
if (res.statusCode !== 200) {
|
|
|
@@ -72,7 +72,7 @@ export function request(requestConfig) {
|
|
|
})
|
|
|
return reject(res)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 业务状态码处理(假设200表示业务成功)
|
|
|
if (res.data.code === 200) {
|
|
|
resolve(res.data.data ?? res.data)
|
|
|
@@ -96,14 +96,15 @@ export function request(requestConfig) {
|
|
|
reject(err)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
// 请求拦截处理
|
|
|
const token = useUserStore().token
|
|
|
+ console.log("请求拦截TOKEN", token)
|
|
|
if (token) {
|
|
|
finalConfig.header.Authorization = 'Bearer ' + token
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
uni.request(finalConfig)
|
|
|
})
|
|
|
}
|
|
|
@@ -122,7 +123,7 @@ export function initHttp() {
|
|
|
handleError(err)
|
|
|
}
|
|
|
})
|
|
|
-
|
|
|
+
|
|
|
// 全局网络状态监听
|
|
|
uni.onNetworkStatusChange(res => {
|
|
|
if (!res.isConnected) {
|
|
|
@@ -142,7 +143,7 @@ function createPlatformHeaders(customHeaders = {}) {
|
|
|
'X-Request-Id': generateRequestId(),
|
|
|
...customHeaders
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 平台特定头配置
|
|
|
// #ifdef MP-WEIXIN
|
|
|
return {
|
|
|
@@ -152,7 +153,7 @@ function createPlatformHeaders(customHeaders = {}) {
|
|
|
'X-WX-Env': config.env
|
|
|
}
|
|
|
// #endif
|
|
|
-
|
|
|
+
|
|
|
// #ifdef H5
|
|
|
return {
|
|
|
...baseHeaders,
|
|
|
@@ -160,7 +161,7 @@ function createPlatformHeaders(customHeaders = {}) {
|
|
|
'X-User-Agent': navigator.userAgent
|
|
|
}
|
|
|
// #endif
|
|
|
-
|
|
|
+
|
|
|
// #ifdef APP-PLUS
|
|
|
return {
|
|
|
...baseHeaders,
|
|
|
@@ -169,24 +170,11 @@ function createPlatformHeaders(customHeaders = {}) {
|
|
|
'X-OS-Version': plus.os.version
|
|
|
}
|
|
|
// #endif
|
|
|
-
|
|
|
+
|
|
|
return baseHeaders
|
|
|
}
|
|
|
|
|
|
-// 添加认证头
|
|
|
-function addAuthHeader(config) {
|
|
|
- const token = uni.getStorageSync('token')
|
|
|
- if (token) {
|
|
|
- config.header.Authorization = `Bearer ${token}`
|
|
|
- }
|
|
|
-
|
|
|
- // 微信小程序检查session
|
|
|
- // #ifdef MP-WEIXIN
|
|
|
- if (wx.getStorageSync('session_key')) {
|
|
|
- config.header['X-Session-Key'] = wx.getStorageSync('session_key')
|
|
|
- }
|
|
|
- // #endif
|
|
|
-}
|
|
|
+
|
|
|
|
|
|
// 生成唯一请求ID
|
|
|
function generateRequestId() {
|