wlzboy
5 天以前 fe33646ee6e2d1e57f2b51812e94983a0e9efb04
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.ruoyi.common.utils;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 短信发送工具类
 * 参考PHP版本的SmsUtils实现
 * 
 * @author ruoyi
 * @date 2025-12-07
 */
public class SmsUtils {
 
    private static final Logger log = LoggerFactory.getLogger(SmsUtils.class);
 
 
    /**
     * 发送 POST 请求
     *
     * @param url  服务器地址
     * @param data 请求参数
     * @return 响应内容
     * @throws IOException
     */
    public static String post(String url, String data) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        try (OutputStream os = connection.getOutputStream()) {
            os.write(data.getBytes(StandardCharsets.UTF_8));
            os.flush();
        }
        StringBuilder response = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            while ((line = br.readLine()) != null) {
                response.append(line);
            }
        }
        return response.toString();
    }
 
    /**
     * 计算 MD5 值
     *
     * @param str 字符串
     * @return MD5 值
     * @throws NoSuchAlgorithmException
     */
    public static String md5(String str) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(str.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    }
 
    /**
     * 获取当前时间戳(毫秒)
     * 
     * @return 时间戳字符串
     */
    public static Long getTimestamp() {
        return  System.currentTimeMillis();
    }
 
    /**
     * 生成签名
     * 签名算法:MD5(userName + password + timestamp)
     * 
     * @param userName 用户名
     * @param password 密码
     * @param timestamp 时间戳
     * @return 签名字符串
     */
    public static String generateSign(String userName, String password, Long timestamp) throws NoSuchAlgorithmException {
        // 时间戳
 
 
        // 计算 sign 参数
        String sign = md5(userName + timestamp + md5(password));
        return sign;
    }
 
    /**
     * 发送HTTP POST请求
     * 
     * @param url 请求地址
     * @param data 请求数据
     * @return 响应结果
     */
    public static JSONObject sendPost(String url, Map<String, Object> data) {
        try {
            String jsonData = JSON.toJSONString(data);
            log.debug("短信API请求,URL:{},数据:{}", url, jsonData);
            
            String response = post(url,jsonData);;//HttpUtils.sendPost(url, jsonData, "application/json");
            log.debug("短信API响应:{}", response);
            
            if (StringUtils.isNotEmpty(response)) {
                return JSON.parseObject(response);
            }
        } catch (Exception e) {
            log.error("短信API请求失败,URL:{}", url, e);
        }
        return null;
    }
 
    /**
     * 群发短信
     * 
     * @param address 短信服务地址
     * @param userName 账号
     * @param password 密码
     * @param phoneList 手机号列表,多个用逗号分隔
     * @param content 短信内容
     * @return 发送结果
     */
    public static JSONObject sendSmsMass(String address, String userName, String password,
                                          String phoneList, String content) throws NoSuchAlgorithmException{
        Long timestamp = getTimestamp();
        String sign = generateSign(userName, password, timestamp);
        String url = "http://" + address + "/sms/api/sendMessageMass";
 
        Map<String, Object> data = new HashMap<>();
        data.put("userName", userName);
        data.put("content", content);
        data.put("phoneList", phoneList);
        data.put("timestamp", timestamp);
        data.put("sign", sign);
        data.put("sendTime", "");
        data.put("extCode", "");
        data.put("callData", "");
 
        return sendPost(url, data);
    }
 
    /**
     * 单条/点对点短信发送
     * 
     * @param address 短信服务地址
     * @param userName 账号
     * @param password 密码
     * @param messageList 消息列表 [{phone: "手机号", content: "内容"}, ...]
     * @return 发送结果
     */
    public static JSONObject sendSmsOne(String address, String userName, String password,
                                         List<Map<String, String>> messageList) throws NoSuchAlgorithmException {
        Long timestamp = getTimestamp();
        String sign = generateSign(userName, password, timestamp);
        String url = "http://" + address + "/sms/api/sendMessageOne";
 
        Map<String, Object> data = new HashMap<>();
        data.put("userName", userName);
        data.put("timestamp", timestamp);
        data.put("sign", sign);
        data.put("messageList", messageList);
 
        return sendPost(url, data);
    }
 
    /**
     * 发送单条短信(简化方法)
     * 
     * @param address 短信服务地址
     * @param userName 账号
     * @param password 密码
     * @param phone 手机号
     * @param content 短信内容
     * @return 发送结果
     */
    public static JSONObject sendSms(String address, String userName, String password,
                                      String phone, String content)  throws NoSuchAlgorithmException{
        List<Map<String, String>> messageList = new ArrayList<>();
        messageList.add(new HashMap<String, String>() {{
            put("phone", phone);
            put("content", content);
        }});
        return sendSmsOne(address, userName, password, messageList);
    }
 
    /**
     * 查询账户余额
     * 
     * @param address 短信服务地址
     * @param userName 账号
     * @param password 密码
     * @return 余额信息
     */
    public static JSONObject getBalance(String address, String userName, String password) throws NoSuchAlgorithmException {
        Long timestamp = getTimestamp();
        String sign = generateSign(userName, password, timestamp);
        String url = "http://" + address + "/sms/api/getBalance";
 
        Map<String, Object> data = new HashMap<>();
        data.put("userName", userName);
        data.put("timestamp", timestamp);
        data.put("sign", sign);
 
        return sendPost(url, data);
    }
 
    /**
     * 获取发送报告
     * 
     * @param address 短信服务地址
     * @param userName 账号
     * @param password 密码
     * @return 发送报告
     */
    public static JSONObject getReport(String address, String userName, String password) throws NoSuchAlgorithmException {
        Long timestamp = getTimestamp();
        String sign = generateSign(userName, password, timestamp);
        String url = "http://" + address + "/sms/api/getReport";
 
        Map<String, Object> data = new HashMap<>();
        data.put("userName", userName);
        data.put("timestamp", timestamp);
        data.put("sign", sign);
 
        return sendPost(url, data);
    }
}