custom.routes.mt.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
  2. import { z } from '@hono/zod-openapi';
  3. import { UserServiceMt } from '../services/user.service.mt';
  4. import { AppDataSource, ErrorSchema } from '@d8d/shared-utils';
  5. import { CreateUserDtoMt, UpdateUserDtoMt, UserSchemaMt } from '../schemas/user.schema.mt';
  6. import { parseWithAwait } from '@d8d/shared-utils';
  7. import { authMiddleware } from '@d8d/auth-module-mt';
  8. import { AuthContext } from '@d8d/shared-types';
  9. // 创建多租户用户路由 - 自定义业务逻辑(密码加密等)
  10. const createUserRouteMt = createRoute({
  11. method: 'post',
  12. path: '/',
  13. middleware: [authMiddleware],
  14. request: {
  15. body: {
  16. content: {
  17. 'application/json': { schema: CreateUserDtoMt }
  18. }
  19. }
  20. },
  21. responses: {
  22. 201: {
  23. description: '用户创建成功',
  24. content: {
  25. 'application/json': { schema: UserSchemaMt }
  26. }
  27. },
  28. 400: {
  29. description: '参数错误',
  30. content: { 'application/json': { schema: ErrorSchema } }
  31. },
  32. 401: {
  33. description: '认证失败',
  34. content: { 'application/json': { schema: ErrorSchema } }
  35. },
  36. 500: {
  37. description: '创建用户失败',
  38. content: { 'application/json': { schema: ErrorSchema } }
  39. }
  40. }
  41. });
  42. // 更新多租户用户路由 - 自定义业务逻辑
  43. const updateUserRouteMt = createRoute({
  44. method: 'put',
  45. path: '/{id}',
  46. middleware: [authMiddleware],
  47. request: {
  48. params: z.object({
  49. id: z.coerce.number().openapi({
  50. param: { name: 'id', in: 'path' },
  51. example: 1,
  52. description: '用户ID'
  53. })
  54. }),
  55. body: {
  56. content: {
  57. 'application/json': { schema: UpdateUserDtoMt }
  58. }
  59. }
  60. },
  61. responses: {
  62. 200: {
  63. description: '用户更新成功',
  64. content: {
  65. 'application/json': { schema: UserSchemaMt }
  66. }
  67. },
  68. 400: {
  69. description: '参数错误',
  70. content: { 'application/json': { schema: ErrorSchema } }
  71. },
  72. 401: {
  73. description: '认证失败',
  74. content: { 'application/json': { schema: ErrorSchema } }
  75. },
  76. 404: {
  77. description: '用户不存在',
  78. content: { 'application/json': { schema: ErrorSchema } }
  79. },
  80. 500: {
  81. description: '更新用户失败',
  82. content: { 'application/json': { schema: ErrorSchema } }
  83. }
  84. }
  85. });
  86. // 删除多租户用户路由 - 自定义业务逻辑
  87. const deleteUserRouteMt = createRoute({
  88. method: 'delete',
  89. path: '/{id}',
  90. middleware: [authMiddleware],
  91. request: {
  92. params: z.object({
  93. id: z.coerce.number().openapi({
  94. param: { name: 'id', in: 'path' },
  95. example: 1,
  96. description: '用户ID'
  97. })
  98. })
  99. },
  100. responses: {
  101. 204: { description: '用户删除成功' },
  102. 401: {
  103. description: '认证失败',
  104. content: { 'application/json': { schema: ErrorSchema } }
  105. },
  106. 404: {
  107. description: '用户不存在',
  108. content: { 'application/json': { schema: ErrorSchema } }
  109. },
  110. 500: {
  111. description: '删除用户失败',
  112. content: { 'application/json': { schema: ErrorSchema } }
  113. }
  114. }
  115. });
  116. const app = new OpenAPIHono<AuthContext>()
  117. .openapi(createUserRouteMt, async (c) => {
  118. try {
  119. const data = c.req.valid('json');
  120. const userService = new UserServiceMt(AppDataSource);
  121. // 从认证上下文中获取租户ID
  122. const tenantId = c.get('tenantId');
  123. const result = await userService.createUser(data, tenantId);
  124. return c.json(await parseWithAwait(UserSchemaMt, result), 201);
  125. } catch (error) {
  126. if (error instanceof z.ZodError) {
  127. return c.json({
  128. code: 400,
  129. message: '参数错误',
  130. errors: error.issues
  131. }, 400);
  132. }
  133. // 处理用户名重复错误
  134. if (error instanceof Error && error.message.includes('用户名已存在')) {
  135. return c.json({
  136. code: 400,
  137. message: '用户名已存在'
  138. }, 400);
  139. }
  140. return c.json({
  141. code: 500,
  142. message: error instanceof Error ? error.message : '创建用户失败'
  143. }, 500);
  144. }
  145. })
  146. .openapi(updateUserRouteMt, async (c) => {
  147. try {
  148. const { id } = c.req.valid('param');
  149. const data = c.req.valid('json');
  150. const userService = new UserServiceMt(AppDataSource);
  151. // 从认证上下文中获取租户ID
  152. const tenantId = c.get('tenantId');
  153. const result = await userService.updateUser(id, data, tenantId);
  154. if (!result) {
  155. return c.json({ code: 404, message: '资源不存在' }, 404);
  156. }
  157. return c.json(await parseWithAwait(UserSchemaMt, result), 200);
  158. } catch (error) {
  159. if (error instanceof z.ZodError) {
  160. return c.json({
  161. code: 400,
  162. message: '参数错误',
  163. errors: error.issues
  164. }, 400);
  165. }
  166. return c.json({
  167. code: 500,
  168. message: error instanceof Error ? error.message : '更新用户失败'
  169. }, 500);
  170. }
  171. })
  172. .openapi(deleteUserRouteMt, async (c) => {
  173. try {
  174. const { id } = c.req.valid('param');
  175. const userService = new UserServiceMt(AppDataSource);
  176. // 从认证上下文中获取租户ID
  177. const tenantId = c.get('tenantId');
  178. const success = await userService.deleteUser(id, tenantId);
  179. if (!success) {
  180. return c.json({ code: 404, message: '资源不存在' }, 404);
  181. }
  182. return c.body(null, 204);
  183. } catch (error) {
  184. return c.json({
  185. code: 500,
  186. message: error instanceof Error ? error.message : '删除用户失败'
  187. }, 500);
  188. }
  189. });
  190. export default app;