user.schema.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { z } from '@hono/zod-openapi';
  2. import { DeleteStatus, DisabledStatus, UserType } from '@d8d/shared-types';
  3. import { RoleSchemaMt } from './role.schema';
  4. import { CompanySchema } from '@d8d/allin-company-module/schemas';
  5. // 多租户基础用户 schema(包含所有字段)
  6. export const UserSchemaMt = z.object({
  7. id: z.number().int().positive().openapi({ description: '用户ID' }),
  8. tenantId: z.number().int().positive().openapi({ description: '租户ID' }),
  9. username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').openapi({
  10. example: 'admin',
  11. description: '用户名,3-255个字符'
  12. }),
  13. password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').openapi({
  14. example: 'password123',
  15. description: '密码,最少6位'
  16. }),
  17. phone: z.string().max(255, '手机号最多255个字符').nullable().openapi({
  18. example: '13800138000',
  19. description: '手机号'
  20. }),
  21. email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().openapi({
  22. example: 'user@example.com',
  23. description: '邮箱'
  24. }),
  25. nickname: z.string().max(255, '昵称最多255个字符').nullable().openapi({
  26. example: '昵称',
  27. description: '用户昵称'
  28. }),
  29. name: z.string().max(255, '姓名最多255个字符').nullable().openapi({
  30. example: '张三',
  31. description: '真实姓名'
  32. }),
  33. avatarFileId: z.number().int().positive().nullable().openapi({
  34. example: 1,
  35. description: '头像文件ID'
  36. }),
  37. userType: z.nativeEnum(UserType).default(UserType.ADMIN).openapi({
  38. example: UserType.ADMIN,
  39. description: '用户类型: admin(管理员), employer(企业用户), talent(人才用户)'
  40. }),
  41. avatarFile: z.object({
  42. id: z.number().int().positive().openapi({ description: '文件ID' }),
  43. name: z.string().max(255).openapi({ description: '文件名', example: 'avatar.jpg' }),
  44. fullUrl: z.string().openapi({ description: '文件完整URL', example: 'https://example.com/avatar.jpg' }),
  45. type: z.string().nullable().openapi({ description: '文件类型', example: 'image/jpeg' }),
  46. size: z.number().nullable().openapi({ description: '文件大小(字节)', example: 102400 })
  47. }).nullable().optional().openapi({
  48. description: '头像文件信息'
  49. }),
  50. companyId: z.number().int().positive().nullable().openapi({
  51. example: 1,
  52. description: '公司ID,引用employer_company.company_id'
  53. }),
  54. company: CompanySchema.nullable().optional().openapi({
  55. description: '关联的公司信息'
  56. }),
  57. personId: z.number().int().positive().nullable().openapi({
  58. example: 1,
  59. description: '残疾人ID,引用disabled_person.person_id'
  60. }),
  61. person: z.object({
  62. id: z.number().int().positive().openapi({ description: '残疾人ID' }),
  63. name: z.string().openapi({ description: '姓名' }),
  64. disabilityId: z.string().openapi({ description: '残疾证号' })
  65. }).nullable().optional().openapi({
  66. description: '关联的残疾人信息'
  67. }),
  68. openid: z.string().max(255).nullable().optional().openapi({
  69. example: 'oABCDEFGH123456789',
  70. description: '微信小程序openid'
  71. }),
  72. unionid: z.string().max(255).nullable().optional().openapi({
  73. example: 'unionid123456789',
  74. description: '微信unionid'
  75. }),
  76. registrationSource: z.string().max(20).default('web').openapi({
  77. example: 'miniapp',
  78. description: '注册来源: web, miniapp'
  79. }),
  80. isDisabled: z.nativeEnum(DisabledStatus).default(DisabledStatus.ENABLED).openapi({
  81. example: DisabledStatus.ENABLED,
  82. description: '是否禁用(0:启用,1:禁用)'
  83. }),
  84. isDeleted: z.number().int().min(0).max(1).default(DeleteStatus.NOT_DELETED).openapi({
  85. example: DeleteStatus.NOT_DELETED,
  86. description: '是否删除(0:未删除,1:已删除)'
  87. }),
  88. roles: z.array(RoleSchemaMt).optional().openapi({
  89. example: [
  90. {
  91. id: 1,
  92. name: 'admin',
  93. description: '管理员',
  94. permissions: ['user:create'],
  95. createdAt: new Date(),
  96. updatedAt: new Date()
  97. }
  98. ],
  99. description: '用户角色列表'
  100. }),
  101. createdAt: z.coerce.date().openapi({ description: '创建时间' }),
  102. updatedAt: z.coerce.date().openapi({ description: '更新时间' })
  103. });
  104. // 多租户创建用户请求 schema
  105. export const CreateUserDtoMt = z.object({
  106. username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').openapi({
  107. example: 'admin',
  108. description: '用户名,3-255个字符'
  109. }),
  110. password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').openapi({
  111. example: 'password123',
  112. description: '密码,最少6位'
  113. }),
  114. phone: z.string().max(255, '手机号最多255个字符').nullable().optional().openapi({
  115. example: '13800138000',
  116. description: '手机号'
  117. }),
  118. email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().optional().openapi({
  119. example: 'user@example.com',
  120. description: '邮箱'
  121. }),
  122. nickname: z.string().max(255, '昵称最多255个字符').nullable().optional().openapi({
  123. example: '昵称',
  124. description: '用户昵称'
  125. }),
  126. name: z.string().max(255, '姓名最多255个字符').nullable().optional().openapi({
  127. example: '张三',
  128. description: '真实姓名'
  129. }),
  130. avatarFileId: z.number().int().positive().nullable().optional().openapi({
  131. example: 1,
  132. description: '头像文件ID'
  133. }),
  134. userType: z.nativeEnum(UserType).default(UserType.ADMIN).optional().openapi({
  135. example: UserType.ADMIN,
  136. description: '用户类型: admin(管理员), employer(企业用户), talent(人才用户)'
  137. }),
  138. companyId: z.number().int().positive().nullable().optional().openapi({
  139. example: 1,
  140. description: '公司ID,引用employer_company.company_id'
  141. }),
  142. personId: z.number().int().positive().nullable().optional().openapi({
  143. example: 1,
  144. description: '残疾人ID,引用disabled_person.person_id'
  145. }),
  146. isDisabled: z.number().int().min(0, '状态值只能是0或1').max(1, '状态值只能是0或1').default(DisabledStatus.ENABLED).optional().openapi({
  147. example: DisabledStatus.ENABLED,
  148. description: '是否禁用(0:启用,1:禁用)'
  149. })
  150. });
  151. // 多租户更新用户请求 schema
  152. export const UpdateUserDtoMt = z.object({
  153. username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').optional().openapi({
  154. example: 'admin',
  155. description: '用户名,3-255个字符'
  156. }),
  157. password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').optional().openapi({
  158. example: 'password123',
  159. description: '密码,最少6位'
  160. }),
  161. phone: z.string().max(255, '手机号最多255个字符').nullable().optional().openapi({
  162. example: '13800138000',
  163. description: '手机号'
  164. }),
  165. email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().optional().openapi({
  166. example: 'user@example.com',
  167. description: '邮箱'
  168. }),
  169. nickname: z.string().max(255, '昵称最多255个字符').nullable().optional().openapi({
  170. example: '昵称',
  171. description: '用户昵称'
  172. }),
  173. name: z.string().max(255, '姓名最多255个字符').nullable().optional().openapi({
  174. example: '张三',
  175. description: '真实姓名'
  176. }),
  177. avatarFileId: z.number().int().positive().nullable().optional().openapi({
  178. example: 1,
  179. description: '头像文件ID'
  180. }),
  181. userType: z.nativeEnum(UserType).optional().openapi({
  182. example: UserType.ADMIN,
  183. description: '用户类型: admin(管理员), employer(企业用户), talent(人才用户)'
  184. }),
  185. companyId: z.number().int().positive().nullable().optional().openapi({
  186. example: 1,
  187. description: '公司ID,引用employer_company.company_id'
  188. }),
  189. personId: z.number().int().positive().nullable().optional().openapi({
  190. example: 1,
  191. description: '残疾人ID,引用disabled_person.person_id'
  192. }),
  193. isDisabled: z.number().int().min(0, '状态值只能是0或1').max(1, '状态值只能是0或1').optional().openapi({
  194. example: DisabledStatus.ENABLED,
  195. description: '是否禁用(0:启用,1:禁用)'
  196. })
  197. });
  198. // 多租户用户列表响应 schema
  199. export const UserListResponseMt = z.object({
  200. data: z.array(UserSchemaMt.omit({ password: true })),
  201. pagination: z.object({
  202. total: z.number().openapi({
  203. example: 100,
  204. description: '总记录数'
  205. }),
  206. current: z.number().openapi({
  207. example: 1,
  208. description: '当前页码'
  209. }),
  210. pageSize: z.number().openapi({
  211. example: 10,
  212. description: '每页数量'
  213. })
  214. })
  215. });
  216. // 多租户单个用户查询响应 schema
  217. export const UserResponseSchemaMt = UserSchemaMt.omit({password:true})
  218. // 非多租户基础用户 schema(不包含tenantId字段)
  219. export const UserSchema = UserSchemaMt.omit({ tenantId: true })
  220. // 非多租户创建用户请求 schema
  221. export const CreateUserDto = CreateUserDtoMt
  222. // 非多租户更新用户请求 schema
  223. export const UpdateUserDto = UpdateUserDtoMt
  224. // 非多租户用户列表响应 schema
  225. export const UserListResponse = UserListResponseMt
  226. // 非多租户单个用户查询响应 schema
  227. export const UserResponseSchema = UserSchema.omit({password:true})
  228. // 非多租户类型导出
  229. export type User = z.infer<typeof UserSchema>;
  230. export type CreateUserRequest = z.infer<typeof CreateUserDto>;
  231. export type UpdateUserRequest = z.infer<typeof UpdateUserDto>;
  232. export type UserListResponseType = z.infer<typeof UserListResponse>;
  233. // 多租户类型导出
  234. export type UserMt = z.infer<typeof UserSchemaMt>;
  235. export type CreateUserRequestMt = z.infer<typeof CreateUserDtoMt>;
  236. export type UpdateUserRequestMt = z.infer<typeof UpdateUserDtoMt>;
  237. export type UserListResponseTypeMt = z.infer<typeof UserListResponseMt>;