<?php
	session_start();
	ini_set('session.gc_maxlifetime', 99999999); //设置时间 永不过期
	
	header("Content-type: text/html; charset=gb2312"); 
	
	$pageid=$_REQUEST["pgid"];
	$strwxid=trim($_SESSION["main_wxudid"]);
	
	require_once("db/db.php");	
	require_once("common/common.php");
	
	$_SESSION["main_vipno"]="";
	$_SESSION["main_vipname"]="";
	$_SESSION["main_vipsex"]="";
	$_SESSION["main_vippswd"]="";
	$_SESSION["main_viptype"]="";
	$_SESSION["main_vipstate"]="";
	$_SESSION["main_viptelno"]="";
	$_SESSION["main_vipborth"]="1900-01-01";
	$_SESSION["main_vipidtype"]="";
	$_SESSION["main_vipidno"]="";
	$_SESSION["main_vipVIPNo"]="";
	$_SESSION["main_vipVIPState"]="";
	$_SESSION["main_viplogined"]="0";	//登录成功
	$_SESSION["main_defvipnoflag"]="0";
	$_SESSION["main_vipwxudid"]="";	//微信号
					
	$chainno="";
	$userno=$_REQUEST["userno"];
	$pswd=$_REQUEST["pwd"];
	$userno=strtoupper($userno);
	$telno="**--**132";
	if(strlen($userno)>10)
	{
		$telno=$userno;
	}
	if(strlen($userno)<1)
	{
		echo "<script>window.location.href='login.php?stext=&pgid=".$pageid."';</script>";
		closeDb();
		exit();
	}
	//=============================================================//
	//=============================================================//
	$intNextFlag=0;
	
	//开始搜索数据库是否可以登录 SELECT convert(varchar(10),getdate(),120)
	$sql = sprintf("SELECT a.VIP_No,a.Sub_No,a.Sub_Name,a.Sub_Sex,a.Sub_Pswd,a.Sub_State,b.VIP_Type,b.VIP_State,a.Sub_TelNo,convert(varchar(10),Sub_Borth,120) as VIPBorth,
					a.Sub_IDType,a.Sub_IDNo,a.wx_no FROM VIP_Item a,VIP_Main b WHERE a.VIP_No=b.VIP_No AND (a.Sub_No='%s' or a.Sub_TelNo='%s');",$userno,$telno);
	
	$result = querySql($sql);
	if($row = fetch_array($result))
	{
		//如果有会员号
		$mpswd = $row['Sub_Pswd']; 
		$mpswd = trim($mpswd);
		//判断密码是否一样
		if($mpswd==$pswd){
			//如果密码正确 VIP_No    VIP_State
			//=============================================================//
			$_SESSION["main_vipno"]=$row['Sub_No'];
			$_SESSION["main_vipname"]=$row['Sub_Name'];
			$_SESSION["main_vipsex"]=$row['Sub_Sex'];
			$_SESSION["main_vippswd"]=$row['Sub_Pswd'];
			$_SESSION["main_viptype"]=$row['VIP_Type'];
			$_SESSION["main_vipstate"]=$row['Sub_State'];
			$_SESSION["main_viptelno"]=$row['Sub_TelNo'];
			$_SESSION["main_vipborth"]=$row['VIPBorth'];
			$_SESSION["main_vipidtype"]=$row['Sub_IDType'];
			$_SESSION["main_vipidno"]=$row['Sub_IDNo'];
			$_SESSION["main_vipVIPNo"]=$row['VIP_No'];
			$_SESSION["main_vipVIPState"]=$row['VIP_State'];
			$_SESSION["main_viplogined"]="1";	//登录成功
			$_SESSION["main_defvipnoflag"]="1";
			$_SESSION["main_vipwxudid"]=$row['wx_no'];
			
			//=============================================================//
			if($_SESSION["main_vipstate"]=='在册')	//跳转到首页
				$intNextFlag=1;
			else
				$intNextFlag=4;		//会员已挂失或注销
		}
		else
		{
			$intNextFlag=2;
		}
	}
	else
	{	//会员号不存在
		$intNextFlag=3;
	}
	//释放数据库
	freeResult($result);
	
	//保存威信到会员资料
	if($intNextFlag==1)
	{
		$sql = sprintf("UPDATE VIP_Item SET wx_no='%s' WHERE Sub_No='%s'",$strwxid,$_SESSION["main_vipno"]);
		$result = querySql($sql);
	}
	closeDb();
	
	//执行跳转指令
	if($intNextFlag==1)
	{
		if($pageid=='0')
		{
			echo "<script>window.location.href='orderlist.php?bstat=0';</script>";
		}
		elseif($pageid=='1')
		{
			echo "<script>window.location.href='orderlist.php?bstat=1';</script>";
		}
		elseif($pageid=='2')
		{
			echo "<script>window.location.href='orderlist.php?bstat=2';</script>";
		}
		elseif($pageid=='3')
		{
			echo "<script>window.location.href='orderlist.php?bstat=3';</script>";
		}
		elseif($pageid=='4')
		{
			echo "<script>window.location.href='orderlist.php?bstat=4';</script>";
		}
		elseif($pageid=='5')
		{
			echo "<script>window.location.href='orderpaysel.php?selbillno=".$_SESSION["temp_wxbill_billno"]."';</script>";
		}
		else
		{
			echo "<script>window.location.href='vipmain.php';</script>";
		}
	}
	elseif($intNextFlag==2)
	{
		//登录失败,密码错误
		echo "<script>window.location.href='login.php?stext=12&pgid=".$pageid."';</script>";
	}
	elseif($intNextFlag==3)
	{	//登录失败,无此用户号
		echo "<script>window.location.href='login.php?stext=13&pgid=".$pageid."';</script>";
	}
	elseif($intNextFlag==4)
	{	//登录失败,无此用户号
		echo "<script>window.location.href='login.php?stext=16&pgid=".$pageid."';</script>";
	}
	else
	{
		echo "<script>window.location.href='login.php?stext=14&pgid=".$pageid."';</script>";
	}
?>



