use of com.itrus.portal.db.DynamicCode in project portal by ixinportal.
the class DynamicCodeServiceImpl method sendCodeZSJS.
public boolean sendCodeZSJS(String mPhone, Long project, String smsType) throws UserInfoServiceException {
// 同一手机号指定时间内不能重发
DynamicCodeExample dynamicCodeExample = new DynamicCodeExample();
DynamicCodeExample.Criteria criteria = dynamicCodeExample.or();
Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, -RESEND_TIME);
criteria.andMPhoneEqualTo(mPhone);
// 发送时间大于等于当前时间减去1分钟
criteria.andSendTimeGreaterThanOrEqualTo(c.getTime());
List<DynamicCode> dynamicCodes = sqlSession.selectList("com.itrus.portal.db.DynamicCodeMapper.selectByExample", dynamicCodeExample);
if (null != dynamicCodes && !dynamicCodes.isEmpty()) {
throw new UserInfoServiceException("此手机号" + RESEND_TIME + "秒内不能连续发送,请稍后重试");
}
// 产生动态码
DynamicCode code = genCode();
if (null == code) {
throw new UserInfoServiceException("发送动态码失败,请稍后重试");
}
// 发送动态码,获取发送的短信内容
MessageTemplate messageTemplate = messageTemplateService.getMsgTemp(project, smsType);
if (null == messageTemplate) {
throw new UserInfoServiceException("未找到对应的短信模版,请联系管理员配置");
}
String content = messageTemplate.getMessageContent();
content = content.replaceAll("code", code.getCode());
if (smsType.equals("ZSJS")) {
smsType = "证书解锁";
}
if (!smsSendService.sendDynamicCodeSms(mPhone, content, smsType, project)) {
// 发送失败
throw new UserInfoServiceException("动态码发送失败,请联系管理员查看原因");
} else {
// 记录动态码之前将该手机号关联的有效验证码设置为无效
dynamicCodeExample.clear();
criteria = dynamicCodeExample.createCriteria();
criteria.andMPhoneEqualTo(mPhone);
criteria.andCodeStatusEqualTo(ComNames.DYNAMIC_CODE_ENROLL);
DynamicCode updateCode = new DynamicCode();
updateCode.setCodeStatus(ComNames.DYNAMIC_CODE_USED);
updateCode.setFailTime(new Date());
Map<String, Object> map = new HashMap<String, Object>();
map.put("record", updateCode);
map.put("example", dynamicCodeExample);
sqlSession.update("com.itrus.portal.db.DynamicCodeMapper.updateByExampleSelective", map);
// 发送成功: 记录动态码
Calendar calendar = Calendar.getInstance();
code.setSendTime(calendar.getTime());
// 失效时间为7天后
calendar.add(Calendar.DATE, FAIL_TIME_ZSJS);
// 设置失效时间
code.setFailTime(calendar.getTime());
code.setmPhone(mPhone);
if (code.getId() == null) {
sqlSession.insert("com.itrus.portal.db.DynamicCodeMapper.insert", code);
} else {
sqlSession.update("com.itrus.portal.db.DynamicCodeMapper.updateByPrimaryKey", code);
}
}
return true;
}
Aggregations