package cn.gistack.sm.intelligentCall.util;
|
|
import cn.gistack.sm.intelligentCall.constant.CallConstant;
|
import com.alibaba.fastjson.JSON;
|
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
public class CallUtil {
|
/**
|
* 计算token
|
* @param timestamp
|
* @param transId
|
* @param map
|
* @return
|
*/
|
public static String getToken(String timestamp,String transId,Map<String,Object> map){
|
// 转 jsonstring
|
String body = JSON.toJSONString(map);
|
// 拼接
|
StringBuilder builder = new StringBuilder("appId");
|
builder.append(CallConstant.AppKey)
|
.append("timestamp").append(timestamp)
|
.append("transId").append(transId)
|
.append(CallConstant.AppSecret);
|
// .append(body).append(CallConstant.AppSecret);
|
String formatString = builder.toString();
|
// md5 小写,md5 生成token
|
String token = encryption(formatString);
|
// 返回
|
return token;
|
}
|
|
/**
|
* 计算token1
|
* @param url
|
* @return
|
*/
|
public static String getToken1(String url){
|
// 计算token
|
Date date = new Date();
|
String dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
|
String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(date);
|
String transId = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(date) + getRandom();
|
// 请求参数拼接
|
String body ="{\"callingumbers\":[\"01086394068\"], \"calleeData\":[{\"被叫号码\":\"15170720695\"\"bizId\":\"1\"}]"+
|
",\"scenesId\":2268,\"taskSchedulelime\":\"2023-04-23 16:30:00\",\"taskNamel\":\"thirdCreatelask7\"}";
|
// 拼接
|
StringBuilder builder = new StringBuilder("appId");
|
builder.append(CallConstant.AppKey)
|
.append("timestamp").append(timestamp)
|
.append("transId").append(transId)
|
.append(body).append(CallConstant.AppSecret);
|
|
String formatString = builder.toString();
|
// md5 小写,md5 生成token
|
String token = encryption(formatString);
|
// 返回
|
return token;
|
}
|
|
/**
|
* 随机数生成
|
* @return
|
*/
|
public static String getRandom(){
|
Random random = new Random();
|
//把随机生成的数字转成字符串
|
String str = String.valueOf(random.nextInt(9));
|
for (int i = 0; i < 5; i++) {
|
str += random.nextInt(9);
|
}
|
return str;
|
}
|
|
/**
|
* md5加密 32位 小写
|
* @param plainText
|
* @return
|
*/
|
public static String encryption(String plainText) {
|
String re_md5 = new String();
|
try {
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
md.update(plainText.getBytes());
|
byte b[] = md.digest();
|
int i;
|
StringBuffer buf = new StringBuffer("");
|
for (int offset = 0; offset < b.length; offset++) {
|
i = b[offset];
|
if (i < 0) {
|
i += 256;
|
}
|
if (i < 16) {
|
buf.append("0");
|
}
|
buf.append(Integer.toHexString(i));
|
}
|
re_md5 = buf.toString();
|
} catch (NoSuchAlgorithmException e) {
|
e.printStackTrace();
|
}
|
return re_md5;
|
}
|
}
|