This commit is contained in:
12600k-rog-d4
2025-12-09 20:33:57 +08:00
parent 144ed3cbdf
commit 8f4d399cfd
1498 changed files with 3778 additions and 3654 deletions

View File

@@ -0,0 +1,58 @@
package api
import (
"bbs-go/internal/pkg/bbsurls"
"log/slog"
"github.com/dchest/captcha"
"github.com/kataras/iris/v12"
"github.com/mlogclub/simple/common/strs"
"github.com/mlogclub/simple/web"
)
type CaptchaController struct {
Ctx iris.Context
}
func (c *CaptchaController) GetRequest() *web.JsonResult {
captchaId := c.Ctx.FormValue("captchaId")
if strs.IsNotBlank(captchaId) { // reload
if !captcha.Reload(captchaId) {
// reload 失败,重新加载验证码
captchaId = captcha.NewLen(4)
}
} else {
captchaId = captcha.NewLen(4)
}
captchaUrl := bbsurls.AbsUrl("/api/captcha/show?captchaId=" + captchaId + "&r=" + strs.UUID())
return web.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 {
slog.Error(err.Error(), slog.Any("err", err))
}
}
func (c *CaptchaController) GetVerify() *web.JsonResult {
captchaId := c.Ctx.URLParam("captchaId")
captchaCode := c.Ctx.URLParam("captchaCode")
success := captcha.VerifyString(captchaId, captchaCode)
return web.NewEmptyRspBuilder().Put("success", success).JsonResult()
}