package com.kidgrow.sms.util;
import com.aliyuncs.CommonResponse;
import com.google.gson.Gson;
import com.kidgrow.sms.exception.SmsException;
import com.kidgrow.sms.template.BatchSmsTemplate;
import com.kidgrow.sms.template.SmsTemplate;
import org.apache.commons.lang3.StringUtils;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
/**
* 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020
*
* @Description: 阿里云短信服务工具类
* @Project:
* @CreateDate: Created in 2020/2/17 08:53
* @Author: liuke
*/
public class AliyunSmsUtils {
private static final String SUCCESS_CODE = "OK";
/**
* 宽松校验即可.
*/
private static final String PHONE_NUMBER_REGEX = "\\d{5,}";
/**
* 生成随机验证码.
*
* @return 随机数
*/
public static int randomCode() {
return 100_000 + ThreadLocalRandom.current().nextInt(1_000_000 - 100_000);
}
/**
* Map 转 json 字符串的简单实现.
*
* @param map the map
*
* @return the json string
*/
public static String toJsonStr(final Map map) {
if (null == map || map.isEmpty()) {
return null;
}
final StringBuilder sb = new StringBuilder();
sb.append('{');
for (final Map.Entry entry : map.entrySet()) {
sb.append('"')
.append(entry.getKey().replace("\"", "\\\""))
.append('"')
.append(':')
.append('"')
.append(entry.getValue().replace("\"", "\\\""))
.append('"')
.append(',');
}
sb.deleteCharAt(sb.length() - 1);
sb.append('}');
return sb.toString();
}
/**
* 校验 SmsTemplate.
*
* @param template the SmsTemplate
*/
public static void checkSmsTemplate(final SmsTemplate template) {
checkNotEmpty(template.getSignName(), "SmsTemplate signName must be not empty");
checkNotEmpty(template.getTemplateCode(), "SmsTemplate templateCode must be not empty");
checkNotEmpty(template.getPhoneNumbers(), "SmsTemplate phoneNumbers must be not empty");
}
/**
* 校验 BatchSmsTemplate.
*
* @param template the BatchSmsTemplate
*/
public static void checkBatchSmsTemplate(final BatchSmsTemplate template) {
checkNotEmpty(template.getSignNames(), "BatchSmsTemplate signNames must be not empty");
checkNotEmpty(template.getPhoneNumbers(), "BatchSmsTemplate phoneNumbers must be not empty");
checkNotEmpty(template.getTemplateCode(), "BatchSmsTemplate templateCode must be not empty");
checkNotEmpty(template.getTemplateParams(), "BatchSmsTemplate templateParams must be not empty");
if (template.getSignNames().size() != template.getPhoneNumbers().size()
&& template.getPhoneNumbers().size() != template.getTemplateParams().size()) {
throw new IllegalArgumentException("BatchSmsTemplate phoneNumbers, signNames, templateParams size must be the same");
}
}
/**
* 校验 SendSmsResponse 状态.
*
* @param response the SendSmsResponse
*/
public static void checkSmsResponse(final CommonResponse response) {
if (null == response) {
throw new SmsException("Response is null");
}
final Gson gson = new Gson();
final Map json = gson.fromJson(response.getData(), Map.class);
if (!SUCCESS_CODE.equalsIgnoreCase(json.get("Code"))) {
//throw new SmsException("Http status: " + response.getHttpStatus() + ", response: " + response.getData());
throw new SmsException(response.getData());
}
}
/**
* 校验手机号码(中国).
*
* @param phoneNumber the phone number
*/
public static void checkPhoneNumber(final String phoneNumber) {
if (null == phoneNumber || !phoneNumber.matches(PHONE_NUMBER_REGEX)) {
throw new IllegalArgumentException("Invalid phone number");
}
}
/**
2018年3月已知
中国电信号段
133,149,153,173,177,180,181,189,199
中国联通号段
130,131,132,145,155,156,166,175,176,185,186
中国移动号段
134(0-8),135,136,137,138,139,147,150,151,152,157,158,159,178,182,183,184,187,188,198
其他号段
14号段以前为上网卡专属号段,如中国联通的是145,中国移动的是147等等。
虚拟运营商
电信:1700,1701,1702
移动:1703,1705,1706
联通:1704,1707,1708,1709,171
卫星通信:148(移动) 1349
*/
public static boolean isMobile(String str) {
boolean b = false;
String s2="^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$";// 验证手机号
if(StringUtils.isNotBlank(str)){
return str.matches(s2);
}
return b;
}
/**
* 校验字符串不为空.
*
* @param str the str
* @param message the message
*/
public static void checkNotEmpty(final String str, final String message) {
if (null == str || str.isEmpty()) {
throw new IllegalArgumentException(message);
}
}
/**
* 校验集合不为空.
*
* @param coll the Collection
* @param message the message
*/
public static void checkNotEmpty(final Collection coll, final String message) {
if (null == coll || coll.isEmpty()) {
throw new IllegalArgumentException(message);
}
}
}