beego 接口生成验证码
作者:互联网
var Cpt *captcha.Captcha func init() { store := cache.NewMemoryCache() Cpt = captcha.NewWithFilter("/captcha/", store) //一定要写在构造函数里面,要不然第一次打开页面有可能是X Cpt.ChallengeNums = 4 } //返回标识 如果是接口直接返回json数据 func (c *MainController) Get() { captcha, _ := Cpt.CreateCaptcha() c.Data["captcha"] = captcha c.TplName = "login.html" }
html 如果接口做对应处理
<input type="text" name="captcha_id" value="{{.captcha}}"> <a class="captcha" href="javascript:"><img onclick="this.src=('/captcha/{{.captcha}}.png?reload='+(new Date()).getTime())" class="captcha-img" src="/captcha/{{.captcha}}.png"></a>
验证
if !Cpt.VerifyReq(c.Ctx.Request) { base = Base{2, "验证码错误", nil} }
标签:beego,验证码,接口,captcha,html,func,Cpt,store 来源: https://www.cnblogs.com/chengfengchi/p/15905180.html