package partial import ( sysmodel "git.qianqiusoft.com/qianqiusoft/light-apiengine/models" "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys" "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils" "strconv" ) const captcha_session = "image_captcha" // _Image // @Title _Image // @Description 获取图片验证码 // @Param w int 图片宽度 // @Param h int 图片高度 // @Param code string 保存的session编码 // @Success 200 {object} Account // @Failure 403 :id is empty func Captcha_Image(c *entitys.CtrlContext) { width, _ := strconv.Atoi(c.Ctx.Query("w")) height, _ := strconv.Atoi(c.Ctx.Query("h")) length, _ := strconv.Atoi(c.Ctx.Query("l")) captchaKey := captcha_session + c.Ctx.Query("code") captchaId, image, err := utils.GenerateImageCaptcha(width, height, length) if err == nil { utils.SetSession(c.Ctx, captchaKey, captchaId) c.Ctx.Header("Cache-Control", "no-cache, no-store, must-revalidate") c.Ctx.Header("Pragma", "no-cache") c.Ctx.Header("Expires", "0") c.Ctx.Header("Content-Type", "image/png") c.Ctx.Writer.Write(image.Bytes()) c.Ctx.JSON(200, sysmodel.SysReturn{200, "", nil}) } else { utils.SetSession(c.Ctx, captchaKey, "") c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil}) } } // _Sms // @Title _Sms // @Description 获取短信验证码 // @Param mobile string false "手机号码" // @Success 200 {object} Account // @Failure 403 :id is empty func Captcha_Sms(c *entitys.CtrlContext) { mobile := c.Ctx.Query("mobile") if mobile == "" { c.Ctx.JSON(500, sysmodel.SysReturn{500, "mobile is cannot empty", nil}) return } data, err := utils.SendSmsCaptcha(mobile) if err == nil { c.Ctx.JSON(200, sysmodel.SysReturn{200, "", string(data)}) } else { c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil}) } } // _Check // @Title _Check // @Description 校验验证码 // @Param type int false "验证类型 0:图片验证码 1:短信验证码" // @Param captcha string false "验证码" // @Param mobile string false "手机号码 短信验证码时传" // @Success 200 {object} Account // @Failure 403 :id is empty func Captcha_Check(c *entitys.CtrlContext) { check_type := c.Ctx.Query("type") captcha := c.Ctx.Query("captcha") if captcha == "" { c.Ctx.JSON(200, sysmodel.SysReturn{200, "验证码不能为空", nil}) return } var check = false //校验结果 switch check_type { case "0": captchaKey := captcha_session + c.Ctx.Query("code") captchaId := utils.GetSession(c.Ctx, captchaKey, "").(string) check = utils.ImageCaptchaCheck(captcha, captchaId) if check { c.Ctx.Set(captchaKey, "") } case "1": mobile := c.Ctx.Query("mobile") if mobile == "" { c.Ctx.JSON(500, sysmodel.SysReturn{500, "手机号码不能为空", nil}) return } check= utils.SmsCaptchaCheck(captcha, mobile) default: c.Ctx.JSON(500, sysmodel.SysReturn{500, "验证类型错误", nil}) return } if check { c.Ctx.JSON(200, sysmodel.SysReturn{200, "验证成功", nil}) }else{ c.Ctx.JSON(500, sysmodel.SysReturn{500, "验证失败", nil}) } } func __none_func_captcha__(params ... interface{}) bool { return true }