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} }