|
|
@@ -1,35 +1,111 @@
|
|
|
+import type { uniappRequestAdapter } from '@alova/adapter-uniapp'
|
|
|
+import type { IResponse } from './types'
|
|
|
import AdapterUniapp from '@alova/adapter-uniapp'
|
|
|
import { createAlova } from 'alova'
|
|
|
+import { createServerTokenAuthentication } from 'alova/client'
|
|
|
+import VueHook from 'alova/vue'
|
|
|
+import { toast } from '@/utils/toast'
|
|
|
+import { ContentTypeEnum, ResultEnum, ShowMessage } from './enum'
|
|
|
|
|
|
-const baseURL = JSON.parse(__VITE_APP_PROXY__)
|
|
|
- ? import.meta.env.VITE_APP_PROXY_PREFIX
|
|
|
- : import.meta.env.VITE_SERVER_BASEURL
|
|
|
+// 配置动态Tag
|
|
|
+export const API_DOMAINS = {
|
|
|
+ DEFAULT: import.meta.env.VITE_SERVER_BASEURL,
|
|
|
+ SECONDARY: import.meta.env.VITE_API_SECONDARY_URL,
|
|
|
+}
|
|
|
|
|
|
-export const http = createAlova({
|
|
|
- baseURL,
|
|
|
+/**
|
|
|
+ * 创建请求实例
|
|
|
+ */
|
|
|
+const { onAuthRequired, onResponseRefreshToken } = createServerTokenAuthentication<
|
|
|
+ typeof VueHook,
|
|
|
+ typeof uniappRequestAdapter
|
|
|
+>({
|
|
|
+ refreshTokenOnError: {
|
|
|
+ isExpired: (error) => {
|
|
|
+ return error.response?.status === ResultEnum.Unauthorized
|
|
|
+ },
|
|
|
+ handler: async () => {
|
|
|
+ try {
|
|
|
+ // await authLogin();
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ // 切换到登录页
|
|
|
+ await uni.reLaunch({ url: '/pages/common/login/index' })
|
|
|
+ throw error
|
|
|
+ }
|
|
|
+ },
|
|
|
+ },
|
|
|
+})
|
|
|
+
|
|
|
+/**
|
|
|
+ * alova 请求实例
|
|
|
+ */
|
|
|
+const alovaInstance = createAlova({
|
|
|
+ baseURL: import.meta.env.VITE_API_BASE_URL,
|
|
|
...AdapterUniapp(),
|
|
|
- async responded(res: UniApp.RequestSuccessCallbackResult, method) {
|
|
|
- console.log('responded:', method, res)
|
|
|
- // 请求成功的拦截器
|
|
|
- // 状态码 2xx,参考 axios 的设计
|
|
|
- const resData = res.data as IResData<any>
|
|
|
- if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
- // 2.1 提取核心数据 res.data
|
|
|
- return resData.data
|
|
|
+ timeout: 5000,
|
|
|
+ statesHook: VueHook,
|
|
|
+
|
|
|
+ beforeRequest: onAuthRequired((method) => {
|
|
|
+ // 设置默认 Content-Type
|
|
|
+ method.config.headers = {
|
|
|
+ ContentType: ContentTypeEnum.JSON,
|
|
|
+ Accept: 'application/json, text/plain, */*',
|
|
|
+ ...method.config.headers,
|
|
|
}
|
|
|
- else if (res.statusCode === 401) {
|
|
|
- // 401错误 -> 清理用户信息,跳转到登录页
|
|
|
- // userStore.clearUserInfo()
|
|
|
- // uni.navigateTo({ url: '/pages/login/login' })
|
|
|
- console.log(res)
|
|
|
- throw new Error(resData.msg || '401错误')
|
|
|
+
|
|
|
+ const { config } = method
|
|
|
+ const ignoreAuth = !config.meta?.ignoreAuth
|
|
|
+ console.log('ignoreAuth===>', ignoreAuth)
|
|
|
+ // 处理认证信息 自行处理认证问题
|
|
|
+ if (ignoreAuth) {
|
|
|
+ const token = 'getToken()'
|
|
|
+ if (!token) {
|
|
|
+ throw new Error('[请求错误]:未登录')
|
|
|
+ }
|
|
|
+ // method.config.headers.token = token;
|
|
|
}
|
|
|
- else {
|
|
|
- uni.showToast({
|
|
|
- icon: 'none',
|
|
|
- title: (resData).msg || '请求错误',
|
|
|
- })
|
|
|
- throw new Error(resData.msg || '请求错误')
|
|
|
+
|
|
|
+ // 处理动态域名
|
|
|
+ if (config.meta?.domain) {
|
|
|
+ method.baseURL = config.meta.domain
|
|
|
+ console.log('当前域名', method.baseURL)
|
|
|
}
|
|
|
- },
|
|
|
+ }),
|
|
|
+
|
|
|
+ responded: onResponseRefreshToken((response, method) => {
|
|
|
+ const { config } = method
|
|
|
+ const { requestType } = config
|
|
|
+ const {
|
|
|
+ statusCode,
|
|
|
+ data: rawData,
|
|
|
+ errMsg,
|
|
|
+ } = response as UniNamespace.RequestSuccessCallbackResult
|
|
|
+
|
|
|
+ // 处理特殊请求类型(上传/下载)
|
|
|
+ if (requestType === 'upload' || requestType === 'download') {
|
|
|
+ return response
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理 HTTP 状态码错误
|
|
|
+ if (statusCode !== 200) {
|
|
|
+ const errorMessage = ShowMessage(statusCode) || `HTTP请求错误[${statusCode}]`
|
|
|
+ console.error('errorMessage===>', errorMessage)
|
|
|
+ toast.error(errorMessage)
|
|
|
+ throw new Error(`${errorMessage}:${errMsg}`)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理业务逻辑错误
|
|
|
+ const { code, message, data } = rawData as IResponse
|
|
|
+ if (code !== ResultEnum.Success) {
|
|
|
+ if (config.meta?.toast !== false) {
|
|
|
+ toast.warning(message)
|
|
|
+ }
|
|
|
+ throw new Error(`请求错误[${code}]:${message}`)
|
|
|
+ }
|
|
|
+ // 处理成功响应,返回业务数据
|
|
|
+ return data
|
|
|
+ }),
|
|
|
})
|
|
|
+
|
|
|
+export const http = alovaInstance
|