警告
本文最后更新于 2020-02-07,文中内容可能已过时。
这个库烦了我两天,我能接触到的例子都是旧版的,新版简直就是大改版哦,构建函数简直和旧版出入太大,函数参数及返回值皆不一样,记录一下最新版的字符验证码的完整实例,(数字验证码、计算验证码、汉字验证码以及语音验证码构建方法举一反三)
在线构建验证码样式
注意:
旧版的base64Captcha.ConfigCharacte
以及 base64Captcha.GenerateCaptcha
方法由于作者重构已经取消,具体实现看下面代码
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
|
package tool
import (
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
"image/color"
)
type CaptchaResult struct {
Id string `json:"id"`
Base64Blob string `json:"base_64_blob"`
VerifyValue string `json:"code"`
}
// 设置自带的store
var store = base64Captcha.DefaultMemStore
// 生成图形化验证码
func GenerateCaptcha(ctx *gin.Context) {
var driver base64Captcha.Driver
var driverString base64Captcha.DriverString
// 配置验证码信息
captchaConfig := base64Captcha.DriverString{
Height: 30,
Width: 60,
NoiseCount: 0,
ShowLineOptions: 2 | 4,
Length: 4,
Source: "1234567890qwertyuioplkjhgfdsazxcvbnm",
BgColor: &color.RGBA{
R: 3,
G: 102,
B: 214,
A: 125,
},
Fonts: []string{"wqy-microhei.ttc"},
}
// 自定义配置,如果不需要自定义配置,则上面的结构体和下面这行代码不用写
driverString = captchaConfig
driver = driverString.ConvertFonts()
captcha := base64Captcha.NewCaptcha(driver, store)
id, b64s, err := captcha.Generate()
if err != nil {
Failed(ctx, err.Error())
}
captchaResult := CaptchaResult{
Id: id,
Base64Blob: b64s,
}
Success(ctx, gin.H{
"captcha_result": captchaResult,
})
}
|
这样子输出的就是一条json,放在项目里面就是这样的
效果图