Ver Fonte

发送短信验证码,获取短信验证码接口

15902849627 há 2 anos atrás
pai
commit
ad949a1565

+ 2 - 2
sckw-common/sckw-common-redis/src/main/java/com/sckw/redis/utils/RedissonUtils.java

@@ -112,10 +112,10 @@ public class RedissonUtils {
      * @param key
      * @param key
      * @param value
      * @param value
      */
      */
-    public static void putString(String key, String value) {
+    public static void putString(String key, String value, long expired) {
         log.debug("添加缓存【{}】 【{}】开始", key, value);
         log.debug("添加缓存【{}】 【{}】开始", key, value);
         RBucket<String> bucket = redissonUtils.redissonClient.getBucket(key, StringCodec.INSTANCE);
         RBucket<String> bucket = redissonUtils.redissonClient.getBucket(key, StringCodec.INSTANCE);
-        bucket.set(value, DEFAULT_EXPIRED, TimeUnit.SECONDS);
+        bucket.set(value, expired <= 0 ? DEFAULT_EXPIRED : expired, TimeUnit.SECONDS);
     }
     }
 
 
     /**
     /**

+ 9 - 29
sckw-common/sckw-common-stream/src/main/java/com/sckw/stream/enums/SmsCodeEnum.java

@@ -1,7 +1,15 @@
 package com.sckw.stream.enums;
 package com.sckw.stream.enums;
 
 
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+@AllArgsConstructor
+@Getter
 public enum SmsCodeEnum {
 public enum SmsCodeEnum {
-    VERIFICATION_CODE("SMS_262585113", "验证码", "您的验证码为:${code},该验证码为平台重要凭证,请勿泄露于他人!");
+
+    VERIFICATION_CODE("VERIFICATION_CODE", "SMS_262585113", "验证码", "您的验证码为:${code},该验证码为平台重要凭证,请勿泄露于他人!");
+
+    private String type;
 
 
     private String name;
     private String name;
 
 
@@ -9,19 +17,6 @@ public enum SmsCodeEnum {
 
 
     private String value;
     private String value;
 
 
-    /**
-     * @description 构造方法
-     * @author zk
-     * @date 2020/6/08 11:28
-     * @param value 键 标题 类型 name 值
-     * @return
-     **/
-    private SmsCodeEnum(String name, String title, String value){
-        this.name = name;
-        this.value = value;
-        this.value = value;
-    }
-
     public static String getNameByValue(String value) {
     public static String getNameByValue(String value) {
         for (SmsCodeEnum entityEnum : SmsCodeEnum.values()) {
         for (SmsCodeEnum entityEnum : SmsCodeEnum.values()) {
             if (entityEnum.getValue().equals(value)) {
             if (entityEnum.getValue().equals(value)) {
@@ -31,19 +26,4 @@ public enum SmsCodeEnum {
         return null;
         return null;
     }
     }
 
 
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-
-    public String getValue() {
-        return value;
-    }
-
-    public void setValue(String value) {
-        this.value = value;
-    }
 }
 }

+ 5 - 0
sckw-common/sckw-common-stream/src/main/java/com/sckw/stream/model/SckwSms.java

@@ -40,5 +40,10 @@ public class SckwSms {
      * 签名
      * 签名
      */
      */
     private String signName;
     private String signName;
+
+    /**
+     * 有效时间
+     */
+    private Long effectiveTime;
 }
 }
 
 

+ 9 - 12
sckw-modules/sckw-message/src/main/java/com/sckw/message/consumer/SckwSmsConsumer.java

@@ -1,31 +1,28 @@
 package com.sckw.message.consumer;
 package com.sckw.message.consumer;
 
 
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSON;
+import com.sckw.message.service.SckwSmsHandlerService;
 import com.sckw.stream.model.SckwSms;
 import com.sckw.stream.model.SckwSms;
 import com.sckw.stream.utils.SmsUtil;
 import com.sckw.stream.utils.SmsUtil;
+import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
 
 
 import java.util.function.Consumer;
 import java.util.function.Consumer;
 
 
-@Configuration
+
+@Component
 @Slf4j
 @Slf4j
+@RequiredArgsConstructor
 public class SckwSmsConsumer {
 public class SckwSmsConsumer {
 
 
+    private final SckwSmsHandlerService sckwSmsHandlerService;
+
     @Bean
     @Bean
     public Consumer<SckwSms> sckwSms() {
     public Consumer<SckwSms> sckwSms() {
         return sckwSms -> {
         return sckwSms -> {
-            try {
-                /**发送短信**/
-                SmsUtil.sendSms(sckwSms.getTelephone(), sckwSms.getSignName(), sckwSms.getTemplateCode().getName(), JSON.toJSONString(sckwSms.getParams()));
-                /**数据入库**/
-
-                System.out.println("短信发送成功");
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-            System.out.println("sckwSms: " + sckwSms);
+            sckwSmsHandlerService.handler(sckwSms);
         };
         };
     }
     }
 
 

+ 9 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/controller/MessageController.java

@@ -0,0 +1,9 @@
+package com.sckw.message.controller;
+
+/**
+ * @author: yzc
+ * @date: 2023-06-08 10:58
+ * @description:
+ */
+public class MessageController {
+}

+ 35 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/controller/SmsController.java

@@ -0,0 +1,35 @@
+package com.sckw.message.controller;
+
+import com.sckw.core.web.response.HttpResult;
+import com.sckw.message.model.vo.req.SendSmsVerifyCoderReqVO;
+import com.sckw.message.service.SmsService;
+import jakarta.validation.Valid;
+import lombok.RequiredArgsConstructor;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * @author: yzc
+ * @date: 2023-06-08 10:58
+ * @description: 短信相關接口
+ */
+
+@RestController
+@RequestMapping(value = "/kwmSms")
+@RequiredArgsConstructor
+public class SmsController {
+
+    private final SmsService smsService;
+
+    @PostMapping(value = "/sendVerifyCode", produces = MediaType.APPLICATION_JSON_VALUE)
+    public HttpResult sendVerifyCode(@Valid @RequestBody SendSmsVerifyCoderReqVO param) {
+        smsService.sendVerifyCode(param);
+        return HttpResult.ok();
+    }
+
+    @GetMapping(value = "/getVerifyCode")
+    public HttpResult getVerifyCode(@RequestParam String phone) {
+        return HttpResult.ok("获取验证码成功", smsService.getVerifyCode(phone));
+    }
+
+}

+ 16 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/dao/KwmSmsMapper.java

@@ -0,0 +1,16 @@
+package com.sckw.message.dao;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.sckw.message.model.KwmSms;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * @author: yzc
+ * @date: 2023-06-08 15:49
+ * @description: 短信发送记录mapper
+ */
+
+@Mapper
+public interface KwmSmsMapper extends BaseMapper<KwmSms> {
+
+}

+ 47 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/model/KwmSms.java

@@ -0,0 +1,47 @@
+package com.sckw.message.model;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.sckw.core.model.base.BaseModel;
+import lombok.Builder;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+/**
+ * @author: yzc
+ * @date: 2023-06-08 15:44
+ * @description: 短信发送记录
+ */
+@Getter
+@Setter
+@ToString
+@Builder
+@TableName("kwm_sms")
+public class KwmSms extends BaseModel {
+    /**
+     * 短信类型
+     */
+    private String type;
+
+    /**
+     * 短信模板code
+     */
+    private String code;
+
+    /**
+     * 手机号
+     */
+    private String telephone;
+
+    /**
+     * 发送内容
+     */
+    private String content;
+
+    /**
+     * 参数
+     */
+    private String params;
+
+
+}

+ 30 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/model/vo/req/SendSmsVerifyCoderReqVO.java

@@ -0,0 +1,30 @@
+package com.sckw.message.model.vo.req;
+
+import jakarta.validation.constraints.NotBlank;
+import jakarta.validation.constraints.NotNull;
+import jakarta.validation.constraints.Pattern;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+import lombok.experimental.Accessors;
+
+/**
+ * @author: yzc
+ * @date: 2023-06-08 11:30
+ * @description: 发送短信验证码请求参数
+ */
+
+@Getter
+@Setter
+@ToString
+@Accessors(chain = true)
+public class SendSmsVerifyCoderReqVO {
+
+    @NotBlank(message = "手机号不能为空!")
+    @Pattern(regexp = "^1[0-9]{10}$", message = "非法的手机号")
+    private String phone;
+
+
+    @NotNull(message = "有效时间不能为空!")
+    private Integer effectiveTime;
+}

+ 30 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/service/KwmSmsService.java

@@ -0,0 +1,30 @@
+package com.sckw.message.service;
+
+import com.sckw.message.dao.KwmSmsMapper;
+import com.sckw.message.model.KwmSms;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author: yzc
+ * @date: 2023-06-08 15:48
+ * @description:
+ */
+@Service
+@Slf4j
+@RequiredArgsConstructor
+public class KwmSmsService {
+
+    private final KwmSmsMapper kwmSmsMapper;
+
+    /**
+     * 添加
+     *
+     * @param kwmSms
+     */
+    public void add(KwmSms kwmSms) {
+        kwmSmsMapper.insert(kwmSms);
+    }
+
+}

+ 46 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/service/SckwSmsHandlerService.java

@@ -0,0 +1,46 @@
+package com.sckw.message.service;
+
+import com.alibaba.fastjson2.JSON;
+import com.sckw.message.model.KwmSms;
+import com.sckw.redis.utils.RedissonUtils;
+import com.sckw.stream.enums.SmsCodeEnum;
+import com.sckw.stream.model.SckwSms;
+import com.sckw.stream.utils.SmsUtil;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author: yzc
+ * @date: 2023-06-08 15:54
+ * @description: 短信处理service
+ */
+@Service
+@Slf4j
+@RequiredArgsConstructor
+public class SckwSmsHandlerService {
+
+    private final KwmSmsService kwmSmsService;
+
+    private static final String MESSAGE_SMS_VERIFY_CODE_KEY = "sckw:message:sms:verifyCode:%s";
+
+    public void handler(SckwSms sckwSms) {
+        String key = getMessageSmsVerifyCodeKey(sckwSms.getTelephone());
+        if (RedissonUtils.exists(key)){
+            return;
+        }
+        //发送短信
+        RedissonUtils.putString(key, String.valueOf(sckwSms.getParams().get("code")), sckwSms.getEffectiveTime());
+        SmsUtil.sendSms(sckwSms.getTelephone(), sckwSms.getSignName(), sckwSms.getTemplateCode().getName(), JSON.toJSONString(sckwSms.getParams()));
+        //数据入库
+        SmsCodeEnum smsCodeEnum = sckwSms.getTemplateCode();
+        KwmSms kwmSms = KwmSms.builder().type(smsCodeEnum.getType()).code(smsCodeEnum.getName())
+                .telephone(sckwSms.getTelephone()).content(smsCodeEnum.getValue())
+                .params(JSON.toJSONString(sckwSms.getParams())).build();
+        kwmSmsService.add(kwmSms);
+    }
+
+    private String getMessageSmsVerifyCodeKey(String phone) {
+        return String.format(MESSAGE_SMS_VERIFY_CODE_KEY, phone);
+    }
+}

+ 58 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/service/SmsService.java

@@ -0,0 +1,58 @@
+package com.sckw.message.service;
+
+import com.alibaba.fastjson2.JSON;
+import com.sckw.core.exception.BusinessException;
+import com.sckw.core.utils.NumberUtils;
+import com.sckw.message.model.vo.req.SendSmsVerifyCoderReqVO;
+import com.sckw.redis.utils.RedissonUtils;
+import com.sckw.stream.enums.SmsCodeEnum;
+import com.sckw.stream.model.SckwSms;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.cloud.stream.function.StreamBridge;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author: yzc
+ * @date: 2023-06-08 11:08
+ * @description:
+ */
+@Service
+@Slf4j
+@RequiredArgsConstructor
+public class SmsService {
+
+    private final StreamBridge streamBridge;
+
+    private static final String MESSAGE_SMS_VERIFY_CODE_KEY = "sckw:message:sms:verifyCode:%s";
+
+    public void sendVerifyCode(SendSmsVerifyCoderReqVO param) {
+        String phone = param.getPhone();
+        // 防重攻击
+        String messageSmsVerifyCodeKey = getMessageSmsVerifyCodeKey(phone);
+        if (Boolean.TRUE.equals(RedissonUtils.exists(messageSmsVerifyCodeKey))) {
+            throw new BusinessException("请勿频繁获取短信验证码");
+        }
+        SckwSms sckwSms = new SckwSms();
+        sckwSms.setTelephone(param.getPhone());
+        sckwSms.setSignName("矿拉拉");
+        sckwSms.setTemplateCode(SmsCodeEnum.VERIFICATION_CODE);
+        Map<String, Object> params = new HashMap<>();
+        params.put("code", NumberUtils.createRandomVcode());
+        sckwSms.setParams(params);
+        sckwSms.setEffectiveTime(Long.valueOf(param.getEffectiveTime()));
+        streamBridge.send("sckw-sms", JSON.toJSONString(sckwSms));
+    }
+
+    public String getVerifyCode(String phone) {
+        String messageSmsVerifyCodeKey = getMessageSmsVerifyCodeKey(phone);
+        return RedissonUtils.getString(messageSmsVerifyCodeKey);
+    }
+
+    private String getMessageSmsVerifyCodeKey(String phone) {
+        return String.format(MESSAGE_SMS_VERIFY_CODE_KEY, phone);
+    }
+}