zyy3
2025-11-01 5de6aebd2807f320414fbbf5c684cead3cec2208
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
package com.yami.trading.common.manager.sms;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 短信发送工具类
 */
public class SmsSendUtil {
 
    // API请求地址
    private static final String SMS_API_URL = "http://190.92.241.43:9090/sms/batch/v2";
 
    private static final String SMS_API_APPKEY = "McLWyT";
 
    private static final String SMS_API_APPSECRET = "WHuzLP";
 
    private static final String SMS_API_APPCODE = "1000";
 
 
    /**
     * 发送短信
     *
     * @param phone     手机号码,多个用逗号分隔(必填,最多1000个)
     * @param msg       短信内容(必填,不超过1000字)
     * @param uid       唯一标识(选填)
     * @param extend    扩展号(选填,数字,总长度不超过20位)
     * @return 短信发送响应结果
     * @throws Exception 异常信息
     */
    public static SmsResponse sendSms(String phone, String msg, String uid, String extend) throws Exception {
        // 参数验证
        //validateParams(SMS_API_APPKEY, SMS_API_APPCODE, SMS_API_APPSECRET, phone, msg, extend);
 
        // 创建HTTP客户端
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 构建请求参数
            URIBuilder uriBuilder = new URIBuilder(SMS_API_URL);
            uriBuilder.setParameter("appkey", SMS_API_APPKEY);
            uriBuilder.setParameter("appcode", SMS_API_APPCODE);
            uriBuilder.setParameter("appsecret", SMS_API_APPSECRET);
            uriBuilder.setParameter("phone", phone);
            uriBuilder.setParameter("msg", msg);
 
            // 可选参数
            if (uid != null && !uid.isEmpty()) {
                uriBuilder.setParameter("uid", uid);
            }
            if (extend != null && !extend.isEmpty()) {
                uriBuilder.setParameter("extend", extend);
            }
 
            URI uri = uriBuilder.build();
            HttpGet httpGet = new HttpGet(uri);
 
            // 发送请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                SmsResponse result = parseResponse(responseBody);
                System.out.println("发送结果:" + result);
                // 判断是否发送成功
                if ("00000".equals(result.getCode())) {
                    System.out.println("提交成功,uid:" + result.getUid());
                } else {
                    System.out.println("提交失败:" + result.getDesc());
                }
                return result;
            }
        }
    }
 
 
 
    /**
     * 参数验证
     */
    private static void validateParams(String appKey, String appCode, String appSecret,
                                       String phone, String msg, String extend) {
        // 验证必填参数
        if (appKey == null || appKey.trim().isEmpty()) {
            throw new IllegalArgumentException("appkey不能为空");
        }
        if (appCode == null || appCode.trim().isEmpty()) {
            throw new IllegalArgumentException("appcode不能为空");
        }
        if (appSecret == null || appSecret.trim().isEmpty()) {
            throw new IllegalArgumentException("appsecret不能为空");
        }
        if (phone == null || phone.trim().isEmpty()) {
            throw new IllegalArgumentException("手机号码不能为空");
        }
 
        // 验证手机号码数量
        String[] phones = phone.split(",");
        if (phones.length > 1000) {
            throw new IllegalArgumentException("一次最多不能超过1000个手机号码");
        }
 
        // 验证短信内容
        if (msg == null || msg.trim().isEmpty()) {
            throw new IllegalArgumentException("短信内容不能为空");
        }
        if (msg.length() > 1000) {
            throw new IllegalArgumentException("短信内容长度不能超过1000个字");
        }
 
        // 验证扩展号
        if (extend != null && !extend.isEmpty()) {
            if (!extend.matches("\\d+")) {
                throw new IllegalArgumentException("扩展号必须为数字");
            }
            // 假设通道主叫号固定为10位,实际使用时请根据供应商提供的信息修改
            if (extend.length() > 10) {
                throw new IllegalArgumentException("扩展号长度超过限制(总长度不能超过20位)");
            }
        }
    }
 
    /**
     * 解析响应结果
     */
    private static SmsResponse parseResponse(String responseBody) {
        JSONObject jsonObject = JSON.parseObject(responseBody);
        SmsResponse smsResponse = new SmsResponse();
        smsResponse.setCode(jsonObject.getString("code"));
        smsResponse.setDesc(jsonObject.getString("desc"));
        smsResponse.setUid(jsonObject.getString("uid"));
 
        // 解析result数组
        JSONArray resultArray = jsonObject.getJSONArray("result");
        if (resultArray != null && !resultArray.isEmpty()) {
            List<SmsResult> resultList = new ArrayList<>();
            for (int i = 0; i < resultArray.size(); i++) {
                JSONObject item = resultArray.getJSONObject(i);
                SmsResult smsResult = new SmsResult();
                smsResult.setStatus(item.getString("status"));
                smsResult.setPhone(item.getString("phone"));
                smsResult.setDesc(item.getString("desc"));
                resultList.add(smsResult);
            }
            smsResponse.setResult(resultList);
        }
 
        return smsResponse;
    }
 
    /**
     * 短信响应实体类
     */
    public static class SmsResponse {
        private String code;
        private String desc;
        private String uid;
        private List<SmsResult> result;
 
        // getter和setter
        public String getCode() { return code; }
        public void setCode(String code) { this.code = code; }
        public String getDesc() { return desc; }
        public void setDesc(String desc) { this.desc = desc; }
        public String getUid() { return uid; }
        public void setUid(String uid) { this.uid = uid; }
        public List<SmsResult> getResult() { return result; }
        public void setResult(List<SmsResult> result) { this.result = result; }
 
        @Override
        public String toString() {
            return "SmsResponse{" +
                    "code='" + code + '\'' +
                    ", desc='" + desc + '\'' +
                    ", uid='" + uid + '\'' +
                    ", result=" + result +
                    '}';
        }
    }
 
    /**
     * 短信结果详情实体类
     */
    public static class SmsResult {
        private String status;
        private String phone;
        private String desc;
 
        // getter和setter
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
        public String getPhone() { return phone; }
        public void setPhone(String phone) { this.phone = phone; }
        public String getDesc() { return desc; }
        public void setDesc(String desc) { this.desc = desc; }
 
        @Override
        public String toString() {
            return "SmsResult{" +
                    "status='" + status + '\'' +
                    ", phone='" + phone + '\'' +
                    ", desc='" + desc + '\'' +
                    '}';
        }
    }
 
 
    public static void main(String[] args) {
        try {
            SmsResponse response = SmsSendUtil.sendSms("8617769309156", "4524", null, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}