Bladeren bron

refactor(api): 将接口类型定义移至独立文件并优化API响应处理

- 将原本在axios.ts中的接口类型定义提取到独立的models/index.ts文件
- 修改API调用逻辑,直接返回响应数据而非整个响应对象
- 保持原有功能不变,提高代码可维护性和可读性
Sakulin 2 maanden geleden
bovenliggende
commit
d0da05c0f1
2 gewijzigde bestanden met toevoegingen van 93 en 80 verwijderingen
  1. 75 0
      src/models/index.ts
  2. 18 80
      src/utils/axios.ts

+ 75 - 0
src/models/index.ts

@@ -0,0 +1,75 @@
+export interface AuthSuccess {
+  code: 200
+  token: string
+}
+
+export interface AuthFailed {
+  code: 401
+  msg: string
+}
+
+export interface PostListSuccess {
+  code: number
+  data: PostSketch[]
+}
+
+export interface PostSketch {
+  id: number
+  title: string
+  createdAt: string
+  updatedAt: string
+  cate: string
+  description: string
+  tags: Tag[]
+}
+
+export interface Tag {
+  name: string
+  id: number
+  color: string
+}
+
+export interface TagList {
+  code: number
+  data: Tag[]
+}
+
+export interface PostGetSuccess {
+  code: 200
+  data: PostDetail
+}
+
+export interface PostDetail {
+  id: number
+  title: string
+  content: string
+  createdAt: string
+  updatedAt: string
+  cate: string
+  description: string
+  tags: Tag[]
+}
+
+export interface DefaultFailedResponse {
+  code: 404
+  msg: string
+}
+
+export interface PostBody {
+  title: string
+  content: string
+  tags: string[]
+}
+
+export interface PushSuccess {
+  code: number
+  msg: string
+  data: {
+    id: number
+  }
+}
+
+export interface UpdateSuccess {
+  code: 200,
+  msg: string
+}

+ 18 - 80
src/utils/axios.ts

@@ -1,3 +1,14 @@
+import type {
+  AuthSuccess,
+  AuthFailed,
+  PostListSuccess,
+  PostGetSuccess,
+  DefaultFailedResponse,
+  TagList,
+  PostBody,
+  PushSuccess,
+  UpdateSuccess,
+} from '@/models'
 import axios from 'axios'
 
 let token = ''
@@ -9,82 +20,9 @@ const baseServer = axios.create({
   },
 })
 
-export interface AuthSuccess {
-  code: 200
-  token: string
-}
-
-export interface AuthFailed {
-  code: 401
-  msg: string
-}
-
-export interface PostListSuccess {
-  code: number
-  data: Datum[]
-}
-
-export interface Datum {
-  id: number
-  title: string
-  createdAt: string
-  updatedAt: string
-  tags: Tag[]
-}
-
-export interface Tag {
-  name: string
-  id: number
-  color: string
-}
-
-export interface TagList {
-  code: number
-  data: Tag[]
-}
-
-export interface PostGetSuccess {
-  code: number
-  data: PostDetail
-}
-
-export interface PostDetail {
-  id: number
-  title: string
-  content: string
-  createdAt: string
-  updatedAt: string
-  tags: Tag[]
-}
-
-export interface DefaultFailedResponse {
-  code: 404
-  msg: string
-}
-
-export interface PostBody {
-  title: string
-  content: string
-  tags: string[]
-}
-
-export interface PushSuccess {
-  code: number
-  msg: string
-  data: {
-    id: number
-  }
-}
-
-export interface UpdateSuccess {
-  code: 200,
-  msg: string
-}
-
-
 export const api = {
   auth(password: string) {
-    return (baseServer.post('/auth/', { password }) as Promise<AuthSuccess | AuthFailed>).then(
+    return (baseServer.post('/auth/', { password }).then(res => res.data) as Promise<AuthSuccess | AuthFailed>).then(
       (data) => {
         if (data.code == 200) token = data.token
         return data
@@ -92,18 +30,18 @@ export const api = {
     )
   },
   postList(length: number = 10, start: number = 10) {
-    return baseServer.get(`/post/list?length=${length}&start=${start}`) as Promise<PostListSuccess>
+    return baseServer.get(`/post/list?length=${length}&start=${start}`).then(res => res.data) as Promise<PostListSuccess>
   },
   postGet(id: number = 2) {
-    return baseServer.get(`/post/get/${id}`) as Promise<PostGetSuccess | DefaultFailedResponse>
+    return baseServer.get(`/post/get/${id}`).then(res => res.data) as Promise<PostGetSuccess | DefaultFailedResponse>
   },
   tagList() {
-    return baseServer.get('/tag/list') as Promise<TagList>
+    return baseServer.get('/tag/list').then(res => res.data) as Promise<TagList>
   },
   postPush(body: PostBody) {
-    return baseServer.post('/post/push', body) as Promise<PushSuccess | AuthFailed>
+    return baseServer.post('/post/push', body).then(res => res.data) as Promise<PushSuccess | AuthFailed>
   },
   postEdit(id: number, body: PostBody) {
-    return baseServer.post(`/post/edit/${id}`, body) as Promise<UpdateSuccess | AuthFailed>
-  }
+    return baseServer.post(`/post/edit/${id}`, body).then(res => res.data) as Promise<UpdateSuccess | AuthFailed>
+  },
 }