一,安装库
1,库的地址
https://github.com/mojocn/base64Captcha
2,安装:
liuhongdi@ku:~$ go get -u github.com/mojocn/base64Captcha
说明:刘宏缔的go森林是一个专注golang的博客, 地址:https://blog.csdn.net/weixin_43881017
说明:作者:刘宏缔 邮箱: [email protected]
二,演示项目的相关信息
1,地址:
https://github.com/liuhongdi/digv18
2,功能说明:演示了用base64Captcha库生成图形验证码
3,项目结构:如图:
三,go代码说明
1,service/capt.go
-
-
-
-
-
-
"github.com/mojocn/base64Captcha"
-
-
-
-
var store = base64Captcha.DefaultMemStore
-
-
-
func CaptMake() (id, b64s string, err error) {
-
var driver base64Captcha.Driver
-
var driverString base64Captcha.DriverString
-
-
-
captchaConfig := base64Captcha.DriverString{
-
-
-
-
-
-
Source: "1234567890qwertyuioplkjhgfdsazxcvbnm",
-
-
-
-
-
-
-
Fonts: []string{"wqy-microhei.ttc"},
-
-
-
driverString = captchaConfig
-
driver = driverString.ConvertFonts()
-
captcha := base64Captcha.NewCaptcha(driver, store)
-
lid, lb64s, lerr := captcha.Generate()
-
-
-
-
-
func CaptVerify(id string,capt string) bool {
-
-
fmt.Println("capt:"+capt)
-
-
if store.Verify(id, capt, false) {
-
-
-
-
-
-
2,controller/idController.go
-
-
-
-
"github.com/gin-gonic/gin"
-
"github.com/liuhongdi/digv18/pkg/result"
-
"github.com/liuhongdi/digv18/service"
-
-
-
-
type IdController struct{}
-
-
func NewIdController() IdController {
-
-
-
-
-
type CaptchaResult struct {
-
-
Base64Blob string `json:"base_64_blob"`
-
-
-
-
-
func (a *IdController) GetOne(ctx *gin.Context) {
-
resultRes := result.NewResult(ctx)
-
id, b64s, err := service.CaptMake()
-
-
resultRes.Error(1,err.Error())
-
-
captchaResult := CaptchaResult{
-
-
-
-
resultRes.Success(captchaResult)
-
-
-
-
-
func (a *IdController) Verify(c *gin.Context) {
-
-
-
capt := c.PostForm("capt")
-
resultRes := result.NewResult(c)
-
if (id == "" || capt == "") {
-
resultRes.Error(400,"param error")
-
-
-
if service.CaptVerify(id, capt) == true {
-
resultRes.Success("success")
-
-
resultRes.Error(1,"verify failed")
-
-
-
3,router/router.go
-
-
-
-
"github.com/gin-gonic/gin"
-
"github.com/liuhongdi/digv18/controller"
-
"github.com/liuhongdi/digv18/global"
-
"github.com/liuhongdi/digv18/pkg/result"
-
-
-
-
-
-
func Router() *gin.Engine {
-
-
-
router.NoRoute(HandleNotFound)
-
router.NoMethod(HandleNotFound)
-
-
-
-
router.StaticFS("/static", http.Dir(global.StaticSetting.StaticDir))
-
-
-
idc:=controller.NewIdController()
-
router.GET("/id/getone", idc.GetOne);
-
router.POST("/id/verify", idc.Verify);
-
-
-
-
-
func HandleNotFound(c *gin.Context) {
-
result.NewResult(c).Error(404,"资源未找到")
-
-
-
-
-
func Recover(c *gin.Context) {
-
-
if r := recover(); r != nil {
-
-
log.Printf("panic: %v\n", r)
-
-
result.NewResult(c).Error(500,"服务器内部错误")
-
-
-
-
-
4,static/getcapt.html
-
-
-
-
-
-
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
-
-
-
<img id="capt" src="" /><br/>
-
<input id="captvalue" placeholder="请输入验证码" /><br/>
-
<input type="button" name="save" value="提交" onclick="submit()" />
-
-
-
-
-
-
-
-
-
-
success: function(result) {
-
curCaptId = result.data.id;
-
document.getElementById("capt").src=result.data.base_64_blob;
-
-
-
-
-
-
var capt = document.getElementById("captvalue").value;
-
-
-
-
-
-
-
-
-
-
success: function(result) {
-
-
-
-
alert("验证错误:"+result.msg);
-
-
-
-
-
-
-
-
5,其他相关代码可访问github查看
四,效果测试
1,访问:
http://127.0.0.1:8000/static/getcapt.html
返回:
2,测试错误返回:
3,测试正确返回:
五,查看库的版本:
-
module github.com/liuhongdi/digv18
-
-
-
-
-
github.com/gin-gonic/gin v1.6.3
-
github.com/go-playground/universal-translator v0.17.0
-
github.com/go-playground/validator/v10 v10.2.0
-
github.com/mojocn/base64Captcha v1.3.1
-
github.com/magiconair/properties v1.8.4 // indirect
-
github.com/mitchellh/mapstructure v1.3.3 // indirect
-
github.com/pelletier/go-toml v1.8.1 // indirect
-
github.com/spf13/afero v1.4.1 // indirect
-
github.com/spf13/cast v1.3.1 // indirect
-
github.com/spf13/jwalterweatherman v1.1.0 // indirect
-
github.com/spf13/pflag v1.0.5 // indirect
-
github.com/spf13/viper v1.7.1
-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
-
golang.org/x/text v0.3.4 // indirect
-
gopkg.in/ini.v1 v1.62.0 // indirect
-
gopkg.in/yaml.v2 v2.3.0 // indirect
-
-
|
请发表评论