From 1ddd05302be394fe768e806678304c6cc2cc18df Mon Sep 17 00:00:00 2001 From: RockYang Date: Thu, 4 Jan 2024 18:15:00 +0800 Subject: [PATCH] feat: update api key last_use_time after dalle3 call --- api/core/types/config.go | 29 ++++++++++++++-------------- api/handler/function_handler.go | 2 ++ api/handler/user_handler.go | 32 +++++++++++-------------------- api/utils/net.go | 4 ++++ database/update-v3.2.4.sql | 3 ++- web/src/views/admin/SysConfig.vue | 30 +++++------------------------ 6 files changed, 39 insertions(+), 61 deletions(-) diff --git a/api/core/types/config.go b/api/core/types/config.go index 4c5bc2c8..54f3e24d 100644 --- a/api/core/types/config.go +++ b/api/core/types/config.go @@ -145,26 +145,27 @@ type ModelAPIConfig struct { } type SystemConfig struct { - Title string `json:"title"` - AdminTitle string `json:"admin_title"` - Models []string `json:"models"` - InitChatCalls int `json:"init_chat_calls"` // 新用户注册赠送对话次数 - InitImgCalls int `json:"init_img_calls"` // 新用户注册赠送绘图次数 - VipMonthCalls int `json:"vip_month_calls"` // VIP 会员每月赠送的对话次数 - VipMonthImgCalls int `json:"vip_month_img_calls"` // VIP 会员每月赠送绘图次数 - EnabledRegister bool `json:"enabled_register"` // 是否启用注册功能,关闭注册功能之后将无法注册 - EnabledMsg bool `json:"enabled_msg"` // 是否启用短信验证码服务 - RewardImg string `json:"reward_img"` // 众筹收款二维码地址 - EnabledReward bool `json:"enabled_reward"` // 启用众筹功能 - ChatCallPrice float64 `json:"chat_call_price"` // 对话单次调用费用 - ImgCallPrice float64 `json:"img_call_price"` // 绘图单次调用费用 + Title string `json:"title"` + AdminTitle string `json:"admin_title"` + InitChatCalls int `json:"init_chat_calls"` // 新用户注册赠送对话次数 + InitImgCalls int `json:"init_img_calls"` // 新用户注册赠送绘图次数 + VipMonthCalls int `json:"vip_month_calls"` // VIP 会员每月赠送的对话次数 + VipMonthImgCalls int `json:"vip_month_img_calls"` // VIP 会员每月赠送绘图次数 + + EnabledRegister bool `json:"enabled_register"` // 是否启用注册功能,关闭注册功能之后将无法注册 + RegisterWays []string `json:"register_ways"` // 注册方式:支持手机,邮箱注册 + + RewardImg string `json:"reward_img"` // 众筹收款二维码地址 + EnabledReward bool `json:"enabled_reward"` // 启用众筹功能 + ChatCallPrice float64 `json:"chat_call_price"` // 对话单次调用费用 + ImgCallPrice float64 `json:"img_call_price"` // 绘图单次调用费用 + EnabledAlipay bool `json:"enabled_alipay"` // 是否启用支付宝支付通道 OrderPayTimeout int `json:"order_pay_timeout"` //订单支付超时时间 DefaultModels []string `json:"default_models"` // 默认开通的 AI 模型 OrderPayInfoText string `json:"order_pay_info_text"` // 订单支付页面说明文字 InviteChatCalls int `json:"invite_chat_calls"` // 邀请用户注册奖励对话次数 InviteImgCalls int `json:"invite_img_calls"` // 邀请用户注册奖励绘图次数 - ForceInvite bool `json:"force_invite"` // 是否强制必须使用邀请码才能注册 ShowDemoNotice bool `json:"show_demo_notice"` // 显示演示站公告 } diff --git a/api/handler/function_handler.go b/api/handler/function_handler.go index de35dfdb..de1cd35d 100644 --- a/api/handler/function_handler.go +++ b/api/handler/function_handler.go @@ -262,6 +262,8 @@ func (h *FunctionHandler) Dall3(c *gin.Context) { resp.ERROR(c, "请求 OpenAI API 失败: "+errRes.Error.Message) return } + // 更新 API KEY 的最后使用时间 + h.db.Model(&apiKey).UpdateColumn("last_used_at", time.Now().Unix()) // 存储图片 imgURL, err := h.uploadManager.GetUploadHandler().PutImg(res.Data[0].Url, false) if err != nil { diff --git a/api/handler/user_handler.go b/api/handler/user_handler.go index 8f4f584a..152924f9 100644 --- a/api/handler/user_handler.go +++ b/api/handler/user_handler.go @@ -61,22 +61,15 @@ func (h *UserHandler) Register(c *gin.Context) { // 检查验证码 key := CodeStorePrefix + data.Mobile - if h.App.SysConfig.EnabledMsg { - code, err := h.redis.Get(c, key).Result() - if err != nil || code != data.Code { - resp.ERROR(c, "短信验证码错误") - return - } + code, err := h.redis.Get(c, key).Result() + if err != nil || code != data.Code { + resp.ERROR(c, "短信验证码错误") + return } // 验证邀请码 inviteCode := model.InviteCode{} - if data.InviteCode == "" { - if h.App.SysConfig.ForceInvite { - resp.ERROR(c, "当前系统设定必须使用邀请码才能注册") - return - } - } else { + if data.InviteCode != "" { res := h.db.Where("code = ?", data.InviteCode).First(&inviteCode) if res.Error != nil { resp.ERROR(c, "无效的邀请码") @@ -139,9 +132,8 @@ func (h *UserHandler) Register(c *gin.Context) { Reward: utils.JsonEncode(types.InviteReward{ChatCalls: h.App.SysConfig.InviteChatCalls, ImgCalls: h.App.SysConfig.InviteImgCalls}), }) } - if h.App.SysConfig.EnabledMsg { - _ = h.redis.Del(c, key) // 注册成功,删除短信验证码 - } + + _ = h.redis.Del(c, key) // 注册成功,删除短信验证码 // 自动登录创建 token token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ @@ -374,12 +366,10 @@ func (h *UserHandler) ResetPass(c *gin.Context) { // 检查验证码 key := CodeStorePrefix + data.Mobile - if h.App.SysConfig.EnabledMsg { - code, err := h.redis.Get(c, key).Result() - if err != nil || code != data.Code { - resp.ERROR(c, "短信验证码错误") - return - } + code, err := h.redis.Get(c, key).Result() + if err != nil || code != data.Code { + resp.ERROR(c, "短信验证码错误") + return } password := utils.GenPassword(data.Password, user.Salt) diff --git a/api/utils/net.go b/api/utils/net.go index 288c4f12..1746c2ea 100644 --- a/api/utils/net.go +++ b/api/utils/net.go @@ -11,6 +11,7 @@ import ( "io" "net/http" "net/url" + "time" ) var logger = logger2.GetLogger() @@ -121,5 +122,8 @@ func OpenAIRequest(db *gorm.DB, prompt string, proxy string) (string, error) { return "", fmt.Errorf("error with http request: %v%v%s", err, r.Err, errRes.Error.Message) } + // 更新 API KEY 的最后使用时间 + db.Model(&apiKey).UpdateColumn("last_used_at", time.Now().Unix()) + return response.Choices[0].Message.Content, nil } diff --git a/database/update-v3.2.4.sql b/database/update-v3.2.4.sql index 99279408..7644a093 100644 --- a/database/update-v3.2.4.sql +++ b/database/update-v3.2.4.sql @@ -4,4 +4,5 @@ ALTER TABLE `chatgpt_api_keys` ADD `api_url` VARCHAR(255) NULL COMMENT 'API 地 ALTER TABLE `chatgpt_api_keys` DROP INDEX `value`; ALTER TABLE `chatgpt_mj_jobs` ADD UNIQUE(`task_id`); ALTER TABLE `chatgpt_api_keys` ADD `use_proxy` TINYINT(1) NULL COMMENT '是否使用代理访问' AFTER `enabled`; -ALTER TABLE `chatgpt_api_keys` ADD `name` VARCHAR(30) NULL COMMENT '名称' AFTER `platform`; \ No newline at end of file +ALTER TABLE `chatgpt_api_keys` ADD `name` VARCHAR(30) NULL COMMENT '名称' AFTER `platform`; +ALTER TABLE `chatgpt_users` ADD `email` VARCHAR(100) NULL COMMENT '邮箱地址' AFTER `mobile`; \ No newline at end of file diff --git a/web/src/views/admin/SysConfig.vue b/web/src/views/admin/SysConfig.vue index 2046d352..975fd27a 100644 --- a/web/src/views/admin/SysConfig.vue +++ b/web/src/views/admin/SysConfig.vue @@ -30,31 +30,11 @@ - - - - - - - - - - - - - - - + + + 手机注册 + 邮箱注册 +