6.3 案例实战之JsonData工具类封装+验证码校验编码实战—小滴课堂学习笔记

210 阅读1分钟

案例实战之JsonData工具类封装+验证码校验编码实战点击小标题观看视频讲解

简介:JsonData工具类封装+验证码校验编码实战

  • JsonData响应工具类封装
public class JsonData {
​
    /**
     * 状态码 0 表示成功
     */
​
    private Integer code;
    /**
     * 数据
     */
    private Object data;
    /**
     * 描述
     */
    private String msg;
​
​
    public JsonData(int code,Object data,String msg){
        this.code = code;
        this.msg = msg;
        this.code = code;
    }
​
    /**
     * 成功,不传入数据
     * @return
     */
    public static JsonData buildSuccess() {
        return new JsonData(0, null, null);
    }
​
    /**
     *  成功,传入数据
     * @param data
     * @return
     */
    public static JsonData buildSuccess(Object data) {
        return new JsonData(0, data, null);
    }
​
    /**
     * 失败,传入描述信息
     * @param msg
     * @return
     */
    public static JsonData buildError(String msg) {
        return new JsonData(-1, null, msg);
    }
    
    //set get 方法省略
}

 

  • 校验逻辑
    /**
     * 支持手机号、邮箱发送验证码
     * @return
     */
    @GetMapping("send_code")
    public JsonData sendRegisterCode(@RequestParam(value = "to", required = true)String to,
                                     @RequestParam(value = "captcha", required = true)String  captcha,
                                     HttpServletRequest request){
​
        String key = getCaptchaKey(request);
        String cacheCaptcha = redisTemplate.opsForValue().get(key);
​
        if(captcha!=null && cacheCaptcha!=null && cacheCaptcha.equalsIgnoreCase(captcha)) {
            redisTemplate.delete(key);
           //TODO 发送验证码
            return jsonData;
        }else {
            return JsonData.buildResult("图形验证码错误");
        }
​
    }

《小滴课堂-Redis6学习笔记》