package controller import ( "strconv" "yitao/util" "github.com/kataras/iris/v12/mvc" ) // TEMP : Auth type CaptchaController struct { BaseController } func (m *CaptchaController) BeforeActivation(b mvc.BeforeActivation) { { // 普通图片验证码 b.Handle("GET", "/img", "GetIMG") b.Handle("GET", "/img/gen", "ImgGen") b.Handle("GET", "/img/{key:string}", "GetImgByKey") b.Handle("GET", "/img/validate/{key:string}", "ImgValidate") } { // 高级点击验证码 b.Handle("GET", "/click/gen", "ClickGen") b.Handle("POST", "/click/{key:string}/submit", "ClickSubmit") } } // 获取验证码信息 // @Summary Captcha Img Generate // @Description 获取一张图片验证码的标识符 // @Tags captcha api // @Accept json // @Produce json // @Success 200 {object} map[string]interface{} "{"key": "ASDFGHJK"}" // @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}" // @Router /api/captcha/img/gen [get] func (con *CaptchaController) ImgGen() map[string]interface{} { hs := con.Ctx.URLParam("h") var h int = 120 if hs != "" { h, _ = strconv.Atoi(hs) } ws := con.Ctx.URLParam("w") var w int = 40 if ws != "" { w, _ = strconv.Atoi(ws) } code, img := con.Service.Captcha.GetIMGWithHW(4, true, h, w) key := util.RandString(8) // 保存到文件 util.SaveToFile("./temp/captcha/img/"+key+".png", img) // 保存到数据库 con.Service.Captcha.SaveToDB(key, code) return map[string]interface{}{ // 打开这个注释就能在 api 里看到答案了 //"code": code, "key": key, } } // 验证码本体 func (con *CaptchaController) GetIMG() { _, img := con.Service.Captcha.GetIMG(4, true) con.Ctx.Write(img) } // 通过 key 获取验证码图片 // @Summary Captcha Img Get // @Description 通过 key 获取验证码图片 // @Tags captcha api // @Accept json // @Produce png // @Param key path string true "验证码标识符" // @Success 200 {object} []byte "IMG" // @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}" // @Router /api/captcha/img/{key} [get] func (con *CaptchaController) GetImgByKey(key string) { b, err := util.ReadFromFile("./temp/captcha/img/" + key + ".png") if err != nil { con.Ctx.Write([]byte("error")) } else { con.Ctx.ResponseWriter().Header().Add("content-type", "image/png") con.Ctx.Write(b) } // 读取完直接删了,都是一次性的 util.DeleteFile("./temp/captcha/img/" + key + ".png") } // 验证码有效性 // @Summary Captcha Img Validate // @Description 图片验证码校验(一般情况下后端校验) // @Tags captcha api // @Accept json // @Produce json // @Param key path string true "验证码标识符" // @Param code query string true "验证码" // @Success 200 {object} map[string]interface{} "{"msg": "正确信息","code":0}" // @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}" // @Router /api/captcha/img/validate/{key} [get] func (con *CaptchaController) ImgValidate(key string) mvc.Result { input := con.Ctx.URLParam("code") e := con.Service.Captcha.Validate(key, input) con.Service.Captcha.Delete(key) if e.Error() { return e.Response() } else { // 好像ecode不出错就返回正确似乎 return e.Response() } } // @Summary Captcha Click Generate // @Description 生成按次序点击验证码 // @Tags captcha api // @Accept json // @Produce json // @Success 200 {object} map[string]interface{} "{"key": "验证码标识符","master":"主图(用户点击图)base64","subimg":"子图(文字次序图)base64"}" // @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}" // @Router /api/captcha/click/gen [get] func (con *CaptchaController) ClickGen() mvc.Result { key, img1, img2 := con.Service.Captcha.GetClick() return mvc.Response{ Object: map[string]interface{}{ "key": key, "master": img1, "subimg": img2, }, } } // @Summary Captcha Click Validate // @Description 校验按次序点击验证码 // @Tags captcha api // @Accept json // @Produce json // @Param key path string true "验证码标识符" // @Param key formData string true "15,851;458,18;58,15;x,y; #用户点击的坐标集合" // @Success 200 {object} map[string]interface{} "{"flag": true|false}" // @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}" // @Router /api/captcha/click/{key}/submit [post] func (con *CaptchaController) ClickSubmit(key string) mvc.Result { input := con.Ctx.PostValue("data") f := con.Service.Captcha.ClickValidate(key, input) return mvc.Response{ Object: map[string]interface{}{ "flag": f, }, } }