SmsUtil.java
5.42 KB
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
package com.glsx.publicservice.util;
import java.text.MessageFormat;
import java.util.TreeMap;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.glsx.common.utils.HttpUtils;
import com.glsx.common.utils.PropertiesUtil;
import com.glsx.publicservice.constants.Constants;
/**
* 短信工具类
*/
@Component("smsUtil")
public class SmsUtil {
private static Logger logger = LoggerFactory.getLogger(SmsUtil.class);
/**
* 短信派单
*
* @param phone
* @param msgContent
*/
public void send(String phone, String msgContent,String source) {
TreeMap<String, String> treeMap = new TreeMap<String, String>();
treeMap.put("method", "glsx.message.sendmessage");
treeMap.put("format", "json");
treeMap.put("akey", "9c991f5abac812b7a2739beecde2fdc7");
treeMap.put("v", "1.0.0");
treeMap.put("channel", "1001");
treeMap.put("lock", "2AC16F3AB37C129E273A4979FD0A46B0");
// String source = "frms_sms_assign";
treeMap.put("source", source);
treeMap.put("type", "1");
treeMap.put("phone", phone);
treeMap.put("content", msgContent);
treeMap.put("subcode", "");
logger.info("Send Sms:" + treeMap.toString());
// String url = "http://192.168.3.206:8118/router";//开发
// String url = "http://qcdidicb.glsx.net:7060/router";//测试(貌似已经废弃了)
// String url = "http://qcapp.oss.glsx.net:7060/gop-message/router";//测试
String url = PropertiesUtil.getProperty(Constants.SMS_SERVER_URL_KEY);
logger.info(url);
HttpUtils httpUtils = new HttpUtils();
try {
String res = httpUtils.get(url, treeMap);
logger.info("Result:" + res);
if (StringUtils.isNotEmpty(res)) {
if (res.contains("<html>")) {
logger.error("短信服务不可用,返回html...");
}
if (res.contains("{")) {
JSONObject jsonObject = JSONObject.parseObject(res);
String code = jsonObject.getString("code");
if (!code.equals("0")) {
res = httpUtils.get(url, treeMap);
logger.info("Failure resend result:" + res);
}
}
} else {
logger.error("短信服务不可用,返回为空");
}
} catch (IllegalStateException ex) {
if (StringUtils.isNotEmpty(url)) {
logger.error("IllegalStateException", ex);
}
} catch (Exception ex) {
if (StringUtils.isNotEmpty(url)) {
logger.error("SendSmsException", ex);
}
try {
String res1 = httpUtils.get(url, treeMap);
logger.info("Exception resend result:" + res1);
} catch (Exception e) {
logger.error("ReSendSmsException", ex);
}
}
}
// /**
// * 短信派单(营销短信)
// *
// * @param phone
// * @param msgContent
// * @param source
// */
// public void send(String phone, String msgContent, String source) {
//
// TreeMap<String, String> treeMap = new TreeMap<String, String>();
// treeMap.put("method", "glsx.message.sendmessage");
// treeMap.put("format", "json");
// treeMap.put("akey", "9c991f5abac812b7a2739beecde2fdc7");
// treeMap.put("v", "1.0.0");
// treeMap.put("channel", "1001");
// treeMap.put("lock", "2AC16F3AB37C129E273A4979FD0A46B0");
//
// treeMap.put("source", source);
// treeMap.put("type", "1");
// treeMap.put("phone", phone);
// treeMap.put("content", msgContent);
// treeMap.put("subcode", "");
//
// logger.info("Send Sms:" + treeMap.toString());
//
//// String url = "http://192.168.3.206:8118/router";//开发
//// String url = "http://qcdidicb.glsx.net:7060/router";//测试(貌似已经废弃了)
// String url = "http://qcapp.oss.glsx.net:7060/gop-message/router";//测试
//
// HttpUtils httpUtils = new HttpUtils();
// try {
// String res = httpUtils.get(url, treeMap);
// logger.info("Result:" + res);
//
// JSONObject jsonObject = JSONObject.parseObject(res);
// String code = jsonObject.getString("code");
// if (!code.equals("0")) {
// res = httpUtils.get(url, treeMap);
// logger.info("Failure resend result:" + res);
// }
// } catch (Exception ex) {
// logger.error("SendSmsException", ex);
// try {
// String res1 = httpUtils.get(url, treeMap);
// logger.info("Exception resend result:" + res1);
// } catch (Exception e) {
// logger.error("ReSendSmsException", ex);
// }
// }
// }
public static void main(String[] args) {
SmsUtil smsUtil = new SmsUtil();
String msgContent = MessageFormat.format("您本次操作验证码为:{0},一分钟内有效。","18508458837");
// smsUtil.send("18508458837", msgContent);
}
}