post.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
  2. import { z } from '@hono/zod-openapi';
  3. import { MiniAuthService } from '../../../modules/auth/mini-auth.service';
  4. import { AppDataSource } from '../../../data-source';
  5. import { ErrorSchema } from '../../../utils/errorHandler';
  6. import { UserEntity } from '../../../modules/users/user.entity';
  7. const MiniLoginSchema = z.object({
  8. code: z.string().openapi({
  9. example: '08123456789012345678901234567890',
  10. description: '小程序登录code'
  11. }),
  12. userInfo: z.object({
  13. nickName: z.string().optional(),
  14. avatarUrl: z.string().optional()
  15. }).optional()
  16. });
  17. const MiniLoginResponseSchema = z.object({
  18. token: z.string().openapi({
  19. example: 'jwt.token.here',
  20. description: 'JWT Token'
  21. }),
  22. user: z.object({
  23. id: z.number(),
  24. username: z.string(),
  25. nickname: z.string().nullable(),
  26. phone: z.string().nullable(),
  27. email: z.string().nullable(),
  28. avatarFileId: z.number().nullable(),
  29. registrationSource: z.string()
  30. }),
  31. isNewUser: z.boolean().openapi({
  32. example: true,
  33. description: '是否为新注册用户'
  34. })
  35. });
  36. const miniLoginRoute = createRoute({
  37. method: 'post',
  38. path: '/mini-login',
  39. request: {
  40. body: {
  41. content: {
  42. 'application/json': {
  43. schema: MiniLoginSchema
  44. }
  45. }
  46. }
  47. },
  48. responses: {
  49. 200: {
  50. description: '小程序登录成功',
  51. content: {
  52. 'application/json': {
  53. schema: MiniLoginResponseSchema
  54. }
  55. }
  56. },
  57. 400: {
  58. description: '参数错误',
  59. content: {
  60. 'application/json': {
  61. schema: ErrorSchema
  62. }
  63. }
  64. },
  65. 500: {
  66. description: '服务器错误',
  67. content: {
  68. 'application/json': {
  69. schema: ErrorSchema
  70. }
  71. }
  72. }
  73. }
  74. });
  75. const miniAuthService = new MiniAuthService(AppDataSource);
  76. const app = new OpenAPIHono().openapi(miniLoginRoute, async (c) => {
  77. try {
  78. const { code, userInfo } = c.req.valid('json');
  79. const result = await miniAuthService.miniLogin(code);
  80. // 如果有用户信息,更新用户资料
  81. if (userInfo) {
  82. await miniAuthService.updateUserProfile(result.user.id, {
  83. nickname: userInfo.nickName,
  84. avatarUrl: userInfo.avatarUrl
  85. });
  86. // 重新获取更新后的用户信息
  87. const updatedUser = await AppDataSource.getRepository(UserEntity).findOne({
  88. where: { id: result.user.id },
  89. relations: ['avatarFile']
  90. });
  91. if (updatedUser) {
  92. result.user = updatedUser;
  93. }
  94. }
  95. return c.json({
  96. token: result.token,
  97. user: {
  98. id: result.user.id,
  99. username: result.user.username,
  100. nickname: result.user.nickname,
  101. phone: result.user.phone,
  102. email: result.user.email,
  103. avatarFileId: result.user.avatarFileId,
  104. registrationSource: result.user.registrationSource
  105. },
  106. isNewUser: result.isNewUser
  107. }, 200);
  108. } catch (error) {
  109. const { code = 500, message = '登录失败' } = error as Error & { code?: number };
  110. return c.json({ code, message }, 500);
  111. }
  112. });
  113. export default app;