|
@@ -1,12 +1,8 @@
|
|
|
-import {
|
|
|
|
|
- BASE_API,
|
|
|
|
|
- MODE,
|
|
|
|
|
- NOT_OAUTH_BASIC_TOKEN,
|
|
|
|
|
- OAUTH_BASIC_TOKEN,
|
|
|
|
|
- SUCCESS_CODE_LIST,
|
|
|
|
|
- TIMEOUT,
|
|
|
|
|
-} from './config'
|
|
|
|
|
-import { getToken } from './tokenProvider'
|
|
|
|
|
|
|
+import { useUserStoreWithOut } from '@/stores/modules/user'
|
|
|
|
|
+
|
|
|
|
|
+import { hideGlobalLoading, showGlobalLoading } from '@/utils/loading'
|
|
|
|
|
+
|
|
|
|
|
+import { BASE_API, MODE, NOT_OAUTH_BASIC_TOKEN, OAUTH_BASIC_TOKEN, TIMEOUT } from './config'
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
/* 类型 */
|
|
/* 类型 */
|
|
@@ -22,6 +18,8 @@ export interface BaseOptions {
|
|
|
header?: UniApp.RequestOptions['header']
|
|
header?: UniApp.RequestOptions['header']
|
|
|
timeout?: number
|
|
timeout?: number
|
|
|
silent?: boolean
|
|
silent?: boolean
|
|
|
|
|
+ loading?: boolean // 是否显示 loading(默认 false)
|
|
|
|
|
+ loadingText?: string // loading 文字描述
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export interface RequestConfig extends BaseOptions {
|
|
export interface RequestConfig extends BaseOptions {
|
|
@@ -35,6 +33,11 @@ export interface RequestConfig extends BaseOptions {
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
export function request<T = any>(config: RequestConfig): Promise<ApiResponse<T>> {
|
|
export function request<T = any>(config: RequestConfig): Promise<ApiResponse<T>> {
|
|
|
|
|
+ const needLoading = config.loading === true
|
|
|
|
|
+ if (needLoading) {
|
|
|
|
|
+ showGlobalLoading(config.loadingText)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
uni.request({
|
|
uni.request({
|
|
|
url: resolveUrl(config.url),
|
|
url: resolveUrl(config.url),
|
|
@@ -44,32 +47,67 @@ export function request<T = any>(config: RequestConfig): Promise<ApiResponse<T>>
|
|
|
header: buildHeaders(config.url, config.header),
|
|
header: buildHeaders(config.url, config.header),
|
|
|
|
|
|
|
|
success(res) {
|
|
success(res) {
|
|
|
- const result = res.data as ApiResponse<T>
|
|
|
|
|
|
|
+ const statusCode = res.statusCode ?? 0
|
|
|
|
|
+ const body = res.data as ApiResponse<T>
|
|
|
|
|
|
|
|
- // 白名单接口:不校验 code
|
|
|
|
|
|
|
+ // 白名单接口:完全放行
|
|
|
if (isWhiteList(config.url)) {
|
|
if (isWhiteList(config.url)) {
|
|
|
- return resolve(result)
|
|
|
|
|
|
|
+ return resolve(body)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 仅校验 HTTP 状态码
|
|
|
|
|
+ if (statusCode === 200) {
|
|
|
|
|
+ return resolve(body)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 401:统一登出
|
|
|
|
|
+ if (statusCode === 401) {
|
|
|
|
|
+ handleUnauthorized()
|
|
|
|
|
+ return reject(normalizeHttpError(res, config.url))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 业务失败
|
|
|
|
|
- if (!SUCCESS_CODE_LIST.includes(result.code)) {
|
|
|
|
|
- return reject(result)
|
|
|
|
|
|
|
+ // 其他 HTTP 错误
|
|
|
|
|
+ if (!config.silent) {
|
|
|
|
|
+ uni.showModal({
|
|
|
|
|
+ title: '提示',
|
|
|
|
|
+ content: getErrorMessage(body, statusCode),
|
|
|
|
|
+ showCancel: false,
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- resolve(result)
|
|
|
|
|
|
|
+ reject(normalizeHttpError(res, config.url))
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
fail(err) {
|
|
fail(err) {
|
|
|
|
|
+ if (!config.silent) {
|
|
|
|
|
+ uni.showModal({
|
|
|
|
|
+ title: '提示',
|
|
|
|
|
+ content: (err as any)?.errMsg || '网络异常,请稍后再试',
|
|
|
|
|
+ showCancel: false,
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
reject(err)
|
|
reject(err)
|
|
|
},
|
|
},
|
|
|
|
|
+ complete() {
|
|
|
|
|
+ if (needLoading) {
|
|
|
|
|
+ hideGlobalLoading()
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * 后端不包 ApiResponse 的接口
|
|
|
|
|
- */
|
|
|
|
|
|
|
+/* -------------------------------------------------------------------------- */
|
|
|
|
|
+/* Raw request */
|
|
|
|
|
+/* -------------------------------------------------------------------------- */
|
|
|
|
|
+
|
|
|
export function requestRaw<T = any>(config: RequestConfig): Promise<T> {
|
|
export function requestRaw<T = any>(config: RequestConfig): Promise<T> {
|
|
|
|
|
+ const needLoading = config.loading === true
|
|
|
|
|
+
|
|
|
|
|
+ if (needLoading) {
|
|
|
|
|
+ showGlobalLoading(config.loadingText)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
uni.request({
|
|
uni.request({
|
|
|
url: resolveUrl(config.url),
|
|
url: resolveUrl(config.url),
|
|
@@ -79,12 +117,44 @@ export function requestRaw<T = any>(config: RequestConfig): Promise<T> {
|
|
|
header: buildHeaders(config.url, config.header),
|
|
header: buildHeaders(config.url, config.header),
|
|
|
|
|
|
|
|
success(res) {
|
|
success(res) {
|
|
|
- resolve(res.data as T)
|
|
|
|
|
|
|
+ const statusCode = res.statusCode ?? 0
|
|
|
|
|
+
|
|
|
|
|
+ if (isWhiteList(config.url) || statusCode === 200) {
|
|
|
|
|
+ return resolve(res.data as T)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (statusCode === 401) {
|
|
|
|
|
+ handleUnauthorized()
|
|
|
|
|
+ return reject(normalizeHttpError(res, config.url))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!config.silent) {
|
|
|
|
|
+ uni.showModal({
|
|
|
|
|
+ title: '提示',
|
|
|
|
|
+ content: `请求失败(HTTP ${statusCode})`,
|
|
|
|
|
+ showCancel: false,
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ reject(normalizeHttpError(res, config.url))
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
fail(err) {
|
|
fail(err) {
|
|
|
|
|
+ if (!config.silent) {
|
|
|
|
|
+ uni.showModal({
|
|
|
|
|
+ title: '提示',
|
|
|
|
|
+ content: (err as any)?.errMsg || '网络异常,请稍后再试',
|
|
|
|
|
+ showCancel: false,
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
reject(err)
|
|
reject(err)
|
|
|
},
|
|
},
|
|
|
|
|
+
|
|
|
|
|
+ complete() {
|
|
|
|
|
+ if (needLoading) {
|
|
|
|
|
+ hideGlobalLoading()
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
@@ -139,7 +209,8 @@ function buildHeaders(url: string, extraHeader?: UniApp.RequestOptions['header']
|
|
|
...(extraHeader || {}),
|
|
...(extraHeader || {}),
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const accessToken = getToken()
|
|
|
|
|
|
|
+ const userStore = useUserStoreWithOut()
|
|
|
|
|
+ const accessToken = userStore.access_token
|
|
|
|
|
|
|
|
if (url.includes('auth/mobile/token/sms')) {
|
|
if (url.includes('auth/mobile/token/sms')) {
|
|
|
headers.Authorization = NOT_OAUTH_BASIC_TOKEN
|
|
headers.Authorization = NOT_OAUTH_BASIC_TOKEN
|
|
@@ -152,3 +223,29 @@ function buildHeaders(url: string, extraHeader?: UniApp.RequestOptions['header']
|
|
|
|
|
|
|
|
return headers
|
|
return headers
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+function handleUnauthorized() {
|
|
|
|
|
+ const userStore = useUserStoreWithOut()
|
|
|
|
|
+ userStore.logout()
|
|
|
|
|
+
|
|
|
|
|
+ uni.reLaunch({
|
|
|
|
|
+ url: '/pages/login/index',
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function getErrorMessage(body: unknown, statusCode: number) {
|
|
|
|
|
+ if (body && typeof body === 'object') {
|
|
|
|
|
+ const msg = (body as any).msg
|
|
|
|
|
+ if (typeof msg === 'string' && msg.trim()) return msg
|
|
|
|
|
+ }
|
|
|
|
|
+ return `请求失败(HTTP ${statusCode})`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function normalizeHttpError(res: UniApp.RequestSuccessCallbackResult, url: string) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ url,
|
|
|
|
|
+ statusCode: res.statusCode ?? 0,
|
|
|
|
|
+ data: res.data,
|
|
|
|
|
+ header: res.header,
|
|
|
|
|
+ }
|
|
|
|
|
+}
|