-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
141 lines (123 loc) · 3.76 KB
/
config.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package coolCaptcha
import "errors"
const (
UppercaseEnglishCharacters = "uppercaseEnglishCharacters"
NumericCharacters = "numericCharacters"
MixedCharacters = "mixedCharacters"
)
type Config struct {
Width int
Height int
BackgroundHexColor string
FontHexColor string
LineHexColors []string
Code string
CodeType string
DevMode bool
}
var (
width = 300
height = 120
backgroundHexColor = "#ec6a52"
fontHexColor = "#312E2E"
lineHexColors = []string{"#93aec1", "#9dbdba", "#f8b042", "#f3b7ad"}
codeType = MixedCharacters
fontPoints = 120
)
type Options func(*Config)
// SetWidth
// @Description: Set the width of the captcha image, which is 300 by default
// @param width
// @return Options
func SetWidth(width int) Options {
return func(c *Config) {
c.Width = width
}
}
// SetHeight
// @Description: Set the height of the captcha image, which is 120 by default
// @param height
// @return Options
func SetHeight(height int) Options {
return func(c *Config) {
c.Height = height
}
}
// SetBackgroundHexColor
// @Description: Set the background color of the captcha image
// @param backgroundHexColor: Background color, only hex can be used
// @return Options
func SetBackgroundHexColor(backgroundHexColor string) Options {
return func(c *Config) {
c.BackgroundHexColor = backgroundHexColor
}
}
// SetFontHexColor
// @Description: Set the font color of the captcha image
// @param fontHexColor: Font color, only hex can be used
// @return Options
func SetFontHexColor(fontHexColor string) Options {
return func(c *Config) {
c.FontHexColor = fontHexColor
}
}
// SetLineHexColors
// @Description: Set the line color of the captcha image, a minimum of 3 colors need to be set, and the line will randomly get 3 colors from them to draw
// @param lineHexColors: Font colors, only hex can be used, a minimum of 3 colors need to be set
// @return Options
func SetLineHexColors(lineHexColors []string) Options {
return func(c *Config) {
c.LineHexColors = lineHexColors
}
}
// SetCodeType
// @Description: Set the type of random validation characters, which takes effect only if the user is not using custom code
// @param codeType: Use one of the built-in types: UppercaseEnglishCharacters, NumericCharacters, MixedCharacters
// @return Options
func SetCodeType(codeType string) Options {
return func(c *Config) {
c.CodeType = codeType
}
}
// SetDevMode
// @Description: In the development mode, the generated image is saved as a local file for easy viewing
// @return Options
func SetDevMode(devMode bool) Options {
return func(c *Config) {
c.DevMode = devMode
}
}
func defaultConfig(c *Config) *Config {
c.Width = width
c.Height = height
c.BackgroundHexColor = backgroundHexColor
c.FontHexColor = fontHexColor
c.LineHexColors = lineHexColors
c.CodeType = codeType
return c
}
func New(options ...Options) *Config {
c := &Config{}
c = defaultConfig(c)
for _, op := range options {
op(c)
}
fontPoints = c.Height
return c
}
// CustomCode
// @Description: Users can use their own generated characters as the verification code, the custom code will be capitalized and then used. Therefore, when validating code, you must use all uppercase code.
// @receiver c
// @param code: Customize the generated verification code, but the length limit of 4 digits needs to be met. The custom code will be capitalized and then used. Therefore, when validating code, you must use all uppercase code.
// @return *Config
func (c *Config) CustomCode(code string) *Config {
c.Code = code
return c
}
func (c *Config) checkConfig() (err error) {
if len(c.LineHexColors) < 3 {
err = errors.New("lineHexColors requires at least three values")
return
}
return
}