From 3774adf63e287503659fe8332485e81f344621f5 Mon Sep 17 00:00:00 2001 From: mlogclub Date: Sun, 31 May 2020 16:25:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8F=91=E5=B8=96=E5=8A=A0=E4=B8=8A?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/api/topic_controller.go | 20 ++++++-- site/nuxt.config.js | 4 +- site/pages/topic/create.vue | 60 +++++++++++++++++----- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/server/controllers/api/topic_controller.go b/server/controllers/api/topic_controller.go index f553ad3e..8170ef9d 100644 --- a/server/controllers/api/topic_controller.go +++ b/server/controllers/api/topic_controller.go @@ -1,6 +1,8 @@ package api import ( + "bbs-go/common" + "github.com/dchest/captcha" "math/rand" "strings" @@ -36,10 +38,20 @@ func (c *TopicController) PostCreate() *simple.JsonResult { if user == nil { return simple.JsonError(simple.ErrorNotLogin) } - nodeId := simple.FormValueInt64Default(c.Ctx, "nodeId", 0) - title := strings.TrimSpace(simple.FormValue(c.Ctx, "title")) - content := strings.TrimSpace(simple.FormValue(c.Ctx, "content")) - tags := simple.FormValueStringArray(c.Ctx, "tags") + + var ( + captchaId = simple.FormValue(c.Ctx, "captchaId") + captchaCode = simple.FormValue(c.Ctx, "captchaCode") + nodeId = simple.FormValueInt64Default(c.Ctx, "nodeId", 0) + title = strings.TrimSpace(simple.FormValue(c.Ctx, "title")) + content = strings.TrimSpace(simple.FormValue(c.Ctx, "content")) + tags = simple.FormValueStringArray(c.Ctx, "tags") + ) + + if !captcha.VerifyString(captchaId, captchaCode) { + return simple.JsonError(common.CaptchaError) + } + topic, err := services.TopicService.Publish(user.Id, nodeId, tags, title, content) if err != nil { return simple.JsonError(err) diff --git a/site/nuxt.config.js b/site/nuxt.config.js index 51af9671..7403ea0c 100644 --- a/site/nuxt.config.js +++ b/site/nuxt.config.js @@ -108,8 +108,8 @@ export default { }, proxy: { - // '/api/': 'http://localhost:8082' - '/api/': 'https://mlog.club' + '/api/': 'http://localhost:8082' + // '/api/': 'https://mlog.club' }, // Doc: https://github.com/shakee93/vue-toasted diff --git a/site/pages/topic/create.vue b/site/pages/topic/create.vue index 8689298c..9110f3ce 100644 --- a/site/pages/topic/create.vue +++ b/site/pages/topic/create.vue @@ -37,8 +37,8 @@ v-for="node in nodes" :key="node.nodeId" :value="node.nodeId" - >{{ node.name }} + >{{ node.name }} + @@ -62,6 +62,24 @@ +
+
+ + + + +
+
+ +
+
+
Date: Sun, 31 May 2020 16:47:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8F=AF=E9=85=8D=E7=BD=AE=E5=8F=91?= =?UTF-8?q?=E5=B8=96=E6=98=AF=E5=90=A6=E6=B7=BB=E5=8A=A0=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/api/captcha_controller.go | 106 ++++++++++--------- server/controllers/api/topic_controller.go | 2 +- server/model/constants.go | 7 +- server/model/data.go | 1 + server/services/sys_config_service.go | 2 + site/pages/admin/settings/index.vue | 6 ++ site/pages/topic/create.vue | 29 +++-- site/pages/user/signin.vue | 9 +- 8 files changed, 95 insertions(+), 67 deletions(-) diff --git a/server/controllers/api/captcha_controller.go b/server/controllers/api/captcha_controller.go index e0dbe3b7..1a028188 100644 --- a/server/controllers/api/captcha_controller.go +++ b/server/controllers/api/captcha_controller.go @@ -1,49 +1,57 @@ -package api - -import ( - "github.com/dchest/captcha" - "github.com/kataras/iris/v12" - "github.com/mlogclub/simple" - "github.com/sirupsen/logrus" - - "bbs-go/common/urls" -) - -type CaptchaController struct { - Ctx iris.Context -} - -func (c *CaptchaController) GetRequest() *simple.JsonResult { - captchaId := captcha.NewLen(4) - captchaUrl := urls.AbsUrl("/api/captcha/show?captchaId=" + captchaId) - return simple.NewEmptyRspBuilder(). - Put("captchaId", captchaId). - Put("captchaUrl", captchaUrl). - JsonResult() -} - -func (c *CaptchaController) GetShow() { - captchaId := c.Ctx.URLParam("captchaId") - - if captchaId == "" { - c.Ctx.StatusCode(404) - return - } - - if !captcha.Reload(captchaId) { - c.Ctx.StatusCode(404) - return - } - - c.Ctx.Header("Content-Type", "image/png") - if err := captcha.WriteImage(c.Ctx.ResponseWriter(), captchaId, captcha.StdWidth, captcha.StdHeight); err != nil { - logrus.Error(err) - } -} - -func (c *CaptchaController) GetVerify() *simple.JsonResult { - captchaId := c.Ctx.URLParam("captchaId") - captchaCode := c.Ctx.URLParam("captchaCode") - success := captcha.VerifyString(captchaId, captchaCode) - return simple.NewEmptyRspBuilder().Put("success", success).JsonResult() -} +package api + +import ( + "github.com/dchest/captcha" + "github.com/kataras/iris/v12" + "github.com/mlogclub/simple" + "github.com/sirupsen/logrus" + + "bbs-go/common/urls" +) + +type CaptchaController struct { + Ctx iris.Context +} + +func (c *CaptchaController) GetRequest() *simple.JsonResult { + captchaId := c.Ctx.FormValue("captchaId") + if simple.IsNotBlank(captchaId) { // reload + if !captcha.Reload(captchaId) { + // reload 失败,重新加载验证码 + captchaId = captcha.NewLen(4) + } + } else { + captchaId = captcha.NewLen(4) + } + captchaUrl := urls.AbsUrl("/api/captcha/show?captchaId=" + captchaId + "&r=" + simple.UUID()) + return simple.NewEmptyRspBuilder(). + Put("captchaId", captchaId). + Put("captchaUrl", captchaUrl). + JsonResult() +} + +func (c *CaptchaController) GetShow() { + captchaId := c.Ctx.URLParam("captchaId") + + if captchaId == "" { + c.Ctx.StatusCode(404) + return + } + + if !captcha.Reload(captchaId) { + c.Ctx.StatusCode(404) + return + } + + c.Ctx.Header("Content-Type", "image/png") + if err := captcha.WriteImage(c.Ctx.ResponseWriter(), captchaId, captcha.StdWidth, captcha.StdHeight); err != nil { + logrus.Error(err) + } +} + +func (c *CaptchaController) GetVerify() *simple.JsonResult { + captchaId := c.Ctx.URLParam("captchaId") + captchaCode := c.Ctx.URLParam("captchaCode") + success := captcha.VerifyString(captchaId, captchaCode) + return simple.NewEmptyRspBuilder().Put("success", success).JsonResult() +} diff --git a/server/controllers/api/topic_controller.go b/server/controllers/api/topic_controller.go index 8170ef9d..f1a8b9e9 100644 --- a/server/controllers/api/topic_controller.go +++ b/server/controllers/api/topic_controller.go @@ -48,7 +48,7 @@ func (c *TopicController) PostCreate() *simple.JsonResult { tags = simple.FormValueStringArray(c.Ctx, "tags") ) - if !captcha.VerifyString(captchaId, captchaCode) { + if services.SysConfigService.GetConfig().TopicCaptcha && !captcha.VerifyString(captchaId, captchaCode) { return simple.JsonError(common.CaptchaError) } diff --git a/server/model/constants.go b/server/model/constants.go index 55ab19c9..db7fdb62 100644 --- a/server/model/constants.go +++ b/server/model/constants.go @@ -12,10 +12,5 @@ const ( SysConfigScoreConfig = "scoreConfig" // 分数配置 SysConfigDefaultNodeId = "defaultNodeId" // 发帖默认节点 SysConfigArticlePending = "articlePending" // 是否开启文章审核 -) - -// 图片样式 -const ( - ImageStyleAvatar = "avatar" // 头像样式 - ImageStyleDetail = "detail" // 图片详情样式 + SysConfigTopicCaptcha = "topicCaptcha" // 是否开启发帖验证码 ) diff --git a/server/model/data.go b/server/model/data.go index e798e35e..31557e44 100644 --- a/server/model/data.go +++ b/server/model/data.go @@ -24,4 +24,5 @@ type ConfigData struct { ScoreConfig ScoreConfig `json:"scoreConfig"` DefaultNodeId int64 `json:"defaultNodeId"` ArticlePending bool `json:"articlePending"` + TopicCaptcha bool `json:"topicCaptcha"` } diff --git a/server/services/sys_config_service.go b/server/services/sys_config_service.go index 0e2d5ba7..493ecce8 100644 --- a/server/services/sys_config_service.go +++ b/server/services/sys_config_service.go @@ -126,6 +126,7 @@ func (s *sysConfigService) GetConfig() *model.ConfigData { scoreConfigStr = cache.SysConfigCache.GetValue(model.SysConfigScoreConfig) defaultNodeIdStr = cache.SysConfigCache.GetValue(model.SysConfigDefaultNodeId) articlePending = cache.SysConfigCache.GetValue(model.SysConfigArticlePending) + topicCaptcha = cache.SysConfigCache.GetValue(model.SysConfigTopicCaptcha) ) var siteKeywordsArr []string @@ -169,5 +170,6 @@ func (s *sysConfigService) GetConfig() *model.ConfigData { ScoreConfig: scoreConfig, DefaultNodeId: defaultNodeId, ArticlePending: strings.ToLower(articlePending) == "true", + TopicCaptcha: strings.ToLower(topicCaptcha) == "true", } } diff --git a/site/pages/admin/settings/index.vue b/site/pages/admin/settings/index.vue index 780d5fa9..278f16f6 100644 --- a/site/pages/admin/settings/index.vue +++ b/site/pages/admin/settings/index.vue @@ -80,6 +80,12 @@ + + + + + +
@@ -87,7 +89,7 @@ :disabled="publishing" @click="submitCreate" class="button is-success" - >发表主题发表话题 @@ -154,6 +156,9 @@ export default { computed: { user() { return this.$store.state.user.current + }, + config() { + return this.$store.state.config.config } }, mounted() { @@ -194,18 +199,24 @@ export default { } }) } catch (e) { - console.error(e) + await this.showCaptcha() this.publishing = false this.$toast.error('提交失败:' + (e.message || e)) } }, async showCaptcha() { - try { - const ret = await this.$axios.get('/api/captcha/request') - this.captchaId = ret.captchaId - this.captchaUrl = ret.captchaUrl - } catch (e) { - this.$toast.error(e.message || e) + if (this.config.topicCaptcha) { + try { + const ret = await this.$axios.get('/api/captcha/request', { + params: { + captchaId: this.captchaId || '' + } + }) + this.captchaId = ret.captchaId + this.captchaUrl = ret.captchaUrl + } catch (e) { + this.$toast.error(e.message || e) + } } } }, diff --git a/site/pages/user/signin.vue b/site/pages/user/signin.vue index 20f11ae9..098b74fb 100644 --- a/site/pages/user/signin.vue +++ b/site/pages/user/signin.vue @@ -43,13 +43,14 @@
-
+