user.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package model
  2. import (
  3. "time"
  4. "gorm.io/gorm"
  5. )
  6. // User 用户模型
  7. type User struct {
  8. ID uint `gorm:"primaryKey" json:"id"`
  9. Username string `gorm:"uniqueIndex;not null;size:50" json:"username"`
  10. Email string `gorm:"uniqueIndex;not null;size:100" json:"email"`
  11. Password string `gorm:"not null;size:255" json:"-"`
  12. FirstName string `gorm:"size:50" json:"first_name"`
  13. LastName string `gorm:"size:50" json:"last_name"`
  14. Avatar string `gorm:"size:255" json:"avatar"`
  15. Phone string `gorm:"size:20" json:"phone"`
  16. Address string `gorm:"size:255" json:"address"`
  17. IsActive bool `gorm:"default:true" json:"is_active"`
  18. Role string `gorm:"default:user;size:20" json:"role"`
  19. CreatedAt time.Time `json:"created_at"`
  20. UpdatedAt time.Time `json:"updated_at"`
  21. DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
  22. }
  23. // TableName 指定表名
  24. func (User) TableName() string {
  25. return "users"
  26. }
  27. // BeforeCreate GORM钩子 - 创建前
  28. func (u *User) BeforeCreate(tx *gorm.DB) error {
  29. // 这里可以添加密码加密等逻辑
  30. return nil
  31. }
  32. // BeforeUpdate GORM钩子 - 更新前
  33. func (u *User) BeforeUpdate(tx *gorm.DB) error {
  34. // 这里可以添加更新前的逻辑
  35. return nil
  36. }
  37. // CreateUserRequest 创建用户请求
  38. type CreateUserRequest struct {
  39. Username string `json:"username" binding:"required,min=3,max=50"`
  40. Email string `json:"email" binding:"required,email"`
  41. Password string `json:"password" binding:"required,min=6"`
  42. FirstName string `json:"first_name" binding:"max=50"`
  43. LastName string `json:"last_name" binding:"max=50"`
  44. Phone string `json:"phone" binding:"max=20"`
  45. Address string `json:"address" binding:"max=255"`
  46. }
  47. // UpdateUserRequest 更新用户请求
  48. type UpdateUserRequest struct {
  49. FirstName string `json:"first_name" binding:"max=50"`
  50. LastName string `json:"last_name" binding:"max=50"`
  51. Avatar string `json:"avatar" binding:"max=255"`
  52. Phone string `json:"phone" binding:"max=20"`
  53. Address string `json:"address" binding:"max=255"`
  54. }
  55. // LoginRequest 登录请求
  56. type LoginRequest struct {
  57. Username string `json:"username" binding:"required"`
  58. Password string `json:"password" binding:"required"`
  59. }
  60. // UserResponse 用户响应
  61. type UserResponse struct {
  62. ID uint `json:"id"`
  63. Username string `json:"username"`
  64. Email string `json:"email"`
  65. FirstName string `json:"first_name"`
  66. LastName string `json:"last_name"`
  67. Avatar string `json:"avatar"`
  68. Phone string `json:"phone"`
  69. Address string `json:"address"`
  70. IsActive bool `json:"is_active"`
  71. Role string `json:"role"`
  72. CreatedAt time.Time `json:"created_at"`
  73. UpdatedAt time.Time `json:"updated_at"`
  74. }
  75. // LoginResponse 登录响应
  76. type LoginResponse struct {
  77. Token string `json:"token"`
  78. User UserResponse `json:"user"`
  79. }
  80. // ToResponse 转换为响应格式
  81. func (u *User) ToResponse() UserResponse {
  82. return UserResponse{
  83. ID: u.ID,
  84. Username: u.Username,
  85. Email: u.Email,
  86. FirstName: u.FirstName,
  87. LastName: u.LastName,
  88. Avatar: u.Avatar,
  89. Phone: u.Phone,
  90. Address: u.Address,
  91. IsActive: u.IsActive,
  92. Role: u.Role,
  93. CreatedAt: u.CreatedAt,
  94. UpdatedAt: u.UpdatedAt,
  95. }
  96. }