安装catcha64 

go get -u github.com/mojocn/base64Captcha

 

models->captcha.go

package models

import (
	"fmt"
	"image/color"

	"github.com/mojocn/base64Captcha"
)

//创建store
var store = base64Captcha.DefaultMemStore

//获取验证码
func MakeCaptcha() (string, string, error) {
	var driver base64Captcha.Driver
	driverString := base64Captcha.DriverString{
		Height:          40,
		Width:           100,
		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"},
	}

	driver = driverString.ConvertFonts()

	c := base64Captcha.NewCaptcha(driver, store)
	id, b64s, err := c.Generate()
	return id, b64s, err

}

//验证验证码
func VerifyCaptcha(id string, VerifyValue string) bool {
	fmt.Println(id, VerifyValue)
	if store.Verify(id, VerifyValue, true) {
		return true
	} else {
		return false
	}
}

  controllers->admin->login.go

package admin

import (
	"fmt"
	"ginshop03/models"
	"net/http"

	"github.com/gin-gonic/gin"
)

type LoginController struct {
	BaseController
}

func (con LoginController) Index(c *gin.Context) {
	c.HTML(http.StatusOK, "admin/login/login.html", gin.H{})

}
func (con LoginController) DoLogin(c *gin.Context) {

	captchaId := c.PostForm("captchaId")

	verifyValue := c.PostForm("verifyValue")

	if flag := models.VerifyCaptcha(captchaId, verifyValue); flag == true {
		c.String(http.StatusOK, "验证码验证成功")
	} else {
		c.String(http.StatusOK, "验证码验证失败")
	}

}

func (con LoginController) Captcha(c *gin.Context) {
	id, b64s, err := models.MakeCaptcha()

	if err != nil {
		fmt.Println(err)
	}
	c.JSON(http.StatusOK, gin.H{
		"captchaId":    id,
		"captchaImage": b64s,
	})
}

static->admin->js->login.js

$(function(){
    app.init();
})
var app={
    init:function(){
        this.getCaptcha()
        this.captchaImgChage()
    },
    getCaptcha:function(){
        $.get("/admin/captcha?t="+Math.random(),function(response){
            console.log(response)
            $("#captchaId").val(response.captchaId)
            $("#captchaImg").attr("src",response.captchaImage)
        })
    },
    captchaImgChage:function(){
        var that=this;
        $("#captchaImg").click(function(){
            that.getCaptcha()
        })
    }
}

templates->admin->login->login.html

{{ define "admin/login/login.html" }}

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>用户登录</title>  
    <link rel="stylesheet" href="/static/admin/css/login.css"> 
    <script type="text/javascript" src="/static/admin/bootstrap/js/jquery-1.10.1.js"></script>
    <script type="text/javascript" src="/static/admin/js/login.js"></script>
   </head>
<body>
<div class="container">
        <div id="login">
                <form action="/admin/doLogin" method="post" id="myform">                    
                    <input type="hidden" name="captchaId" id="captchaId">
                    <div class="l_title">小米商城后台管理系统-IT营</div>
                    <dl>
                        <dd>管理员姓名:<input class="text" type="text" name="username" id="username"></dd>
                        <dd>管理员密码:<input class="text" type="password" name="password" id="password"></dd>
                        <dd>验 证 码:<input id="verify" type="text" name="verifyValue">
                            <img id="captchaImg" src="">
                         </dd>            
                        <dd><input type="submit" class="submit" name="dosubmit" value=""></dd>            
                    </dl>
                </form>
            </div>
</div>

</body>
</html>

{{end}}

 

  

posted on 2022-10-09 13:38  KOA2后端  阅读(139)  评论(0)    收藏  举报