编码阶段四 —— 手机验证码登录
这是对应的视频链接【黑马程序员Java项目实战《瑞吉外卖》,轻松掌握springboot + mybatis plus开发核心技术的真java实战项目-哔哩哔哩】 https://b23.tv/3nr8oMw
大家如果做了可以一起探讨一下,我在其他博客实现了那些视频中没有完成的功能
代码开发-梳理交互过程
- 再登录页面输入手机号,点击[获取验证码]按钮,页面发送ajax请求,在服务端调用短信服务API给指定手机号发送验证码短信
- 在登录页面输入验证码,点击[登录]按钮,发送ajax请求,在服务端处理登录请求
首先是使用原来的User实体类就行,然后搭建相应的框架,先给大家导入两个工具,一个是阿里云发送短信的,另一个是随机生成四或六位数验证码
/**
* 短信发送工具类
*/
public class SMSUtils {
/**
* 发送短信
* @param signName 签名
* @param templateCode 模板
* @param phoneNumbers 手机号
* @param param 参数
*/
public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", "");
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setSysRegionId("cn-hangzhou");
request.setPhoneNumbers(phoneNumbers);
request.setSignName(signName);
request.setTemplateCode(templateCode);
request.setTemplateParam("{\"code\":\""+param+"\"}");
try {
SendSmsResponse response = client.getAcsResponse(request);
System.out.println("短信发送成功");
}catch (ClientException e) {
e.printStackTrace();
}
}
}
/**
* 随机生成验证码工具类
*/
public class