提交代码

dev
胡锐 6 days ago
parent 4a958e7217
commit b4174f487e
  1. 2
      cweb/src/main/java/com/cweb/controller/SmsController.java
  2. 6
      service/pom.xml
  3. 45
      service/src/main/java/com/hfkj/config/AliMessageConfig.java

@ -1 +1 @@
package com.cweb.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.config.MessageConfig; 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; /** * @Auther: 胡锐 * @Description: * @Date: 2021/3/26 23:08 */ @Controller @RequestMapping(value = "/sms") @Api(value = "短信服务") public class SmsController { private static Logger log = LoggerFactory.getLogger(SmsController.class); @Resource private RedisUtil redisUtil; @RequestMapping(value = "/sendLoginSMSCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "获取登录验证码") public ResponseData sendLoginSMSCode(@RequestBody JSONObject body) { try { if (body == null || StringUtils.isBlank(body.getString("phone"))) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 校验手机号格式 if (MemberValidateUtil.validatePhone(phone) == false) { log.error("LoginController --> phone() error!", "请输入正确的手机号"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 生成随机6位验证码 String smsCode = String.valueOf(new Random().nextInt(899999) + 100000); MessageConfig.req(phone,smsCode, MessageConfig.HWMSG_ID5); // 验证码缓存5分钟 redisUtil.set("SMS_CODE:"+phone, smsCode, 60*5); return ResponseMsgUtil.success("短信发送成功"); } catch (Exception e) { log.error("SMSController --> getLoginSMSCode() error!", e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/sendBindCardSMSCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "获取绑定卡片验证码") public ResponseData sendBindCardSMSCode(@RequestBody JSONObject body) { try { if (body == null || StringUtils.isBlank(body.getString("phone"))) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 校验手机号格式 if (MemberValidateUtil.validatePhone(phone) == false) { log.error("LoginController --> phone() error!", "请输入正确的手机号"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 生成随机6位验证码 String smsCode = String.valueOf(new Random().nextInt(899999) + 100000); MessageConfig.req(phone,smsCode, MessageConfig.HWMSG_ID7); // 验证码缓存5分钟 redisUtil.set("SMS_CODE:"+phone, smsCode, 60*5); return ResponseMsgUtil.success("短信发送成功"); } catch (Exception e) { log.error("SMSController --> getLoginSMSCode() error!", e); return ResponseMsgUtil.exception(e); } } } package com.cweb.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.config.AliMessageConfig; import com.hfkj.config.MessageConfig; 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; /** * @Auther: 胡锐 * @Description: * @Date: 2021/3/26 23:08 */ @Controller @RequestMapping(value = "/sms") @Api(value = "短信服务") public class SmsController { private static Logger log = LoggerFactory.getLogger(SmsController.class); @Resource private RedisUtil redisUtil; @RequestMapping(value = "/sendLoginSMSCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "获取登录验证码") public ResponseData sendLoginSMSCode(@RequestBody JSONObject body) { try { if (body == null || StringUtils.isBlank(body.getString("phone"))) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 校验手机号格式 if (MemberValidateUtil.validatePhone(phone) == false) { log.error("LoginController --> phone() error!", "请输入正确的手机号"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 生成随机6位验证码 String smsCode = String.valueOf(new Random().nextInt(899999) + 100000); AliMessageConfig.sendSmsCode(phone,smsCode); // 验证码缓存5分钟 redisUtil.set("SMS_CODE:"+phone, smsCode, 60*5); return ResponseMsgUtil.success("短信发送成功"); } catch (Exception e) { log.error("SMSController --> getLoginSMSCode() error!", e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/sendBindCardSMSCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "获取绑定卡片验证码") public ResponseData sendBindCardSMSCode(@RequestBody JSONObject body) { try { if (body == null || StringUtils.isBlank(body.getString("phone"))) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 校验手机号格式 if (MemberValidateUtil.validatePhone(phone) == false) { log.error("LoginController --> phone() error!", "请输入正确的手机号"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 生成随机6位验证码 String smsCode = String.valueOf(new Random().nextInt(899999) + 100000); MessageConfig.req(phone,smsCode, MessageConfig.HWMSG_ID7); // 验证码缓存5分钟 redisUtil.set("SMS_CODE:"+phone, smsCode, 60*5); return ResponseMsgUtil.success("短信发送成功"); } catch (Exception e) { log.error("SMSController --> getLoginSMSCode() error!", e); return ResponseMsgUtil.exception(e); } } }

@ -255,6 +255,12 @@
<artifactId>jetcache-starter-redis</artifactId> <artifactId>jetcache-starter-redis</artifactId>
<version>2.5.0</version> <version>2.5.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<!-- 请将 'the-latest-version' 替换为最新版本号:https://mvnrepository.com/artifact/com.aliyun/dysmsapi20170525 -->
<version>3.0.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.integration</groupId> <groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId> <artifactId>spring-integration-mqtt</artifactId>

@ -0,0 +1,45 @@
package com.hfkj.config;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.dysmsapi20170525.Client;
public class AliMessageConfig {
public static Client createClient() throws Exception {
Config config = new Config()
// 配置 AccessKey ID,请确保代码运行环境配置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
.setAccessKeyId("LTAI5tP9XgTDu49zAAQLGAtJ")
// 配置 AccessKey Secret,请确保代码运行环境配置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
.setAccessKeySecret("bHESLcgX17KZ9amioQi7LNFDL51hoM");
// System.getenv()方法表示获取系统环境变量,不要直接在getenv()中填入AccessKey信息。
// 配置 Endpoint。中国站请使用dysmsapi.aliyuncs.com
config.endpoint = "dysmsapi.aliyuncs.com";
return new Client(config);
}
public static SendSmsResponse sendSmsCode(String phone, String code) throws Exception{
// 初始化请求客户端
Client client = AliMessageConfig.createClient();
JSONObject jsonObject = new JSONObject();
jsonObject.put("code",code);
jsonObject.put("number",phone);
// 构造API请求对象,请替换请求参数值
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(phone)
.setSignName("重庆惠付科技有限公司")
.setTemplateCode("SMS_481195265")
// TemplateParam为序列化后的JSON字符串。其中\"表示转义后的双引号。
.setTemplateParam(jsonObject.toJSONString());
// 获取响应对象
return client.sendSms(sendSmsRequest);
}
}
Loading…
Cancel
Save