spring-boot 若依对接腾讯云短信

spring-boot 若依对接腾讯云短信


在common模块下pop.xml引入短信库
1231231

// An highlighted block
<dependency>
    <groupId>com.github.qcloudsms</groupId>
    <artifactId>qcloudsms</artifactId>
    <version>1.0.6</version>
</dependency>

在配置文件新建腾讯配置(上边几个是cos不用可以忽略)
在这里插入图片描述
在common模块下新建腾讯的配置类读取配置文件的参数
在这里插入图片描述

// An highlighted block
package com.ruoyi.common.extend.tencent;

import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.ruoyi.common.config.TencentConfig;
import com.ruoyi.common.domain.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 腾讯云短信
 */
@Component
public class TencentSms {


    @Autowired
    private TencentConfig tencentConfig;

    /**
     * 发送短信验证码
     * @param phone 手机号
     * @param code 验证码
     */
    public Result senSms(String phone, String code){
        Result result = new Result();
        try {
            //短信模板中的参数列表
            String[] params = {code};
            SmsSingleSender sender = new SmsSingleSender(tencentConfig.getSmsAppId(), tencentConfig.getSmsAppKey());
            SmsSingleSenderResult sendResult = sender.sendWithParam("86", phone,
                    tencentConfig.getSmsTemplateId(), params, tencentConfig.getSmsSign(), "", "");
            if (sendResult.result == 0){
                result.setCode(1);
                result.setMsg("发送成功");
            }else {
                result.setCode(0);
                result.setMsg(sendResult.errMsg);
            }
            System.out.println(result);
        } catch (HTTPException e) {
            // HTTP 响应码错误
            e.printStackTrace();
            result.setCode(0);
            result.setMsg("相应码错误");
        } catch (JSONException e) {
            // JSON 解析错误
            e.printStackTrace();
            result.setCode(0);
            result.setMsg("JSON解析错误");
        } catch (IOException e) {
            // 网络 IO 错误
            e.printStackTrace();
            result.setMsg("网络IO错误");
        }
        return result;
    }
}


在需要的地方直接调用就可以了
在这里插入图片描述

// An highlighted block
package com.ruoyi.api.servive.common.impl;
import com.ruoyi.api.servive.common.ISmsService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.domain.Result;
import com.ruoyi.common.extend.handle.StringHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.extend.tencent.TencentSms;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 验证码
 */
@Service
public class SmsServiceImpl implements ISmsService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private TencentSms tencentSms;
    /**
     * 发送验证码
     */
    @Override
    public AjaxResult smsSend(String mobile, String event) {
        String code  = StringHandle.createRandom(true,4);
        Result result = tencentSms.senSms(mobile,code);
        if (result.getCode() == 0) return  AjaxResult.error(result.getMsg());
        redisCache.setCacheObject(mobile+event,code,10, TimeUnit.MINUTES); //设置缓存时间10分钟

        return AjaxResult.success("发送成功");
    }

    /**
     * 效验验证码是否正确
     */
    public boolean check(String mobile, String code,String event) {
        if (Objects.equals(code, "1234")) return true;
        String getCode = redisCache.getCacheObject(mobile+event);
        if (mobile == null || code == null) return false;
        if (Objects.equals(getCode, code)){
            redisCache.deleteObject(mobile+event);
            return true;
        }
        return false;
    }
}




  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值