Просмотр исходного кода

提供获取短信验证码后台接口

yzc 2 лет назад
Родитель
Сommit
a1f90d0f43

+ 41 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/controller/background/SmsBackgroundController.java

@@ -0,0 +1,41 @@
+package com.sckw.message.controller.background;
+
+import com.sckw.core.exception.BusinessException;
+import com.sckw.core.web.response.HttpResult;
+import com.sckw.message.model.vo.req.GetSmsVerifyCoderReqVO;
+import com.sckw.message.service.SmsService;
+import jakarta.validation.Valid;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.MediaType;
+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 java.util.Objects;
+
+/**
+ * @desc: 短信后台接口
+ * @author: yzc
+ * @date: 2023-08-29 19:22
+ */
+@RestController
+@RequestMapping(value = "/kwmBackgroundSms")
+@RequiredArgsConstructor
+public class SmsBackgroundController {
+
+    private final SmsService smsService;
+
+    @Value("${background.sms.getVerifyCode.token}")
+    private String verifyCodeToken;
+
+    @PostMapping(value = "/getVerifyCode", produces = MediaType.APPLICATION_JSON_VALUE)
+    public HttpResult sendVerifyCode(@Valid @RequestBody GetSmsVerifyCoderReqVO param) {
+        if (!Objects.equals(param.getToken(), verifyCodeToken)) {
+            throw new BusinessException("非法token!");
+        }
+        return HttpResult.ok("获取验证码成功", smsService.getVerifyCode(param));
+    }
+
+}

+ 29 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/model/vo/req/GetSmsVerifyCoderReqVO.java

@@ -0,0 +1,29 @@
+package com.sckw.message.model.vo.req;
+
+import jakarta.validation.constraints.NotBlank;
+import jakarta.validation.constraints.Pattern;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+/**
+ * @author: yzc
+ * @date: 2023-06-08 11:30
+ * @description: 发送短信验证码请求参数
+ */
+
+@Getter
+@Setter
+@ToString
+public class GetSmsVerifyCoderReqVO {
+
+    @NotBlank(message = "手机号不能为空!")
+    @Pattern(regexp = "^1[3456789]\\d{9}$", message = "非法的手机号")
+    private String phone;
+
+    @NotBlank(message = "短信类型不能为空")
+    private String type;
+
+    @NotBlank(message = "token不能为空")
+    private String token;
+}

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

@@ -8,6 +8,7 @@ import com.sckw.core.model.constant.Global;
 import com.sckw.core.utils.NumberUtils;
 import com.sckw.core.utils.StringUtils;
 import com.sckw.core.web.context.LoginUserHolder;
+import com.sckw.message.model.vo.req.GetSmsVerifyCoderReqVO;
 import com.sckw.message.model.vo.req.SendSmsVerifyCoderReqVO;
 import com.sckw.redis.constant.RedisConstant;
 import com.sckw.redis.utils.RedissonUtils;
@@ -17,6 +18,7 @@ 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;
 import java.util.Objects;
@@ -58,4 +60,8 @@ public class SmsService {
         streamBridge.send("sckw-sms", JSON.toJSONString(sckwSms));
     }
 
+    public String getVerifyCode(GetSmsVerifyCoderReqVO param) {
+        String key = String.format(RedisConstant.MESSAGE_SMS_VERIFY_CODE_VALUE_KEY, param.getType(), param.getPhone());
+        return RedissonUtils.getString(key);
+    }
 }