From 686c24790f4bbab5f1ab6d444fe4d352852e1c9c Mon Sep 17 00:00:00 2001 From: hu177768073 <177768073@qq.com> Date: Fri, 1 Nov 2024 16:25:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cweb/src/main/java/com/hfkj/controller/SmsController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cweb/src/main/java/com/hfkj/controller/SmsController.java b/cweb/src/main/java/com/hfkj/controller/SmsController.java index 58fed87..3d3d879 100644 --- a/cweb/src/main/java/com/hfkj/controller/SmsController.java +++ b/cweb/src/main/java/com/hfkj/controller/SmsController.java @@ -1 +1 @@ -package com.hfkj.controller; import com.alibaba.fastjson.JSONObject; import com.aliyun.dysmsapi20170525.models.SendSmsRequest; import com.aliyun.dysmsapi20170525.models.SendSmsResponse; import com.hfkj.common.exception.ErrorCode; import com.hfkj.common.exception.ErrorHelp; import com.hfkj.common.exception.SysCode; import com.hfkj.common.utils.MemberValidateUtil; import com.hfkj.common.utils.RedisUtil; import com.hfkj.common.utils.ResponseMsgUtil; import com.hfkj.model.ResponseData; import com.hfkj.platform.aliyun.config.AliyunConfig; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.Random; @Controller @RequestMapping(value = "/sms") @Api(value = "短信服务") public class SmsController { private static Logger log = LoggerFactory.getLogger(SmsController.class); @Resource private RedisUtil redisUtil; @RequestMapping(value = "/sendLoginCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "获取登录验证码 {'phone': '手机号'}") public ResponseData sendLoginCode(@RequestBody JSONObject body) { try { if (body == null || StringUtils.isBlank(body.getString("phone"))) { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 校验手机号格式 if (!MemberValidateUtil.validatePhone(phone)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 生成随机6位验证码 String smsCode = String.valueOf(new Random().nextInt(899999) + 100000); // 发送短信 SendSmsRequest sendSmsRequest = new SendSmsRequest() .setPhoneNumbers(phone) .setSignName("元气优淘") .setTemplateCode("SMS_305460844") .setTemplateParam("{\"code\":\""+smsCode+"\"}"); SendSmsResponse sendSmsResponse = AliyunConfig.createClient().sendSms(sendSmsRequest); if (!sendSmsResponse.getStatusCode().equals(200)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, sendSmsResponse.body.getMessage()); } // 验证码缓存5分钟 redisUtil.set("SMS_LOGIN_CODE:"+phone, smsCode, 60*5); return ResponseMsgUtil.success(sendSmsResponse); } catch (Exception e) { return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/sendBindPhoneCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "发送绑定手机号验证码 {'phone': '手机号'}") public ResponseData sendBindPhoneCode(@RequestBody JSONObject body) { try { if (body == null || StringUtils.isBlank(body.getString("phone"))) { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 校验手机号格式 if (!MemberValidateUtil.validatePhone(phone)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 生成随机6位验证码 String smsCode = String.valueOf(new Random().nextInt(899999) + 100000); // 发送短信 SendSmsRequest sendSmsRequest = new SendSmsRequest() .setPhoneNumbers(phone) .setSignName("元气优淘") .setTemplateCode("SMS_305460844") .setTemplateParam("{\"code\":\""+smsCode+"\"}"); SendSmsResponse sendSmsResponse = AliyunConfig.createClient().sendSms(sendSmsRequest); if (!sendSmsResponse.getStatusCode().equals(200)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, sendSmsResponse.body.getMessage()); } // 验证码缓存5分钟 redisUtil.set("SMS_BIND_PHONE_CODE:"+phone, smsCode, 60*5); return ResponseMsgUtil.success("短信发送成功"); } catch (Exception e) { return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/sendUpdatePhoneCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "发送修改手机号验证码 {'phone': '手机号'}") public ResponseData sendUpdatePhoneCode(@RequestBody JSONObject body) { try { if (body == null || StringUtils.isBlank(body.getString("phone"))) { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 校验手机号格式 if (!MemberValidateUtil.validatePhone(phone)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 生成随机6位验证码 String smsCode = String.valueOf(new Random().nextInt(899999) + 100000); // 发送短信 SendSmsRequest sendSmsRequest = new SendSmsRequest() .setPhoneNumbers(phone) .setSignName("元气优淘") .setTemplateCode("SMS_305460844") .setTemplateParam("{\"code\":\""+smsCode+"\"}"); SendSmsResponse sendSmsResponse = AliyunConfig.createClient().sendSms(sendSmsRequest); if (!sendSmsResponse.getStatusCode().equals(200)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, sendSmsResponse.body.getMessage()); } // 验证码缓存5分钟 redisUtil.set("SMS_UPDATE_PHONE_CODE:"+phone, smsCode, 60*5); return ResponseMsgUtil.success("短信发送成功"); } catch (Exception e) { return ResponseMsgUtil.exception(e); } } } \ No newline at end of file +package com.hfkj.controller; import com.alibaba.fastjson.JSONObject; import com.aliyun.dysmsapi20170525.models.SendSmsRequest; import com.aliyun.dysmsapi20170525.models.SendSmsResponse; import com.hfkj.common.exception.ErrorCode; import com.hfkj.common.exception.ErrorHelp; import com.hfkj.common.exception.SysCode; import com.hfkj.common.utils.MemberValidateUtil; import com.hfkj.common.utils.RedisUtil; import com.hfkj.common.utils.ResponseMsgUtil; import com.hfkj.model.ResponseData; import com.hfkj.platform.aliyun.config.AliyunConfig; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.Random; @Controller @RequestMapping(value = "/sms") @Api(value = "短信服务") public class SmsController { private static Logger log = LoggerFactory.getLogger(SmsController.class); @Resource private RedisUtil redisUtil; @RequestMapping(value = "/sendLoginCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "获取登录验证码 {'phone': '手机号'}") public ResponseData sendLoginCode(@RequestBody JSONObject body) { try { if (body == null || StringUtils.isBlank(body.getString("phone"))) { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 校验手机号格式 if (!MemberValidateUtil.validatePhone(phone)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 生成随机6位验证码 String smsCode = String.valueOf(new Random().nextInt(899999) + 100000); // 发送短信 SendSmsRequest sendSmsRequest = new SendSmsRequest() .setPhoneNumbers(phone) .setSignName("元气优淘") .setTemplateCode("SMS_305460844") .setTemplateParam("{\"code\":\""+smsCode+"\"}"); SendSmsResponse sendSmsResponse = AliyunConfig.createClient().sendSms(sendSmsRequest); if (!sendSmsResponse.getStatusCode().equals(200)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, sendSmsResponse.body.getMessage()); } // 验证码缓存5分钟 redisUtil.set("SMS_LOGIN_CODE:"+phone, smsCode, 60*5); return ResponseMsgUtil.success("发送成功"); } catch (Exception e) { return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/sendBindPhoneCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "发送绑定手机号验证码 {'phone': '手机号'}") public ResponseData sendBindPhoneCode(@RequestBody JSONObject body) { try { if (body == null || StringUtils.isBlank(body.getString("phone"))) { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 校验手机号格式 if (!MemberValidateUtil.validatePhone(phone)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 生成随机6位验证码 String smsCode = String.valueOf(new Random().nextInt(899999) + 100000); // 发送短信 SendSmsRequest sendSmsRequest = new SendSmsRequest() .setPhoneNumbers(phone) .setSignName("元气优淘") .setTemplateCode("SMS_305460844") .setTemplateParam("{\"code\":\""+smsCode+"\"}"); SendSmsResponse sendSmsResponse = AliyunConfig.createClient().sendSms(sendSmsRequest); if (!sendSmsResponse.getStatusCode().equals(200)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, sendSmsResponse.body.getMessage()); } // 验证码缓存5分钟 redisUtil.set("SMS_BIND_PHONE_CODE:"+phone, smsCode, 60*5); return ResponseMsgUtil.success("短信发送成功"); } catch (Exception e) { return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/sendUpdatePhoneCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "发送修改手机号验证码 {'phone': '手机号'}") public ResponseData sendUpdatePhoneCode(@RequestBody JSONObject body) { try { if (body == null || StringUtils.isBlank(body.getString("phone"))) { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 校验手机号格式 if (!MemberValidateUtil.validatePhone(phone)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 生成随机6位验证码 String smsCode = String.valueOf(new Random().nextInt(899999) + 100000); // 发送短信 SendSmsRequest sendSmsRequest = new SendSmsRequest() .setPhoneNumbers(phone) .setSignName("元气优淘") .setTemplateCode("SMS_305460844") .setTemplateParam("{\"code\":\""+smsCode+"\"}"); SendSmsResponse sendSmsResponse = AliyunConfig.createClient().sendSms(sendSmsRequest); if (!sendSmsResponse.getStatusCode().equals(200)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, sendSmsResponse.body.getMessage()); } // 验证码缓存5分钟 redisUtil.set("SMS_UPDATE_PHONE_CODE:"+phone, smsCode, 60*5); return ResponseMsgUtil.success("短信发送成功"); } catch (Exception e) { return ResponseMsgUtil.exception(e); } } } \ No newline at end of file