赞
踩
阿里云官方:阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台
- package com.example.regiee_take_out.utils;
-
- import com.aliyuncs.DefaultAcsClient;
- import com.aliyuncs.IAcsClient;
- import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
- import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
- import com.aliyuncs.exceptions.ClientException;
- import com.aliyuncs.profile.DefaultProfile;
-
- /**
- * 短信发送工具类
- */
- 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();
- }
- }
-
- }
- package com.example.regiee_take_out.utils;
-
- import java.util.Random;
-
- /**
- * 随机生成验证码工具类
- */
- public class ValidateCodeUtils {
- /**
- * 随机生成验证码
- * @param length 长度为4位或者6位
- * @return
- */
- public static Integer generateValidateCode(int length){
- Integer code =null;
- if(length == 4){
- code = new Random().nextInt(9999);//生成随机数,最大为9999
- if(code < 1000){
- code = code + 1000;//保证随机数为4位数字
- }
- }else if(length == 6){
- code = new Random().nextInt(999999);//生成随机数,最大为999999
- if(code < 100000){
- code = code + 100000;//保证随机数为6位数字
- }
- }else{
- throw new RuntimeException("只能生成4位或6位数字验证码");
- }
- return code;
- }
-
- /**
- * 随机生成指定长度字符串验证码
- * @param length 长度
- * @return
- */
- public static String generateValidateCode4String(int length){
- Random rdm = new Random();
- String hash1 = Integer.toHexString(rdm.nextInt());
- String capstr = hash1.substring(0, length);
- return capstr;
- }
- }
- package com.example.regiee_take_out.controller;
-
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.example.regiee_take_out.common.R;
- import com.example.regiee_take_out.entity.User;
- import com.example.regiee_take_out.service.UserService;
- import com.example.regiee_take_out.utils.SMSUtils;
- import com.example.regiee_take_out.utils.ValidateCodeUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import javax.servlet.http.HttpSession;
- import java.util.Map;
-
- /**
- * @date :Created in 2023/3/15 17:35
- * @description:
- * @modified By:
- * @version:
- */
- @RestController
- @RequestMapping("/user")
- @Slf4j
- public class UserController {
-
- @Autowired
- private UserService userService;
-
- /**
- *
- * @param user
- * @return
- */
- @PostMapping("/sendMsg")
- public R<String> sendMsg(@RequestBody User user, HttpSession session){
- // 获取手机号
- String phone = user.getPhone();
- if ( StringUtils.isNotEmpty(phone) ){
- // 生成随机的4位验证码
- String code = ValidateCodeUtils.generateValidateCode(4).toString();
- log.info("code={}",phone);
- log.info("code={}",code);
- // 调用阿里云提供的短信服务API完成发送短信
- // SMS_xxxx阿里云测试专用
- // SMSUtils.sendMessage("岭师小白","SMS_xxxx",phone,code);
- // SMSUtils.sendMessage("岭师小白","SMS_xxxx",phone,code);
- // 需要将生成的验证码保存到Session
- session.setAttribute(phone,code);
-
- return R.success("手机验证码短信发送成功");
- }
-
-
- return R.success("短信发送失败");
- }
-
- /**
- * 移动端登录
- * @param map
- * @param session
- * @return
- */
- @PostMapping("/login")
- public R<User> login(@RequestBody Map map, HttpSession session){
-
- log.info(map.toString());
-
- // 获取手机号
- String phone = map.get("phone").toString();
- // 获取验证码
- String code = map.get("code").toString();
-
- // 从Session中获取保存的验证码
- Object codeInSession = session.getAttribute(phone);
- // 进行验证码的对比(页面提交的验证码和Session中保存的验证码
- if ( codeInSession!=null&&codeInSession.equals(code) ){
- // 如果比对成功,说明登录成功
- LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(User::getPhone,phone);
- User user = userService.getOne(queryWrapper);
- if ( user==null ){
- // 判断当前手机号对应的用户是否为新用户,如果是新用户就自动注册
- user = new User();
- user.setPhone(phone);
- user.setStatus(1);
- userService.save(user);
-
- }
- // 不加的话会闪退
- session.setAttribute("user",user.getId());
- return R.success(user);
- }
- return R.error("登录失败");
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。