From d68f2ef12c99084df400aa79bfcd775293a984dd Mon Sep 17 00:00:00 2001 From: RockYang Date: Fri, 24 Nov 2023 12:02:28 +0800 Subject: [PATCH] feat: add support for registing use force use invite code --- api/core/types/config.go | 13 +++---- api/handler/sms_handler.go | 10 ------ api/handler/user_handler.go | 7 +++- api/main.go | 1 - api/service/fun/func_mj.go | 9 ++++- api/service/fun/function.go | 2 +- web/src/views/Invitation.vue | 58 ++++++++++++++++++------------- web/src/views/Register.vue | 18 ++++++++-- web/src/views/admin/SysConfig.vue | 13 +++++++ 9 files changed, 83 insertions(+), 48 deletions(-) diff --git a/api/core/types/config.go b/api/core/types/config.go index 0b1c568c..5023a413 100644 --- a/api/core/types/config.go +++ b/api/core/types/config.go @@ -140,12 +140,12 @@ 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"` // 会员每个赠送的调用次数 - EnabledRegister bool `json:"enabled_register"` - EnabledMsg bool `json:"enabled_msg"` // 启用短信验证码服务 - EnabledDraw bool `json:"enabled_draw"` // 启动 AI 绘画功能 + InitChatCalls int `json:"init_chat_calls"` // 新用户注册赠送对话次数 + InitImgCalls int `json:"init_img_calls"` // 新用户注册赠送绘图次数 + VipMonthCalls int `json:"vip_month_calls"` // 会员每个赠送的调用次数 + EnabledRegister bool `json:"enabled_register"` // 是否启用注册功能,关闭注册功能之后将无法注册 + EnabledMsg bool `json:"enabled_msg"` // 是否启用短信验证码服务 + EnabledDraw bool `json:"enabled_draw"` // 是否启用 AI 绘画功能 RewardImg string `json:"reward_img"` // 众筹收款二维码地址 EnabledFunction bool `json:"enabled_function"` // 启用 API 函数功能 EnabledReward bool `json:"enabled_reward"` // 启用众筹功能 @@ -155,4 +155,5 @@ type SystemConfig struct { OrderPayInfoText string `json:"order_pay_info_text"` // 订单支付页面说明文字 InviteChatCalls int `json:"invite_chat_calls"` // 邀请用户注册奖励对话次数 InviteImgCalls int `json:"invite_img_calls"` // 邀请用户注册奖励绘图次数 + ForceInvite bool `json:"force_invite"` // 是否强制必须使用邀请码才能注册 } diff --git a/api/handler/sms_handler.go b/api/handler/sms_handler.go index 07e26067..9ccb4e3d 100644 --- a/api/handler/sms_handler.go +++ b/api/handler/sms_handler.go @@ -58,13 +58,3 @@ func (h *SmsHandler) SendCode(c *gin.Context) { resp.SUCCESS(c) } - -type statusVo struct { - EnabledMsgService bool `json:"enabled_msg_service"` - EnabledRegister bool `json:"enabled_register"` -} - -// Status check if the message service is enabled -func (h *SmsHandler) Status(c *gin.Context) { - resp.SUCCESS(c, statusVo{EnabledMsgService: h.App.SysConfig.EnabledMsg, EnabledRegister: h.App.SysConfig.EnabledRegister}) -} diff --git a/api/handler/user_handler.go b/api/handler/user_handler.go index e43ea064..9a64a37e 100644 --- a/api/handler/user_handler.go +++ b/api/handler/user_handler.go @@ -71,7 +71,12 @@ func (h *UserHandler) Register(c *gin.Context) { // 验证邀请码 inviteCode := model.InviteCode{} - if data.InviteCode != "" { + if data.InviteCode == "" { + if h.App.SysConfig.ForceInvite { + resp.ERROR(c, "当前系统设定必须使用邀请码才能注册") + return + } + } else { res := h.db.Where("code = ?", data.InviteCode).First(&inviteCode) if res.Error != nil { resp.ERROR(c, "无效的邀请码") diff --git a/api/main.go b/api/main.go index 7aea70d0..f81976fa 100644 --- a/api/main.go +++ b/api/main.go @@ -237,7 +237,6 @@ func main() { }), fx.Invoke(func(s *core.AppServer, h *handler.SmsHandler) { group := s.Engine.Group("/api/sms/") - group.GET("status", h.Status) group.POST("code", h.SendCode) }), fx.Invoke(func(s *core.AppServer, h *handler.CaptchaHandler) { diff --git a/api/service/fun/func_mj.go b/api/service/fun/func_mj.go index 0b5899db..bbd83554 100644 --- a/api/service/fun/func_mj.go +++ b/api/service/fun/func_mj.go @@ -4,6 +4,7 @@ import ( "chatplus/core/types" "chatplus/service/mj" "chatplus/utils" + "errors" ) // AI 绘画函数 @@ -11,15 +12,21 @@ import ( type FuncMidJourney struct { name string service *mj.Service + config types.MidJourneyConfig } -func NewMidJourneyFunc(mjService *mj.Service) FuncMidJourney { +func NewMidJourneyFunc(mjService *mj.Service, config types.MidJourneyConfig) FuncMidJourney { return FuncMidJourney{ name: "MidJourney AI 绘画", + config: config, service: mjService} } func (f FuncMidJourney) Invoke(params map[string]interface{}) (string, error) { + if !f.config.Enabled { + return "", errors.New("MidJourney AI 绘画功能没有启用") + } + logger.Infof("MJ 绘画参数:%+v", params) prompt := utils.InterfaceToString(params["prompt"]) f.service.PushTask(types.MjTask{ diff --git a/api/service/fun/function.go b/api/service/fun/function.go index 76e523bd..8b33dcbb 100644 --- a/api/service/fun/function.go +++ b/api/service/fun/function.go @@ -34,6 +34,6 @@ func NewFunctions(config *types.AppConfig, mjService *mj.Service) map[string]Fun types.FuncZaoBao: NewZaoBao(config.ApiConfig), types.FuncWeibo: NewWeiboHot(config.ApiConfig), types.FuncHeadLine: NewHeadLines(config.ApiConfig), - types.FuncMidJourney: NewMidJourneyFunc(mjService), + types.FuncMidJourney: NewMidJourneyFunc(mjService, config.MjConfig), } } diff --git a/web/src/views/Invitation.vue b/web/src/views/Invitation.vue index 57a2c8b7..2667437d 100644 --- a/web/src/views/Invitation.vue +++ b/web/src/views/Invitation.vue @@ -78,7 +78,7 @@

您推荐用户

- +
@@ -91,6 +91,8 @@ import {httpGet} from "@/utils/http"; import {ElMessage} from "element-plus"; import Clipboard from "clipboard"; import InviteList from "@/components/InviteList.vue"; +import {checkSession} from "@/action/session"; +import {useRouter} from "vue-router"; const inviteURL = ref("") const qrImg = ref("") @@ -100,34 +102,40 @@ const users = ref([]) const hits = ref(0) const regNum = ref(0) const rate = ref(0) - +const router = useRouter() +const isLogin = ref(false) onMounted(() => { - httpGet("/api/invite/code").then(res => { - const text = `${location.protocol}//${location.host}/register?invite_code=${res.data.code}` - hits.value = res.data["hits"] - regNum.value = res.data["reg_num"] - if (hits.value > 0) { - rate.value = ((regNum.value / hits.value) * 100).toFixed(2) - } - QRCode.toDataURL(text, {width: 400, height: 400, margin: 2}, (error, url) => { - if (error) { - console.error(error) - } else { - qrImg.value = url; + checkSession().then(() => { + isLogin.value = true + httpGet("/api/invite/code").then(res => { + const text = `${location.protocol}//${location.host}/register?invite_code=${res.data.code}` + hits.value = res.data["hits"] + regNum.value = res.data["reg_num"] + if (hits.value > 0) { + rate.value = ((regNum.value / hits.value) * 100).toFixed(2) } - }); - inviteURL.value = text - }).catch(e => { - ElMessage.error("获取邀请码失败:" + e.message) - }) + QRCode.toDataURL(text, {width: 400, height: 400, margin: 2}, (error, url) => { + if (error) { + console.error(error) + } else { + qrImg.value = url; + } + }); + inviteURL.value = text + }).catch(e => { + ElMessage.error("获取邀请码失败:" + e.message) + }) - httpGet("/api/admin/config/get?key=system").then(res => { - inviteChatCalls.value = res.data["invite_chat_calls"] - inviteImgCalls.value = res.data["invite_img_calls"] - }).catch(e => { - ElMessage.error("获取系统配置失败:" + e.message) - }) + httpGet("/api/admin/config/get?key=system").then(res => { + inviteChatCalls.value = res.data["invite_chat_calls"] + inviteImgCalls.value = res.data["invite_img_calls"] + }).catch(e => { + ElMessage.error("获取系统配置失败:" + e.message) + }) + }).catch(() => { + router.push('/login') + }); // 复制链接 const clipboard = new Clipboard('.copy-link'); diff --git a/web/src/views/Register.vue b/web/src/views/Register.vue index 3940d435..fe32612d 100644 --- a/web/src/views/Register.vue +++ b/web/src/views/Register.vue @@ -118,7 +118,7 @@ import {ref} from "vue"; import {Checked, Iphone, Lock, Message} from "@element-plus/icons-vue"; import {httpGet, httpPost} from "@/utils/http"; -import {ElMessage} from "element-plus"; +import {ElMessage, ElNotification} from "element-plus"; import {useRouter} from "vue-router"; import FooterBar from "@/components/FooterBar.vue"; import SendMsg from "@/components/SendMsg.vue"; @@ -141,11 +141,23 @@ const enableMsg = ref(false) const enableRegister = ref(true) const wxImg = ref("/images/wx.png") -httpGet('/api/sms/status').then(res => { +httpGet("/api/admin/config/get?key=system").then(res => { if (res.data) { - enableMsg.value = res.data['enabled_msg_service'] + enableMsg.value = res.data['enabled_msg'] enableRegister.value = res.data['enabled_register'] + console.log(res.data) + if (res.data['force_invite']) { + ElNotification({ + title: '提示:', + dangerouslyUseHTMLString: true, + message: '当前系统开启了强制邀请注册功能,必须有邀请码才能注册哦。扫描下面二维码获取邀请码。
qrcode', + type: 'info', + duration: 0, + }) + } } +}).catch(e => { + ElMessage.error("获取系统配置失败:" + e.message) }) httpGet("/api/invite/hits", {code: formData.value.invite_code}).then(() => { diff --git a/web/src/views/admin/SysConfig.vue b/web/src/views/admin/SysConfig.vue index 3255afcc..13751a62 100644 --- a/web/src/views/admin/SysConfig.vue +++ b/web/src/views/admin/SysConfig.vue @@ -27,6 +27,19 @@ + + + + + + + +