user.schema.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { z } from '@hono/zod-openapi';
  2. import { DeleteStatus, DisabledStatus } from '@d8d/shared-types';
  3. import { RoleSchema } from './role.schema';
  4. // 基础用户 schema(包含所有字段)
  5. export const UserSchema = z.object({
  6. id: z.number().int().positive().openapi({ description: '用户ID' }),
  7. username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').openapi({
  8. example: 'admin',
  9. description: '用户名,3-255个字符'
  10. }),
  11. password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').openapi({
  12. example: 'password123',
  13. description: '密码,最少6位'
  14. }),
  15. phone: z.string().max(255, '手机号最多255个字符').nullable().openapi({
  16. example: '13800138000',
  17. description: '手机号'
  18. }),
  19. email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().openapi({
  20. example: 'user@example.com',
  21. description: '邮箱'
  22. }),
  23. nickname: z.string().max(255, '昵称最多255个字符').nullable().openapi({
  24. example: '昵称',
  25. description: '用户昵称'
  26. }),
  27. name: z.string().max(255, '姓名最多255个字符').nullable().openapi({
  28. example: '张三',
  29. description: '真实姓名'
  30. }),
  31. avatarFileId: z.number().int().positive().nullable().openapi({
  32. example: 1,
  33. description: '头像文件ID'
  34. }),
  35. avatarFile: z.object({
  36. id: z.number().int().positive().openapi({ description: '文件ID' }),
  37. name: z.string().max(255).openapi({ description: '文件名', example: 'avatar.jpg' }),
  38. fullUrl: z.string().openapi({ description: '文件完整URL', example: 'https://example.com/avatar.jpg' }),
  39. type: z.string().nullable().openapi({ description: '文件类型', example: 'image/jpeg' }),
  40. size: z.number().nullable().openapi({ description: '文件大小(字节)', example: 102400 })
  41. }).nullable().optional().openapi({
  42. description: '头像文件信息'
  43. }),
  44. openid: z.string().max(255).nullable().optional().openapi({
  45. example: 'oABCDEFGH123456789',
  46. description: '微信小程序openid'
  47. }),
  48. unionid: z.string().max(255).nullable().optional().openapi({
  49. example: 'unionid123456789',
  50. description: '微信unionid'
  51. }),
  52. registrationSource: z.string().max(20).default('web').openapi({
  53. example: 'miniapp',
  54. description: '注册来源: web, miniapp'
  55. }),
  56. isDisabled: z.nativeEnum(DisabledStatus).default(DisabledStatus.ENABLED).openapi({
  57. example: DisabledStatus.ENABLED,
  58. description: '是否禁用(0:启用,1:禁用)'
  59. }),
  60. isDeleted: z.number().int().min(0).max(1).default(DeleteStatus.NOT_DELETED).openapi({
  61. example: DeleteStatus.NOT_DELETED,
  62. description: '是否删除(0:未删除,1:已删除)'
  63. }),
  64. roles: z.array(RoleSchema).optional().openapi({
  65. example: [
  66. {
  67. id: 1,
  68. name: 'admin',
  69. description: '管理员',
  70. permissions: ['user:create'],
  71. createdAt: new Date(),
  72. updatedAt: new Date()
  73. }
  74. ],
  75. description: '用户角色列表'
  76. }),
  77. createdAt: z.coerce.date().openapi({ description: '创建时间' }),
  78. updatedAt: z.coerce.date().openapi({ description: '更新时间' })
  79. });
  80. // 创建用户请求 schema
  81. export const CreateUserDto = z.object({
  82. username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').openapi({
  83. example: 'admin',
  84. description: '用户名,3-255个字符'
  85. }),
  86. password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').openapi({
  87. example: 'password123',
  88. description: '密码,最少6位'
  89. }),
  90. phone: z.string().max(255, '手机号最多255个字符').nullable().optional().openapi({
  91. example: '13800138000',
  92. description: '手机号'
  93. }),
  94. email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().optional().openapi({
  95. example: 'user@example.com',
  96. description: '邮箱'
  97. }),
  98. nickname: z.string().max(255, '昵称最多255个字符').nullable().optional().openapi({
  99. example: '昵称',
  100. description: '用户昵称'
  101. }),
  102. name: z.string().max(255, '姓名最多255个字符').nullable().optional().openapi({
  103. example: '张三',
  104. description: '真实姓名'
  105. }),
  106. avatarFileId: z.number().int().positive().nullable().optional().openapi({
  107. example: 1,
  108. description: '头像文件ID'
  109. }),
  110. isDisabled: z.number().int().min(0, '状态值只能是0或1').max(1, '状态值只能是0或1').default(DisabledStatus.ENABLED).optional().openapi({
  111. example: DisabledStatus.ENABLED,
  112. description: '是否禁用(0:启用,1:禁用)'
  113. })
  114. });
  115. // 更新用户请求 schema
  116. export const UpdateUserDto = z.object({
  117. username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').optional().openapi({
  118. example: 'admin',
  119. description: '用户名,3-255个字符'
  120. }),
  121. password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').optional().openapi({
  122. example: 'password123',
  123. description: '密码,最少6位'
  124. }),
  125. phone: z.string().max(255, '手机号最多255个字符').nullable().optional().openapi({
  126. example: '13800138000',
  127. description: '手机号'
  128. }),
  129. email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().optional().openapi({
  130. example: 'user@example.com',
  131. description: '邮箱'
  132. }),
  133. nickname: z.string().max(255, '昵称最多255个字符').nullable().optional().openapi({
  134. example: '昵称',
  135. description: '用户昵称'
  136. }),
  137. name: z.string().max(255, '姓名最多255个字符').nullable().optional().openapi({
  138. example: '张三',
  139. description: '真实姓名'
  140. }),
  141. avatarFileId: z.number().int().positive().nullable().optional().openapi({
  142. example: 1,
  143. description: '头像文件ID'
  144. }),
  145. isDisabled: z.number().int().min(0, '状态值只能是0或1').max(1, '状态值只能是0或1').optional().openapi({
  146. example: DisabledStatus.ENABLED,
  147. description: '是否禁用(0:启用,1:禁用)'
  148. })
  149. });
  150. // 用户列表响应 schema
  151. export const UserListResponse = z.object({
  152. data: z.array(UserSchema.omit({ password: true })),
  153. pagination: z.object({
  154. total: z.number().openapi({
  155. example: 100,
  156. description: '总记录数'
  157. }),
  158. current: z.number().openapi({
  159. example: 1,
  160. description: '当前页码'
  161. }),
  162. pageSize: z.number().openapi({
  163. example: 10,
  164. description: '每页数量'
  165. })
  166. })
  167. });
  168. // 单个用户查询响应 schema
  169. export const UserResponseSchema = UserSchema.omit({password:true})
  170. // 类型导出
  171. export type User = z.infer<typeof UserSchema>;
  172. export type CreateUserRequest = z.infer<typeof CreateUserDto>;
  173. export type UpdateUserRequest = z.infer<typeof UpdateUserDto>;
  174. export type UserListResponseType = z.infer<typeof UserListResponse>;