|
@@ -2,26 +2,16 @@ package cn.com.qmth.examcloud.exchange.inner.service.impl;
|
|
|
|
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
import cn.com.qmth.examcloud.commons.util.DateUtil;
|
|
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.commons.util.UUID;
|
|
import cn.com.qmth.examcloud.exchange.inner.service.SmsService;
|
|
import cn.com.qmth.examcloud.exchange.inner.service.SmsService;
|
|
import cn.com.qmth.examcloud.exchange.inner.service.bean.ShortMessageInfo;
|
|
import cn.com.qmth.examcloud.exchange.inner.service.bean.ShortMessageInfo;
|
|
import cn.com.qmth.examcloud.support.CacheConstants;
|
|
import cn.com.qmth.examcloud.support.CacheConstants;
|
|
import cn.com.qmth.examcloud.support.cache.CacheHelper;
|
|
import cn.com.qmth.examcloud.support.cache.CacheHelper;
|
|
import cn.com.qmth.examcloud.support.cache.bean.SmsAssemblyCacheBean;
|
|
import cn.com.qmth.examcloud.support.cache.bean.SmsAssemblyCacheBean;
|
|
|
|
+import cn.com.qmth.examcloud.support.sms.SmsHelper;
|
|
import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
import cn.com.qmth.examcloud.web.redis.RedisClient;
|
|
import cn.com.qmth.examcloud.web.redis.RedisClient;
|
|
-import com.aliyuncs.CommonRequest;
|
|
|
|
-import com.aliyuncs.CommonResponse;
|
|
|
|
-import com.aliyuncs.DefaultAcsClient;
|
|
|
|
-import com.aliyuncs.IAcsClient;
|
|
|
|
-import com.aliyuncs.http.MethodType;
|
|
|
|
-import com.aliyuncs.http.ProtocolType;
|
|
|
|
-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.JsonParser;
|
|
|
|
-import org.apache.tomcat.util.buf.StringUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -33,40 +23,35 @@ import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * {@link StatusException} 状态码范围:101XXX<br>
|
|
|
|
- * <p>
|
|
|
|
- * 阿里云短信服务
|
|
|
|
|
|
+ * 短信发送
|
|
*
|
|
*
|
|
* @author WANGWEI
|
|
* @author WANGWEI
|
|
* @date 2019年3月27日
|
|
* @date 2019年3月27日
|
|
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
|
*/
|
|
*/
|
|
@Service
|
|
@Service
|
|
public class SmsServiceImpl implements SmsService {
|
|
public class SmsServiceImpl implements SmsService {
|
|
|
|
|
|
- @Autowired
|
|
|
|
- RedisClient redisClient;
|
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(SmsServiceImpl.class);
|
|
|
|
|
|
- private static final Logger LOG = LoggerFactory.getLogger(SmsServiceImpl.class);
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisClient redisClient;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void sendSms(String smsAssemblyCode, List<String> phoneList,
|
|
|
|
- Map<String, String> params) {
|
|
|
|
|
|
+ public void sendSms(String smsAssemblyCode, List<String> phoneList, Map<String, String> params) {
|
|
SmsAssemblyCacheBean smsAssembly = CacheHelper.getSmsAssembly(smsAssemblyCode);
|
|
SmsAssemblyCacheBean smsAssembly = CacheHelper.getSmsAssembly(smsAssemblyCode);
|
|
if (null == smsAssembly) {
|
|
if (null == smsAssembly) {
|
|
throw new StatusException("101040", "smsAssemblyCode is wrong");
|
|
throw new StatusException("101040", "smsAssemblyCode is wrong");
|
|
}
|
|
}
|
|
|
|
+
|
|
boolean virtualEnable = PropertyHolder.getBoolean("sms.virtual.enable", false);
|
|
boolean virtualEnable = PropertyHolder.getBoolean("sms.virtual.enable", false);
|
|
if (!virtualEnable) {
|
|
if (!virtualEnable) {
|
|
- execute(smsAssembly, "SendSms", phoneList, params);
|
|
|
|
|
|
+ SmsHelper.send(smsAssembly.getExt1(), smsAssembly.getExt2(), phoneList, params);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void sendSmsCode(String phone, String code) {
|
|
public void sendSmsCode(String phone, String code) {
|
|
-
|
|
|
|
boolean virtualEnable = PropertyHolder.getBoolean("sms.smsCode.virtual.enable", false);
|
|
boolean virtualEnable = PropertyHolder.getBoolean("sms.smsCode.virtual.enable", false);
|
|
-
|
|
|
|
if (virtualEnable) {
|
|
if (virtualEnable) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -82,7 +67,6 @@ public class SmsServiceImpl implements SmsService {
|
|
}
|
|
}
|
|
|
|
|
|
Date now = new Date();
|
|
Date now = new Date();
|
|
-
|
|
|
|
if (null == sm) {
|
|
if (null == sm) {
|
|
sm = new ShortMessageInfo();
|
|
sm = new ShortMessageInfo();
|
|
sm.setCreationTime(now);
|
|
sm.setCreationTime(now);
|
|
@@ -107,7 +91,7 @@ public class SmsServiceImpl implements SmsService {
|
|
}
|
|
}
|
|
Map<String, String> params = Maps.newHashMap();
|
|
Map<String, String> params = Maps.newHashMap();
|
|
params.put("code", code);
|
|
params.put("code", code);
|
|
- this.sendSms(YZM_SMS_ASSEMBLY_CODE, Arrays.asList(new String[]{phone}), params);
|
|
|
|
|
|
+ this.sendSms(SmsHelper.SMS_YZM, Arrays.asList(new String[]{phone}), params);
|
|
|
|
|
|
sm.getSendTimeList().add(now);
|
|
sm.getSendTimeList().add(now);
|
|
sm.setTimes(sm.getSendTimeList().size());
|
|
sm.setTimes(sm.getSendTimeList().size());
|
|
@@ -118,7 +102,6 @@ public class SmsServiceImpl implements SmsService {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void validateSmsCode(String phone, String code) {
|
|
public void validateSmsCode(String phone, String code) {
|
|
-
|
|
|
|
boolean virtualEnable = PropertyHolder.getBoolean("sms.smsCode.virtual.enable", false);
|
|
boolean virtualEnable = PropertyHolder.getBoolean("sms.smsCode.virtual.enable", false);
|
|
|
|
|
|
if (virtualEnable) {
|
|
if (virtualEnable) {
|
|
@@ -150,56 +133,6 @@ public class SmsServiceImpl implements SmsService {
|
|
sm.setLastMessage(UUID.randomUUID());
|
|
sm.setLastMessage(UUID.randomUUID());
|
|
redisClient.set(key, sm, 60 * 60 * 24);
|
|
redisClient.set(key, sm, 60 * 60 * 24);
|
|
}
|
|
}
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 执行
|
|
|
|
- *
|
|
|
|
- * @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);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|