Pu Zhibing
2024-12-06 9912ae9e80f9a6a3411e4a1474abf9f3a8335eaf
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
package com.stylefeng.guns.modular.system.util;
 
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.MD5Util;
import com.stylefeng.guns.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
 
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author zhibing.pu
 * @Date 2023/3/31 10:05
 */
public class SMSUtil {
 
    private static String username = "Ztrbkjhy";
 
    private static String password = "Zycx2023Rbkj";
 
    private static String signature = "【昭阳出行】";
 
 
    /**
     * 模板短信发送
     * @param tpId          模板id
     * @param records       模板变量
     *          "records":[
     *         {
     *             "mobile":"138****0000",
     *             "tpContent":{
     *                 "var1":"变量1",
     *                 "var2":"变量2"
     *             }
     *         },
     *         {
     *             "mobile":"138****0001",
     *             "tpContent":{
     *                 "var1":"变量2",
     *                 "var2":"变量2"
     *             }
     *         }
     *     ]
     */
    public static void sendSmsTp(Long tpId, JSONArray records){
        //地址
        String urls = "https://api.mix2.zthysms.com/v2/sendSmsTp";
        //请求入参
        JSONObject requestJson = new JSONObject();
        //账号
        requestJson.put("username", username);
        //tKey
        long tKey = System.currentTimeMillis() / 1000;
        requestJson.put("tKey", tKey);
        //明文密码
        requestJson.put("password", SecureUtil.md5(SecureUtil.md5(password) + tKey));
        //模板ID
        requestJson.put("tpId", tpId);
        //签名
        requestJson.put("signature", signature);
        //扩展号
        requestJson.put("ext", "");
        //自定义参数
        requestJson.put("extend", "");
        requestJson.put("records", records);
        String result = HttpRequest.post(urls)
                .timeout(60000)
                .body(requestJson.toJSONString(), MediaType.APPLICATION_JSON_UTF8_VALUE).execute().body();
        if(ToolUtil.isNotEmpty(result)){
            JSONObject jsonObject = JSON.parseObject(result);
            int code = jsonObject.getIntValue("code");
            if(200 != code){
                System.err.println(jsonObject.getString("msg"));
            }
        }else{
            System.err.println("短信发送异常");
        }
    }
 
}