用户注册+短信验证

短信验证

https://luosimao.com/docs/api/25

1、引入API测试代码

 private String testSend(){
        // just replace key here
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
            "api","key-d609b769db914a4d959bae3414ed1f7X"));
        WebResource webResource = client.resource(
            "http://sms-api.luosimao.com/v1/send.json");
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        formData.add("mobile", "13761428267");
        formData.add("message", "验证码:286221【铁壳测试】");
        ClientResponse response =  webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).
        post(ClientResponse.class, formData);
        String textEntity = response.getEntity(String.class);
        int status = response.getStatus();
        //System.out.print(textEntity);
        //System.out.print(status);
        return textEntity;
    }

2、 maven依赖查询下载相关jar

相关jar包下载:
jersey-bundle-1.19.jar
json-org.jar

3、更改key

用户注册+短信验证_第1张图片
用户注册+短信验证_第2张图片

4、main方法测试

 public static void main(String[] args) {
        String s = testSend();
        System.out.println(s);
    }

5、提取到一个工具包将手机号和验证码改成动态

public static String testSend(String phone,String yzm){
        // just replace key here
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
                "api","key-2172ea9ebbbb2dad933a2705af824e92"));
        WebResource webResource = client.resource(
                "http://voice-api.luosimao.com/v1/verify.json");
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        formData.add("mobile", phone);
        formData.add("code", yzm);
        ClientResponse response =  webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).
                post(ClientResponse.class, formData);
        String textEntity = response.getEntity(String.class);
        int status = response.getStatus();
        //System.out.print(textEntity);
        //System.out.print(status);
        return textEntity;
    }

6、 前台利用ajax将手机号传到后台

 $("#jy").on("click",function () {
        $.ajax({
            url:"getYzm.do",
            data:{"phone":$("#phone").val},
            dataType:"json",
            type:"post",
            success:function (data) {
               if(data.error==0) alert("发送成功");
               else alert("发送失败,请稍后再试!");
            }
    
        })
    })

7、控制层方法并将随机验证码存放在session中

@RequestMapping("getYzm")
    @ResponseBody
    public String getYzm(HttpSession session, String  phone){
        Random r=new Random();//实例化一个随机函数
        int i = r.nextInt(1000000);

        DecimalFormat df=new DecimalFormat("000000");//格式化数字,自动补零
        String yzm = df.format(i);
        System.out.println(yzm);
        session.setAttribute(phone,yzm);

        return checkPhone.testSend(phone,yzm);

    }

8、 前台注册ajax

$('#zc').on('click',function(){
    $.ajax({
        type:"post",
        url:"save.do",
        async : true,
        data:$('#login').serializeArray(),
        dataType:"json",

        success:function (data) {
            if(data){
                alert("注册成功!");
            }else{
                alert("注册失败");
            }

        }
    })
});

9、控制层保存方法

  • 获取sessin中存放的验证码,对比前台传递的验证码,如果一直,保存用户信息
@RequestMapping("save")
    @ResponseBody
    public Boolean saveUser(HttpSession session,User user){


        String newYzm = (String)session.getAttribute(user.getUsPhone());
        System.out.println(newYzm);
        if(newYzm.equals(user.getUsYzm())){
            userService.saveUser(user);
            return true;
        }else{
            return false;
        }


    }

你可能感兴趣的:(java)