<%@ page contentType="text/html;charset=gbk" %>
<%@ 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.*" %>
<%//@ page import="com.dynamicode.invoked.Standard" %>
<%//@ page import="com.dynamicode.parsereturncode.ParseReturnCode" %>
<head>
<title><%=net.sz600.check.SzHasp.getInstance().getCustomerName()%>协同网络办公平台</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<%
	//验证码
	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("验证码不正确"));
		}
	}


    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");
/*
    		//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);
	  	  	////绑定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);
    		}	  		
	        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);	
						
	    		}
	      }	  	  

	      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"));
        }
         
          SzLog.getInstance().loginLog(szLoginInfo.getDLID(), szLoginInfo.getDLXM(), sIp, COMPANY );
        } else {
          flag = false;
        }
    }
    if (!flag) {
    	SzLog.getInstance().loginFailLog(0, LoginName, sIp, COMPANY );
	String sMessage = SzOut.getMessage();
        out.println(SzPageHelp.jsAlertBack(sMessage));
    }

%>