|
@@ -1,16 +1,16 @@
|
|
package cn.com.qmth.examcloud.exchange.inner.service.impl;
|
|
package cn.com.qmth.examcloud.exchange.inner.service.impl;
|
|
|
|
|
|
-import java.util.Arrays;
|
|
|
|
-import java.util.Date;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
-
|
|
|
|
-import org.apache.tomcat.util.buf.StringUtils;
|
|
|
|
-import org.slf4j.Logger;
|
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
-
|
|
|
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
|
+import cn.com.qmth.examcloud.commons.util.DateUtil;
|
|
|
|
+import cn.com.qmth.examcloud.commons.util.JsonUtil;
|
|
|
|
+import cn.com.qmth.examcloud.commons.util.UUID;
|
|
|
|
+import cn.com.qmth.examcloud.exchange.inner.service.SmsService;
|
|
|
|
+import cn.com.qmth.examcloud.exchange.inner.service.bean.ShortMessageInfo;
|
|
|
|
+import cn.com.qmth.examcloud.support.CacheConstants;
|
|
|
|
+import cn.com.qmth.examcloud.support.cache.CacheHelper;
|
|
|
|
+import cn.com.qmth.examcloud.support.cache.bean.SmsAssemblyCacheBean;
|
|
|
|
+import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
|
+import cn.com.qmth.examcloud.web.redis.RedisClient;
|
|
import com.aliyuncs.CommonRequest;
|
|
import com.aliyuncs.CommonRequest;
|
|
import com.aliyuncs.CommonResponse;
|
|
import com.aliyuncs.CommonResponse;
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
@@ -21,17 +21,16 @@ import com.aliyuncs.profile.DefaultProfile;
|
|
import com.google.common.collect.Maps;
|
|
import com.google.common.collect.Maps;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
import com.google.gson.JsonParser;
|
|
|
|
+import org.apache.tomcat.util.buf.StringUtils;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
|
-import cn.com.qmth.examcloud.commons.util.DateUtil;
|
|
|
|
-import cn.com.qmth.examcloud.commons.util.JsonUtil;
|
|
|
|
-import cn.com.qmth.examcloud.commons.util.UUID;
|
|
|
|
-import cn.com.qmth.examcloud.exchange.inner.service.SmsService;
|
|
|
|
-import cn.com.qmth.examcloud.exchange.inner.service.bean.ShortMessageInfo;
|
|
|
|
-import cn.com.qmth.examcloud.support.cache.CacheHelper;
|
|
|
|
-import cn.com.qmth.examcloud.support.cache.bean.SmsAssemblyCacheBean;
|
|
|
|
-import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
|
-import cn.com.qmth.examcloud.web.redis.RedisClient;
|
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* {@link StatusException} 状态码范围:101XXX<br>
|
|
* {@link StatusException} 状态码范围:101XXX<br>
|
|
@@ -45,163 +44,162 @@ import cn.com.qmth.examcloud.web.redis.RedisClient;
|
|
@Service
|
|
@Service
|
|
public class SmsServiceImpl implements SmsService {
|
|
public class SmsServiceImpl implements SmsService {
|
|
|
|
|
|
- @Autowired
|
|
|
|
- RedisClient redisClient;
|
|
|
|
-
|
|
|
|
- private static final Logger LOG = LoggerFactory.getLogger(SmsServiceImpl.class);
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void sendSms(String smsAssemblyCode, List<String> phoneList,
|
|
|
|
- Map<String, String> params) {
|
|
|
|
- SmsAssemblyCacheBean smsAssembly = CacheHelper.getSmsAssembly(smsAssemblyCode);
|
|
|
|
- if (null == smsAssembly) {
|
|
|
|
- throw new StatusException("101040", "smsAssemblyCode is wrong");
|
|
|
|
- }
|
|
|
|
- boolean virtualEnable = PropertyHolder.getBoolean("sms.virtual.enable", false);
|
|
|
|
- if (!virtualEnable) {
|
|
|
|
- execute(smsAssembly, "SendSms", phoneList, params);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void sendSmsCode(String phone, String code) {
|
|
|
|
-
|
|
|
|
- boolean virtualEnable = PropertyHolder.getBoolean("sms.smsCode.virtual.enable", false);
|
|
|
|
-
|
|
|
|
- if (virtualEnable) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- String key = "$_SMS:" + phone;
|
|
|
|
- ShortMessageInfo sm = redisClient.get(key, ShortMessageInfo.class);
|
|
|
|
- if (null != sm) {
|
|
|
|
- Date creationTime = sm.getCreationTime();
|
|
|
|
- boolean sameDay = DateUtil.isSameDay(creationTime, new Date());
|
|
|
|
- if (!sameDay) {
|
|
|
|
- sm = null;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- Date now = new Date();
|
|
|
|
-
|
|
|
|
- if (null == sm) {
|
|
|
|
- sm = new ShortMessageInfo();
|
|
|
|
- sm.setCreationTime(now);
|
|
|
|
- sm.setPhone(phone);
|
|
|
|
- sm.setTimes(0);
|
|
|
|
- } else {
|
|
|
|
- List<Date> sendTimeList = sm.getSendTimeList();
|
|
|
|
-
|
|
|
|
- if (10 <= sendTimeList.size()) {
|
|
|
|
- throw new StatusException("101000", "一天内发送次数超过10次");
|
|
|
|
- } else if (5 <= sendTimeList.size()) {
|
|
|
|
- Date d = sendTimeList.get(sendTimeList.size() - 5);
|
|
|
|
- if (now.getTime() - d.getTime() < 1000 * 60 * 60) {
|
|
|
|
- throw new StatusException("101000", "一小时内发送次数超过5次");
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- Date d = sendTimeList.get(sendTimeList.size() - 1);
|
|
|
|
- if (now.getTime() - d.getTime() < 1000 * 60) {
|
|
|
|
- throw new StatusException("101000", "一分钟内发送次数超过1次");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- Map<String, String> params = Maps.newHashMap();
|
|
|
|
- params.put("code", code);
|
|
|
|
- this.sendSms(YZM_SMS_ASSEMBLY_CODE, Arrays.asList(new String[]{phone}), params);
|
|
|
|
-
|
|
|
|
- sm.getSendTimeList().add(now);
|
|
|
|
- sm.setTimes(sm.getSendTimeList().size());
|
|
|
|
- sm.setLastMessage(code);
|
|
|
|
-
|
|
|
|
- redisClient.set(key, sm, 60 * 60 * 24);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void validateSmsCode(String phone, String code) {
|
|
|
|
-
|
|
|
|
- boolean virtualEnable = PropertyHolder.getBoolean("sms.smsCode.virtual.enable", false);
|
|
|
|
-
|
|
|
|
- if (virtualEnable) {
|
|
|
|
- String virtualCode = PropertyHolder.getString("sms.smsCode.virtual.code", "5220");
|
|
|
|
- if (!virtualCode.equals(code)) {
|
|
|
|
- throw new StatusException("102008", "验证码错误");
|
|
|
|
- } else {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- String key = "$_SMS:" + phone;
|
|
|
|
- ShortMessageInfo sm = redisClient.get(key, ShortMessageInfo.class);
|
|
|
|
-
|
|
|
|
- if (null == sm) {
|
|
|
|
- throw new StatusException("101001", "未发送验证码");
|
|
|
|
- }
|
|
|
|
- List<Date> sendTimeList = sm.getSendTimeList();
|
|
|
|
- Date date = sendTimeList.get(sendTimeList.size() - 1);
|
|
|
|
- Date now = new Date();
|
|
|
|
-
|
|
|
|
- if (now.getTime() - date.getTime() > 1000 * 60 * 5) {
|
|
|
|
- throw new StatusException("101002", "验证码过期");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (!sm.getLastMessage().equals(code)) {
|
|
|
|
- throw new StatusException("101003", "验证码错误");
|
|
|
|
- } else {
|
|
|
|
- sm.setLastMessage(UUID.randomUUID());
|
|
|
|
- redisClient.set(key, sm);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 执行
|
|
|
|
- *
|
|
|
|
- * @param smsAssembly
|
|
|
|
- * @param action
|
|
|
|
- * @param phoneList
|
|
|
|
- * @param templateParams
|
|
|
|
- * 短信模板参数
|
|
|
|
- * @author WANGWEI
|
|
|
|
- */
|
|
|
|
- private void execute(SmsAssemblyCacheBean smsAssembly, String action, List<String> phoneList,
|
|
|
|
- Map<String, String> templateParams) {
|
|
|
|
- String accessKeyId = PropertyHolder.getString("aliyun.sms.accessKeyId");
|
|
|
|
- String accessSecret = PropertyHolder.getString("aliyun.sms.accessKeySecret");
|
|
|
|
- String signName = smsAssembly.getExt1();
|
|
|
|
- String templateCode = smsAssembly.getExt2();
|
|
|
|
-
|
|
|
|
- DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
|
|
|
|
- IAcsClient client = new DefaultAcsClient(profile);
|
|
|
|
-
|
|
|
|
- CommonRequest request = new CommonRequest();
|
|
|
|
- request.setProtocol(ProtocolType.HTTPS);
|
|
|
|
- request.setMethod(MethodType.POST);
|
|
|
|
- request.setDomain("dysmsapi.aliyuncs.com");
|
|
|
|
- request.setVersion("2017-05-25");
|
|
|
|
- request.setAction(action);
|
|
|
|
- String phoneNumbers = StringUtils.join(phoneList, ',');
|
|
|
|
- request.putQueryParameter("PhoneNumbers", phoneNumbers);
|
|
|
|
- request.putQueryParameter("SignName", signName);
|
|
|
|
- request.putQueryParameter("TemplateCode", templateCode);
|
|
|
|
- request.putQueryParameter("TemplateParam", JsonUtil.toJson(templateParams));
|
|
|
|
- try {
|
|
|
|
- CommonResponse response = client.getCommonResponse(request);
|
|
|
|
- String data = response.getData();
|
|
|
|
- JsonParser jp = new JsonParser();
|
|
|
|
- JsonObject jsonObj = jp.parse(data).getAsJsonObject();
|
|
|
|
- String code = jsonObj.get("Code").getAsString();
|
|
|
|
-
|
|
|
|
- if (!code.equals("OK")) {
|
|
|
|
- LOG.error("aliyun sms response: " + JsonUtil.toJson(response));
|
|
|
|
- throw new StatusException("101001", "短信发送失败");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- } catch (StatusException e) {
|
|
|
|
- throw e;
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new StatusException("101002", "短信发送失败", e);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ RedisClient redisClient;
|
|
|
|
+
|
|
|
|
+ private static final Logger LOG = LoggerFactory.getLogger(SmsServiceImpl.class);
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void sendSms(String smsAssemblyCode, List<String> phoneList,
|
|
|
|
+ Map<String, String> params) {
|
|
|
|
+ SmsAssemblyCacheBean smsAssembly = CacheHelper.getSmsAssembly(smsAssemblyCode);
|
|
|
|
+ if (null == smsAssembly) {
|
|
|
|
+ throw new StatusException("101040", "smsAssemblyCode is wrong");
|
|
|
|
+ }
|
|
|
|
+ boolean virtualEnable = PropertyHolder.getBoolean("sms.virtual.enable", false);
|
|
|
|
+ if (!virtualEnable) {
|
|
|
|
+ execute(smsAssembly, "SendSms", phoneList, params);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void sendSmsCode(String phone, String code) {
|
|
|
|
+
|
|
|
|
+ boolean virtualEnable = PropertyHolder.getBoolean("sms.smsCode.virtual.enable", false);
|
|
|
|
+
|
|
|
|
+ if (virtualEnable) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String key = CacheConstants.CACHE_EX_SMS + phone;
|
|
|
|
+ ShortMessageInfo sm = redisClient.get(key, ShortMessageInfo.class);
|
|
|
|
+ if (null != sm) {
|
|
|
|
+ Date creationTime = sm.getCreationTime();
|
|
|
|
+ boolean sameDay = DateUtil.isSameDay(creationTime, new Date());
|
|
|
|
+ if (!sameDay) {
|
|
|
|
+ sm = null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Date now = new Date();
|
|
|
|
+
|
|
|
|
+ if (null == sm) {
|
|
|
|
+ sm = new ShortMessageInfo();
|
|
|
|
+ sm.setCreationTime(now);
|
|
|
|
+ sm.setPhone(phone);
|
|
|
|
+ sm.setTimes(0);
|
|
|
|
+ } else {
|
|
|
|
+ List<Date> sendTimeList = sm.getSendTimeList();
|
|
|
|
+
|
|
|
|
+ if (10 <= sendTimeList.size()) {
|
|
|
|
+ throw new StatusException("101000", "一天内发送次数超过10次");
|
|
|
|
+ } else if (5 <= sendTimeList.size()) {
|
|
|
|
+ Date d = sendTimeList.get(sendTimeList.size() - 5);
|
|
|
|
+ if (now.getTime() - d.getTime() < 1000 * 60 * 60) {
|
|
|
|
+ throw new StatusException("101000", "一小时内发送次数超过5次");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ Date d = sendTimeList.get(sendTimeList.size() - 1);
|
|
|
|
+ if (now.getTime() - d.getTime() < 1000 * 60) {
|
|
|
|
+ throw new StatusException("101000", "一分钟内发送次数超过1次");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ Map<String, String> params = Maps.newHashMap();
|
|
|
|
+ params.put("code", code);
|
|
|
|
+ this.sendSms(YZM_SMS_ASSEMBLY_CODE, Arrays.asList(new String[]{phone}), params);
|
|
|
|
+
|
|
|
|
+ sm.getSendTimeList().add(now);
|
|
|
|
+ sm.setTimes(sm.getSendTimeList().size());
|
|
|
|
+ sm.setLastMessage(code);
|
|
|
|
+
|
|
|
|
+ redisClient.set(key, sm, 60 * 60 * 24);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void validateSmsCode(String phone, String code) {
|
|
|
|
+
|
|
|
|
+ boolean virtualEnable = PropertyHolder.getBoolean("sms.smsCode.virtual.enable", false);
|
|
|
|
+
|
|
|
|
+ if (virtualEnable) {
|
|
|
|
+ String virtualCode = PropertyHolder.getString("sms.smsCode.virtual.code", "5220");
|
|
|
|
+ if (!virtualCode.equals(code)) {
|
|
|
|
+ throw new StatusException("102008", "验证码错误");
|
|
|
|
+ } else {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String key = CacheConstants.CACHE_EX_SMS + phone;
|
|
|
|
+ ShortMessageInfo sm = redisClient.get(key, ShortMessageInfo.class);
|
|
|
|
+
|
|
|
|
+ if (null == sm) {
|
|
|
|
+ throw new StatusException("101001", "未发送验证码");
|
|
|
|
+ }
|
|
|
|
+ List<Date> sendTimeList = sm.getSendTimeList();
|
|
|
|
+ Date date = sendTimeList.get(sendTimeList.size() - 1);
|
|
|
|
+ Date now = new Date();
|
|
|
|
+
|
|
|
|
+ if (now.getTime() - date.getTime() > 1000 * 60 * 5) {
|
|
|
|
+ throw new StatusException("101002", "验证码过期");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!sm.getLastMessage().equals(code)) {
|
|
|
|
+ throw new StatusException("101003", "验证码错误");
|
|
|
|
+ } else {
|
|
|
|
+ sm.setLastMessage(UUID.randomUUID());
|
|
|
|
+ redisClient.set(key, sm);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 执行
|
|
|
|
+ *
|
|
|
|
+ * @param smsAssembly
|
|
|
|
+ * @param action
|
|
|
|
+ * @param phoneList
|
|
|
|
+ * @param templateParams 短信模板参数
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ private void execute(SmsAssemblyCacheBean smsAssembly, String action, List<String> phoneList,
|
|
|
|
+ Map<String, String> templateParams) {
|
|
|
|
+ String accessKeyId = PropertyHolder.getString("aliyun.sms.accessKeyId");
|
|
|
|
+ String accessSecret = PropertyHolder.getString("aliyun.sms.accessKeySecret");
|
|
|
|
+ String signName = smsAssembly.getExt1();
|
|
|
|
+ String templateCode = smsAssembly.getExt2();
|
|
|
|
+
|
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
|
|
|
|
+ IAcsClient client = new DefaultAcsClient(profile);
|
|
|
|
+
|
|
|
|
+ CommonRequest request = new CommonRequest();
|
|
|
|
+ request.setProtocol(ProtocolType.HTTPS);
|
|
|
|
+ request.setMethod(MethodType.POST);
|
|
|
|
+ request.setDomain("dysmsapi.aliyuncs.com");
|
|
|
|
+ request.setVersion("2017-05-25");
|
|
|
|
+ request.setAction(action);
|
|
|
|
+ String phoneNumbers = StringUtils.join(phoneList, ',');
|
|
|
|
+ request.putQueryParameter("PhoneNumbers", phoneNumbers);
|
|
|
|
+ request.putQueryParameter("SignName", signName);
|
|
|
|
+ request.putQueryParameter("TemplateCode", templateCode);
|
|
|
|
+ request.putQueryParameter("TemplateParam", JsonUtil.toJson(templateParams));
|
|
|
|
+ try {
|
|
|
|
+ CommonResponse response = client.getCommonResponse(request);
|
|
|
|
+ String data = response.getData();
|
|
|
|
+ JsonParser jp = new JsonParser();
|
|
|
|
+ JsonObject jsonObj = jp.parse(data).getAsJsonObject();
|
|
|
|
+ String code = jsonObj.get("Code").getAsString();
|
|
|
|
+
|
|
|
|
+ if (!code.equals("OK")) {
|
|
|
|
+ LOG.error("aliyun sms response: " + JsonUtil.toJson(response));
|
|
|
|
+ throw new StatusException("101001", "短信发送失败");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } catch (StatusException e) {
|
|
|
|
+ throw e;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new StatusException("101002", "短信发送失败", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|