/
captcha.go
44 lines (35 loc) · 1.12 KB
/
captcha.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2016 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author: polaris polaris@studygolang.com
package middleware
import (
"net/http"
"github.com/studygolang/studygolang/logic"
"github.com/studygolang/studygolang/model"
"github.com/studygolang/studygolang/util"
"github.com/dchest/captcha"
echo "github.com/labstack/echo/v4"
)
// CheckCaptcha 用于 echo 框架校验发布验证码
func CheckCaptcha() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
curUser := ctx.Get("user").(*model.Me)
if ctx.Request().Method == "POST" {
if logic.NeedCaptcha(curUser) {
captchaId := ctx.FormValue("captchaid")
if !captcha.VerifyString(captchaId, ctx.FormValue("captchaSolution")) {
util.SetCaptcha(captchaId)
return ctx.String(http.StatusOK, `{"ok":0,"error":"验证码错误,记得刷新验证码!"}`)
}
}
}
if err := next(ctx); err != nil {
return err
}
return nil
}
}
}