Files
2025-11-17 13:32:54 +08:00

99 lines
3.8 KiB
Go
Raw Permalink 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 model
import (
"time"
"gorm.io/gorm"
)
// User 用户
type User struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
OpenID string `json:"openid" gorm:"column:open_id;type:varchar(50);not null;uniqueIndex"`
UnionID string `json:"unionid" gorm:"column:union_id;type:varchar(50)"`
Nickname string `json:"nickname" gorm:"size:50"`
Avatar string `json:"avatar" gorm:"size:255"`
Gender int `json:"gender" gorm:"default:0"` // 0未知1男2女
Phone string `json:"phone" gorm:"size:20"`
Email string `json:"email" gorm:"size:100"`
Birthday *time.Time `json:"birthday"`
Points int `json:"points" gorm:"default:0"`
Level int `json:"level" gorm:"default:1"`
Status int `json:"status" gorm:"default:1"` // 0禁用1正常
// 微信相关字段(按照官方文档标准)
WeChatSessionKey string `json:"-" gorm:"column:wechat_session_key;type:varchar(255);index"` // 不返回给前端,敏感信息
SessionExpiry *time.Time `json:"-" gorm:"column:session_expiry;type:timestamp;index"` // 不返回给前端使用指针类型支持NULL
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"index"`
// 关联数据
Addresses []UserAddress `json:"addresses,omitempty" gorm:"foreignKey:UserID"`
Favorites []UserFavorite `json:"favorites,omitempty" gorm:"foreignKey:UserID"`
Coupons []UserCoupon `json:"coupons,omitempty" gorm:"foreignKey:UserID"`
Roles []Role `json:"roles,omitempty" gorm:"many2many:ai_user_roles;"`
}
// UserAddress 用户地址
type UserAddress struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
UserID uint `json:"user_id" gorm:"not null"`
Name string `json:"name" gorm:"size:50;not null"`
Phone string `json:"phone" gorm:"size:20;not null"`
CountryName string `json:"country_name" gorm:"size:50;default:中国"`
ProvinceName string `json:"province_name" gorm:"size:50;not null"`
CityName string `json:"city_name" gorm:"size:50;not null"`
DistrictName string `json:"district_name" gorm:"size:50;not null"`
DetailAddress string `json:"detail_address" gorm:"size:255;not null"`
AddressTag string `json:"address_tag" gorm:"size:20"` // 家、公司等标签
Latitude float64 `json:"latitude" gorm:"type:decimal(10,6)"`
Longitude float64 `json:"longitude" gorm:"type:decimal(10,6)"`
IsDefault bool `json:"is_default" gorm:"default:false"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 关联数据
User User `json:"user,omitempty" gorm:"foreignKey:UserID"`
}
// UserFavorite 用户收藏
type UserFavorite struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
UserID uint `json:"user_id" gorm:"not null"`
ProductID uint `json:"product_id" gorm:"not null"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 关联数据
User User `json:"user,omitempty" gorm:"foreignKey:UserID"`
Product Product `json:"product,omitempty" gorm:"foreignKey:ProductID"`
}
// TableName 指定表名
func (User) TableName() string {
return "ai_users"
}
// TableName 指定表名
func (UserAddress) TableName() string {
return "ai_user_addresses"
}
// TableName 指定表名
func (UserFavorite) TableName() string {
return "ai_user_favorites"
}
// BeforeCreate 创建前钩子
func (u *User) BeforeCreate(tx *gorm.DB) error {
// 可以在这里添加创建前的逻辑
return nil
}
// BeforeUpdate 更新前钩子
func (u *User) BeforeUpdate(tx *gorm.DB) error {
// 可以在这里添加更新前的逻辑
return nil
}