浏览代码

rename cache keys.

deason 2 年之前
父节点
当前提交
85d17a7691

+ 177 - 179
examcloud-exchange-inner-service/src/main/java/cn/com/qmth/examcloud/exchange/inner/service/impl/SmsServiceImpl.java

@@ -1,16 +1,16 @@
 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.CommonResponse;
 import com.aliyuncs.DefaultAcsClient;
@@ -21,17 +21,16 @@ import com.aliyuncs.profile.DefaultProfile;
 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.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>
@@ -45,163 +44,162 @@ import cn.com.qmth.examcloud.web.redis.RedisClient;
 @Service
 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);
+        }
+    }
 
 }

+ 59 - 61
examcloud-exchange-outer-service/src/main/java/cn/com/qmth/examcloud/exchange/outer/service/impl/CourseGroupServiceImpl.java

@@ -1,14 +1,5 @@
 package cn.com.qmth.examcloud.exchange.outer.service.impl;
 
-import java.io.File;
-import java.util.List;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-import com.google.common.reflect.TypeToken;
-import com.thoughtworks.xstream.XStream;
-
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.commons.helpers.XStreamBuilder;
 import cn.com.qmth.examcloud.commons.util.JsonUtil;
@@ -16,7 +7,15 @@ import cn.com.qmth.examcloud.commons.util.PathUtil;
 import cn.com.qmth.examcloud.exchange.outer.service.CourseGroupService;
 import cn.com.qmth.examcloud.exchange.outer.service.bean.Course;
 import cn.com.qmth.examcloud.exchange.outer.service.bean.CourseGroup;
+import cn.com.qmth.examcloud.support.CacheConstants;
 import cn.com.qmth.examcloud.web.redis.RedisClient;
+import com.google.common.reflect.TypeToken;
+import com.thoughtworks.xstream.XStream;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.util.List;
 
 /**
  * 类注释
@@ -28,57 +27,56 @@ import cn.com.qmth.examcloud.web.redis.RedisClient;
 @Service
 public class CourseGroupServiceImpl implements CourseGroupService {
 
-	@Autowired
-	RedisClient redisClient;
-
-	@Override
-	public List<Course> getCourseList(Long rootOrgId, String courseGroupName) {
-
-		String key = "EX_COURSE_LIST:" + rootOrgId + "_" + courseGroupName;
-		List<Course> list = null;
-
-		String json = redisClient.get(key, String.class);
-		if (null != json) {
-			@SuppressWarnings("serial")
-			List<Course> obj = JsonUtil.fromJson(json, new TypeToken<List<Course>>() {
-			}.getType());
-			list = obj;
-			return list;
-		}
-
-		list = getCourseListFromXml(rootOrgId, courseGroupName);
-		redisClient.set(key, JsonUtil.toJson(list), 60 * 5);
-		return list;
-	}
-
-	private List<Course> getCourseListFromXml(Long rootOrgId, String courseGroupName) {
-
-		String resoucePath = PathUtil
-				.getResoucePath("course-group/course-group-" + rootOrgId + ".xml");
-		File file = new File(resoucePath);
-
-		XStream xStream = XStreamBuilder.newInstance().build();
-		xStream.allowTypes(new Class[]{CourseGroup.class, Course.class, List.class});
-		xStream.alias("groups", List.class);
-		xStream.alias("group", CourseGroup.class);
-		xStream.alias("course", Course.class);
-
-		List<CourseGroup> list = null;
-		try {
-			@SuppressWarnings("unchecked")
-			List<CourseGroup> obj = (List<CourseGroup>) xStream.fromXML(file);
-			list = obj;
-		} catch (Exception e) {
-			throw new StatusException("620001", "配置文件错误", e);
-		}
-
-		for (CourseGroup cur : list) {
-			if (courseGroupName.equals(cur.getName())) {
-				return cur.getCourseList();
-			}
-		}
-
-		throw new StatusException("860001", "课程组未配置");
-	}
+    @Autowired
+    RedisClient redisClient;
+
+    @Override
+    public List<Course> getCourseList(Long rootOrgId, String courseGroupName) {
+        String key = CacheConstants.CACHE_EX_COURSE_LIST + rootOrgId + "_" + courseGroupName;
+        List<Course> list = null;
+
+        String json = redisClient.get(key, String.class);
+        if (null != json) {
+            @SuppressWarnings("serial")
+            List<Course> obj = JsonUtil.fromJson(json, new TypeToken<List<Course>>() {
+            }.getType());
+            list = obj;
+            return list;
+        }
+
+        list = getCourseListFromXml(rootOrgId, courseGroupName);
+        redisClient.set(key, JsonUtil.toJson(list), 60 * 5);
+        return list;
+    }
+
+    private List<Course> getCourseListFromXml(Long rootOrgId, String courseGroupName) {
+
+        String resoucePath = PathUtil
+                .getResoucePath("course-group/course-group-" + rootOrgId + ".xml");
+        File file = new File(resoucePath);
+
+        XStream xStream = XStreamBuilder.newInstance().build();
+        xStream.allowTypes(new Class[]{CourseGroup.class, Course.class, List.class});
+        xStream.alias("groups", List.class);
+        xStream.alias("group", CourseGroup.class);
+        xStream.alias("course", Course.class);
+
+        List<CourseGroup> list = null;
+        try {
+            @SuppressWarnings("unchecked")
+            List<CourseGroup> obj = (List<CourseGroup>) xStream.fromXML(file);
+            list = obj;
+        } catch (Exception e) {
+            throw new StatusException("620001", "配置文件错误", e);
+        }
+
+        for (CourseGroup cur : list) {
+            if (courseGroupName.equals(cur.getName())) {
+                return cur.getCourseList();
+            }
+        }
+
+        throw new StatusException("860001", "课程组未配置");
+    }
 
 }