Search in sources :

Example 6 with DynamicCode

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;
}
Also used : MessageTemplate(com.itrus.portal.db.MessageTemplate) DynamicCodeExample(com.itrus.portal.db.DynamicCodeExample) HashMap(java.util.HashMap) Calendar(java.util.Calendar) DynamicCode(com.itrus.portal.db.DynamicCode) UserInfoServiceException(com.itrus.portal.exception.UserInfoServiceException) Date(java.util.Date)

Aggregations

DynamicCode (com.itrus.portal.db.DynamicCode)6 DynamicCodeExample (com.itrus.portal.db.DynamicCodeExample)6 Date (java.util.Date)6 HashMap (java.util.HashMap)5 MessageTemplate (com.itrus.portal.db.MessageTemplate)4 UserInfoServiceException (com.itrus.portal.exception.UserInfoServiceException)4 Calendar (java.util.Calendar)4