随笔 - 35  文章 - 20 评论 - 0 阅读 - 3509
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

安装catcha64 

go get -u github.com/mojocn/base64Captcha

 

models->captcha.go

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
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

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
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   KOA2后端  阅读(123)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示