From 48a128be6a161dde9bda4c10da0026c73c9224a8 Mon Sep 17 00:00:00 2001 From: hu177768073 <177768073@qq.com> Date: Fri, 1 Nov 2024 16:18:15 +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 --- .../com/hfkj/controller/SmsController.java | 2 +- service/pom.xml | 6 +++++ .../platform/aliyun/config/AliyunConfig.java | 26 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 service/src/main/java/com/hfkj/platform/aliyun/config/AliyunConfig.java diff --git a/cweb/src/main/java/com/hfkj/controller/SmsController.java b/cweb/src/main/java/com/hfkj/controller/SmsController.java index 3e3a6be..58fed87 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.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 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); // 验证码缓存5分钟 redisUtil.set("SMS_LOGIN_CODE:"+phone, 123456, 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); // 验证码缓存5分钟 redisUtil.set("SMS_BIND_PHONE_CODE:"+phone, 123456, 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); // 验证码缓存5分钟 redisUtil.set("SMS_UPDATE_PHONE_CODE:"+phone, 123456, 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(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 diff --git a/service/pom.xml b/service/pom.xml index 45f70b3..0a0dbea 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -290,6 +290,12 @@ 1.0.1 ${basedir}/lib/pop-sdk-1.18.23-all.jar + + + com.aliyun + dysmsapi20170525 + 3.0.0 + diff --git a/service/src/main/java/com/hfkj/platform/aliyun/config/AliyunConfig.java b/service/src/main/java/com/hfkj/platform/aliyun/config/AliyunConfig.java new file mode 100644 index 0000000..63edde8 --- /dev/null +++ b/service/src/main/java/com/hfkj/platform/aliyun/config/AliyunConfig.java @@ -0,0 +1,26 @@ +package com.hfkj.platform.aliyun.config; + +import com.aliyun.dysmsapi20170525.Client; +import com.aliyun.teaopenapi.models.Config; + +/** + * @className: AliyunConfig + * @author: HuRui + * @date: 2024/11/1 + **/ +public class AliyunConfig { + + /** + * 构建客户端 + * @return + * @throws Exception + */ + public static Client createClient() throws Exception { + Config config = new Config() + .setAccessKeyId("LTAI5tHpe3pnXx6CKLaHkq16") + .setAccessKeySecret("rPN29MiECptHl3IhHVWl4fR8KJYhXj"); + // 配置 Endpoint + config.endpoint = "dysmsapi.aliyuncs.com"; + return new Client(config); + } +}