SendMessageBiz.java
7.27 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
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
package com.cjs.site.biz.pub;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.cjs.site.dao.pub.SendMessageLogDao;
import com.cjs.site.dao.user.account.ClientInfoDao;
import com.cjs.site.model.user.account.ClientInfo;
import com.cjs.site.model.user.account.UserInfo;
import com.cjs.site.util.lang.StringUtil;
import com.cjs.site.util.web.ActionUtil;
import com.jianzhou.sdk.BusinessService;
/**
* 发送短信
*
* @author tongyufu
*
*/
@Service
public class SendMessageBiz {
/**存储验证码的session key*/
public static final String RANDOM_NUM = "randomNums";
@Value("${${env}.message.username}")
private String account;
@Value("${${env}.message.password}")
private String password;
private Logger log = LogManager.getLogger(SendMessageBiz.class);
@Value("${env}")
private String environment;
@Autowired
private SendMessageLogDao sendMsgLogDao;
@Autowired
private ClientInfoDao clientInfoDao;
/**
* 发送短信
* @param phoneNumber
* @param content
* @return
*/
public Map<String, Object> send(String phoneNumber, String content) {
Map<String, Object> outmap = new HashMap<String, Object>();
if (StringUtils.trim(phoneNumber).length() != 11) {
log.info("非法的手机号,请检查后重新输入");
outmap.put("resultCode", "0");
outmap.put("resultMsg", "非法的手机号,请检查后重新输入");
return outmap;
// return -3;
//throw new ServiceException("非法的手机号,请检查后重新输入");
}
// if (!commonBiz.checkOpenTime()) {
// log.info("当前非开户时间,不可发送短信");
// return -2;
// //throw new ServiceException("当前非开户时间,不可发送短信");
// }
int count = sendMsgLogDao.queryCountByPhoneNumber(phoneNumber);
if (count >= 50) {
log.info("号码:" + phoneNumber + "今日发送短信数目已达到5条,无法继续发送");
outmap.put("resultCode", "0");
outmap.put("resultMsg", "号码:" + phoneNumber + "今日发送短信数目已达到5条,无法继续发送");
return outmap;
// return -1;
//throw new ServiceException("号码:" + phoneNumber + "今日发送短信数目已达到5条,无法继续发送");
}
// WebService方式
int result = 0;
if (StringUtils.equals(environment, "prod")) {
BusinessService bs = new BusinessService();
bs.setWebService(
"http://www.jianzhou.sh.cn/JianzhouSMSWSServer/services/BusinessService");
result = bs.sendBatchMessage(account, password, phoneNumber, content);
log.info("短信【" + phoneNumber + "】发送结果:" + result + "\t发送内容:" + content);
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("phoneNumber", phoneNumber);
params.put("message", content);
params.put("sendResult", result);
sendMsgLogDao.insertMegLog(params);
outmap.put("resultCode", "1");
outmap.put("result", result);
return outmap;
}
/**
* 发送短信验证码,支持同一页面发送多次时,验证码均可验证
*
* @param phoneNumber
* @param content
* @param session
* @return 发送结果,成功返回""
*/
@SuppressWarnings("unchecked")
public String sendValidCode(String phoneNo, String validCode, String content,
HttpSession session) {
if (!StringUtil.isPhoneNo(phoneNo)) {
return "手机号格式不正确";
}
Map<String, Object> result = this.send(phoneNo, content);
if ("0".equals(result.get("resultCode"))) {
return "短信发送失败";
}
List<Map<String, Object>> randomNums = (List<Map<String, Object>>) session
.getAttribute("randomNums");
if (randomNums == null) {
randomNums = new ArrayList<Map<String, Object>>();
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("tel", phoneNo);
map.put("smsCode", validCode);
randomNums.add(map);
session.setAttribute("randomNums", randomNums);
return "";
}
/**
* 通用发短信方法
* @param type 1.绑定充值卡短信 2.资金提现短信
* @param session
* @return
*/
public String sendMsg(Integer type, HttpSession session) {
String validCode = this.generateValidCode(6);
UserInfo userInfo = ActionUtil.getUser();
ClientInfo clientInfo = clientInfoDao.queryByClientId(userInfo.getUserId());
String phoneNo = clientInfo.getMobileTel();
String contentTop = "";
switch (type) {
case 1:
contentTop = "您正在进行绑定充值银行卡操作,验证码:";
break;
case 2:
contentTop = "您正在进行资金提现操作,验证码:";
default:
break;
}
String content = contentTop + validCode + ",请确保本人操作。如有疑问,详询400-969-0800。【赵涌牛】";
String result = this.sendValidCode(phoneNo, validCode, content, session);
if (StringUtils.isBlank(result)) {
result = "验证码发送成功";
}
return result;
}
/**
* 校对验证码
* @param validCode
* @param session
* @return
*/
@SuppressWarnings("unchecked")
public boolean compareValidCode(String validCode, HttpSession session) {
List<Map<String, Object>> randomNums = (List<Map<String, Object>>) session
.getAttribute("randomNums");
if (randomNums == null) {
return false;
}
for (Map<String, Object> random : randomNums) {
if (StringUtils.equals(validCode, (String) random.get("smsCode"))) {
return true;
}
}
return false;
}
/**
* 生成随机数字验证码
*
* @param length 验证码位数
* @return 验证码
*/
public String generateValidCode(Integer length) {
if (!StringUtils.equals(environment, "prod")) {
return "111111";
}
String validCode = (int) (Math.random() * 1000000) + "";
if (validCode.length() < length) {
validCode = StringUtil.lpad(length, validCode);
}
return validCode;
}
public String getReceipt(int taskId) {
BusinessService bs = new BusinessService();
bs.setWebService("http://www.jianzhou.sh.cn/JianzhouSMSWSServer/services/BusinessService");
return bs.getReceipt(account, password, taskId);
}
}