榛子云——短信(工具)
一、介绍
二、添加依赖
2.1 pom.xml文件(SpringBoot项目)
<!-- 榛子云短信的依赖 -->
<dependency>
<groupId>com.zhenzikj</groupId>
<artifactId>zhenzisms</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
2.2 全局配置
#榛子云短信配置
zhenzi:
apiUrl: https://sms_developer.zhenzikj.com #注意区别个人用户和企业用户
appId: 填写自己的APPID
appSecret: 填写自己的APPSecret
templateId: 填写自己的模板id #模板id
invalidTimer: 1 #失效时间1分钟(提示)
2.3 自定义读取短信参数的工具类
/**
* @Description
* @Author cb
* @Date 2021-11-25 10:06
**/
@Data
@Component
@ConfigurationProperties(prefix = "zhenzi")
public class ZhenZiYunSMSUtils {
private String apiUrl; //apiUrl
private String appId; //应用id
private String appSecret; //应用secret
private String templateId; //模板id
private String invalidTimer; //失效时间
/**
* 发送短信验证码
*
* @param telNumber 接收者手机号码
* @param validateCode 随机验证码(四位或六位)
* @return
*/
public void sendSMS(String telNumber, String validateCode) throws Exception {
//榛子云短信 客户端
//请求地址,个人开发者使用https://sms_developer.zhenzikj.com,企业开发者使用https://sms.zhenzikj.com
ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
//存放请求参数的map集合
Map<String, Object> params = new HashMap<String, Object>();
//接收者手机号码
params.put("number", telNumber);
//短信模板ID
params.put("templateId", templateId);
//短信模板参数
String[] templateParams = new String[2];
templateParams[0] = validateCode;
templateParams[1] = invalidTimer;
params.put("templateParams", templateParams);
/**
* 1.send方法用于单条发送短信,所有请求参数需要封装到Map中;
* 2.返回结果为json串:{ "code":0,"data":"发送成功"}
* 3.备注:(code: 发送状态,0为成功。非0为发送失败,可从data中查看错误信息)
*/
String result = client.send(params);
}
}
2.4 随机验证码生成(拓展工具类)
/**
* @author cb
* @Description 随机生成验证码工具类
* @Date 2021/12/26 10:59
*/
public class RandomUtil {
/**
* 随机生成验证码
* @param length 长度为4位或者6位
* @return
*/
public static String generateValidateCode(int length){
Integer code =null;
if(length == 4){
code = new Random().nextInt(9999);//生成随机数,最大为9999
if(code < 1000){
code = code + 1000;//保证随机数为4位数字
}
}else if(length == 6){
code = new Random().nextInt(999999);//生成随机数,最大为999999
if(code < 100000){
code = code + 100000;//保证随机数为6位数字
}
}else{
throw new RuntimeException("只能生成4位或6位数字验证码");
}
return String.valueOf(code);
}
}
2.5 正则表达式验证数据(拓展工具类)
三、案例演示
/**
* @Description
* @Author cb
* @Date 2022-01-05 23:51
**/
@RestController
@RequestMapping("/api/sms")
@Api(tags = "短信管理的接口")
public class SmsApiController {
@Autowired
private RedisUtils redisUtils;
@Autowired
private SmsService smsService;
//发送手机验证码
@ApiOperation("发送手机验证码")
@GetMapping("send/{phone}")
public Result sendCode(@PathVariable String phone) {
//判断手机号是否为空
if (StringUtils.isEmpty(phone)) {
return Result.setResult(ResponseEnum.MOBILE_NULL_ERROR);
}
boolean flag = RegexValidateUtil.checkphone(phone);
// if (!flag) {
// return Result.setResult(ResponseEnum.MOBILE_ERROR);
// }
//生成验证码
String code = RandomUtil.generateValidateCode(4);
//调用service的方法来发送验证码短信
boolean b = smsService.sendShortMessage(phone, code);
if (b) {
//将验证码存入redis
String key = "ymjr:sms:code:" + phone;
redisUtils.set(key, code, 120);
return Result.ok().message("短信发送成功");
}
return Result.error().message("短信发送失败");
}
}