commit
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
d814dab594
commit
d9ee6ebad7
@ -0,0 +1,92 @@
|
|||||||
|
package CaptchaController
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/mojocn/base64Captcha"
|
||||||
|
"image/color"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
//模块的路由配置
|
||||||
|
func Routers(r *gin.RouterGroup) {
|
||||||
|
rr := r.Group("/captchaRelate")
|
||||||
|
rr.GET("/GetOne", GetOne)
|
||||||
|
rr.GET("/Verify", Verify)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置自带的store
|
||||||
|
var store = base64Captcha.DefaultMemStore
|
||||||
|
|
||||||
|
//存储验证码的结构
|
||||||
|
type CaptchaResult struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Base64Blob string `json:"base_64_blob"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成图形化验证码
|
||||||
|
func GetOne(c *gin.Context) {
|
||||||
|
id, b64s, err := CaptMake()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"success": false, "info": "发生错误!"})
|
||||||
|
}
|
||||||
|
captchaResult := CaptchaResult{
|
||||||
|
Id: id,
|
||||||
|
Base64Blob: b64s,
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"success": true, "captcha": captchaResult})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//验证captcha是否正确
|
||||||
|
func Verify(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
|
||||||
|
}
|
||||||
|
|
||||||
|
//生成验证码
|
||||||
|
func CaptMake() (id, b64s string, err error) {
|
||||||
|
var driver base64Captcha.Driver
|
||||||
|
var driverString base64Captcha.DriverString
|
||||||
|
|
||||||
|
// 配置验证码信息
|
||||||
|
captchaConfig := base64Captcha.DriverString{
|
||||||
|
Height: 60,
|
||||||
|
Width: 200,
|
||||||
|
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"},
|
||||||
|
}
|
||||||
|
|
||||||
|
driverString = captchaConfig
|
||||||
|
driver = driverString.ConvertFonts()
|
||||||
|
captcha := base64Captcha.NewCaptcha(driver, store)
|
||||||
|
lid, lb64s, lerr := captcha.Generate()
|
||||||
|
return lid, lb64s, lerr
|
||||||
|
}
|
||||||
|
|
||||||
|
//验证captcha是否正确
|
||||||
|
func CaptVerify(id string, capt string) bool {
|
||||||
|
fmt.Println("id:" + id)
|
||||||
|
fmt.Println("capt:" + capt)
|
||||||
|
return store.Verify(id, capt, false)
|
||||||
|
}
|
Loading…
Reference in new issue