|
@@ -1,6 +1,5 @@
|
|
|
// HTTP 请求封装
|
|
// HTTP 请求封装
|
|
|
-
|
|
|
|
|
-const BASE_URL = 'http://localhost:8081/api'
|
|
|
|
|
|
|
+import { BASE_URL, REQUEST_TIMEOUT } from '../config'
|
|
|
|
|
|
|
|
interface RequestOptions {
|
|
interface RequestOptions {
|
|
|
url: string
|
|
url: string
|
|
@@ -25,6 +24,26 @@ function translateErrorMsg(msg: string): string {
|
|
|
return msg
|
|
return msg
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+function getNetworkErrorMessage(err: any): string {
|
|
|
|
|
+ // #ifdef H5
|
|
|
|
|
+ if (typeof err === 'object') {
|
|
|
|
|
+ // CORS / 预检失败常见表现
|
|
|
|
|
+ if (err.status === 0 || err.readyState === 0) {
|
|
|
|
|
+ return '无法连接服务器,请检查网络或后端地址配置(CORS/跨域)'
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // #endif
|
|
|
|
|
+
|
|
|
|
|
+ const errMsg = String(err?.errMsg || err?.message || err || '')
|
|
|
|
|
+ if (errMsg.includes('timeout') || errMsg.includes('超时')) {
|
|
|
|
|
+ return '请求超时,请稍后重试'
|
|
|
|
|
+ }
|
|
|
|
|
+ if (errMsg.includes('fail') || errMsg.includes('无法连接') || errMsg.includes('connect')) {
|
|
|
|
|
+ return '网络连接失败,请检查网络或服务器地址'
|
|
|
|
|
+ }
|
|
|
|
|
+ return '网络请求失败,请检查网络'
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export async function request<T = any>(options: RequestOptions): Promise<T> {
|
|
export async function request<T = any>(options: RequestOptions): Promise<T> {
|
|
|
const { url, method = 'GET', data, params, header = {}, loading = true } = options
|
|
const { url, method = 'GET', data, params, header = {}, loading = true } = options
|
|
|
|
|
|
|
@@ -60,6 +79,7 @@ export async function request<T = any>(options: RequestOptions): Promise<T> {
|
|
|
method,
|
|
method,
|
|
|
data,
|
|
data,
|
|
|
header: requestHeader,
|
|
header: requestHeader,
|
|
|
|
|
+ timeout: REQUEST_TIMEOUT,
|
|
|
success: (res) => {
|
|
success: (res) => {
|
|
|
if (loading) uni.hideLoading()
|
|
if (loading) uni.hideLoading()
|
|
|
|
|
|
|
@@ -87,15 +107,21 @@ export async function request<T = any>(options: RequestOptions): Promise<T> {
|
|
|
uni.removeStorageSync('user')
|
|
uni.removeStorageSync('user')
|
|
|
uni.reLaunch({ url: '/pages/login/login' })
|
|
uni.reLaunch({ url: '/pages/login/login' })
|
|
|
reject(new Error('Unauthorized'))
|
|
reject(new Error('Unauthorized'))
|
|
|
|
|
+ } else if (res.statusCode === 0 || res.statusCode === undefined) {
|
|
|
|
|
+ // 部分平台网络异常时 statusCode 为 0
|
|
|
|
|
+ const msg = getNetworkErrorMessage(res)
|
|
|
|
|
+ uni.showToast({ title: msg, icon: 'none' })
|
|
|
|
|
+ reject(new Error(msg))
|
|
|
} else {
|
|
} else {
|
|
|
- uni.showToast({ title: `请求失败: ${res.statusCode}`, icon: 'none' })
|
|
|
|
|
|
|
+ uni.showToast({ title: `请求失败 (${res.statusCode})`, icon: 'none' })
|
|
|
reject(new Error(`HTTP ${res.statusCode}`))
|
|
reject(new Error(`HTTP ${res.statusCode}`))
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
|
if (loading) uni.hideLoading()
|
|
if (loading) uni.hideLoading()
|
|
|
- uni.showToast({ title: '网络请求失败', icon: 'none' })
|
|
|
|
|
- reject(err)
|
|
|
|
|
|
|
+ const msg = getNetworkErrorMessage(err)
|
|
|
|
|
+ uni.showToast({ title: msg, icon: 'none' })
|
|
|
|
|
+ reject(new Error(msg))
|
|
|
},
|
|
},
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|