2026-01-15 13:51:44 +08:00
|
|
|
|
package models
|
|
|
|
|
|
|
2026-01-19 13:53:32 +08:00
|
|
|
|
import (
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-01-15 13:51:44 +08:00
|
|
|
|
// User 用户模型
|
|
|
|
|
|
type User struct {
|
2026-01-19 13:53:32 +08:00
|
|
|
|
ID uint `json:"id" gorm:"primaryKey;column:id"`
|
|
|
|
|
|
Username string `json:"username" gorm:"column:username;uniqueIndex;not null"`
|
|
|
|
|
|
Email string `json:"email" gorm:"column:email"`
|
2026-06-25 17:57:51 +08:00
|
|
|
|
Avatar string `json:"avatar" gorm:"column:avatar"`
|
2026-06-26 17:00:02 +08:00
|
|
|
|
Bio string `json:"bio" gorm:"column:bio;type:text"`
|
|
|
|
|
|
Phone string `json:"phone" gorm:"column:phone"`
|
|
|
|
|
|
Wechat string `json:"wechat" gorm:"column:wechat"`
|
|
|
|
|
|
WechatQrcode string `json:"wechatQrcode" gorm:"column:wechat_qrcode"`
|
2026-07-30 14:11:26 +08:00
|
|
|
|
WxOpenID string `json:"-" gorm:"column:wx_openid;uniqueIndex"`
|
2026-01-16 17:03:34 +08:00
|
|
|
|
Password string `json:"password,omitempty" gorm:"-"` // Virtual field for input
|
2026-01-19 13:53:32 +08:00
|
|
|
|
PasswordHash string `json:"-" gorm:"column:password_hash"`
|
|
|
|
|
|
RoleID uint `json:"roleId" gorm:"column:role_id"`
|
|
|
|
|
|
Role string `json:"role" gorm:"column:role"` // 保持兼容,或者作为Role Name
|
|
|
|
|
|
IsActive int `json:"isActive" gorm:"column:is_active;default:1"`
|
|
|
|
|
|
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
|
|
|
|
|
|
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
|
|
|
|
|
|
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TableName 指定表名
|
|
|
|
|
|
func (User) TableName() string {
|
|
|
|
|
|
return "users"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BeforeCreate 创建前钩子
|
|
|
|
|
|
func (u *User) BeforeCreate(tx *gorm.DB) error {
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
|
if u.CreatedAt == 0 {
|
|
|
|
|
|
u.CreatedAt = now
|
|
|
|
|
|
}
|
|
|
|
|
|
if u.UpdatedAt == 0 {
|
|
|
|
|
|
u.UpdatedAt = now
|
|
|
|
|
|
}
|
|
|
|
|
|
if u.DeletedAt == 0 {
|
|
|
|
|
|
u.DeletedAt = 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BeforeUpdate 更新前钩子
|
|
|
|
|
|
func (u *User) BeforeUpdate(tx *gorm.DB) error {
|
|
|
|
|
|
u.UpdatedAt = time.Now().Unix()
|
|
|
|
|
|
return nil
|
2026-01-15 13:51:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UserResponse 用户响应模型
|
|
|
|
|
|
type UserResponse struct {
|
2026-06-26 17:00:02 +08:00
|
|
|
|
ID uint `json:"id"`
|
|
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
|
Email string `json:"email"`
|
|
|
|
|
|
Avatar string `json:"avatar,omitempty"`
|
|
|
|
|
|
Bio string `json:"bio,omitempty"`
|
|
|
|
|
|
Phone string `json:"phone,omitempty"`
|
|
|
|
|
|
Wechat string `json:"wechat,omitempty"`
|
|
|
|
|
|
WechatQrcode string `json:"wechatQrcode,omitempty"`
|
|
|
|
|
|
RoleID uint `json:"roleId"`
|
|
|
|
|
|
Role string `json:"role"`
|
|
|
|
|
|
IsActive int `json:"isActive"`
|
|
|
|
|
|
CreatedAt string `json:"createdAt"`
|
|
|
|
|
|
UpdatedAt string `json:"updatedAt"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UserPublicProfile 公开用户资料(前台展示)
|
|
|
|
|
|
type UserPublicProfile struct {
|
|
|
|
|
|
ID uint `json:"id"`
|
|
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
|
Email string `json:"email"`
|
|
|
|
|
|
Avatar string `json:"avatar,omitempty"`
|
|
|
|
|
|
Bio string `json:"bio,omitempty"`
|
|
|
|
|
|
Phone string `json:"phone,omitempty"`
|
|
|
|
|
|
Wechat string `json:"wechat,omitempty"`
|
|
|
|
|
|
WechatQrcode string `json:"wechatQrcode,omitempty"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateProfileRequest 当前用户更新资料请求
|
|
|
|
|
|
type UpdateProfileRequest struct {
|
|
|
|
|
|
Email string `json:"email"`
|
|
|
|
|
|
Avatar string `json:"avatar"`
|
|
|
|
|
|
Bio string `json:"bio"`
|
|
|
|
|
|
Phone string `json:"phone"`
|
|
|
|
|
|
Wechat string `json:"wechat"`
|
|
|
|
|
|
WechatQrcode string `json:"wechatQrcode"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdatePasswordRequest 修改密码请求
|
|
|
|
|
|
type UpdatePasswordRequest struct {
|
|
|
|
|
|
OldPassword string `json:"oldPassword" binding:"required"`
|
|
|
|
|
|
NewPassword string `json:"newPassword" binding:"required"`
|
2026-01-15 13:51:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// LoginRequest 登录请求模型
|
|
|
|
|
|
type LoginRequest struct {
|
|
|
|
|
|
Username string `json:"username" binding:"required"`
|
|
|
|
|
|
Password string `json:"password" binding:"required"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// LoginResponse 登录响应模型
|
|
|
|
|
|
type LoginResponse struct {
|
|
|
|
|
|
Token string `json:"token"`
|
|
|
|
|
|
User UserResponse `json:"user"`
|
|
|
|
|
|
Expire int64 `json:"expire"`
|
|
|
|
|
|
}
|
2026-07-30 14:11:26 +08:00
|
|
|
|
|
|
|
|
|
|
// WxLoginRequest 微信小程序登录
|
|
|
|
|
|
type WxLoginRequest struct {
|
|
|
|
|
|
Code string `json:"code" binding:"required"`
|
|
|
|
|
|
}
|