腾讯云发送短信验证码

//实体
@Data
public class SmsDto extends Req {

    /**
     * 手机号码
     */
    private String phone;

    /**
     *默认为86/+86
     */
    private String country_code;

    @Override
    public void validate() {
        Assert.isNotNull(phone,"手机号码");
        Assert.isNotNull(country_code,"默认为86/+86");
    }
}


//接口
@Override
    public AjaxResult sendPassword(SmsDto smsDto) {
        try {
            String code = VERIFICATION_CODE + smsDto.getPhone();
            Object o = redisTemplate.opsForValue().get(code);
            //判断验证码是否过期
            if (ObjectUtil.isNotEmpty(o)) {
                return AjaxResult.error("请勿重复发送验证码!");
            } else {
                String codeNew = OrderNumGeneration.generateNum();
                String[] params = {codeNew};
                String appid = env.getProperty("smsConstant.appid");
                String appkey = env.getProperty("smsConstant.appkey");
                SmsSingleSenderResult singleSenderResult = TencentSmsUtils.smsSingleSender(Convert.toInt(appid),appkey, smsDto.getCountry_code(), smsDto.getPhone(), 472173, params, "专卖店", "", "");
                if (singleSenderResult.result == 0) {
                    log.debug("发送短信登录密码,短信密码:{}", codeNew);
                    redisTemplate.opsForValue().set(code, codeNew, 5L, TimeUnit.MINUTES);
                    return AjaxResult.success("验证码发送成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success("验证码发送失败");
        }
        return null;
    }





package cn.com.soole.report.common.utils;

import com.github.qcloudsms.*;

/**
 * 腾讯云发送短信工具类
 * author fusen
 */
public class TencentSmsUtils {

    /**
     * 指定模板ID单发短信
     * @param appid 短信应用SDK AppID
     * @param appkey 短信应用SDK AppKey
     * @param nationCode 国家码,如 86 为中国
     * @param phoneNumber 不带国家码的手机号
     * @param templateId 发送短信的模板ID
     * @param params 参数列表
     * @param sign 签名参数未提供或者为空时,会使用默认签名发送短信
     * @param extend 扩展码,可填空
     * @param ext 服务端原样返回的参数,可填空
     * @return SmsSingleSenderResult
     */
    public static SmsSingleSenderResult smsSingleSender(int appid, String appkey, String nationCode, String phoneNumber,
                                                        int templateId, String[] params, String sign, String extend, String ext){
        SmsSingleSender ssender =null;
        SmsSingleSenderResult result = null;
        try {
            ssender = new SmsSingleSender(appid, appkey);
            result = ssender.sendWithParam(nationCode,phoneNumber,templateId,params,sign,extend,ext);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 指定模板ID群发短信
     * @param appid 短信应用SDK AppID
     * @param appkey 短信应用SDK AppKey
     * @param nationCode 国家码,如 86 为中国
     * @param phoneNumbers 不带国家码的手机号
     * @param templateId 发送短信的模板ID
     * @param params 参数列表
     * @param sign 签名参数未提供或者为空时,会使用默认签名发送短信
     * @param extend 扩展码,可填空
     * @param ext 服务端原样返回的参数,可填空
     * @return SmsMultiSenderResult
     */
    public static SmsMultiSenderResult smsMultiSender(int appid, String appkey, String nationCode, String[] phoneNumbers,
                                                      int templateId, String[] params, String sign, String extend, String ext){
        SmsMultiSender msender = null;
        SmsMultiSenderResult result = null;
        try {
            msender = new SmsMultiSender(appid, appkey);
            result =  msender.sendWithParam(nationCode, phoneNumbers,
                    templateId, params, sign, extend, ext);  // 签名参数未提供或者为空时,会使用默认签名发送短信
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 发送语音验证码
     * @param appid 短信应用SDK AppID
     * @param appkey 短信应用SDK AppKey
     * @param nationCode 国家码,如 86 为中国
     * @param phoneNumber 不带国家码的手机号
     * @param msg
     * @param playtimes
     * @param ext
     * @return SmsVoiceVerifyCodeSenderResult
     */
    public static SmsVoiceVerifyCodeSenderResult smsVoiceVerifyCodeSender(int appid, String appkey, String nationCode, String phoneNumber, String msg,
                                                                          int playtimes, String ext){
        SmsVoiceVerifyCodeSender vvcsender =null;
        SmsVoiceVerifyCodeSenderResult result = null;
        try {
            vvcsender = new SmsVoiceVerifyCodeSender(appid,appkey);
            result = vvcsender.send(nationCode, phoneNumber,
                    msg,playtimes, ext);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     *  发送语音通知
     * @param appid 短信应用SDK AppID
     * @param appkey 短信应用SDK AppKey
     * @param nationCode 国家码,如 86 为中国
     * @param phoneNumber 不带国家码的手机号
     * @param prompttype 类型,目前固定值为2
     * @param playtimes 播放次数
     * @param msg 语音通知消息内容
     * @param ext  "扩展字段,原样返回"
     * @return SmsVoicePromptSenderResult
     */
    public static SmsVoicePromptSenderResult smsVoicePromptSender(int appid, String appkey, String nationCode, String phoneNumber, int prompttype,
                                                                  int playtimes, String msg, String ext){
        SmsVoicePromptSender vpsender =null;
        SmsVoicePromptSenderResult result = null;
        try {
            vpsender = new SmsVoicePromptSender(appid, appkey);
            result = vpsender.send(nationCode, phoneNumber,
                    prompttype, playtimes,msg,ext);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 拉取短信回执
     *
     * 说明:短信拉取功能需要联系腾讯云短信技术支持(QQ:3012203387)开通权限
     *
     * @param appid 短信应用SDK AppID
     * @param appkey 短信应用SDK AppKey
     * @param maxNum 单次拉取最大量
     * @return SmsStatusPullCallbackResult
     */
    public static SmsStatusPullCallbackResult smsStatusPullCallback(int appid, String appkey, int maxNum){
        SmsStatusPuller spuller = null;
        SmsStatusPullCallbackResult callbackResult = null;
        try {
            spuller = new SmsStatusPuller(appid, appkey);
            // 拉取短信回执
            callbackResult = spuller.pullCallback(maxNum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return callbackResult;
    }

    /**
     * 拉取回复
     *
     * 说明:短信拉取功能需要联系腾讯云短信技术支持(QQ:3012203387)开通权限
     *
     * @param appid 短信应用SDK AppID
     * @param appkey 短信应用SDK AppKey
     * @param maxNum 单次拉取最大量
     * @return SmsStatusPullReplyResult
     */
    public static SmsStatusPullReplyResult smsStatusPullReply(int appid, String appkey, int maxNum){
        SmsStatusPuller spuller = null;
        SmsStatusPullReplyResult replyResult = null;
        try {
            spuller = new SmsStatusPuller(appid, appkey);
            // 拉取回复
            replyResult = spuller.pullReply(maxNum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return replyResult;
    }

    /**
     * 拉取单个手机短信状态 (拉取短信回执)
     * @param appid 短信应用SDK AppID
     * @param appkey 短信应用SDK AppKey
     * @param nationCode 国家码,如 86 为中国
     * @param phoneNumber 不带国家码的手机号
     * @param beginTime 开始时间(unix timestamp)
     * @param endTime 结束时间(unix timestamp)
     * @param maxNum 单次拉取最大量
     * @return SmsStatusPullCallbackResult
     */
    public static SmsStatusPullCallbackResult smsStatusPullCallbackResult(int appid, String appkey, String nationCode, String phoneNumber, int beginTime, int endTime, int maxNum){
        SmsMobileStatusPuller mspuller = null;
        SmsStatusPullCallbackResult callbackResult = null;
        try {
            mspuller = new SmsMobileStatusPuller(appid, appkey);
            // 拉取短信回执
            callbackResult = mspuller.pullCallback(nationCode,
                    phoneNumber, beginTime, endTime, maxNum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return callbackResult;

    }

    /**
     * 拉取单个手机短信状态 (拉取回复)
     * @param appid 短信应用SDK AppID
     * @param appkey 短信应用SDK AppKey
     * @param nationCode 国家码,如 86 为中国
     * @param phoneNumber 不带国家码的手机号
     * @param beginTime 开始时间(unix timestamp)
     * @param endTime 结束时间(unix timestamp)
     * @param maxNum 单次拉取最大量
     * @return SmsStatusPullReplyResult
     */
    public static SmsStatusPullReplyResult smsStatusPullReply(int appid, String appkey, String nationCode, String phoneNumber, int beginTime, int endTime, int maxNum){
        SmsMobileStatusPuller mspuller = null;
        SmsStatusPullReplyResult replyResult = null;
        try {
            mspuller = new SmsMobileStatusPuller(appid, appkey);
            // 拉取回复
            replyResult = mspuller.pullReply(nationCode,
                    phoneNumber, beginTime, endTime, maxNum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return replyResult;

    }


}


//添加依赖
 <!--腾讯云短信sdk-->
        <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>${qcloud.sms.sdk.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-text</artifactId>
            <version>1.9</version>
        </dependency>




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值