From 3531c7f356ca50e9c64586be520d68157108c0d4 Mon Sep 17 00:00:00 2001 From: RockYang Date: Mon, 1 Sep 2025 07:35:37 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/core/types/moderation.go | 1 + api/handler/product_handler.go | 8 +-- api/service/migration_service.go | 58 +++++++++++++++++++ api/store/model/admin_user.go | 20 +++---- api/store/model/api_key.go | 8 +-- api/store/model/app_type.go | 6 +- api/store/model/chat_app.go | 2 +- api/store/model/chat_item.go | 2 +- api/store/model/chat_message.go | 2 +- api/store/model/chat_model.go | 2 +- api/store/model/config.go | 2 +- api/store/model/dalle_job.go | 2 +- api/store/model/file.go | 2 +- api/store/model/function.go | 6 +- api/store/model/invite_code.go | 2 +- api/store/model/invite_log.go | 2 +- api/store/model/jimeng_job.go | 2 +- api/store/model/menu.go | 14 ++--- api/store/model/mj_job.go | 2 +- api/store/model/moderation.go | 16 +++++ api/store/model/order.go | 2 +- api/store/model/power_log.go | 2 +- api/store/model/product.go | 2 +- api/store/model/redeem.go | 2 +- api/store/model/sd_job.go | 2 +- api/store/model/suno_job.go | 2 +- api/store/model/user.go | 2 +- api/store/model/user_login_log.go | 2 +- api/store/model/video_job.go | 2 +- api/store/mysql.go | 4 +- .../views/admin/settings/ModerationConfig.vue | 12 ++-- 31 files changed, 133 insertions(+), 60 deletions(-) create mode 100644 api/store/model/moderation.go diff --git a/api/core/types/moderation.go b/api/core/types/moderation.go index 835c9224..20a9eb94 100644 --- a/api/core/types/moderation.go +++ b/api/core/types/moderation.go @@ -11,6 +11,7 @@ package types type ModerationConfig struct { Enable bool `json:"enable"` // 是否启用文本审查 Active string `json:"active"` + EnableGuide bool `json:"enable_guide"` // 是否启用模型引导提示词 GuidePrompt string `json:"guide_prompt"` // 模型引导提示词 Gitee ModerationGiteeConfig `json:"gitee"` Baidu ModerationBaiduConfig `json:"baidu"` diff --git a/api/handler/product_handler.go b/api/handler/product_handler.go index bd873b05..3558f693 100644 --- a/api/handler/product_handler.go +++ b/api/handler/product_handler.go @@ -9,7 +9,6 @@ package handler import ( "geekai/core" - "geekai/core/middleware" "geekai/store/model" "geekai/store/vo" "geekai/utils" @@ -30,12 +29,7 @@ func NewProductHandler(app *core.AppServer, db *gorm.DB) *ProductHandler { // RegisterRoutes 注册路由 func (h *ProductHandler) RegisterRoutes() { group := h.App.Engine.Group("/api/product/") - - // 需要用户授权的接口 - group.Use(middleware.UserAuthMiddleware(h.App.Config.Session.SecretKey, h.App.Redis)) - { - group.GET("list", h.List) - } + group.GET("list", h.List) } // List 模型列表 diff --git a/api/service/migration_service.go b/api/service/migration_service.go index aaae62c9..d80eb731 100644 --- a/api/service/migration_service.go +++ b/api/service/migration_service.go @@ -51,6 +51,7 @@ func (s *MigrationService) StartMigrate() { s.MigrateConfig(s.appConfig) s.TableMigration() s.MigrateLicense() + s.TableRename() }() } @@ -168,6 +169,63 @@ func (s *MigrationService) TableMigration() { } } +// 遍历所有的数据表,如果表名中包含 chatgpt_,则重命名为 geekai_,只执行一次 +func (s *MigrationService) TableRename() { + key := "migrate:table_rename" + if s.redisClient.Get(context.Background(), key).Val() == "1" { + logger.Info("数据表重命名已执行,跳过重命名") + return + } + + logger.Info("开始重命名数据表...") + + // 定义需要重命名的表映射 + tableRenames := map[string]string{ + "chatgpt_users": "geekai_users", + "chatgpt_orders": "geekai_orders", + "chatgpt_products": "geekai_products", + "chatgpt_configs": "geekai_configs", + "chatgpt_sd_jobs": "geekai_sd_jobs", + "chatgpt_mj_jobs": "geekai_mj_jobs", + "chatgpt_suno_jobs": "geekai_suno_jobs", + "chatgpt_dall_jobs": "geekai_dall_jobs", + "chatgpt_video_jobs": "geekai_video_jobs", + "chatgpt_jimeng_jobs": "geekai_jimeng_jobs", + "chatgpt_files": "geekai_files", + "chatgpt_menus": "geekai_menus", + "chatgpt_functions": "geekai_functions", + "chatgpt_invite_codes": "geekai_invite_codes", + "chatgpt_invite_logs": "geekai_invite_logs", + "chatgpt_redeems": "geekai_redeems", + "chatgpt_power_logs": "geekai_power_logs", + "chatgpt_user_login_logs": "geekai_user_login_logs", + } + + // 执行重命名操作 + for oldTableName, newTableName := range tableRenames { + // 检查旧表是否存在 + if s.db.Migrator().HasTable(oldTableName) { + // 检查新表是否已存在 + if !s.db.Migrator().HasTable(newTableName) { + err := s.db.Exec(fmt.Sprintf("ALTER TABLE %s RENAME TO %s", oldTableName, newTableName)).Error + if err != nil { + logger.Errorf("重命名数据表 %s 到 %s 失败: %v", oldTableName, newTableName, err) + } else { + logger.Infof("成功重命名数据表: %s -> %s", oldTableName, newTableName) + } + } else { + logger.Infof("目标表 %s 已存在,跳过重命名 %s", newTableName, oldTableName) + } + } else { + logger.Infof("源表 %s 不存在,跳过重命名", oldTableName) + } + } + + // 标记重命名已完成 + s.redisClient.Set(context.Background(), key, "1", 0) + logger.Info("数据表重命名完成") +} + // 迁移配置数据 func (s *MigrationService) MigrateConfig(config *types.AppConfig) error { diff --git a/api/store/model/admin_user.go b/api/store/model/admin_user.go index 066f0462..2978cb70 100644 --- a/api/store/model/admin_user.go +++ b/api/store/model/admin_user.go @@ -5,18 +5,18 @@ import ( ) type AdminUser struct { - Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` - Username string `gorm:"column:username;type:varchar(30);uniqueIndex;not null;comment:用户名" json:"username"` - Password string `gorm:"column:password;type:char(64);not null;comment:密码" json:"password"` - Salt string `gorm:"column:salt;type:char(12);not null;comment:密码盐" json:"salt"` - Status bool `gorm:"column:status;type:tinyint(1);not null;comment:当前状态" json:"status"` - LastLoginAt int64 `gorm:"column:last_login_at;type:int;not null;comment:最后登录时间" json:"last_login_at"` - LastLoginIp string `gorm:"column:last_login_ip;type:char(16);not null;comment:最后登录 IP" json:"last_login_ip"` - CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null;comment:创建时间" json:"created_at"` - UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;not null;comment:更新时间" json:"updated_at"` + Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` + Username string `gorm:"column:username;type:varchar(30);uniqueIndex;not null;comment:用户名" json:"username"` + Password string `gorm:"column:password;type:char(64);not null;comment:密码" json:"password"` + Salt string `gorm:"column:salt;type:char(12);not null;comment:密码盐" json:"salt"` + Status bool `gorm:"column:status;type:tinyint(1);not null;comment:当前状态" json:"status"` + LastLoginAt int64 `gorm:"column:last_login_at;type:int;not null;comment:最后登录时间" json:"last_login_at"` + LastLoginIp string `gorm:"column:last_login_ip;type:char(16);not null;comment:最后登录 IP" json:"last_login_ip"` + CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null;comment:创建时间" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;not null;comment:更新时间" json:"updated_at"` } // TableName 表名 func (m *AdminUser) TableName() string { - return "chatgpt_admin_users" + return "geekai_admin_users" } diff --git a/api/store/model/api_key.go b/api/store/model/api_key.go index e020435f..0ddd3c7b 100644 --- a/api/store/model/api_key.go +++ b/api/store/model/api_key.go @@ -6,13 +6,13 @@ import ( // ApiKey OpenAI API 模型 type ApiKey struct { - Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` + Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` Name string `gorm:"column:name;type:varchar(30);comment:名称" json:"name"` Value string `gorm:"column:value;type:varchar(255);not null;comment:API KEY value" json:"value"` Type string `gorm:"column:type;type:varchar(10);default:chat;not null;comment:用途(chat=>聊天,img=>图片)" json:"type"` - LastUsedAt int64 `gorm:"column:last_used_at;type:int;not null;comment:最后使用时间" json:"last_used_at"` + LastUsedAt int64 `gorm:"column:last_used_at;type:int;not null;comment:最后使用时间" json:"last_used_at"` ApiURL string `gorm:"column:api_url;type:varchar(255);comment:API 地址" json:"api_url"` - Enabled bool `gorm:"column:enabled;type:tinyint(1);comment:是否启用" json:"enabled"` + Enabled bool `gorm:"column:enabled;type:tinyint(1);comment:是否启用" json:"enabled"` ProxyURL string `gorm:"column:proxy_url;type:varchar(100);comment:代理地址" json:"proxy_url"` CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;not null" json:"updated_at"` @@ -20,5 +20,5 @@ type ApiKey struct { // TableName 表名 func (m *ApiKey) TableName() string { - return "chatgpt_api_keys" + return "geekai_api_keys" } diff --git a/api/store/model/app_type.go b/api/store/model/app_type.go index c7db9813..f491a372 100644 --- a/api/store/model/app_type.go +++ b/api/store/model/app_type.go @@ -3,15 +3,15 @@ package model import "time" type AppType struct { - Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` + Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` Name string `gorm:"column:name;type:varchar(50);not null;comment:名称" json:"name"` Icon string `gorm:"column:icon;type:varchar(255);not null;comment:图标URL" json:"icon"` SortNum int `gorm:"column:sort_num;type:tinyint;not null;comment:排序" json:"sort_num"` - Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;comment:是否启用" json:"enabled"` + Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;comment:是否启用" json:"enabled"` CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null" json:"created_at"` } // TableName 表名 func (m *AppType) TableName() string { - return "chatgpt_app_types" + return "geekai_app_types" } diff --git a/api/store/model/chat_app.go b/api/store/model/chat_app.go index ac912a01..9336aca4 100644 --- a/api/store/model/chat_app.go +++ b/api/store/model/chat_app.go @@ -20,5 +20,5 @@ type ChatApp struct { } func (m *ChatApp) TableName() string { - return "chatgpt_chat_roles" + return "geekai_chat_roles" } diff --git a/api/store/model/chat_item.go b/api/store/model/chat_item.go index 2d457bdd..33c8d36c 100644 --- a/api/store/model/chat_item.go +++ b/api/store/model/chat_item.go @@ -17,5 +17,5 @@ type ChatItem struct { } func (m *ChatItem) TableName() string { - return "chatgpt_chat_items" + return "geekai_chat_items" } diff --git a/api/store/model/chat_message.go b/api/store/model/chat_message.go index eccb832b..977dcdee 100644 --- a/api/store/model/chat_message.go +++ b/api/store/model/chat_message.go @@ -21,5 +21,5 @@ type ChatMessage struct { } func (m *ChatMessage) TableName() string { - return "chatgpt_chat_history" + return "geekai_chat_history" } diff --git a/api/store/model/chat_model.go b/api/store/model/chat_model.go index 2e495906..53947b0d 100644 --- a/api/store/model/chat_model.go +++ b/api/store/model/chat_model.go @@ -25,5 +25,5 @@ type ChatModel struct { } func (m *ChatModel) TableName() string { - return "chatgpt_chat_models" + return "geekai_chat_models" } diff --git a/api/store/model/config.go b/api/store/model/config.go index 494311b7..33ccb19b 100644 --- a/api/store/model/config.go +++ b/api/store/model/config.go @@ -7,5 +7,5 @@ type Config struct { } func (m *Config) TableName() string { - return "chatgpt_configs" + return "geekai_configs" } diff --git a/api/store/model/dalle_job.go b/api/store/model/dalle_job.go index aa244f0e..0120f50f 100644 --- a/api/store/model/dalle_job.go +++ b/api/store/model/dalle_job.go @@ -17,5 +17,5 @@ type DallJob struct { } func (m *DallJob) TableName() string { - return "chatgpt_dall_jobs" + return "geekai_dall_jobs" } diff --git a/api/store/model/file.go b/api/store/model/file.go index cb68fafa..fc06a78a 100644 --- a/api/store/model/file.go +++ b/api/store/model/file.go @@ -14,5 +14,5 @@ type File struct { } func (m *File) TableName() string { - return "chatgpt_files" + return "geekai_files" } diff --git a/api/store/model/function.go b/api/store/model/function.go index cb7b18a7..f5fd4d4d 100644 --- a/api/store/model/function.go +++ b/api/store/model/function.go @@ -1,16 +1,16 @@ package model type Function struct { - Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` + Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` Name string `gorm:"column:name;type:varchar(30);uniqueIndex;not null;comment:函数名称" json:"name"` Label string `gorm:"column:label;type:varchar(30);comment:函数标签" json:"label"` Description string `gorm:"column:description;type:varchar(255);comment:函数描述" json:"description"` Parameters string `gorm:"column:parameters;type:text;comment:函数参数(JSON)" json:"parameters"` Token string `gorm:"column:token;type:varchar(255);comment:API授权token" json:"token"` Action string `gorm:"column:action;type:varchar(255);comment:函数处理 API" json:"action"` - Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;default:0;comment:是否启用" json:"enabled"` + Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;default:0;comment:是否启用" json:"enabled"` } func (m *Function) TableName() string { - return "chatgpt_functions" + return "geekai_functions" } diff --git a/api/store/model/invite_code.go b/api/store/model/invite_code.go index f016f74a..2e56475d 100644 --- a/api/store/model/invite_code.go +++ b/api/store/model/invite_code.go @@ -12,5 +12,5 @@ type InviteCode struct { } func (m *InviteCode) TableName() string { - return "chatgpt_invite_codes" + return "geekai_invite_codes" } diff --git a/api/store/model/invite_log.go b/api/store/model/invite_log.go index c36e13c8..75dc7adf 100644 --- a/api/store/model/invite_log.go +++ b/api/store/model/invite_log.go @@ -15,5 +15,5 @@ type InviteLog struct { } func (m *InviteLog) TableName() string { - return "chatgpt_invite_logs" + return "geekai_invite_logs" } diff --git a/api/store/model/jimeng_job.go b/api/store/model/jimeng_job.go index 38e21e2f..624cd90c 100644 --- a/api/store/model/jimeng_job.go +++ b/api/store/model/jimeng_job.go @@ -51,5 +51,5 @@ const ( // TableName 返回数据表名称 func (JimengJob) TableName() string { - return "chatgpt_jimeng_jobs" + return "geekai_jimeng_jobs" } diff --git a/api/store/model/menu.go b/api/store/model/menu.go index 63babb67..b8698e23 100644 --- a/api/store/model/menu.go +++ b/api/store/model/menu.go @@ -2,14 +2,14 @@ package model // Menu 系统菜单 type Menu struct { - Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` - Name string `gorm:"column:name;type:varchar(30);not null;comment:菜单名称" json:"name"` - Icon string `gorm:"column:icon;type:varchar(150);not null;comment:菜单图标" json:"icon"` - URL string `gorm:"column:url;type:varchar(100);not null;comment:地址" json:"url"` - SortNum int `gorm:"column:sort_num;type:smallint;not null;comment:排序" json:"sort_num"` - Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;comment:是否启用" json:"enabled"` + Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` + Name string `gorm:"column:name;type:varchar(30);not null;comment:菜单名称" json:"name"` + Icon string `gorm:"column:icon;type:varchar(150);not null;comment:菜单图标" json:"icon"` + URL string `gorm:"column:url;type:varchar(100);not null;comment:地址" json:"url"` + SortNum int `gorm:"column:sort_num;type:smallint;not null;comment:排序" json:"sort_num"` + Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;comment:是否启用" json:"enabled"` } func (m *Menu) TableName() string { - return "chatgpt_menus" + return "geekai_menus" } diff --git a/api/store/model/mj_job.go b/api/store/model/mj_job.go index 9bbe55fa..1f6cfaff 100644 --- a/api/store/model/mj_job.go +++ b/api/store/model/mj_job.go @@ -24,5 +24,5 @@ type MidJourneyJob struct { } func (m *MidJourneyJob) TableName() string { - return "chatgpt_mj_jobs" + return "geekai_mj_jobs" } diff --git a/api/store/model/moderation.go b/api/store/model/moderation.go new file mode 100644 index 00000000..8b261798 --- /dev/null +++ b/api/store/model/moderation.go @@ -0,0 +1,16 @@ +package model + +import "time" + +type Moderation struct { + Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` + UserId uint `gorm:"column:user_id;type:int(11);not null;comment:用户ID" json:"user_id"` + Input string `gorm:"column:prompt;type:text;not null;comment:用户输入" json:"input"` + Output string `gorm:"column:output;type:text;not null;comment:AI 输出" json:"output"` + Result string `gorm:"column:result;type:text;not null;comment:鉴别结果" json:"result"` + CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null" json:"created_at"` +} + +func (m *Moderation) TableName() string { + return "geekai_moderation" +} diff --git a/api/store/model/order.go b/api/store/model/order.go index 7fb5ce72..67b25bd6 100644 --- a/api/store/model/order.go +++ b/api/store/model/order.go @@ -26,5 +26,5 @@ type Order struct { } func (m *Order) TableName() string { - return "chatgpt_orders" + return "geekai_orders" } diff --git a/api/store/model/power_log.go b/api/store/model/power_log.go index 38220a80..fd34c69f 100644 --- a/api/store/model/power_log.go +++ b/api/store/model/power_log.go @@ -20,5 +20,5 @@ type PowerLog struct { } func (m *PowerLog) TableName() string { - return "chatgpt_power_logs" + return "geekai_power_logs" } diff --git a/api/store/model/product.go b/api/store/model/product.go index 1278ecbb..d5e22206 100644 --- a/api/store/model/product.go +++ b/api/store/model/product.go @@ -18,5 +18,5 @@ type Product struct { } func (m *Product) TableName() string { - return "chatgpt_products" + return "geekai_products" } diff --git a/api/store/model/redeem.go b/api/store/model/redeem.go index 2c0bc089..4d2ef9fa 100644 --- a/api/store/model/redeem.go +++ b/api/store/model/redeem.go @@ -16,5 +16,5 @@ type Redeem struct { } func (m *Redeem) TableName() string { - return "chatgpt_redeems" + return "geekai_redeems" } diff --git a/api/store/model/sd_job.go b/api/store/model/sd_job.go index c681bf96..5d16a244 100644 --- a/api/store/model/sd_job.go +++ b/api/store/model/sd_job.go @@ -19,5 +19,5 @@ type SdJob struct { } func (m *SdJob) TableName() string { - return "chatgpt_sd_jobs" + return "geekai_sd_jobs" } diff --git a/api/store/model/suno_job.go b/api/store/model/suno_job.go index 131ea8a5..583823d2 100644 --- a/api/store/model/suno_job.go +++ b/api/store/model/suno_job.go @@ -31,5 +31,5 @@ type SunoJob struct { } func (m *SunoJob) TableName() string { - return "chatgpt_suno_jobs" + return "geekai_suno_jobs" } diff --git a/api/store/model/user.go b/api/store/model/user.go index a1866ffb..6a47ed59 100644 --- a/api/store/model/user.go +++ b/api/store/model/user.go @@ -29,5 +29,5 @@ type User struct { } func (m *User) TableName() string { - return "chatgpt_users" + return "geekai_users" } diff --git a/api/store/model/user_login_log.go b/api/store/model/user_login_log.go index 2675c43e..434beba3 100644 --- a/api/store/model/user_login_log.go +++ b/api/store/model/user_login_log.go @@ -15,5 +15,5 @@ type UserLoginLog struct { } func (m *UserLoginLog) TableName() string { - return "chatgpt_user_login_logs" + return "geekai_user_login_logs" } diff --git a/api/store/model/video_job.go b/api/store/model/video_job.go index 7eb8b350..7fe8728b 100644 --- a/api/store/model/video_job.go +++ b/api/store/model/video_job.go @@ -23,5 +23,5 @@ type VideoJob struct { } func (m *VideoJob) TableName() string { - return "chatgpt_video_jobs" + return "geekai_video_jobs" } diff --git a/api/store/mysql.go b/api/store/mysql.go index 8233c695..66430bc4 100644 --- a/api/store/mysql.go +++ b/api/store/mysql.go @@ -21,8 +21,8 @@ func NewGormConfig() *gorm.Config { return &gorm.Config{ Logger: logger.Default.LogMode(logger.Warn), NamingStrategy: schema.NamingStrategy{ - TablePrefix: "chatgpt_", // 设置表前缀 - SingularTable: false, // 使用单数表名形式 + TablePrefix: "geekai_", // 设置表前缀 + SingularTable: false, // 使用单数表名形式 }, } } diff --git a/web/src/views/admin/settings/ModerationConfig.vue b/web/src/views/admin/settings/ModerationConfig.vue index 176a0059..a18eda50 100644 --- a/web/src/views/admin/settings/ModerationConfig.vue +++ b/web/src/views/admin/settings/ModerationConfig.vue @@ -72,11 +72,11 @@ - - + + - +