短信发送、短信验证

1.添加依赖

        
            com.aliyun
            aliyun-java-sdk-core
            4.1.0
        

2.短信发送(发送短信验证码 && 发送普通短信)

/**
     * 发送短信验证码
     *
     * @param mobile
     * @return
     */
    public Boolean sendSmsCode(String mobile) {
        boolean flag = false;
        try {
            String key = CommonConstant.SMS_PREFIX + mobile;
            String code = redisTemplate.opsForValue().get(key);
            if (ObjectUtils.isEmpty(code)) {
                code = CodeUtil.getGenerateCode(6);
            }
            DefaultProfile profile = DefaultProfile.getProfile(regionId,id,secret);
            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain("xxx");
            request.setVersion("");
            request.setAction("SendSms");
            request.putQueryParameter("SignName", "xxx");
            request.putQueryParameter("TemplateCode", "xxx");
            request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
            request.putQueryParameter("RegionId", regionId);
            //短信接收号码,多个号码逗号分隔
            request.putQueryParameter("PhoneNumbers", mobile);
            CommonResponse response = client.getCommonResponse(request);
            JSONObject jsonObject = JSONObject.parseObject(response.getData());
            if ("OK".equals(jsonObject.get("Code"))) {
                redisTemplate.opsForValue().set(key, code, 15, TimeUnit.MINUTES);
                flag = true;
            }
        } catch (Exception e) {
            LOGGER.error("sendSmsCode-发送短信验证码失败:{}", e);
        }
        return flag;
    }

    /**
     * 发送普通短信(区别于发送短信验证码,不需要存Redis)
     *
     * @param mobile 接收短信手机号
     * @param templateCode 模板编号
     * @param needTemplateParam 是否需要模板传参
     * @param templateParam 模板参数
     * @return 是否发送成功
     */
    public Boolean sendSmsMessage(String mobile, String templateCode, boolean needTemplateParam, String templateParam) {
        boolean flag = false;
        try {
            DefaultProfile profile = DefaultProfile.getProfile(regionId,id,secret);
            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain("xxx");
            request.setVersion("");
            request.setAction("SendSms");
            request.putQueryParameter("SignName", "xxx");
            request.putQueryParameter("TemplateCode", templateCode);
            if(needTemplateParam){
                request.putQueryParameter("TemplateParam", templateParam);
            }
            request.putQueryParameter("RegionId", regionId);
            //短信接收号码,多个号码逗号分隔
            request.putQueryParameter("PhoneNumbers", mobile);
            CommonResponse response = client.getCommonResponse(request);
            JSONObject jsonObject = JSONObject.parseObject(response.getData());
            if ("OK".equals(jsonObject.get("Code"))) {
                flag = true;
            }
        } catch (Exception e) {
            LOGGER.error("sendSmsMessage-发送短信消息失败:{}", e);
        }
        return flag;
    }

3.短信验证(验证短信验证码)

/**
     * 验证短信验证码
     *
     * @param map
     */
    public boolean verifySmsCode(LinkedHashMap map) {
        Object phone = map.get("phone");
        Object smsCode = map.get("smsCode");
        // 验证手机号跟验证码不为空
        ................

        String key = CommonConstant.SMS_PREFIX + phone.toString();
        String value = redisTemplate.opsForValue().get(key);
        if (!ObjectUtils.isEmpty(value) && value.equals(smsCode)) {
            return true;
            // 由于事务回滚时,Redis不回滚,所以等后续业务成功后再删除Redis key.
            //redisTemplate.delete(key);
        } else {
            throw new Exception(ResponseEnum.VERIFICATION_CODE_ERROR.getCode(),"短信验证码不正确");
        }
    }

 

你可能感兴趣的:(杂⑦杂⑧,Redis)