起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1720|回复: 0

发送手机短信获取验证码功能第二步

[复制链接]

6

主题

6

帖子

12

积分

新手上路

Rank: 1

积分
12
QQ
发表于 2019-7-1 09:44:51 | 显示全部楼层 |阅读模式
二、接下来我们就该在后台进行短息发送处理了(Demo是用java整合Spring MVC框架写的):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
     * 订单查询发送验证码
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/sendCode",method={RequestMethod.POST,RequestMethod.GET})
    public String sendCode(HttpServletRequest request,HttpServletResponse response){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        HttpSession session = request.getSession();
        String ret="";
        String o_tel = request.getParameter("o_tel");//获取前端传送过来的电话号码
        if(o_tel!=null&&o_tel!=""){
            int Random  = (int) ((Math.random()*9+1)*1000);//随机生成的4位数(验证码)
            String mes = Random+"(用于测试的验证码,两分钟内有效)【学而思博客】";//需在短信中显示的文字信息描述
            ApiSendMobile asm = new ApiSendMobile();
            String msg = asm.sendSM(o_tel, mes);//发送短信
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
            if("发送成功".equals(msg)){
                ret = "{\"msg\":\"SUCCESS\"}";
                //把验证码与电话号码存入session中,并设置120秒有效期限
                session.setAttribute("code", Random);
                session.setAttribute("tel", o_tel);
                session.setMaxInactiveInterval(2*60);
            }else {
                ret = "{\"msg\":\"ERROR\"}";
            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
        }else{
            ret = "{\"msg\":\"ERROR\"}";
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.write(ret);//推送回前端
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        return null;
    }
   后台的大概逻辑就是这样,不熟悉SpringMVC的请不要在意太多细节。因为接下来的才是我要说的关键点:
ApiSendMobile asm = new ApiSendMobile();
   String msg = asm.sendSM(o_tel, mes);

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-5-9 06:08 , Processed in 0.059608 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表