-
Notifications
You must be signed in to change notification settings - Fork 1
/
captcha.go
103 lines (93 loc) · 2.3 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package controller_public
import (
"strconv"
"github.com/anden007/afocus-godf/src/interfaces/interface_core"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
)
type CaptchaController struct {
Ctx iris.Context
Captcha interface_core.ICaptcha
}
type CaptchaResult struct {
Success bool `json:"success"`
Message string `json:"message"`
CaptchaId string `json:"captchaId"`
Image string `json:"image"`
}
func (m *CaptchaController) BeforeActivation(b mvc.BeforeActivation) {
b.Handle("GET", "/init", "Init")
b.Handle("GET", "/reload/{id:string}", "Reload")
}
func (m *CaptchaController) AfterActivation(a mvc.AfterActivation) {
if m.Captcha == nil {
panic("CaptchaController中的Captcha尚未注册!")
}
}
// @Description Init Captcha
// @Accept json
// @Produce json
// @Success 200 {string} string "ok"
// @Router /api/public/captcha/init [get]
func (m *CaptchaController) Init() mvc.Result {
reload := false
reload, _ = strconv.ParseBool(m.Ctx.URLParam("reload"))
id := m.Ctx.URLParam("id")
result := CaptchaResult{
Success: false,
Message: "未知错误",
CaptchaId: "",
Image: "",
}
if !reload || (reload && id == "") {
captcha, err := m.Captcha.New()
if err == nil {
result.Success = true
result.Message = ""
result.CaptchaId = captcha.ID
result.Image = captcha.Base64Image
}
} else {
captcha, err := m.Captcha.Reload(id)
if err == nil {
result.Success = true
result.Message = ""
result.CaptchaId = captcha.ID
result.Image = captcha.Base64Image
}
}
return mvc.Response{
Object: result,
}
}
// @Description Reload Captcha
// @Accept json
// @Produce json
// @Param id path string false "captcha id"
// @Success 200 {string} string "ok"
// @Router /api/public/captcha/reload/{id} [get]
func (m *CaptchaController) Reload(id string) mvc.Result {
result := CaptchaResult{
Success: false,
Message: "未知错误",
CaptchaId: "",
Image: "",
}
if id == "" {
result.Success = false
result.Message = "参数'id'必须传入"
result.CaptchaId = ""
result.Image = ""
} else {
captcha, err := m.Captcha.Reload(id)
if err == nil {
result.Success = true
result.Message = ""
result.CaptchaId = captcha.ID
result.Image = captcha.Base64Image
}
}
return mvc.Response{
Object: result,
}
}