记录golang使用mojocn/base64Captcha生成验证码

2,294 阅读1分钟

1. 验证码类型

Driver Digit 纯数字验证码

image.png

Driver String 字符验证码

image.png

Driver Math 算数式验证码

image.png

Driver Chinese 中文验证码

image.png

Driver Audio 音频验证码

无法上传音频,可以自行测试

2. 验证码样式配置

// 根据验证码类型进行配置
base64Captcha.DriverMath{
    Length:          4,     // 验证码长度
    Height:          30,    // 验证码图片高度
    Width:           60,    // 验证码图片宽度
    Source:         "abcd",// 会出现字符 
    NoiseCount:      0,     // 干扰词数量
    DotCount:       0// 干扰点数量
    MaxSkew:        1// 倾斜角度 0-1
    ShowLineOptions: 2 | 4, // 线条数量
    BgColor: &color.RGBA{   // 背景颜色
            R: 128,
            G: 98,
            B: 112,
            A: 0,
    },
    Fonts: []string{"3Dumb.ttf"},  // 字体
    Language:       "zh",  // 音频语言
}

3.方法

NewMemoryStore() 生成store

NewCaptcha() 创建一个验证码实例

需传入绘制参数,以及stroe

Generate() 绘制验证码

返回base64验证码,校验id,错误信息

Verify() 解析验证码

传入验证码id,前端输入验证码 返回布尔值