Fix the api filtering method

This commit is contained in:
2025-10-25 20:52:50 +08:00
parent a3eebbde92
commit f41e3dac21
4 changed files with 99 additions and 148 deletions

View File

@@ -1,12 +1,7 @@
package models
import (
"crypto/rand"
"encoding/hex"
"strings"
"time"
"gorm.io/gorm"
)
// API 接口表模型
@@ -21,9 +16,6 @@ type API struct {
// API类型int型
APIType int `gorm:"not null;comment:API类型" json:"api_type"`
// API密钥
APIKey string `gorm:"size:255;not null;uniqueIndex;comment:API密钥唯一标识" json:"api_key"`
// 应用UUID关联到App表
AppUUID string `gorm:"size:36;not null;index;comment:关联的应用UUID" json:"app_uuid"`
@@ -55,17 +47,6 @@ type API struct {
UpdatedAt time.Time `gorm:"comment:更新时间" json:"updated_at"`
}
// BeforeCreate 在创建记录前自动生成API密钥
func (api *API) BeforeCreate(tx *gorm.DB) error {
if api.APIKey == "" {
// 生成16位大写十六进制API密钥
bytes := make([]byte, 8) // 8字节 = 16位十六进制字符
rand.Read(bytes)
api.APIKey = strings.ToUpper(hex.EncodeToString(bytes))
}
return nil
}
// TableName 指定表名
func (API) TableName() string {
return "apis"