<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.*" %>
<%@ page import="net.sz600.database.*" %>
<%@ page import="net.sz600.tool.SzConvert" %>
<%@ page import="net.sz600.system.SzPageHelp" %>
<%@ page import="net.sz600.system.SzOut" %>
<%@ page import="net.sz600.system.SzProperties" %>
<%@ page import="net.sz600.organize.SzUserSet" %>
<%@ page import="net.sz600.organize.SzLogin" %>
<%@ page import="net.sz600.organize.SzLoginInfo" %>
<%@ page import="net.sz600.organize.SzUserHelp" %>
<%@ page import="net.sz600.system.SzLog" %>
<%@ page import="net.sz600.online.*" %>
<%
	String sMessage = "";
	//验证码
	if (SzProperties.needVerificationCode(request.getRemoteHost())){
		String SzVerificationCode = SzConvert.toSpaceStr((String)session.getAttribute("SzVerificationCode"));
		String VerificationCode = SzConvert.toSpaceStr(request.getParameter("txtVerificationCode"));
	   	if (!SzVerificationCode.equals(VerificationCode)) {
	     //out.println(SzPageHelp.jsAlertBack("验证码不正确"));
	     sMessage = "验证码不正确";
		}
	}

    SzDataWriter dataWriter = new SzDataWriter();
    dataWriter.update("delete from SZXT_DL where DLID NOT IN (select YGDL from SZXT_YG)");
    dataWriter.update("delete from SZXT_YG where YGDL NOT IN (select DLID FROM SZXT_DL)");
    dataWriter.close();

    String skin = SzConvert.strWriteData(request.getParameter("skin"));
    String skinType = SzConvert.toSpaceStr(request.getParameter("skinType")); //界面信息 
    String LoginName = SzConvert.strWriteData(request.getParameter("txtLoginName"));
    String LoginPassword = request.getParameter("txtLoginPassword");
    String LoginPassword2 = request.getParameter("txtLoginPassword2");
    //System.out.println("LoginName=" + LoginName + "==LoginPassword==" + LoginPassword);
/*
    		//2011-3-22加入密码验证
		List<HashMap<String, String>> serverList = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> server1 = new HashMap<String, String>();
		server1.put("host", "localhost");
		server1.put("port", "2008");
		server1.put("key", "20080808");
		serverList.add(server1);

		Standard standard = new Standard(serverList);

		//验证密码
		ParseReturnCode returnMsg = standard. CheckDynamicPwdByUser ("zh_CN",57, LoginName, LoginPassword2);
		if (!returnMsg.getRetcode().equals("0")) {
			out.println(SzPageHelp.jsAlertBack("动态密码不正确"));
			return;
		}
*/
    String COMPANY = SzConvert.toSpaceStr(request.getParameter("COMPANY")); //分公司信息    

    //验证是否正式用户
    SzLogin szLogin = new SzLogin();
    if (LoginName.indexOf("?") >= 0) {
    	LoginName = request.getParameter("txtLoginName");
    }
    //记录访问日志
    String sIp = SzConvert.toSpaceStr( request.getRemoteHost());   
    //2013-06-08增加ip校验
    boolean flag = szLogin.isSystemUser(LoginName, LoginPassword, COMPANY);
  
    
    if (flag) {    	
        //登陆成功
        SzLoginInfo szLoginInfo = (SzLoginInfo)szLogin.getInfo();
        if (szLoginInfo != null) {
          //设置皮肤
          if (!skin.equals("")) {
              SzUserSet userSet = new SzUserSet();
              userSet.setSkin(szLoginInfo.getDLID(), skin, skinType);  		
          }  
          session.setMaxInactiveInterval(-1);	//设置为永不断线，2010-06-30
          session.setAttribute("SzDLID", Integer.toString(szLoginInfo.getDLID()));
          session.setAttribute("SzDLZH", LoginName);
          session.setAttribute("SzDLMM", LoginPassword);
          session.setAttribute("SzDLXM", szLoginInfo.getDLXM());
          session.setAttribute("SzIP", sIp);          
          session.setAttribute("SzCOMPANY", COMPANY);
          session.setAttribute("SzLoginInfo", szLoginInfo);
          session.setAttribute("SzSkinType", skinType);

          session.setAttribute("SzBMID", Integer.toString(szLoginInfo.getDLBM())); 
          session.setAttribute("SzBMMC", SzUserHelp.getDepartName(szLoginInfo.getDLBM()));
    	  session.setAttribute("SzZWMC", SzUserHelp.getPostName(szLoginInfo.getDLZW()));
          session.setAttribute("SzYGBH", szLoginInfo.getYGBH());
          session.setAttribute("SzYGYX", szLoginInfo.getYGYX());
	
          //2014-06增加：主要用来区别是否显示控件
          session.setAttribute("SzNavigator", SzConvert.toSpaceStr(request.getParameter("navigator")));
	  ////绑定listener 2008-09-19
	  session.setAttribute(Integer.toString(szLoginInfo.getDLID()), net.sz600.online.OlUserSession.getInstance());
	  	  
	      //增加用户Cookie
	      if (!LoginName.equals("")) {
	      	//SzLoginCookieDepart
	  		//SzLoginCookieMessage
	  		//SzLoginCookie
	  		//获得部门名称
	  		int iBMID_SESSION = szLoginInfo.getDLBM();
	      	String sBMMC_SESSION = "";
    		if (iBMID_SESSION != 0) {
       			sBMMC_SESSION = SzUserHelp.getDepartName(iBMID_SESSION);
    		}	
                //2014-05-30增加登陆ID
	        Cookie szLoginCookieId =new Cookie("SzLoginCookieId", Integer.toString(szLoginInfo.getDLID()));  		
	      	szLoginCookieId.setMaxAge(60*60*24*30*12);
	      	szLoginCookieId.setPath("/");       
	      	response.addCookie(szLoginCookieId);

	        Cookie szLoginCookieDepart=new Cookie("SzLoginCookieDepart", java.net.URLEncoder.encode(sBMMC_SESSION,"GBK"));
	      	Cookie szLoginCookie=new Cookie("SzLoginCookie", java.net.URLEncoder.encode(LoginName,"GBK"));
	      	//Cookie保留时间 缺省为一年    	

	      	szLoginCookie.setMaxAge(60*60*24*30*12);
	      	szLoginCookie.setPath("/");       
	      	response.addCookie(szLoginCookie);
	      	
	      	szLoginCookieDepart.setMaxAge(60*60*24*30*12);
	      	szLoginCookieDepart.setPath("/");       
	      	response.addCookie(szLoginCookieDepart);	   

	        int iCount = OlUserSession.getInstance().refresh(szLoginInfo.getDLID());
	        
	        //更新cookie
	        Cookie szLoginCookieMessage = new Cookie("SzLoginCookieMessage", Integer.toString(iCount));
	        szLoginCookieMessage.setMaxAge(60*60*24*30*12);
	        szLoginCookieMessage.setPath("/");       
	        response.addCookie(szLoginCookieMessage);	    
	        
	        //在线SzOnlineCookie
	        Cookie szOnlineCookie = new Cookie("SzOnlineCookie", "1");
	        szOnlineCookie.setMaxAge(0);
	        szOnlineCookie.setPath("/");       
	        response.addCookie(szOnlineCookie);	
	        
	        
	    		Cookie[] cks =request.getCookies();
	    		String temp_cookie = null;
	    		boolean hasOpenTempCookie = false;
	    		String loginTime = "1";
	    		session.setAttribute("SzGuideSession", "1");
	    		if(cks!=null) {
	        	for(int icks = 0; icks < cks.length; icks++)   {
	              temp_cookie = cks[icks].getName();
	              if(temp_cookie.equals("SzGuideCookie" + Integer.toString(szLoginInfo.getDLID()))) {
	                  if (cks[icks].getValue().equals("1")) {
	                	  loginTime = "2";	                	  
	                  } else if (cks[icks].getValue().equals("2")) {
	                	  loginTime = "3";
	                  }
	              }
	        	}
	    		}
	    		if (loginTime.equals("1") || loginTime.equals("2")) {
						Cookie szGuideCookie = new Cookie("SzGuideCookie" + Integer.toString(szLoginInfo.getDLID()), loginTime);
						//Cookie保留时间 缺省为一年    	
						szGuideCookie.setMaxAge(60*60*24*30*12);
						szGuideCookie.setPath("/");       
						response.addCookie(szGuideCookie);	
						
	    		}
	      }	  	  
	      SzLog.getInstance().loginLog(szLoginInfo.getDLID(), szLoginInfo.getDLXM(), sIp, COMPANY );
	      /*
	      if (skinType.equals("2")) {
            		out.println(SzPageHelp.jsGoUrl("main/2index.jsp"));
	      } else if (skinType.equals("3")) {
            		out.println(SzPageHelp.jsGoUrl("main/3index.jsp")); 
	      } else {
            		out.println(SzPageHelp.jsGoUrl("main/index.jsp"));
        }
	      */
         
          
        } else {
        	//if (szLoginInfo != null) {
          flag = false;
        }
    }
    if (!flag) {
    	SzLog.getInstance().loginFailLog(0, LoginName, sIp, COMPANY );
			sMessage = SzOut.getMessage();
      //out.println(SzPageHelp.jsAlertBack(sMessage));
    }
  //out.clear()
	//out.println(sMessage);

	//out.clear(); 
	//out = pageContext.pushBody();   
	out.clearBuffer(); 
	out.print(sMessage);%>
