<?php
	session_start();
	
	ini_set('session.gc_maxlifetime', 99999999); //设置时间 永不过期
	
	require_once("db/db.php");	
	require_once("common/common.php");
	
	$strwxudid= trim($_SESSION["main_wx_userwxudid"]);
	
	$chainno="";
	$userno=$_REQUEST["userno"];
	$pswd=$_REQUEST["pwd"];
	$pswdnew='';
	
	if($pswd=='[x*x*w*]')
	{
		//不用判断密码
		$sql=sprintf("SELECT User_Password FROM User_QuanXian WHERE Use_No='%s'",$userno);
			
		$result = querySql($sql);
		
		if($row = fetch_array($result))
		{
			$pswdnew=$row['User_Password'];
		}
		freeResult($result);
	}
	else
	{
		$pswdnew=getpwd($pswd);	
	}
	
	$_SESSION["main_wx_userlogined"]="0";
	
	if($userno=="")
	{
		echo "<script>window.location.href='loginwx.php?rset=11&stext=';</script>";
		exit();
	}
		
	//开始搜索数据库是否可以登录
	//$sql = sprintf("select [dbo].[TRF_AppUserLoginGL](GETDATE(),'%s','%s') as res;", $userno, $pswdnew);
	$sql = sprintf("SELECT COUNT(Use_No)AS RetCount FROM User_QuanXian WHERE Use_No='%s' And User_Password='%s'",$userno, $pswdnew);
	$result = querySql($sql);
	$row = fetch_array($result);
	if($row['RetCount']>0)
	{
		//登录成功
		$_SESSION["main_wx_userno"]=$userno;		//当前登录的用户编号
		$_SESSION["main_wx_username"]='';			//当前登录的用户姓名
		$_SESSION["main_wx_userpswd"]=$pswd;		//当前登录的用户密码
		$_SESSION["main_wx_userlogined"]='1';		//当前用户是否登录
		$_SESSION["main_wx_chainno"]='';			//当前用户是否登录
		$_SESSION["main_wx_chainname"]='';			//当前用户是否登录
		$_SESSION["main_wx_chaincount"]='0';
				
		freeResult($result);
		
		$intRowCount=0;
		//得到该用户可以访问的分店信息
		$sql=sprintf("SELECT TOP 2 a.Chain_No,a.Chain_Name FROM Chain_Infor a,dbo.TRF_AppGetChainFromUser('%s') b WHERE a.Chain_No=b.Chain_No",$userno);
			
		$result = querySql($sql);
		
		while($row = fetch_array($result))
		{
			if($intRowCount==0)
			{
				$_SESSION['main_wx_chainno'] = $row['Chain_No'];  
				$_SESSION['main_wx_chainname'] = $row['Chain_Name']; 
			}
			
			$intRowCount=$intRowCount +1;
		}
		
				
		//判断是否有多条记录
		if($intRowCount>1)
		{
			$_SESSION['main_wx_chaincount'] ="2";
		}
		else
		{
			$_SESSION['main_wx_chaincount'] ="1";
		}

		//添加微信记录
		if(strlen($strwxudid)>20)
		{
			$sql= sprintf("UPDATE User_QuanXian SET wxudid='%s' WHERE Use_No='%s'",$strwxudid,$userno);
			executeSql($sql);
			
			//取消其他用户的邦定
			$sql= sprintf("UPDATE User_QuanXian SET wxudid='' WHERE Use_No<>'%s' And wxudid='%s'",$userno,$strwxudid);
			executeSql($sql);
		}
		
		//echo $intRowCount;
		echo "<script>window.location.href='indexm.php?flpid=1';</script>";
		
		freeResult($result);
		closeDb();
		exit();
	}
	else
	{					
		if($row['res'] == 0)
		{	//返回0无此工号,1登录成功,2密码错误,3无此权限
			$strmsg="登录失败,无此用户号.";
		}
		elseif($row['res'] == 2)
		{	//返回0无此工号,1登录成功,2密码错误,3无此权限
			$strmsg="登录失败,密码错误.";
		}
		elseif($row['res'] == 3)
		{	//返回0无此工号,1登录成功,2密码错误,3无此权限
			
			$strmsg="登录失败,您没有权限.";
		}
		else
		{
			$strmsg="登录失败！";
		}
		echo "<script>window.location.href='loginwx.php?rset=11&stext=".$strmsg."';</script>";
	}				
	//释放数据库
	freeResult($result);
	closeDb();
?>


