2
0

user.entity.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable, CreateDateColumn, UpdateDateColumn, OneToMany } from 'typeorm';
  2. import { Role, RoleSchema } from './role.entity';
  3. import { z } from '@hono/zod-openapi';
  4. import { DeleteStatus, DisabledStatus } from '@/share/types';
  5. @Entity({ name: 'users' })
  6. export class UserEntity {
  7. @PrimaryGeneratedColumn({ unsigned: true, comment: '用户ID' })
  8. id!: number;
  9. @Column({ name: 'username', type: 'varchar', length: 255, unique: true, comment: '用户名' })
  10. username!: string;
  11. @Column({ name: 'password', type: 'varchar', length: 255, comment: '密码' })
  12. password!: string;
  13. @Column({ name: 'phone', type: 'varchar', length: 255, nullable: true, comment: '手机号' })
  14. phone!: string | null;
  15. @Column({ name: 'email', type: 'varchar', length: 255, nullable: true, comment: '邮箱' })
  16. email!: string | null;
  17. @Column({ name: 'nickname', type: 'varchar', length: 255, nullable: true, comment: '昵称' })
  18. nickname!: string | null;
  19. @Column({ name: 'name', type: 'varchar', length: 255, nullable: true, comment: '真实姓名' })
  20. name!: string | null;
  21. @Column({ name: 'avatar', type: 'varchar', length: 255, nullable: true, comment: '头像' })
  22. avatar!: string | null;
  23. @Column({ name: 'is_disabled', type: 'int', default: DisabledStatus.ENABLED, comment: '是否禁用(0:启用,1:禁用)' })
  24. isDisabled!: DisabledStatus;
  25. @Column({ name: 'is_deleted', type: 'int', default: DeleteStatus.NOT_DELETED, comment: '是否删除(0:未删除,1:已删除)' })
  26. isDeleted!: DeleteStatus;
  27. @ManyToMany(() => Role)
  28. @JoinTable()
  29. roles!: Role[];
  30. @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
  31. createdAt!: Date;
  32. @UpdateDateColumn({ name: 'updated_at', type: 'timestamp' })
  33. updatedAt!: Date;
  34. constructor(partial?: Partial<UserEntity>) {
  35. Object.assign(this, partial);
  36. }
  37. }
  38. export const UserSchema = z.object({
  39. id: z.number().int().positive().openapi({ description: '用户ID' }),
  40. username: z.string().min(3).max(255).openapi({
  41. example: 'admin',
  42. description: '用户名,3-255个字符'
  43. }),
  44. password: z.string().min(6).max(255).openapi({
  45. example: 'password123',
  46. description: '密码,最少6位'
  47. }),
  48. phone: z.string().max(255).nullable().openapi({
  49. example: '13800138000',
  50. description: '手机号'
  51. }),
  52. email: z.string().email().max(255).nullable().openapi({
  53. example: 'user@example.com',
  54. description: '邮箱'
  55. }),
  56. nickname: z.string().max(255).nullable().openapi({
  57. example: '昵称',
  58. description: '用户昵称'
  59. }),
  60. name: z.string().max(255).nullable().openapi({
  61. example: '张三',
  62. description: '真实姓名'
  63. }),
  64. avatar: z.string().max(255).nullable().openapi({
  65. example: 'https://example.com/avatar.jpg',
  66. description: '用户头像'
  67. }),
  68. isDisabled: z.number().int().min(0).max(1).default(DisabledStatus.ENABLED).openapi({
  69. example: DisabledStatus.ENABLED,
  70. description: '是否禁用(0:启用,1:禁用)'
  71. }),
  72. isDeleted: z.number().int().min(0).max(1).default(DeleteStatus.NOT_DELETED).openapi({
  73. example: DeleteStatus.NOT_DELETED,
  74. description: '是否删除(0:未删除,1:已删除)'
  75. }),
  76. roles: z.array(RoleSchema).optional().openapi({
  77. example: [
  78. { id: 1, name: 'admin',description:'管理员', permissions: ['user:create'] ,createdAt: new Date(), updatedAt: new Date() }
  79. ],
  80. description: '用户角色列表'
  81. }),
  82. createdAt: z.date().openapi({ description: '创建时间' }),
  83. updatedAt: z.date().openapi({ description: '更新时间' })
  84. });