|
|
@@ -1,8 +1,16 @@
|
|
|
-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'
|
|
|
+import { TIMEOUT } from './config'
|
|
|
+import {
|
|
|
+ buildHeaders,
|
|
|
+ getErrorMessage,
|
|
|
+ handleUnauthorized,
|
|
|
+ isWhiteList,
|
|
|
+ normalizeHttpError,
|
|
|
+ resetUnauthorizedHandled,
|
|
|
+ resolveUrl,
|
|
|
+ setAuthHandlers,
|
|
|
+} from './utils'
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
/* 类型 */
|
|
|
@@ -50,27 +58,23 @@ export function request<T = any>(config: RequestConfig): Promise<ApiResponse<T>>
|
|
|
const statusCode = res.statusCode ?? 0
|
|
|
const body = res.data as ApiResponse<T>
|
|
|
|
|
|
- // 白名单接口:完全放行
|
|
|
if (isWhiteList(config.url)) {
|
|
|
return resolve(body)
|
|
|
}
|
|
|
|
|
|
- // 仅校验 HTTP 状态码
|
|
|
if (statusCode === 200) {
|
|
|
return resolve(body)
|
|
|
}
|
|
|
|
|
|
- // 401:统一登出
|
|
|
if (statusCode === 401) {
|
|
|
handleUnauthorized()
|
|
|
return reject(normalizeHttpError(res, config.url))
|
|
|
}
|
|
|
|
|
|
- // 其他 HTTP 错误
|
|
|
if (!config.silent) {
|
|
|
uni.showModal({
|
|
|
title: '提示',
|
|
|
- content: getErrorMessage(body, statusCode),
|
|
|
+ content: getErrorMessage(body),
|
|
|
showCancel: false,
|
|
|
})
|
|
|
}
|
|
|
@@ -131,7 +135,7 @@ export function requestRaw<T = any>(config: RequestConfig): Promise<T> {
|
|
|
if (!config.silent) {
|
|
|
uni.showModal({
|
|
|
title: '提示',
|
|
|
- content: `请求失败(HTTP ${statusCode})`,
|
|
|
+ content: getErrorMessage(res.data as any),
|
|
|
showCancel: false,
|
|
|
})
|
|
|
}
|
|
|
@@ -183,69 +187,4 @@ const http = {
|
|
|
|
|
|
export default http
|
|
|
|
|
|
-/* -------------------------------------------------------------------------- */
|
|
|
-/* 工具函数 */
|
|
|
-/* -------------------------------------------------------------------------- */
|
|
|
-
|
|
|
-function resolveUrl(url: string) {
|
|
|
- // #ifdef MP
|
|
|
- return BASE_API + url
|
|
|
- // #endif
|
|
|
-
|
|
|
- // #ifdef H5
|
|
|
- if (MODE === 'development') return url
|
|
|
- return BASE_API + url
|
|
|
- // #endif
|
|
|
-
|
|
|
- return BASE_API + url
|
|
|
-}
|
|
|
-
|
|
|
-function isWhiteList(url: string) {
|
|
|
- return ['/auth/oauth/token'].some((item) => url.includes(item))
|
|
|
-}
|
|
|
-
|
|
|
-function buildHeaders(url: string, extraHeader?: UniApp.RequestOptions['header']) {
|
|
|
- const headers: Record<string, any> = {
|
|
|
- ...(extraHeader || {}),
|
|
|
- }
|
|
|
-
|
|
|
- const userStore = useUserStoreWithOut()
|
|
|
- const accessToken = userStore.access_token
|
|
|
-
|
|
|
- if (url.includes('auth/mobile/token/sms')) {
|
|
|
- headers.Authorization = NOT_OAUTH_BASIC_TOKEN
|
|
|
- } else if (url.includes('auth/oauth/token')) {
|
|
|
- headers.Authorization = OAUTH_BASIC_TOKEN
|
|
|
- headers['TENANT-ID'] = '1'
|
|
|
- } else if (accessToken) {
|
|
|
- headers.Authorization = `Bearer ${accessToken}`
|
|
|
- }
|
|
|
-
|
|
|
- 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,
|
|
|
- }
|
|
|
-}
|
|
|
+export { resetUnauthorizedHandled, setAuthHandlers }
|