/**
   * Description:执行会员基本信息录入,即注册流程第一步<br>
   *
   * @author hujianpan
   * @version 0.1 2014年8月30日
   * @param memberRegisterCnd
   * @return String
   */
  @RequestMapping(value = "/registMemberInfoCollect")
  @ResponseBody
  public MessageBox registMemberInfoCollect(
      Member member,
      HttpServletRequest request,
      HttpSession session,
      HttpServletResponse response) {
    String result = BusinessConstants.SUCCESS;
    try {
      if (verifyUserNameLength(member)) {
        return new MessageBox("0", "用户名称长度应该位于2~20位之间");
      }
      if (!VerifyContainSpecialChar.isContainSpecialChars(member.getUsername())) {
        return new MessageBox("0", "用户名包含特殊字符串");
      }
      if (null == member.getLogpassword() || "".equals(member.getLogpassword().trim())) {
        return new MessageBox("0", "密码输入有误!");
      }

      // 验证验证码
      // 推广抢红包注册2,无验证码;
      if (request.getParameter("novalidatecode") == null) {
        String validatecode = request.getParameter("validatecode");
        String randCode = (String) session.getAttribute("randomCode");
        if (null == validatecode || null == randCode || !validatecode.equals(randCode)) {
          return new MessageBox("0", "验证码输入有误!");
        }
      }

      String inviterName = request.getParameter("inviterName");
      member.setPlatform(BusinessConstants.MEMBER_OPERATE_ON_PORTAL);
      member.setIp(HttpTookit.getRealIpAddr(request));
      result =
          memberRegisterService.insertMemberWithoutEmail(member, inviterName, request, session);
      if (!"success".equals(result)) {
        return new MessageBox("0", result);
      }
      // 注册借款用户需后台审核,审核通过才能登录系统
      if (member.getIsFinancialUser() != null
          && member.getIsFinancialUser() == Integer.parseInt(Constants.IS_FINANCIAL_USER)) {
        if ("success".equals(result)) {
          // 用于sso
          cookieRetrievingCookieGenerator.addCookie(
              request, response, TicketCryptor.encrypt(currentUser().getSsoTicket()));
          return new MessageBox("1", result);
        }
      } else {
        return new MessageBox("2", "尊敬的用户,您好!您注册的用户是借款用户,需要审核后才能登录系统进行下一步操作!");
      }

    } catch (Exception e) {
      logger.error("register", e);
      return new MessageBox("0", e.getMessage());
    }

    return new MessageBox("0", result);
  }
  /**
   * Description:执行会员注册方法<br>
   *
   * @author justin.xu
   * @version 0.1 2014年4月22日
   * @param memberRegisterCnd
   * @return String
   */
  @RequestMapping(value = "/register")
  public @ResponseBody String register(
      Member member,
      String inviterName,
      HttpServletRequest request,
      HttpSession session,
      HttpServletResponse response) {
    String result = "success";
    try {
      result = memberRegisterService.insertMember(member, inviterName, request, session);
      if ("success".equals(result)) {
        // 用于sso
        cookieRetrievingCookieGenerator.addCookie(
            request, response, TicketCryptor.encrypt(currentUser().getSsoTicket()));
      }
    } catch (Exception e) {
      result = "网络连接异常,请稍候重试!";
      logger.error("register", e);
    }

    return result;
  }
  /**
   * Description:执行会员基本信息录入,即注册流程第一步<br>
   *
   * @author hujianpan
   * @version 0.1 2014年8月30日
   * @param memberRegisterCnd
   * @return String
   */
  @RequestMapping(value = "/registMemberInfo")
  @ResponseBody
  public MessageBox registMemberInfo(
      Member member,
      HttpServletRequest request,
      HttpSession session,
      HttpServletResponse response) {
    String result = BusinessConstants.SUCCESS;
    try {
      if (null == member.getActiveCode() && !"".equals(member.getActiveCode().trim().trim())) {
        return new MessageBox("0", "请输入手机验证码");
      }
      // 验证手机验证码是否输入正确
      // 验证验证码是否正确
      String valiateResult =
          phoneService.compareSmsValidate(
              member.getMobileNum(),
              member.getActiveCode(),
              BusinessConstants.MOBILE_APPRO_FUNCTION);
      if (valiateResult != "success") {
        return new MessageBox("0", valiateResult);
      }

      if (verifyUserNameLength(member)) {
        return new MessageBox("0", "用户名称长度应该位于2~16位之间");
      }
      if (!VerifyContainSpecialChar.isContainSpecialChars(member.getUsername())) {
        return new MessageBox("0", "用户名包含特殊字符串");
      }
      if (null == member.getLogpassword() || "".equals(member.getLogpassword().trim())) {
        return new MessageBox("0", "密码输入有误!");
      }
      // 校验红包ID是否存在  liutao 20151103
      if (null != member.getRedId()
          && StringUtils.isNotEmpty(member.getRedId())
          && null != member.getInviterid()) {
        int membercount = redAccountMapper.isExistRed(member);
        if (membercount <= 0) {
          return new MessageBox("0", "尊敬的用户,您好!推荐您注册的红包不存在");
        }
      }
      String inviterName = request.getParameter("inviterName");
      member.setPlatform(BusinessConstants.MEMBER_OPERATE_ON_PORTAL);
      member.setIp(HttpTookit.getRealIpAddr(request));
      result =
          memberRegisterService.insertMemberWithoutEmail(member, inviterName, request, session);
      if (!"success".equals(result)) {
        return new MessageBox("0", result);
      }
      // 注册借款用户需后台审核,审核通过才能登录系统
      if (member.getIsFinancialUser() != null
          && member.getIsFinancialUser() == Integer.parseInt(Constants.IS_FINANCIAL_USER)) {
        if ("success".equals(result)) {
          cookieRetrievingCookieGenerator.addCookie(
              request, response, TicketCryptor.encrypt(currentUser().getSsoTicket()));
          return new MessageBox("1", result);
        }
      } else {
        return new MessageBox("2", "尊敬的用户,您好!您注册的用户是借款用户,需要审核后才能登录系统进行下一步操作!");
      }

    } catch (Exception e) {
      logger.error("register", e);
      return new MessageBox("0", e.getMessage());
    }

    return new MessageBox("0", result);
  }
  /**
   * Description:登录方法<br>
   *
   * @author justin.xu
   * @version 0.1 2014年5月7日
   * @param request
   * @param session
   * @param response
   * @param memberLoginCnd
   * @return String
   */
  @RequestMapping(value = "/login")
  @ValidateToken
  @ResponseBody
  public MessageBox login(
      HttpSession session, HttpServletResponse response, MemberLoginCnd memberLoginCnd)
      throws AuthenticationException {
    try {
      Object object = session.getAttribute(CSRFTokenManager.CSRF_PARAM_COUNTER);
      if (object != null && ((Integer) (object)).intValue() > 2) {
        if (memberLoginCnd.getCheckCode() == null || "".equals(memberLoginCnd.getCheckCode())) {
          return MessageBox.build("0", "请你输入验证码");
        }
        String randomCode = (String) currentSession().getAttribute("randomCode");
        if (!memberLoginCnd.getCheckCode().equals(randomCode)) {
          return MessageBox.build("0", "验证码不正确");
        }
      }

      long t1 = System.currentTimeMillis();
      logger.info(memberLoginCnd.getUsername() + " ----- login ----- begin time : " + t1);

      // shiro登录
      UsernamePasswordToken token =
          new UsernamePasswordToken(
              memberLoginCnd.getUsername(),
              MD5.toMD5(memberLoginCnd.getPasswd()),
              BusinessConstants.MEMBER_OPERATE_ON_PORTAL);
      Subject subject = SecurityUtils.getSubject();
      subject.login(token);

      long t2 = System.currentTimeMillis();
      logger.info(
          memberLoginCnd.getUsername() + " ----- login ----- shiro login cost : " + (t2 - t1));

      LoginCnd loginCnd = new LoginCnd();
      loginCnd.setUserId(currentUser().getUserId());
      loginCnd.setUserName(currentUser().getUserName());
      loginCnd.setIp(HttpTookit.getRealIpAddr(currentRequest()));
      loginCnd.setSessionId(session.getId());
      loginCnd.setPlatform(currentUser().getPlatform());
      // 调用登录逻辑
      String msg = memberService.saveLogin(loginCnd);

      long t3 = System.currentTimeMillis();
      logger.info(
          memberLoginCnd.getUsername() + " ----- login ----- save login cost : " + (t3 - t2));

      // 用于sso
      cookieRetrievingCookieGenerator.addCookie(
          currentRequest(), response, TicketCryptor.encrypt(currentUser().getSsoTicket()));

      long t4 = System.currentTimeMillis();
      logger.error(
          memberLoginCnd.getUsername() + " ----- login ----- add sso cookie cost : " + (t4 - t3));
      System.out.println(memberLoginCnd.getSaveid() + "hhe");
      // 保存cookie
      if ("1".equals(memberLoginCnd.getSaveid())) {
        CookieGenerator cookieGenerator = new CookieGenerator();
        cookieGenerator.setCookieMaxAge(2147483647);
        cookieGenerator.setCookieName(COOKIE_LOGIN_USERID);
        cookieGenerator.addCookie(response, memberLoginCnd.getCookieusername());
      } else {
        CookieGenerator cookieGenerator = new CookieGenerator();
        cookieGenerator.setCookieMaxAge(0);
        cookieGenerator.setCookieName(COOKIE_LOGIN_USERID);
        cookieGenerator.addCookie(response, memberLoginCnd.getCookieusername());
      }

      long t5 = System.currentTimeMillis();
      logger.error(
          memberLoginCnd.getUsername()
              + " ----- login ----- add username cookie cost : "
              + (t5 - t4));

      // 小红点提示状态
      RedAccount redAccount = redAccountService.queryRedDotState(currentUser().getUserId());
      if (redAccount != null) {
        currentSession().setAttribute("redDot", redAccount);
      }
      // 抽奖机会提醒
      int lotteryChanceCount =
          lotteryChanceInfoService.queryLotteryNumTotal("11", currentUser().getUserId());
      if (lotteryChanceCount > 0) {
        currentSession().setAttribute("lotteryChanceCount", lotteryChanceCount);
      }
      if (BusinessConstants.VISITOR_UNAUTHERIZED.equals(msg)) {
        return MessageBox.build("2", "请先前往认证");
      }

      if (object != null) {
        currentSession().removeAttribute(CSRFTokenManager.CSRF_PARAM_COUNTER);
      }
    } catch (UnknownAccountException e) {
      return MessageBox.build("0", "账户名不存在");
    } catch (IncorrectCredentialsException ice) {
      return MessageBox.build("0", "账户名与密码不匹配");
    } catch (LockedAccountException lae) {
      return MessageBox.build("0", "账户已经锁定");
    } catch (Exception e) {
      if (isLogin()) {
        SecurityUtils.getSubject().logout();
      }
      logger.error("登录失败", e);
      return MessageBox.buildForCounter("3", "账号与密码不匹配");
    }

    String backUrl = memberLoginCnd.getBackUrl();
    if (backUrl != null && !backUrl.trim().equals("")) {
      logger.info("******自动跳转登陆前页面=" + backUrl + "******");
      return MessageBox.build("8", backUrl);
    }

    return MessageBox.build("1", "success");
  }