1. 圖形驗證碼生成 依賴 "github.com/mojocn/base64Captcha"
// 圖形驗證碼
func CaptchaImage(c *gin.Context) {
//config struct for digits
//數字驗證碼配置
//var configD = base64Captcha.ConfigDigit{
// Height: 80,
// Width: 240,
// MaxSkew: 0.7,
// DotCount: 80,
// CaptchaLen: 5,
//}
//config struct for audio
//聲音驗證碼配置
//var configA = base64Captcha.ConfigAudio{
// CaptchaLen: 6,
// Language: "zh",
//}
//config struct for Character
//字符,公式,驗證碼配置
var configC = base64Captcha.ConfigCharacter{
Height: 60,
Width: 240,
//const CaptchaModeNumber:數字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算術,CaptchaModeNumberAlphabet:數字字母混合.
Mode: base64Captcha.CaptchaModeNumber,
ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
IsShowHollowLine: false,
IsShowNoiseDot: false,
IsShowNoiseText: false,
IsShowSlimeLine: false,
IsShowSineLine: false,
CaptchaLen: 6,
}
//創建聲音驗證碼
//GenerateCaptcha 第一個參數為空字符串,包會自動在服務器一個隨機種子給你產生隨機uiid.
//idKeyA, capA := base64Captcha.GenerateCaptcha("", configA)
//以base64編碼
//base64stringA := base64Captcha.CaptchaWriteToBase64Encoding(capA)
//創建字符公式驗證碼.
//GenerateCaptcha 第一個參數為空字符串,包會自動在服務器一個隨機種子給你產生隨機uiid.
idKeyC, capC := base64Captcha.GenerateCaptcha("", configC)
//以base64編碼
base64stringC := base64Captcha.CaptchaWriteToBase64Encoding(capC)
//創建數字驗證碼.
//GenerateCaptcha 第一個參數為空字符串,包會自動在服務器一個隨機種子給你產生隨機uiid.
//idKeyD, capD := base64Captcha.GenerateCaptcha("", configD)
//以base64編碼
//base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD)
c.JSON(http.StatusOK, model.CaptchaRes{
Code: 0,
IdKey: idKeyC, //驗證請求時須傳的參
Data: base64stringC,
Msg: "操作成功",
})
}
2. 驗證碼驗證
//比對驗證碼
verifyResult := base64Captcha.VerifyCaptcha(req.IdKey, req.ValidateCode)
if !verifyResult {
response.ErrorResp(c).SetMsg("驗證碼不正確").WriteJsonExit()
return
}
