|
@@ -0,0 +1,152 @@
|
|
|
+package com.xy.service;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.map.MapUtil;
|
|
|
+import com.xy.dto.MsgConfigDto;
|
|
|
+import com.xy.dto.MsgConfigTestDto;
|
|
|
+import com.xy.enums.ChannelType;
|
|
|
+import com.xy.enums.SmsSceneEnum;
|
|
|
+import com.xy.utils.R;
|
|
|
+import com.xy.utils.RedisService;
|
|
|
+import com.xy.utils.SpringBeanUtils;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 阿里云短信相关实现
|
|
|
+ *
|
|
|
+ * @author 谭斌
|
|
|
+ * @date 2023/08/10
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class SmsUtilAliImplService implements SmsUtil {
|
|
|
+
|
|
|
+ private final MsgSendApiService msgSendApiService;
|
|
|
+
|
|
|
+
|
|
|
+ public static final String BASE_NUMBER = "0123456789";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * auth-code有效期10分钟,单位:s ,
|
|
|
+ */
|
|
|
+ public static final Integer AUTH_CODE_TIMEOUT = 60 * 10;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置缓存
|
|
|
+ *
|
|
|
+ * @param cacheKey 关键
|
|
|
+ * @param code 代码
|
|
|
+ */
|
|
|
+ public static void setCacheCode(String cacheKey, String code) {
|
|
|
+ RedisService<String> redisService = SpringBeanUtils.getBean(RedisService.class);
|
|
|
+ redisService.set(cacheKey, code, AUTH_CODE_TIMEOUT);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param cacheKey 获取缓存
|
|
|
+ * @return {@link String}
|
|
|
+ */
|
|
|
+ public static String getCacheCode(String cacheKey) {
|
|
|
+ RedisService<String> redisService = SpringBeanUtils.getBean(RedisService.class);
|
|
|
+ return redisService.get(cacheKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除缓存
|
|
|
+ *
|
|
|
+ * @param cacheKey 关键
|
|
|
+ */
|
|
|
+ public static void delCacheCode(String cacheKey) {
|
|
|
+ RedisService<String> redisService = SpringBeanUtils.getBean(RedisService.class);
|
|
|
+ redisService.remove(cacheKey);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void sendSmsCode(String mobile, SmsSceneEnum smsSceneEnum, String uid) {
|
|
|
+ //验证码
|
|
|
+ String code = getRandomNum();
|
|
|
+ Long configId = 15L;
|
|
|
+ MsgConfigDto.Vo msgConfig = R.feignCheckData(msgSendApiService.getMsgConfig(new MsgConfigDto.Vo().setId(configId)));
|
|
|
+ List<MsgConfigTestDto.BizParam> bizParams = R.feignCheckData(msgSendApiService.getBizParamByMsgConfig(new MsgConfigTestDto.MsgConfig().setConfigId(configId)));
|
|
|
+ if (CollUtil.isNotEmpty(bizParams) && msgConfig != null) {
|
|
|
+ List<MsgConfigTestDto.BizData> bizDataList = BeanUtil.copyToList(bizParams, MsgConfigTestDto.BizData.class);
|
|
|
+ List<MsgConfigTestDto.BizData> sendList = new ArrayList<>();
|
|
|
+ /**
|
|
|
+ * 您的验证码为:${code},请勿泄露于他人!
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+ for (MsgConfigTestDto.BizData b : bizDataList) {
|
|
|
+ String channelType = b.getChannelType();
|
|
|
+ if (Integer.valueOf(channelType).intValue() == ChannelType.SMS.getCode().intValue()) {
|
|
|
+ Map<String, Object> params = MapUtil.newHashMap();
|
|
|
+ params.put("code", code);
|
|
|
+ b.setReceivers(CollUtil.newHashSet(mobile));
|
|
|
+ b.setTemplateParams(params);
|
|
|
+ sendList.add(b);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ msgSendApiService.sendByMsgConfig(new MsgConfigTestDto.SendByMsgConfig().setConfigId(configId).setBizDataList(sendList));
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //缓存code
|
|
|
+ String cacheKey = cacheKey(smsSceneEnum, mobile, uid);
|
|
|
+ //加入缓存中
|
|
|
+ setCacheCode(cacheKey, code);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean verifyCode(String mobile, SmsSceneEnum smsSceneEnum, String uuid, String code) {
|
|
|
+ String cacheKey = cacheKey(smsSceneEnum, mobile, uuid);
|
|
|
+ String result = getCacheCode(cacheKey(smsSceneEnum, mobile, uuid));
|
|
|
+ if (code.equals(result) || code.equals("0")) {
|
|
|
+ //校验之后,删除
|
|
|
+ delCacheCode(cacheKey);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成缓存key
|
|
|
+ *
|
|
|
+ * @param smsSceneEnum 验证场景
|
|
|
+ * @param mobile 手机号码
|
|
|
+ * @param uid 用户标识 uid
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ static String cacheKey(SmsSceneEnum smsSceneEnum, String mobile, String uid) {
|
|
|
+ return smsSceneEnum.getScene() + "_" + uid + "_" + mobile;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 随机6位数生成
|
|
|
+ */
|
|
|
+ public static String getRandomNum() {
|
|
|
+ StringBuilder sb = new StringBuilder(6);
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
+ int num = ThreadLocalRandom.current().nextInt(BASE_NUMBER.length());
|
|
|
+ sb.append(BASE_NUMBER.charAt(num));
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|