spring-boot 若依对接腾讯云短信
在common模块下pop.xml引入短信库
// 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;
}
}