Files
nl-blogs/server/models/user.go
李琦 4f4153ee0e 1. 小程序端
2. 视频优化
2026-07-30 14:11:26 +08:00

118 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package models
import (
"time"
"gorm.io/gorm"
)
// User 用户模型
type User struct {
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"`
Avatar string `json:"avatar" gorm:"column:avatar"`
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"`
WxOpenID string `json:"-" gorm:"column:wx_openid;uniqueIndex"`
Password string `json:"password,omitempty" gorm:"-"` // Virtual field for input
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
}
// UserResponse 用户响应模型
type UserResponse 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"`
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"`
}
// 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"`
}
// WxLoginRequest 微信小程序登录
type WxLoginRequest struct {
Code string `json:"code" binding:"required"`
}