Merge branch 'master' of ssh://10.10.14.176:222/bigdata/dsMin
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
# Conflicts: # dsSzxy/.idea/workspace.xml # dsSzxy/build/dsSzxy # dsSzxy/go.summaster
commit
4b6ee7e526
@ -0,0 +1,127 @@
|
||||
package CaptchaController
|
||||
|
||||
import (
|
||||
"dsSzxy/Utils/ConfigUtil"
|
||||
"fmt"
|
||||
"gitee.com/aesoper/cache"
|
||||
"gitee.com/aesoper/cache/redis"
|
||||
"gitee.com/aesoper/captchaStore"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"image/color"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Routers 模块的路由配置
|
||||
func Routers(r *gin.RouterGroup) {
|
||||
rr := r.Group("/captchaRelate")
|
||||
rr.GET("/getCaptcha", getCaptcha)
|
||||
rr.POST("/verifyCaptcha", verifyCaptcha)
|
||||
}
|
||||
|
||||
// CaptchaResult 存储验证码的结构
|
||||
type CaptchaResult struct {
|
||||
Id string `json:"id"`
|
||||
Base64Blob string `json:"base_64_blob"`
|
||||
}
|
||||
|
||||
// GetOne 生成图形化验证码
|
||||
func getCaptcha(c *gin.Context) {
|
||||
id, b64s := CaptMake()
|
||||
captchaResult := CaptchaResult{
|
||||
Id: id,
|
||||
Base64Blob: b64s,
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "captcha": captchaResult})
|
||||
return
|
||||
}
|
||||
|
||||
// Verify 验证captcha是否正确
|
||||
func verifyCaptcha(c *gin.Context) {
|
||||
id := c.PostForm("id")
|
||||
capt := c.PostForm("capt")
|
||||
if id == "" || capt == "" {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "msg": "参数错误!"})
|
||||
}
|
||||
if CaptVerify(id, capt) == true {
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var store *captchaStore.CustomCaptchaStore
|
||||
|
||||
// CaptMake 生成验证码
|
||||
func CaptMake() (string, string) {
|
||||
// 生成默认数字
|
||||
//driver := &base64Captcha.DriverDigit{
|
||||
// Height: 100,
|
||||
// Width: 200,
|
||||
// Length: 4,
|
||||
// MaxSkew: 1,
|
||||
// DotCount: 2,
|
||||
//}
|
||||
//https://captcha.mojotv.cn/
|
||||
|
||||
//四则运算
|
||||
//driver := &base64Captcha.DriverMath{
|
||||
// Height: 60,
|
||||
// Width: 240,
|
||||
// NoiseCount: 0,
|
||||
// BgColor: &color.RGBA{
|
||||
// R: 52,
|
||||
// G: 56,
|
||||
// B: 98,
|
||||
// A: 58,
|
||||
// },
|
||||
// Fonts: []string{"wqy-microhei.ttc"},
|
||||
//}
|
||||
//driver.ConvertFonts()
|
||||
|
||||
//中文
|
||||
driver := &base64Captcha.DriverChinese{
|
||||
Height: 50,
|
||||
Width: 300,
|
||||
NoiseCount: 0,
|
||||
ShowLineOptions: 1,
|
||||
Length: 2,
|
||||
BgColor: &color.RGBA{
|
||||
R: 52,
|
||||
G: 56,
|
||||
B: 98,
|
||||
A: 58,
|
||||
},
|
||||
Source: "东师理想,大数据,超融合,智慧校园,数字校园,黄海,吴缤,超人气,抗疫情,努力,加油,长春",
|
||||
Fonts: []string{"wqy-microhei.ttc"},
|
||||
}
|
||||
driver.ConvertFonts()
|
||||
|
||||
// 生成base64图片
|
||||
c, err := cache.New(cache.Cfg{
|
||||
Driver: "redis",
|
||||
Redis: redis.Options{
|
||||
Addr: ConfigUtil.RedisIp + ":" + ConfigUtil.RedisPort,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("初始化Redis服务器失败:", err)
|
||||
}
|
||||
store = captchaStore.NewCustomCaptchaStore(c, time.Minute*5, false)
|
||||
captcha := base64Captcha.NewCaptcha(driver, store)
|
||||
// 生成验证码base64字符串
|
||||
id, b64s, err := captcha.Generate()
|
||||
if err != nil {
|
||||
fmt.Println("Register GetCaptchaPhoto get base64Captcha has err:", err)
|
||||
}
|
||||
return id, b64s
|
||||
}
|
||||
|
||||
// CaptVerify 验证captcha是否正确
|
||||
func CaptVerify(id string, capt string) bool {
|
||||
fmt.Println("id:" + id)
|
||||
fmt.Println("capt:" + capt)
|
||||
return store.Verify(id, capt, false)
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package CaptchaController
|
||||
|
||||
import (
|
||||
"context"
|
||||
"dsSzxy/Utils/RedisUtil"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
const CAPTCHA = "captcha:"
|
||||
|
||||
type RedisStore struct {
|
||||
}
|
||||
|
||||
// Set set a capt
|
||||
func (r RedisStore) Set(id string, value string) {
|
||||
key := CAPTCHA + id
|
||||
RedisUtil.SET(key, value, time.Minute*2)
|
||||
}
|
||||
|
||||
// Get get a capt
|
||||
func (r RedisStore) Get(id string, clear bool) string {
|
||||
key := CAPTCHA + id
|
||||
|
||||
val, err := RedisUtil.GET(key)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return ""
|
||||
}
|
||||
if clear {
|
||||
RedisUtil.DEL(key)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// Verify verify a capt
|
||||
func (r RedisStore) Verify(id, answer string, clear bool) bool {
|
||||
v := RedisStore{}.Get(id, clear)
|
||||
return v == answer
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,7 @@
|
||||
go get -u github.com/jteeuwen/go-bindata/...
|
||||
|
||||
go-bindata fonts
|
||||
sed -i "s/package main/package base64Captcha/g" bindata.go
|
||||
|
||||
|
||||
https://github.com/jteeuwen/go-bindata
|
Binary file not shown.
@ -0,0 +1,50 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<img id="capt" src=""/><br/>
|
||||
<input id="captvalue" placeholder="请输入验证码"/><br/>
|
||||
<input type="button" name="save" value="提交" onclick="submit()"/>
|
||||
<script>
|
||||
var curCaptId = "";
|
||||
//得到图形验证码和id
|
||||
$.ajax({
|
||||
type: "GET",
|
||||
url: "/dsSzxy/captchaRelate/getCaptcha",
|
||||
data: {},
|
||||
dataType: "JSON",
|
||||
success: function (result) {
|
||||
curCaptId = result.captcha.id;
|
||||
document.getElementById("capt").src = result.captcha.base_64_blob;
|
||||
}
|
||||
});
|
||||
|
||||
//提交验证码和id
|
||||
function submit() {
|
||||
var capt = document.getElementById("captvalue").value;
|
||||
var postdata = {
|
||||
"id": curCaptId,
|
||||
"capt": capt
|
||||
};
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "/dsSzxy/captchaRelate/verifyCaptcha",
|
||||
data: postdata,
|
||||
dataType: "JSON",
|
||||
success: function (result) {
|
||||
console.log(result.success);
|
||||
if (result.success) {
|
||||
alert("验证成功");
|
||||
} else {
|
||||
alert("验证错误!" );
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,7 @@
|
||||
go get -u github.com/jteeuwen/go-bindata/...
|
||||
|
||||
go-bindata fonts
|
||||
sed -i "s/package main/package base64Captcha/g" bindata.go
|
||||
|
||||
|
||||
https://github.com/jteeuwen/go-bindata
|
Binary file not shown.
@ -0,0 +1,50 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<img id="capt" src=""/><br/>
|
||||
<input id="captvalue" placeholder="请输入验证码"/><br/>
|
||||
<input type="button" name="save" value="提交" onclick="submit()"/>
|
||||
<script>
|
||||
var curCaptId = "";
|
||||
//得到图形验证码和id
|
||||
$.ajax({
|
||||
type: "GET",
|
||||
url: "/dsSzxy/captchaRelate/getCaptcha",
|
||||
data: {},
|
||||
dataType: "JSON",
|
||||
success: function (result) {
|
||||
curCaptId = result.captcha.id;
|
||||
document.getElementById("capt").src = result.captcha.base_64_blob;
|
||||
}
|
||||
});
|
||||
|
||||
//提交验证码和id
|
||||
function submit() {
|
||||
var capt = document.getElementById("captvalue").value;
|
||||
var postdata = {
|
||||
"id": curCaptId,
|
||||
"capt": capt
|
||||
};
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "/dsSzxy/captchaRelate/verifyCaptcha",
|
||||
data: postdata,
|
||||
dataType: "JSON",
|
||||
success: function (result) {
|
||||
console.log(result.success);
|
||||
if (result.success) {
|
||||
alert("验证成功");
|
||||
} else {
|
||||
alert("验证错误!" );
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in new issue