com.hn.sms.SmsUtil Maven / Gradle / Ivy
package com.hn.sms;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.hn.sms.chuanglan.ChuangLanSms;
import com.hn.sms.chuanglan.domain.ChuangLanParam;
import com.hn.sms.chuangrui.ChuangRuiSms;
import com.hn.sms.chuangrui.domain.ChuangRuiParam;
import com.hn.sms.juhe.JuHeSms;
import com.hn.sms.juhe.domain.JuHeParam;
import java.util.*;
/**
* 描述:
* 短信工具类
*
* @author fei
* 2019-03-18 18:05
*/
public final class SmsUtil {
private static final Log log = LogFactory.get();
private SmsUtil() {
}
private static Map smsMap = new HashMap<>();
/**
* 取指定平台 默认场景 发送短信
*
* @param msgPlatform {@link MsgPlatform}
* @param phone 手机号
* @param code 短信验证码 无验证码传null
* @return boolean
*/
public static boolean send(MsgPlatform msgPlatform, String phone, String code) {
return send(msgPlatform, "", phone, code);
}
/**
* 取指定平台 指定场景 发送短信
*
* @param msgPlatform {@link MsgPlatform}
* @param scene 短信场景
* @param phone 手机号
* @param code 短信验证码 无验证码传null
* @return boolean
*/
public static boolean send(MsgPlatform msgPlatform, String scene, String phone, String code) {
String key = msgPlatform.name();
if (StrUtil.isNotBlank(scene)) {
key = key.concat(":").concat(scene);
}
Sms sms = smsMap.get(key);
if (sms == null) {
log.debug("sms is null after new sms");
sms = msgPlatform.create(scene);
smsMap.put(key, sms);
}
return sms.send(phone, code);
}
/**
* 聚合
*
* @return Sms
*/
@Deprecated
public static Sms juHe() {
return new JuHeSms("");
}
/**
* 聚合
*
* @param scene 短信场景
* @return Sms
*/
@Deprecated
public static Sms juHe(String scene) {
return new JuHeSms(scene);
}
/**
* 聚合
*
* @param param {@link JuHeParam}
* @return Sms
*/
@Deprecated
public static Sms juHe(JuHeParam param) {
return new JuHeSms(param);
}
/**
* 创瑞
*
* @return Sms
*/
@Deprecated
public static Sms chuangRui() {
return new ChuangRuiSms("");
}
/**
* 创瑞
*
* @param scene 短信场景
* @return Sms
*/
@Deprecated
public static Sms chuangRui(String scene) {
return new ChuangRuiSms(scene);
}
/**
* 创瑞
*
* @param param {@link ChuangRuiParam}
* @return Sms
*/
@Deprecated
public static Sms chuangRui(ChuangRuiParam param) {
return new ChuangRuiSms(param);
}
/**
* 创蓝
*
* @return Sms
*/
@Deprecated
public static Sms chuangLan() {
return new ChuangLanSms("");
}
/**
* 创蓝
*
* @param scene 短信场景
* @return Sms
*/
@Deprecated
public static Sms chuangLan(String scene) {
return new ChuangLanSms(scene);
}
/**
* 创蓝
*
* @param param {@link ChuangLanParam}
* @return Sms
*/
@Deprecated
public static Sms chuangLan(ChuangLanParam param) {
return new ChuangLanSms(param);
}
/**
* 验证码code生成
* 默认生成6位数字
*
* @return code 验证码
*/
public static String code() {
return RandomUtil.randomNumbers(6);
}
/**
* 验证码code生成
*
* @param length 验证码长度
* @return code 验证码
*/
public static String code(int length) {
return RandomUtil.randomNumbers(length);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy