2
0
Эх сурвалжийг харах

📝 docs(mini): 更新API文档和目录结构说明

- 修改项目目录结构说明,将API封装文件从utils/api.ts移至api.ts
- 更新认证相关API路径,将密码登录接口从`/api/v1/auth/login/password`调整为`/api/v1/auth/login`
- 更新API调用示例代码,反映新的API客户端使用方式

♻️ refactor(mini): 重构API客户端和认证工具

- 将API封装从utils/api.ts迁移到api.ts,重命名为authClient
- 更新auth.ts中API调用方式,使用新的authClient
- 使用hono的InferResponseType自动推断User类型,移除手动定义的User接口
- 增强API请求错误处理,添加状态码检查和错误抛出
- 统一API请求参数格式,使用json和param字段明确传递数据
yourname 4 сар өмнө
parent
commit
af1671b3be
2 өөрчлөгдсөн 39 нэмэгдсэн , 23 устгасан
  1. 13 8
      mini/README.md
  2. 26 15
      mini/src/utils/auth.ts

+ 13 - 8
mini/README.md

@@ -29,8 +29,8 @@ mini/
 │   │   ├── login/          # 登录页
 │   │   └── register/       # 注册页
 │   ├── utils/
-│   │   ├── api.ts          # API封装
 │   │   └── auth.ts         # 认证工具
+│   ├── api.ts              # API客户端
 │   └── app.config.ts       # 小程序配置
 ├── config/
 │   ├── dev.ts              # 开发配置
@@ -82,7 +82,7 @@ npm run build:h5
 ## API接口
 
 ### 认证相关
-- POST `/api/v1/auth/login/password` - 密码登录
+- POST `/api/v1/auth/login` - 密码登录
 - POST `/api/v1/auth/register` - 用户注册
 - GET `/api/v1/auth/me` - 获取当前用户信息
 - POST `/api/v1/auth/logout` - 退出登录
@@ -130,19 +130,24 @@ await authManager.logout()
 使用封装的API客户端进行网络请求:
 
 ```typescript
-import { authApi, userApi } from '@/utils/api'
+import { authClient } from '@/api'
 
 // 登录
-const response = await authApi.login({
-  username: 'username',
-  password: 'password'
+const response = await authClient.login.$post({
+  json: {
+    username: 'username',
+    password: 'password'
+  }
 })
 
 // 获取用户列表
-const users = await userApi.getUsers()
+const users = await userClient.$get({})
 
 // 更新用户信息
-const updated = await userApi.updateUser(1, { username: 'newname' })
+const updated = await userClient[':id'].$put({
+  param: { id: 1 },
+  json: { username: 'newname' }
+})
 ```
 
 ## 环境配置

+ 26 - 15
mini/src/utils/auth.ts

@@ -1,15 +1,9 @@
 import Taro from '@tarojs/taro'
-import { authApi } from '../api'
+import { authClient } from '../api'
+import { InferResponseType } from 'hono'
 
 // 用户类型定义
-export interface User {
-  id: number
-  username: string
-  email?: string
-  avatar?: string
-  createdAt: string
-  updatedAt: string
-}
+export type User = InferResponseType<typeof authClient.me.$get, 200>
 
 // 认证状态管理
 class AuthManager {
@@ -61,8 +55,13 @@ class AuthManager {
   // 登录
   async login(username: string, password: string): Promise<User> {
     try {
-      const response = await authApi.login({ username, password })
-      const { token, user } = response
+      const response = await authClient.login.$post({
+        json: { username, password }
+      })
+      if (response.status !== 200) {
+        throw new Error('登录失败')
+      }
+      const { token, user } = await response.json()
       
       this.setToken(token)
       this.setUserInfo(user)
@@ -80,8 +79,13 @@ class AuthManager {
     email?: string
   }): Promise<User> {
     try {
-      const response = await authApi.register(data)
-      const { token, user } = response
+      const response = await authClient.register.$post({
+        json: data
+      })
+      if (response.status !== 201) {
+        throw new Error('注册失败')
+      }
+      const { token, user } = await response.json()
       
       this.setToken(token)
       this.setUserInfo(user)
@@ -95,7 +99,10 @@ class AuthManager {
   // 登出
   async logout(): Promise<void> {
     try {
-      await authApi.logout()
+      const response = await authClient.logout.$post({})
+      if (response.status !== 200) {
+        throw new Error('登出失败')
+      }
     } catch (error) {
       console.error('Logout error:', error)
     } finally {
@@ -112,7 +119,11 @@ class AuthManager {
     }
 
     try {
-      const user = await authApi.getCurrentUser()
+      const response = await authClient.me.$get({})
+      if (response.status !== 200) {
+        throw new Error('获取用户信息失败')
+      }
+      const user = await response.json()
       this.setUserInfo(user)
       return user
     } catch (error) {