11import type { IDoubleTokenRes } from '@/api/types/login'
2- import type { CustomRequestOptions } from '@/http/types'
2+ import type { CustomRequestOptions , IResponse } from '@/http/types'
33import { nextTick } from 'vue'
44import { LOGIN_PAGE } from '@/router/config'
55import { useTokenStore } from '@/store/token'
66import { isDoubleTokenMode } from '@/utils'
7+ import { ResultEnum } from './tools/enum'
78
89// 刷新 token 状态管理
910let refreshing = false // 防止重复刷新 token 标识
1011let taskQueue : ( ( ) => void ) [ ] = [ ] // 刷新 token 请求队列
1112
1213export function http < T > ( options : CustomRequestOptions ) {
1314 // 1. 返回 Promise 对象
14- return new Promise < IResData < T > > ( ( resolve , reject ) => {
15+ return new Promise < T > ( ( resolve , reject ) => {
1516 uni . request ( {
1617 ...options ,
1718 dataType : 'json' ,
@@ -22,8 +23,12 @@ export function http<T>(options: CustomRequestOptions) {
2223 success : async ( res ) => {
2324 // 状态码 2xx,参考 axios 的设计
2425 if ( res . statusCode >= 200 && res . statusCode < 300 ) {
25- // 2.1 提取核心数据 res.data
26- return resolve ( res . data as IResData < T > )
26+ // 2.1 处理业务逻辑错误
27+ const { code, message, data } = res . data as IResponse < T >
28+ if ( code !== ResultEnum . Success ) {
29+ throw new Error ( `请求错误[${ code } ]:${ message } ` )
30+ }
31+ return resolve ( data as T )
2732 }
2833 const resData : IResData < T > = res . data as IResData < T >
2934 if ( ( res . statusCode === 401 ) || ( resData . code === 401 ) ) {
0 commit comments