添加发送短信

This commit is contained in:
韩鹏辉
2024-07-11 11:35:35 +08:00
parent 6a8ea3416f
commit 21fb470052
6 changed files with 225 additions and 1 deletions

View File

@@ -0,0 +1,86 @@
package cn.ysk.cashier.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
/**
* @author lyf
*/
public class StringUtil {
/**
* 生成指定长度的随机数
* @param length
* @return
*/
public static String genRandomNum(int length) {
int maxNum = 36;
int i;
int count = 0;
char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
StringBuffer pwd = new StringBuffer("");
Random r = new Random();
while (count < length) {
i = Math.abs(r.nextInt(maxNum));
pwd.append(str[i]);
count++;
}
return pwd.toString();
}
/**
* 生成订单号
*
* @return
*/
public static synchronized String getBillno() {
StringBuilder billno = new StringBuilder();
// 日期(格式:20080524)
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
billno.append(format.format(new Date()));
return billno.toString();
}
public static JSONArray stringChangeList(String listString){
// 使用Fastjson将JSON字符串转换为JSONArray对象
return JSON.parseArray(listString);
}
public static String random(int length) {
Random random = new Random();
String result = "";
for (int i = 0; i < length; i++) {
result += random.nextInt(10);
}
return result;
}
private static final String CHINESE_CHARS = "你我他她它们这里那里多少是否好坏快慢上下左右前后高低大小长短方圆胖瘦黑白红绿蓝黄紫粉红桔红橙黄棕灰褐";
// 生成随机中文昵称
public static String generateRandomNickname(int length) {
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
int index = random.nextInt(CHINESE_CHARS.length());
char randomChar = CHINESE_CHARS.charAt(index);
sb.append(randomChar);
}
return desensitizeNickname(sb.toString());
}
// 对昵称进行脱敏处理
public static String desensitizeNickname(String nickname) {
if (nickname == null || nickname.length() != 5) {
return nickname;
}
return nickname.charAt(0) + "***" + nickname.charAt(4);
}
}

View File

@@ -0,0 +1,76 @@
package cn.ysk.cashier.utils;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
/**
* 获取验证码相关工具
* @author 12847
*/
@Component
public class ValidateCodeUtil {
/**
* 阿里云key
*/
@Value("${aliyun.keyid}")
private String ACCESSKEYID;
/**
* 阿里云secret
*/
@Value("${aliyun.keysecret}")
private String ACCESSKEYSECRET;
/**
* 十小时
*/
protected static final long MILLIS_MINUTE = 10 *60 * 60 *1000;
@Autowired
private HttpServletRequest request;
/**
* 获取验证码(阿里云)
*/
public SendSmsResponse requestValidateCodeAli(String phone, String checkCode,String templateCode) throws Exception {
Client client = null;
try {
client = createClient();
} catch (Exception e) {
e.printStackTrace();
}
// 1.发送短信
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setSignName("银收客")
.setTemplateCode(templateCode)
.setTemplateParam("{\"code\":" +"'"+checkCode +"'"+"}")
.setPhoneNumbers(phone);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
assert client != null;
return client.sendSmsWithOptions(sendSmsRequest, runtime);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/**
* 发送短信(阿里云)
*
* @return
* @throws Exception
*/
public Client createClient() throws Exception {
Config config = new Config();
config.accessKeyId = ACCESSKEYID;
config.accessKeySecret = ACCESSKEYSECRET;
return new com.aliyun.dysmsapi20170525.Client(config);
}
}