RedissonClient的使用

import org.redisson.api.RedissonClient;

@Resource
private RedissonClient redissonClient;

public AjaxResult getSmsVerificationCode(CarrierInfoReq carrierInfoReq) {
        log.info("承运商短信验证码接口传参={}", carrierInfoReq.getContactNumber());
        if (StringUtils.isBlank(carrierInfoReq.getContactNumber())) {
            throw new CustomException("手机号不能为空!");
        }
        String regex = "^1[0-9]{10}$";
        boolean matches = Pattern.matches(regex, carrierInfoReq.getContactNumber());
        if (!matches) {
            throw new CustomException("请输入正确的手机号!");
        }
        String key = "scm:basedata:sms:verificationCode:" + carrierInfoReq.getContactNumber();
        int min = 100000; // 6位数的最小值
        int max = 999999; // 6位数的最大值
        Random random = new Random();
        // 6位随机验证码
        int verificationCode = random.nextInt(max - min + 1) + min;
        SendSmsBean sendSmsBean = new SendSmsBean();
        sendSmsBean.setPhoneNumbers(carrierInfoReq.getContactNumber());
        sendSmsBean.setAppName("承运商平台系统登陆");
        sendSmsBean.setTemplateCode("17126");
        //不可以改
        sendSmsBean.setSignName("供应链");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", verificationCode);
        String codeString = JSON.toJSONString(jsonObject);
        sendSmsBean.setTemplateParamJson(codeString);
        Map<String, Object> mapResult = new HashMap<>();
        mapResult.put("timeStamp", DateUtils.getTime());
        mapResult.put("contactNumber", carrierInfoReq.getContactNumber());
        try {
            // 从缓存里面取值验证码
            String verificationCodeValue = null;
            RBucket<String> bucket = redissonClient.getBucket(key);
            if(bucket.isExists()){
                verificationCodeValue = bucket.get();
            }
            if (StringUtils.isNotBlank(verificationCodeValue)) {
                mapResult.put("smsVerificationCode", verificationCodeValue);
                return AjaxResult.success(mapResult);
            }

            AjaxResult ajaxResult = smsService.sendSms(sendSmsBean);

            if ("200".equals(MapUtils.getString(ajaxResult, "code", "0"))) {
                // 验证码放入缓存,过期时间60秒
                bucket.set(String.valueOf(verificationCode));
                bucket.expire(60, TimeUnit.SECONDS); // 设置过期时间为60秒
                mapResult.put("smsVerificationCode", verificationCode);
            } else {
                log.error("承运商短信验证码接口异常=" + ajaxResult.toString());
                return AjaxResult.error("获取承运商短信验证码失败!", ajaxResult);
            }
        } catch (Exception e) {
            log.error("承运商短信验证码接口异常=", e);
            return AjaxResult.error("获取承运商短信验证码失败!", mapResult);
        }
        return AjaxResult.success(mapResult);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方巴黎~Sunsiny

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值