zhongrj
2023-04-23 0d1abeb26fa722bddd42e09dd6237f3652ddc8ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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;
    }
}