Search in sources :

Example 1 with DynamicCodeExample

use of com.itrus.portal.db.DynamicCodeExample in project portal by ixinportal.

the class DynamicCodeServiceImpl method sendCodeCert.

/**
 * 银行卡认证时,发送动态码
 *
 * @param mPhone
 *            手机
 * @param project
 *            项目id
 * @param smsType
 *            短信模版类型(DTM:动态码)
 *
 * @throws UserInfoServiceException
 */
public void sendCodeCert(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("DTM")) {
        smsType = "动态码";
    }
    if (!smsSendService.sendDynamicCodeSmsCert(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());
        // 失效时间为5分钟后
        calendar.add(Calendar.MINUTE, FAIL_TIME);
        // 设置失效时间
        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);
        }
    }
}
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)

Example 2 with DynamicCodeExample

use of com.itrus.portal.db.DynamicCodeExample in project portal by ixinportal.

the class DynamicCodeServiceImpl method genCode.

// 产生动态码
private DynamicCode genCode() {
    String code = null;
    int genCodeTime = 0;
    DynamicCodeExample mphoneCodeExample = new DynamicCodeExample();
    DynamicCodeExample.Criteria criteria = mphoneCodeExample.or();
    DynamicCode codeDb;
    // 如需要大量授权码,需要加长授权码的长度以增加同时有效授权码数量
    do {
        code = AuthCodeEngine.generatorAuthCode(MPHONE_CODE_LENGTH, REPEAT_NUM);
        criteria.andCodeEqualTo(code);
        codeDb = sqlSession.selectOne("com.itrus.portal.db.DynamicCodeMapper.selectByExample", mphoneCodeExample);
        /*
			 * 以下三种情况跳出循环: 1.数据库中没有此授权码; 2.存在此授权码,但已超出有效期;
			 * 3.存在此授权码,有效期之内,但此授权码为已使用状态;
			 */
        if (codeDb == null || new Date().after(codeDb.getFailTime()) || (ComNames.DYNAMIC_CODE_USED == codeDb.getCodeStatus())) {
            if (codeDb == null)
                codeDb = new DynamicCode();
            codeDb.setCode(code);
            codeDb.setCodeStatus(ComNames.DYNAMIC_CODE_ENROLL);
            break;
        }
        genCodeTime++;
    } while (genCodeTime < 1000000);
    return codeDb;
}
Also used : DynamicCodeExample(com.itrus.portal.db.DynamicCodeExample) DynamicCode(com.itrus.portal.db.DynamicCode) Date(java.util.Date)

Example 3 with DynamicCodeExample

use of com.itrus.portal.db.DynamicCodeExample in project portal by ixinportal.

the class DynamicCodeServiceImpl method verifyCode.

/**
 * 验证动态码
 *
 * @param mPhone
 * @param code
 * @return
 */
public boolean verifyCode(String mPhone, String code) {
    DynamicCodeExample dynamicCodeExample = new DynamicCodeExample();
    DynamicCodeExample.Criteria criteria = dynamicCodeExample.or();
    criteria.andMPhoneEqualTo(mPhone);
    criteria.andCodeEqualTo(code);
    criteria.andFailTimeGreaterThanOrEqualTo(new Date());
    criteria.andCodeStatusEqualTo(ComNames.DYNAMIC_CODE_ENROLL);
    List<DynamicCode> dynamicCodes = sqlSession.selectList("com.itrus.portal.db.DynamicCodeMapper.selectByExample", dynamicCodeExample);
    if (dynamicCodes == null || dynamicCodes.isEmpty()) {
        // 验证失败
        return false;
    }
    // 把该动态码设置为无效状态
    DynamicCode dynamicCode = new DynamicCode();
    dynamicCode.setFailTime(new Date());
    dynamicCode.setCodeStatus(ComNames.DYNAMIC_CODE_USED);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("record", dynamicCode);
    map.put("example", dynamicCodeExample);
    sqlSession.update("com.itrus.portal.db.DynamicCodeMapper.updateByExampleSelective", map);
    return true;
}
Also used : DynamicCodeExample(com.itrus.portal.db.DynamicCodeExample) HashMap(java.util.HashMap) DynamicCode(com.itrus.portal.db.DynamicCode) Date(java.util.Date)

Example 4 with DynamicCodeExample

use of com.itrus.portal.db.DynamicCodeExample in project portal by ixinportal.

the class DynamicCodeServiceImpl method sendCodeJSSQYZ.

/**
 * 证书解锁申请验证码发送接口
 * @param mPhone
 * @param project
 * @param smsType
 * @throws UserInfoServiceException
 */
public void sendCodeJSSQYZ(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("JSSQYZ")) {
        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());
        // 失效时间为30分钟后
        calendar.add(Calendar.MINUTE, FAIL_TIME_30_MINUTE);
        // 设置失效时间
        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);
        }
    }
}
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)

Example 5 with DynamicCodeExample

use of com.itrus.portal.db.DynamicCodeExample in project portal by ixinportal.

the class DynamicCodeServiceImpl method sendCode.

/**
 * 用户登录/注册时,发送动态码
 *
 * @param mPhone
 *            手机
 * @param project
 *            项目id
 * @param smsType
 *            短信模版类型(DTM:动态码)
 *
 * @throws UserInfoServiceException
 */
public void sendCode(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("DTM")) {
        smsType = "动态码";
    } else if (smsType.equals("ZSBD")) {
        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());
        // 失效时间为5分钟后
        calendar.add(Calendar.MINUTE, FAIL_TIME);
        // 设置失效时间
        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);
        }
    }
}
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