Kaynağa Gözat

♻️ refactor(auth): 拆分微信绑定和解绑路由文件

- 将微信绑定和解绑功能拆分为独立文件(wechat-bind.ts和wechat-unbind.ts)
- 调整导入方式,从单独导入函数改为导入路由实例
- 保持原有功能逻辑不变,优化代码组织结构
yourname 6 ay önce
ebeveyn
işleme
09aaf1f40e

+ 2 - 1
src/server/api/auth/wechat/index.ts

@@ -1,7 +1,8 @@
 import { OpenAPIHono } from '@hono/zod-openapi';
 import authorizeRoute from './wechat-authorize';
 import loginRoute from './wechat-login';
-import { bindApp, unbindApp } from './wechat-bind';
+import bindApp from './wechat-bind';
+import unbindApp from './wechat-unbind';
 
 const app = new OpenAPIHono()
   .route('/', authorizeRoute)

+ 2 - 51
src/server/api/auth/wechat/wechat-bind.ts

@@ -62,39 +62,7 @@ const bindWechatRoute = createRoute({
   }
 });
 
-const unbindWechatRoute = createRoute({
-  method: 'post',
-  path: '/unbind',
-  middleware: [authMiddleware],
-  responses: {
-    200: {
-      description: '解绑微信账号成功',
-      content: {
-        'application/json': {
-          schema: UserSchema
-        }
-      }
-    },
-    400: {
-      description: '请求参数错误',
-      content: {
-        'application/json': {
-          schema: ErrorSchema
-        }
-      }
-    },
-    401: {
-      description: '认证失败',
-      content: {
-        'application/json': {
-          schema: ErrorSchema
-        }
-      }
-    }
-  }
-});
-
-const bindApp = new OpenAPIHono<AuthContext>().openapi(bindWechatRoute, async (c) => {
+const app = new OpenAPIHono<AuthContext>().openapi(bindWechatRoute, async (c) => {
   try {
     const user = c.get('user');
     const { code } = await c.req.json();
@@ -112,21 +80,4 @@ const bindApp = new OpenAPIHono<AuthContext>().openapi(bindWechatRoute, async (c
   }
 });
 
-const unbindApp = new OpenAPIHono<AuthContext>().openapi(unbindWechatRoute, async (c) => {
-  try {
-    const user = c.get('user');
-    const result = await wechatAuthService.unbindWechatFromUser(user.id);
-    
-    // 使用 parseWithAwait 确保返回数据符合Schema定义
-    const validatedResult = await parseWithAwait(UserSchema, result);
-    
-    return c.json(validatedResult, 200);
-  } catch (error) {
-    return c.json({ 
-      code: 400, 
-      message: error instanceof Error ? error.message : '解绑微信账号失败' 
-    }, 400);
-  }
-});
-
-export { bindApp, unbindApp };
+export default app;

+ 65 - 0
src/server/api/auth/wechat/wechat-unbind.ts

@@ -0,0 +1,65 @@
+import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
+import { WechatAuthService } from '@/server/modules/wechat/wechat-auth.service';
+import { ErrorSchema } from '@/server/utils/errorHandler';
+import { AppDataSource } from '@/server/data-source';
+import { UserService } from '@/server/modules/users/user.service';
+import { AuthService } from '@/server/modules/auth/auth.service';
+import { UserSchema } from '@/server/modules/users/user.schema';
+import { authMiddleware } from '@/server/middleware/auth.middleware';
+import { AuthContext } from '@/server/types/context';
+import { parseWithAwait } from '@/server/utils/parseWithAwait';
+
+const userService = new UserService(AppDataSource);
+const authService = new AuthService(userService);
+const wechatAuthService = new WechatAuthService(userService, authService);
+
+const unbindWechatRoute = createRoute({
+  method: 'post',
+  path: '/unbind',
+  middleware: [authMiddleware],
+  responses: {
+    200: {
+      description: '解绑微信账号成功',
+      content: {
+        'application/json': {
+          schema: UserSchema
+        }
+      }
+    },
+    400: {
+      description: '请求参数错误',
+      content: {
+        'application/json': {
+          schema: ErrorSchema
+        }
+      }
+    },
+    401: {
+      description: '认证失败',
+      content: {
+        'application/json': {
+          schema: ErrorSchema
+        }
+      }
+    }
+  }
+});
+
+const app = new OpenAPIHono<AuthContext>().openapi(unbindWechatRoute, async (c) => {
+  try {
+    const user = c.get('user');
+    const result = await wechatAuthService.unbindWechatFromUser(user.id);
+    
+    // 使用 parseWithAwait 确保返回数据符合Schema定义
+    const validatedResult = await parseWithAwait(UserSchema, result);
+    
+    return c.json(validatedResult, 200);
+  } catch (error) {
+    return c.json({ 
+      code: 400, 
+      message: error instanceof Error ? error.message : '解绑微信账号失败' 
+    }, 400);
+  }
+});
+
+export default app;