feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers

This commit is contained in:
2026-04-02 11:19:50 +08:00
parent e59a77bc49
commit dcc1f186f8
298 changed files with 62603 additions and 0 deletions

View File

@@ -0,0 +1,127 @@
package domain
import "time"
// CustomFieldType 自定义字段类型
type CustomFieldType int
const (
CustomFieldTypeString CustomFieldType = iota // 字符串
CustomFieldTypeNumber // 数字
CustomFieldTypeBoolean // 布尔
CustomFieldTypeDate // 日期
)
// CustomField 自定义字段定义
type CustomField struct {
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
Name string `gorm:"type:varchar(50);not null" json:"name"` // 字段名称
FieldKey string `gorm:"type:varchar(50);uniqueIndex;not null" json:"field_key"` // 字段标识符
Type CustomFieldType `gorm:"type:int;not null" json:"type"` // 字段类型
Required bool `gorm:"default:false" json:"required"` // 是否必填
DefaultVal string `gorm:"type:varchar(255)" json:"default_val"` // 默认值
MinLen int `gorm:"default:0" json:"min_len"` // 最小长度(字符串)
MaxLen int `gorm:"default:255" json:"max_len"` // 最大长度(字符串)
MinVal float64 `gorm:"default:0" json:"min_val"` // 最小值(数字)
MaxVal float64 `gorm:"default:0" json:"max_val"` // 最大值(数字)
Options string `gorm:"type:varchar(500)" json:"options"` // 选项列表(逗号分隔)
Sort int `gorm:"default:0" json:"sort"` // 排序
Status int `gorm:"type:int;default:1" json:"status"` // 状态1启用 0禁用
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
}
// TableName 指定表名
func (CustomField) TableName() string {
return "custom_fields"
}
// UserCustomFieldValue 用户自定义字段值
type UserCustomFieldValue struct {
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
UserID int64 `gorm:"not null;index;uniqueIndex:idx_user_field" json:"user_id"`
FieldID int64 `gorm:"not null;index;uniqueIndex:idx_user_field" json:"field_id"`
FieldKey string `gorm:"type:varchar(50);not null" json:"field_key"` // 反规范化存储便于查询
Value string `gorm:"type:text" json:"value"` // 存储为字符串
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
}
// TableName 指定表名
func (UserCustomFieldValue) TableName() string {
return "user_custom_field_values"
}
// CustomFieldValueResponse 自定义字段值响应
type CustomFieldValueResponse struct {
FieldKey string `json:"field_key"`
Value interface{} `json:"value"`
}
// GetValueAsInterface 根据字段类型返回解析后的值
func (v *UserCustomFieldValue) GetValueAsInterface(field *CustomField) interface{} {
switch field.Type {
case CustomFieldTypeString:
return v.Value
case CustomFieldTypeNumber:
var f float64
for _, c := range v.Value {
if c >= '0' && c <= '9' || c == '.' {
continue
}
return v.Value
}
if _, err := parseFloat(v.Value, &f); err == nil {
return f
}
return v.Value
case CustomFieldTypeBoolean:
return v.Value == "true" || v.Value == "1"
case CustomFieldTypeDate:
t, err := time.Parse("2006-01-02", v.Value)
if err == nil {
return t.Format("2006-01-02")
}
return v.Value
default:
return v.Value
}
}
func parseFloat(s string, f *float64) (int, error) {
var sign, decimals int
varMantissa := 0
*f = 0
i := 0
if i < len(s) && s[i] == '-' {
sign = 1
i++
}
for ; i < len(s); i++ {
c := s[i]
if c == '.' {
decimals = 1
continue
}
if c < '0' || c > '9' {
return i, nil
}
n := float64(c - '0')
*f = *f*10 + n
varMantissa++
}
if decimals > 0 {
for ; decimals > 0; decimals-- {
*f /= 10
}
}
if sign == 1 {
*f = -*f
}
return i, nil
}