A:增加“资料库”;增加模型注入

This commit is contained in:
jimmy committed 2025-06-15 20:05:40 +08:00
1 parent b8dc199cff
commit 592b01a89b
10 files changed
+250 -68

No files matched your search

+4 -4
View File
@@ -7,9 +7,9 @@ import (
// BaseModel 基础模型,包含通用字段
type BaseModel struct {
ID uint `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ID uint `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 软删除 【查询时会检测删除时间】
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
IsDeleted bool `json:"is_deleted"`
}
+9
View File
@@ -0,0 +1,9 @@
package model
type LibraryTable struct {
BaseModel
// 照片存储库路径
ImgPath string `gorm:"not null;size:255" json:"imgPath"`
// 是否开启
IsEnable bool `gorm:"default:false;" json:"isEnable"`
}
+1 -19
View File
@@ -1,29 +1,11 @@
package model
import "gorm.io/gorm"
type User struct {
BaseModel
Username string `gorm:"uniqueIndex;not null;size:50" json:"username"`
Email string `gorm:"uniqueIndex;not null;size:100" json:"email"`
// json:"-" - 序列化时忽略此字段(密码不会出现在JSON中)
Password string `gorm:"not null;size:255" json:"-"`
Age int `gorm:"default:0" json:"age"`
Status int `gorm:"default:1;comment:1-active,0-inactive" json:"status"`
}
// TableName 指定表名
func (User) GetTableName() string {
return "users"
}
// BeforeCreate GORM钩子,创建前执行
func (u *User) BeforeCreate(tx *gorm.DB) error {
// 这里可以添加创建前的逻辑,比如密码加密
return nil
}
// AfterCreate GORM钩子,创建后执行
func (u *User) AfterCreate(tx *gorm.DB) error {
// 这里可以添加创建后的逻辑
return nil
}