刚刚开始了解go,肯定是使用开源类,类地址:
github.com/mojocn/base64Captcha
func (c *LoginController) Code() {
//数字验证码配置
//字符,公式,验证码配置
var configC = base64Captcha.ConfigCharacter{
Height: 45,
Width: 125,
//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: 5,
}
//创建数字验证码.
//GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
_, capD := base64Captcha.GenerateCaptcha("", configC)
//以base64编码
base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD)
c.Ctx.WriteString(base64stringD)
}
后来把图片改成了异步请求才行,记录一下:
<img src="/login/code" id="code_img" />
<a id="kanbuq" href="javascript:;">看不清,换一张</a>
--------------------------------------------------------------------------------------------------------
<script>
$(document).ready(function () {
getcode();
})
function getcode() {
$.get('/login/code',function (data) {
$("#code_img").prop('src',data);
})
}
$("#kanbuq").on('click',function () {
getcode();
})
</script>