/**
  * Description:进行手机认证<br>
  *
  * @author justin.xu
  * @version 0.1 2014年4月30日
  * @param request
  * @param session
  * @param mobile 手机号
  * @param activeCode 验证码
  * @return String
  */
 @RequestMapping(value = "activeMobile")
 @ResponseBody
 public MessageBox activeMobile(
     HttpServletRequest request, HttpSession session, String mobile, String activeCode) {
   String result = "success";
   try {
     ShiroUser shiroUser = currentUser();
     MobileApproVo mobileApproVo =
         mobileApproService.queryMobileApproByUserId(shiroUser.getUserId());
     // 修改手机号码操作
     if (null != mobileApproVo
         && null != mobileApproVo.getPassed()
         && mobileApproVo.getPassed() == Constants.YES) {
       return new MessageBox("0", "手机号码已认证通过");
     }
     MemberCnd memberCnd = new MemberCnd();
     memberCnd.setId(shiroUser.getUserId());
     MemberVo memberVo = memberService.queryMemberByCnd(memberCnd);
     result =
         mobileApproService.saveMobileAppro(
             memberVo, mobile, activeCode, request, BusinessConstants.MOBILE_APPRO_FUNCTION);
   } catch (AppException ae) {
     result = ae.getMessage();
   } catch (Exception e) {
     result = "网络连接异常,请刷新页面或稍后重试!";
     e.printStackTrace();
   }
   if (BusinessConstants.SUCCESS.equals(result)) {
     return new MessageBox("1", "短信认证成功!");
   }
   return new MessageBox("0", result);
 }
  /**
   * Description:进入手机认证页面<br>
   *
   * @author justin.xu
   * @version 0.1 2014年4月25日
   * @param session
   * @param request
   * @return ModelAndView
   */
  @RequiresAuthentication
  @RequestMapping(value = "mobileforinsert")
  public ModelAndView mobileforinsert(HttpSession session, HttpServletRequest request) {
    ModelAndView mv = new ModelAndView();
    try {
      ShiroUser shiroUser = currentUser();
      // 手机认证
      MobileApproVo mobileAppro =
          mobileApproService.queryMobileApproByUserId(shiroUser.getUserId());
      // 邮件认证
      EmailApproVo emailApproVo = emailApprService.queryEmailApproByUserId(shiroUser.getUserId());
      mv.addObject("mobileAppro", mobileAppro);
      mv.addObject("emailApproVo", emailApproVo);

      // 修改手机号时原有手机号是否验证通过,不为空代表通过
      if (null
          != session.getAttribute(
              BusinessConstants.MOBILE_APPRO_RESET_FUNCTION + shiroUser.getUserId())) {
        mv.setViewName("account/approve/mobile/newMobile");
        return mv;
      }
      if (null != mobileAppro
          && null != mobileAppro.getPassed()
          && mobileAppro.getPassed() == Constants.YES) {
        mv.setViewName("account/approve/mobile/verifyOldMobile");
      } else {
        mv.setViewName("account/approve/mobile/newMobile");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return mv;
  }
 /**
  * Description:重设手机提交方法<br>
  *
  * @author justin.xu
  * @version 0.1 2014年4月25日
  * @param session
  * @param request
  * @return ModelAndView
  */
 @RequestMapping(value = "toModifyPhone")
 public ModelAndView toModifyPhone(
     HttpSession session, HttpServletRequest request, String resetActiveCode) {
   ModelAndView mv = new ModelAndView("account/approve/mobile/resetMobile");
   try {
     ShiroUser shiroUser = currentUser();
     MobileApproVo mobileApproVo =
         mobileApproService.queryMobileApproByUserId(shiroUser.getUserId());
     mv.addObject("mobileAppro", mobileApproVo);
     // 验证验证码是否正确
     String valiateResult =
         phoneService.compareSmsValidate(
             mobileApproVo.getMobileNum(),
             resetActiveCode,
             BusinessConstants.MOBILE_APPRO_RESET_FUNCTION);
     if (valiateResult != "success") {
       mv.addObject("errorMsg", valiateResult);
     } else {
       mv.setViewName("account/approve/mobile/newMobile");
     }
   } catch (Exception e) {
     mv.addObject("errorMsg", "网络异常,请刷新页面或稍候重试");
     e.printStackTrace();
   }
   return mv;
 }
  /**
   * Description:进行手机认证<br>
   *
   * @author hujianpan
   * @version 0.1 2014年8月30日
   * @param request
   * @param session
   * @param mobile 手机号
   * @param activeCode 验证码
   * @return String
   */
  @RequestMapping(value = "verificationMobailActiveCode")
  @ResponseBody
  public MessageBox verificationMobailActiveCode(HttpServletRequest request, HttpSession session) {
    String result = BusinessConstants.SUCCESS;
    Member member = currentMember();
    String userNameParam = "@@@@";
    String mobileParam = request.getParameter("mobile");
    String activeCodeParam = request.getParameter("activeCode");
    if (member != null) {
      userNameParam = member.getUsername();
      if (StringUtils.isEmpty(userNameParam)) {
        return new MessageBox("0", "用户名不能为空");
      }
    } else {
      return new MessageBox("0", "注册信息丢失,请重新登入或注册");
    }

    if (StringUtils.isEmpty(mobileParam)) {
      return new MessageBox("0", "用户手机号不能为空");
    }
    if (StringUtils.isEmpty(activeCodeParam)) {
      return new MessageBox("0", "手机验证码不能为空");
    }

    result =
        mobileApproService.verificationMobailActiveCode(
            request, member, mobileParam, activeCodeParam);
    if (!BusinessConstants.SUCCESS.equals(result)) {
      return new MessageBox("0", result);
    }

    return new MessageBox("1", "验证成功");
  }
 /**
  * Description:修改绑定手机时,对原来的手机号码认证<br>
  *
  * @author hujianpan
  * @version 0.1 2014年4月30日
  * @param request
  * @param session
  * @param mobile 手机号
  * @param activeCode 验证码
  * @return String
  */
 @RequestMapping(value = "verifyCurrentUserMobile")
 @ResponseBody
 public MessageBox verifyCurrentUserMobile(
     HttpServletRequest request, HttpSession session, String mobile, String activeCode) {
   String result = "success";
   ShiroUser shiroUser = currentUser();
   try {
     MemberCnd memberCnd = new MemberCnd();
     memberCnd.setId(shiroUser.getUserId());
     MemberVo memberVo = memberService.queryMemberByCnd(memberCnd);
     memberVo.setIsModify(true);
     result =
         mobileApproService.saveMobileAppro(
             memberVo,
             mobile,
             activeCode,
             request,
             BusinessConstants.MOBILE_APPRO_RESET_FUNCTION,
             BusinessConstants.SMS_TEMPLATE_TYPE_VERIFYSUCCESS_MOBILE_CODE);
   } catch (AppException ae) {
     result = ae.getMessage();
   } catch (Exception e) {
     result = "网络连接异常,请刷新页面或稍后重试!";
     e.printStackTrace();
   }
   if (BusinessConstants.SUCCESS.equals(result)) {
     // 记录到session中,代表验证原有手机通过
     session.setAttribute(
         BusinessConstants.MOBILE_APPRO_RESET_FUNCTION + shiroUser.getUserId(), "true");
     return new MessageBox("1", "短信认证成功!");
   }
   return new MessageBox("0", result);
 }
  /**
   * Description:安全中心-验证账户信息-发送手机认证验证码<br>
   *
   * @author hujianpan
   * @version 0.1 2014年8月30日
   * @param request
   * @param session
   * @return String
   */
  @RequiresAuthentication
  @RequestMapping(value = "/sendMobailActiveInSaftCenter")
  @ResponseBody
  public MessageBox sendMobailActiveInSaftCenter(HttpServletRequest request, HttpSession session) {
    String result = BusinessConstants.SUCCESS;
    Member member = currentMember();
    if (StringUtils.isEmpty(member.getUsername())) {
      return new MessageBox("0", "请先登入!");
    }
    String mobile = request.getParameter("mobile");
    String activeCode = request.getParameter("activeCode");
    try {
      MobileApproVo mobileApproVo = mobileApproService.queryMobileApproByUserId(member.getId());
      // 修改手机号码操作
      if (null
              == session.getAttribute(
                  BusinessConstants.MOBILE_APPRO_RESET_FUNCTION + member.getId())
          && (null != mobileApproVo
              && null != mobileApproVo.getPassed()
              && mobileApproVo.getPassed() == Constants.YES)) {
        return new MessageBox("0", "手机号码已认证通过");
      }
      result =
          mobileApproService.sendMobileApprValidate(
              mobile, request, member.getUsername(), BusinessConstants.MOBILE_APPRO_FUNCTION);
      if (!BusinessConstants.SUCCESS.equals(result)) {
        return new MessageBox("0", result);
      }
      ShiroUser shiroUser = currentUser();
      MemberVo memberVo = new MemberVo();
      memberVo.setId(shiroUser.getUserId());

      mobileApproService.packageMobileApproCode(memberVo, mobile, activeCode, request);
      // 删除验证原有手机号码session
      session.removeAttribute(BusinessConstants.MOBILE_APPRO_RESET_FUNCTION + member.getId());
    } catch (Exception e) {
      result = "发送验证码出错,请联系客服";
      e.printStackTrace();
    }
    if (!BusinessConstants.SUCCESS.equals(result)) {
      return new MessageBox("0", result);
    }
    return new MessageBox("1", "发送成功,请注意查收");
  }
 /**
  * Description:根据当前用户的手机号发送验证码<br>
  *
  * @author justin.xu
  * @version 0.1 2014年4月30日
  * @param request
  * @param session
  * @return String
  */
 @RequestMapping(value = "sendResetMsg")
 @ResponseBody
 public MessageBox sendResetMsg(HttpServletRequest request, HttpSession session) {
   String result = BusinessConstants.SUCCESS;
   try {
     ShiroUser shiroUser = currentUser();
     MobileApproVo mobileApproVo =
         mobileApproService.queryMobileApproByUserId(shiroUser.getUserId());
     result =
         mobileApproService.sendMobileApprValidate(
             mobileApproVo.getMobileNum(),
             request,
             shiroUser.getUserName(),
             BusinessConstants.MOBILE_APPRO_RESET_FUNCTION,
             BusinessConstants.SMS_TEMPLATE_TYPE_VERIFY_MOBILE_CODE);
   } catch (Exception e) {
     result = "网络连接异常,请刷新页面或稍后重试!";
     e.printStackTrace();
   }
   if (BusinessConstants.SUCCESS.equals(result)) {
     return new MessageBox("1", "验证码发送成功,请查收手机短信。");
   }
   return new MessageBox("0", result);
 }
  /**
   * Description:进行手机认证<br>
   *
   * @author hujianpan
   * @version 0.1 2014年8月30日
   * @param request
   * @param session
   * @param mobile 手机号
   * @param activeCode 验证码
   * @return String
   */
  @RequestMapping(
      value = "/jsonpVerificationMobail",
      produces = {"text/plain;charset=UTF-8"})
  @ResponseBody
  public String verificationMobailActiveCodeJsonp(
      HttpServletRequest request, HttpSession session, String jsonpcallback) {
    MessageBox box = null;
    String result = BusinessConstants.SUCCESS;
    Member member = currentMember();
    String userNameParam = "@@@@";
    String mobileParam = request.getParameter("mobile");
    String activeCodeParam = request.getParameter("activeCode");
    if (member != null) {
      userNameParam = member.getUsername();
      if (StringUtils.isEmpty(userNameParam)) {
        box = new MessageBox("0", "用户名不能为空");
        return jsonpcallback + "(" + JsonUtils.bean2Json(box) + ")";
      }
    } else {
      box = new MessageBox("0", "注册信息丢失,请重新登入或注册");
      return jsonpcallback + "(" + JsonUtils.bean2Json(box) + ")";
    }

    if (StringUtils.isEmpty(mobileParam)) {
      box = new MessageBox("0", "用户手机号不能为空");
      return jsonpcallback + "(" + JsonUtils.bean2Json(box) + ")";
    }
    if (StringUtils.isEmpty(activeCodeParam)) {

      box = new MessageBox("0", "手机验证码不能为空");
      return jsonpcallback + "(" + JsonUtils.bean2Json(box) + ")";
    }

    result =
        mobileApproService.verificationMobailActiveCode(
            request, member, mobileParam, activeCodeParam);
    if (!BusinessConstants.SUCCESS.equals(result)) {
      box = new MessageBox("0", result);
      return jsonpcallback + "(" + JsonUtils.bean2Json(box) + ")";
    }

    box = new MessageBox("1", "验证成功");
    return jsonpcallback + "(" + JsonUtils.bean2Json(box) + ")";
  }
  /**
   * Description:注册-验证账户信息-发送手机认证验证码<br>
   *
   * @author hujianpan
   * @version 0.1 2014年8月30日
   * @param request
   * @param session
   * @return String
   */
  @RequestMapping(value = "/sendMobailActiveByMessage")
  @ResponseBody
  public MessageBox sendMobailActiveByMessage(HttpServletRequest request, HttpSession session) {
    try {
      String result = BusinessConstants.SUCCESS;
      Member member = currentMember();

      if (null == member || StringUtils.isEmpty(member.getUsername())) {
        return new MessageBox("0", "未关联到要绑定的用户!");
      }
      result = mobileApproService.sendMobailMessageActiveCode(request, member);
      if (!BusinessConstants.SUCCESS.equals(result)) {
        return new MessageBox("0", result);
      }
      return new MessageBox("1", "发送成功,请注意查收");
    } catch (Exception e) {
      logger.error("注册-验证账户信息-发送手机认证验证码出错", e);
      return new MessageBox("0", "发送异常");
    }
  }
  /**
   * Description:发送手机认证验证码<br>
   *
   * @author justin.xu
   * @version 0.1 2014年4月29日
   * @param request
   * @param session
   * @return String
   */
  @RequestMapping(value = "activeMobileAndSendMsg")
  @ResponseBody
  public String activeMobileAndSendMsg(
      HttpServletRequest request, HttpSession session, String mobile, String activeCode) {
    String result = "success";
    try {
      ShiroUser shiroUser = currentUser();
      result =
          mobileApproService.sendMobileApprValidate(
              mobile, request, shiroUser.getUserName(), BusinessConstants.MOBILE_APPRO_FUNCTION);
      MemberVo memberVo = new MemberVo();
      memberVo.setId(shiroUser.getUserId());

      // mobileApproService.packageMobileApproCode(memberVo, mobile,
      // activeCode, request);
    } catch (Exception e) {
      result = "网络连接异常,请刷新页面或稍后重试!";
      e.printStackTrace();
    }
    return result;
  }
 /**
  * Description:手机短信验证码发送<br>
  *
  * @author yubin
  * @version 0.1 2015年9月18日
  * @return MessageBox
  */
 @RequestMapping(value = "/sendMobileCode")
 public @ResponseBody MessageBox sendMobileCode() {
   String result = "success";
   try {
     String mobileNum = currentRequest().getParameter("mobileNum");
     if (null != mobileNum && !"".equals(mobileNum.trim())) {
       // 验证码发送
       result =
           mobileApproService.sendMobileApprValidate(
               mobileNum,
               currentRequest(),
               "",
               BusinessConstants.MOBILE_APPRO_FUNCTION,
               BusinessConstants.SMS_TEMPLATE_TYPE_WX_SAFECENTER_MOBILEVALIDATE);
     }
   } catch (Exception e) {
     result = "failer";
     logger.error("网络异常");
   }
   if (!BusinessConstants.SUCCESS.equals(result)) {
     return new MessageBox("0", result);
   }
   return new MessageBox("1", result);
 }
  /**
   * Description:用户注册手机验证<br>
   *
   * @author yubin
   * @version 0.1 2015年9月18日
   * @param member
   * @param request
   * @param session
   * @return MessageBox
   */
  @RequestMapping(value = "/registVerify")
  @ResponseBody
  public MessageBox registVerify(Member member, HttpServletRequest request, HttpSession session) {
    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");
      // 验证推荐人
      if (null != inviterName && !"".equals(inviterName.trim())) {
        MemberRegisterCnd inviterNameCnd = new MemberRegisterCnd();
        inviterNameCnd.setInviterName(inviterName);
        String flag = memberRegisterService.queryInviterName(inviterNameCnd);
        if (!flag.equals(BusinessConstants.SUCCESS)) {
          return new MessageBox("0", flag);
        }
        List<MemberRegisterVo> inviterNameList =
            memberRegisterMapper.queryinviterList(inviterNameCnd);
        // 设置推荐人
        member.setInviterid(inviterNameList.get(0).getId());
        // 校验红包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", "尊敬的用户,您好!推荐您注册的红包不存在");
          }
        }
      }
      if (!"success".equals(result)) {
        return new MessageBox("0", result);
      } else {
        // 发送短信验证码
        System.out.println("发送短信验证码====");
        mobileApproService.sendMobileApprValidate(
            member.getMobileNum(),
            request,
            "",
            BusinessConstants.MOBILE_APPRO_FUNCTION,
            BusinessConstants.SMS_TEMPLATE_TYPE_WX_SAFECENTER_MOBILEVALIDATE);
        return new MessageBox("1", result);
      }

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