菲鸽 2 years ago
parent
commit
67bee60ed3
2 changed files with 13 additions and 12 deletions
  1. 9 0
      src/typings.d.ts
  2. 4 12
      src/utils/http.ts

+ 9 - 0
src/typings.d.ts

@@ -1,3 +1,11 @@
+// 全局要用的类型放到这里
+
+export type IResData<T> = {
+  code: number
+  msg: string
+  result: T
+}
+
 export type UserInfo = {
 export type UserInfo = {
   nickname?: string
   nickname?: string
   avatar?: string
   avatar?: string
@@ -5,6 +13,7 @@ export type UserInfo = {
   openid?: string
   openid?: string
   token?: string
   token?: string
 }
 }
+
 export type UserItem = {
 export type UserItem = {
   username: string
   username: string
   age: number
   age: number

+ 4 - 12
src/utils/http.ts

@@ -1,16 +1,10 @@
 /* eslint-disable no-param-reassign */
 /* eslint-disable no-param-reassign */
 import qs from 'qs'
 import qs from 'qs'
 import { useUserStore } from '@/store'
 import { useUserStore } from '@/store'
-import { UserInfo } from '@/typings'
+import { IResData, UserInfo } from '@/typings'
 
 
 type CustomRequestOptions = UniApp.RequestOptions & { query?: Record<string, any> }
 type CustomRequestOptions = UniApp.RequestOptions & { query?: Record<string, any> }
 
 
-type Data<T> = {
-  code: number
-  msg: string
-  result: T
-}
-
 // 请求基地址
 // 请求基地址
 const baseURL = import.meta.env.VITE_SERVER_BASEURL
 const baseURL = import.meta.env.VITE_SERVER_BASEURL
 // console.log(import.meta.env)
 // console.log(import.meta.env)
@@ -19,8 +13,6 @@ const baseURL = import.meta.env.VITE_SERVER_BASEURL
 const httpInterceptor = {
 const httpInterceptor = {
   // 拦截前触发
   // 拦截前触发
   invoke(options: CustomRequestOptions) {
   invoke(options: CustomRequestOptions) {
-    console.log(options)
-
     // 接口请求支持通过 query 参数配置 queryString
     // 接口请求支持通过 query 参数配置 queryString
     if (options.query) {
     if (options.query) {
       const queryStr = qs.stringify(options.query)
       const queryStr = qs.stringify(options.query)
@@ -58,7 +50,7 @@ uni.addInterceptor('uploadFile', httpInterceptor)
 
 
 export const http = <T>(options: CustomRequestOptions) => {
 export const http = <T>(options: CustomRequestOptions) => {
   // 1. 返回 Promise 对象
   // 1. 返回 Promise 对象
-  return new Promise<Data<T>>((resolve, reject) => {
+  return new Promise<IResData<T>>((resolve, reject) => {
     uni.request({
     uni.request({
       ...options,
       ...options,
       dataType: 'json',
       dataType: 'json',
@@ -68,7 +60,7 @@ export const http = <T>(options: CustomRequestOptions) => {
         // 状态码 2xx,参考 axios 的设计
         // 状态码 2xx,参考 axios 的设计
         if (res.statusCode >= 200 && res.statusCode < 300) {
         if (res.statusCode >= 200 && res.statusCode < 300) {
           // 2.1 提取核心数据 res.data
           // 2.1 提取核心数据 res.data
-          resolve(res.data as Data<T>)
+          resolve(res.data as IResData<T>)
         } else if (res.statusCode === 401) {
         } else if (res.statusCode === 401) {
           // 401错误  -> 清理用户信息,跳转到登录页
           // 401错误  -> 清理用户信息,跳转到登录页
           // userStore.clearUserInfo()
           // userStore.clearUserInfo()
@@ -78,7 +70,7 @@ export const http = <T>(options: CustomRequestOptions) => {
           // 其他错误 -> 根据后端错误信息轻提示
           // 其他错误 -> 根据后端错误信息轻提示
           uni.showToast({
           uni.showToast({
             icon: 'none',
             icon: 'none',
-            title: (res.data as Data<T>).msg || '请求错误',
+            title: (res.data as IResData<T>).msg || '请求错误',
           })
           })
           reject(res)
           reject(res)
         }
         }