美文网首页
6.3-案例实战之JsonData工具类封装+验证码校验编码实战

6.3-案例实战之JsonData工具类封装+验证码校验编码实战

作者: 小滴极客编程学习 | 来源:发表于2021-11-01 09:40 被阅读0次

    6.3-案例实战之JsonData工具类封装+验证码校验编码实战


    案例实战之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学习笔记》

    相关文章

      网友评论

          本文标题:6.3-案例实战之JsonData工具类封装+验证码校验编码实战

          本文链接:https://www.haomeiwen.com/subject/hmujaltx.html