|
@@ -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);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|