From dffdbf697b5b7d001a917ea71e267393f7aeeb53 Mon Sep 17 00:00:00 2001 From: GeekMaster Date: Tue, 6 May 2025 18:51:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7=E8=A1=A8?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=BF=81=E7=A7=BBbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/core/app_server.go | 14 +++++++++++ api/handler/admin/user_handler.go | 3 ++- api/handler/user_handler.go | 16 ++++++------ api/store/model/user.go | 42 +++++++++++++++---------------- api/store/mysql.go | 5 ++-- 5 files changed, 49 insertions(+), 31 deletions(-) diff --git a/api/core/app_server.go b/api/core/app_server.go index 14188562..3b97a3b4 100644 --- a/api/core/app_server.go +++ b/api/core/app_server.go @@ -99,6 +99,20 @@ func (s *AppServer) Run(db *gorm.DB) error { &model.UserLoginLog{}, &model.DallJob{}, ) + // 手动删除字段 + if db.Migrator().HasColumn(&model.Order{}, "deleted_at") { + db.Migrator().DropColumn(&model.Order{}, "deleted_at") + } + if db.Migrator().HasColumn(&model.ChatItem{}, "deleted_at") { + db.Migrator().DropColumn(&model.ChatItem{}, "deleted_at") + } + if db.Migrator().HasColumn(&model.ChatMessage{}, "deleted_at") { + db.Migrator().DropColumn(&model.ChatMessage{}, "deleted_at") + } + if db.Migrator().HasColumn(&model.User{}, "chat_config") { + db.Migrator().DropColumn(&model.User{}, "chat_config") + } + logger.Info("Database tables migrated successfully") // 统计安装信息 diff --git a/api/handler/admin/user_handler.go b/api/handler/admin/user_handler.go index 6f8a59fa..90ff4b20 100644 --- a/api/handler/admin/user_handler.go +++ b/api/handler/admin/user_handler.go @@ -178,6 +178,7 @@ func (h *UserHandler) Save(c *gin.Context) { Power: data.Power, Status: true, ChatRoles: utils.JsonEncode(data.ChatRoles), + ChatConfig: "{}", ChatModels: utils.JsonEncode(data.ChatModels), ExpiredTime: utils.Str2stamp(data.ExpiredTime), } @@ -353,4 +354,4 @@ func (h *UserHandler) GenLoginLink(c *gin.Context) { } resp.SUCCESS(c, tokenString) -} \ No newline at end of file +} diff --git a/api/handler/user_handler.go b/api/handler/user_handler.go index dee60047..60037952 100644 --- a/api/handler/user_handler.go +++ b/api/handler/user_handler.go @@ -137,13 +137,15 @@ func (h *UserHandler) Register(c *gin.Context) { salt := utils.RandString(8) user := model.User{ - Username: data.Username, - Password: utils.GenPassword(data.Password, salt), - Avatar: "/images/avatar/user.png", - Salt: salt, - Status: true, - ChatRoles: utils.JsonEncode([]string{"gpt"}), // 默认只订阅通用助手角色 - Power: h.App.SysConfig.InitPower, + Username: data.Username, + Password: utils.GenPassword(data.Password, salt), + Avatar: "/images/avatar/user.png", + Salt: salt, + Status: true, + ChatRoles: utils.JsonEncode([]string{"gpt"}), // 默认只订阅通用助手角色 + ChatConfig: "{}", + ChatModels: "{}", + Power: h.App.SysConfig.InitPower, } // check if the username is existing diff --git a/api/store/model/user.go b/api/store/model/user.go index 3a6fda61..a1866ffb 100644 --- a/api/store/model/user.go +++ b/api/store/model/user.go @@ -5,27 +5,27 @@ import ( ) type User struct { - Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` - Username string `gorm:"column:username;type:varchar(30);uniqueIndex;not null;comment:用户名" json:"username"` - Mobile string `gorm:"column:mobile;type:char(11);comment:手机号" json:"mobile"` - Email string `gorm:"column:email;type:varchar(50);comment:邮箱地址" json:"email"` - Nickname string `gorm:"column:nickname;type:varchar(30);not null;comment:昵称" json:"nickname"` - Password string `gorm:"column:password;type:char(64);not null;comment:密码" json:"password"` - Avatar string `gorm:"column:avatar;type:varchar(255);not null;comment:头像" json:"avatar"` - Salt string `gorm:"column:salt;type:char(12);not null;comment:密码盐" json:"salt"` - Power int `gorm:"column:power;type:int;not null;default:0;comment:剩余算力" json:"power"` - ExpiredTime int64 `gorm:"column:expired_time;type:int;not null;comment:用户过期时间" json:"expired_time"` - Status bool `gorm:"column:status;type:tinyint(1);not null;comment:当前状态" json:"status"` - ChatConfig string `gorm:"column:chat_config;type:text;not null;comment:聊天配置json" json:"chat_config"` - ChatRoles string `gorm:"column:chat_roles_json;type:text;not null;comment:聊天角色 json" json:"chat_roles_json"` - ChatModels string `gorm:"column:chat_models_json;type:text;not null;comment:AI模型 json" json:"chat_models_json"` - LastLoginAt int64 `gorm:"column:last_login_at;type:int;not null;comment:最后登录时间" json:"last_login_at"` - Vip bool `gorm:"column:vip;type:tinyint(1);not null;default:0;comment:是否会员" json:"vip"` - LastLoginIp string `gorm:"column:last_login_ip;type:char(16);not null;comment:最后登录 IP" json:"last_login_ip"` - OpenId string `gorm:"column:openid;type:varchar(100);comment:第三方登录账号ID" json:"openid"` - Platform string `gorm:"column:platform;type:varchar(30);comment:登录平台" json:"platform"` - 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"` + Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` + Username string `gorm:"column:username;type:varchar(30);uniqueIndex;not null;comment:用户名" json:"username"` + Mobile string `gorm:"column:mobile;type:char(11);comment:手机号" json:"mobile"` + Email string `gorm:"column:email;type:varchar(50);comment:邮箱地址" json:"email"` + Nickname string `gorm:"column:nickname;type:varchar(30);not null;comment:昵称" json:"nickname"` + Password string `gorm:"column:password;type:char(64);not null;comment:密码" json:"password"` + Avatar string `gorm:"column:avatar;type:varchar(255);not null;comment:头像" json:"avatar"` + Salt string `gorm:"column:salt;type:char(12);not null;comment:密码盐" json:"salt"` + Power int `gorm:"column:power;type:int;default:0;comment:剩余算力" json:"power"` + ExpiredTime int64 `gorm:"column:expired_time;type:int;not null;comment:用户过期时间" json:"expired_time"` + Status bool `gorm:"column:status;type:tinyint(1);not null;comment:当前状态" json:"status"` + ChatConfig string `gorm:"column:chat_config_json;type:text;default:null;comment:聊天配置json" json:"chat_config"` + ChatRoles string `gorm:"column:chat_roles_json;type:text;default:null;comment:聊天角色 json" json:"chat_roles"` + ChatModels string `gorm:"column:chat_models_json;type:text;default:null;comment:AI模型 json" json:"chat_models"` + LastLoginAt int64 `gorm:"column:last_login_at;type:int;not null;comment:最后登录时间" json:"last_login_at"` + Vip bool `gorm:"column:vip;type:tinyint(1);not null;default:0;comment:是否会员" json:"vip"` + LastLoginIp string `gorm:"column:last_login_ip;type:char(16);not null;comment:最后登录 IP" json:"last_login_ip"` + OpenId string `gorm:"column:openid;type:varchar(100);comment:第三方登录账号ID" json:"openid"` + Platform string `gorm:"column:platform;type:varchar(30);comment:登录平台" json:"platform"` + 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"` } func (m *User) TableName() string { diff --git a/api/store/mysql.go b/api/store/mysql.go index 70aba960..bc4707a5 100644 --- a/api/store/mysql.go +++ b/api/store/mysql.go @@ -9,11 +9,12 @@ package store import ( "geekai/core/types" + "time" + "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" - "time" ) func NewGormConfig() *gorm.Config { @@ -36,9 +37,9 @@ func NewMysql(config *gorm.Config, appConfig *types.AppConfig) (*gorm.DB, error) if err != nil { return nil, err } + sqlDB.SetMaxIdleConns(32) sqlDB.SetMaxOpenConns(512) sqlDB.SetConnMaxLifetime(time.Hour) - return db, nil }