go 数字图形验证码功能
最近在开发一个项目,需要用到验证码,很久没有用到验证码的项目还不习惯;
在网上参考了一些资料,简单用Go语言开发了一个数字验证码功能;
用到的框架是最近比较火的一款 go-zero 微服务框架;
首先,应用包,我用的是常用的包,验证码用的默认数字图形验证码;
引入包:
github.com/mojocn/base64Captcha
etc yaml配置文件配置如下:
Captcha:
KeyLong: 6
ImgWidth: 240
ImgHeight: 80
Config 文件验证码结构体:
KeyLong int64
ImgWidth int64
ImgHeight int64
}
到此,配置完成了,最后直接调用包new一个验证码
// 获取验证码
func (l *GetCaptchaCodeLogic) GetCaptchaCode(in *member.GetCaptchaReq) (*member.GetCaptchaResp, error) {
// 验证码
driver := base64Captcha.NewDriverDigit(int(l.svcCtx.Config.Captcha.ImgHeight), int(l.svcCtx.Config.Captcha.ImgWidth), int(l.svcCtx.Config.Captcha.KeyLong), 0.7, 80)
cp := base64Captcha.NewCaptcha(driver, storeBlog)
id, b64s, err := cp.Generate()
fmt.Println(err)
if err != nil {
return &member.GetCaptchaResp{
Code: 500,
Msg: "获取验证码失败",
}, nil
}
return &member.GetCaptchaResp{
Code: 200,
Msg: "获取成功",
CaptchaId: id,
PicPath: b64s,
}, nil
}
用go-zero完成的简单的图形数字验证码功能就OK了。
如果想要了解go-zero的api和protobuf文件,可以点击下面链接进行跳转。
还没有评论,快来发表第一个评论吧