<?php
	session_start();
	include "wechat.class.php";
	include 'errCode.php';
	require_once("../Wx.Config.php");
			
	$opt = array(
		'appsecret'=>WxMainConfig::APPSECRET,//填写高级调用功能的密钥
		'appid'=>WxMainConfig::APPID	//填写高级调用功能的appid
	);
	$we = new Wechat($opt);
	$auth = $we->checkAuth();
	//var_dump($auth);
	$js_ticket = $we->getJsTicket();
	if (!$js_ticket) {
		echo "获取js_ticket失败！<br>";
		echo '错误码：'.$we->errCode;
		echo ' 错误原因：'.ErrCode::getErrText($we->errCode);
		//echo "dddddddddddddddd";
		exit;
	}
	$url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	$url= str_ireplace('172.16.14.191:8081','ty112.com',$url);
	
	$js_sign = $we->getJsSign($url);
	//var_dump($js_sign);
	//exit();

?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="gb2312">	
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
	<meta name="apple-mobile-web-app-capable" content="1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="format-detection" content="telephone=0">
	<meta name="apple-touch-fullscreen" content="1">
	
	<title>微信点单</title>
	
	<link rel="stylesheet" href="jsp4/jquery.mobile-1.4.3.min.css">
	<script src="jsp4/jquery.js"></script>
	<script src="jsp4/jquery.mobile-1.4.3.min.js"></script>
	
	
	<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
	
	
</head>

<script>	
		//隐藏维信右上角菜单
		function onBridgeReady(){
			WeixinJSBridge.call('hideOptionMenu');
		}

		if (typeof WeixinJSBridge == "undefined")
		{
			if( document.addEventListener )
			{
				document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
			}
			else if (document.attachEvent)
			{
				document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
				document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
			}
		}
		else
		{
			onBridgeReady();
		}
</script>

<script language="JavaScript"> 
		//不允许进行后退
		javascript:window.history.forward(0); 
	</script>
	
<script>
	//禁止页面上下滑动,整个页面都不能滑动了
	document.addEventListener('touchmove', function(e) {
		e.preventDefault();
	});
</script>


	
<body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0">
<?php 
	$strselBillNo=trim($_REQUEST['selbillno']);
	$strNowDListNo=trim($_SESSION["main_posChainDListNo"]);
?>

<section data-role="page" id="pageItemList">

	<article data-role="content">
		<table width="100%" border="0" cellpadding="0" cellspacing="0" >
			<tr><td height="50" colspan="3"></td></tr>
			<tr>
				<td height="26" width="10"></td>
				<td height="26" align="left">
					<div id="SubDiv1" style="width:100%;height:100%;text-align:center;padding-top:0px;border:0px solid #fff;">
						<span style="color: #000;font-size:18px;text-shadow:none;">正在加载扫码设备。。。</span>
					</div>
				</td>
				<td height="26" width="10"></td>
			</tr>
			<tr><td height="20" colspan="3"></td></tr>
			<tr>
				<td height="50" width="10"></td>
				<td height="50" align="center">
					<!--扫描入账 -->
					<div id="ScaleEnable" onclick="SubClick()" style="width:95px;height:48px;top:0px;left:0px;">
						<img src="images/saomiao4.png" style="width:100%;height:100%"/>
					</div>
					
				</td>
				<td height="50" width="10"></td>
			</tr>
			
			<tr><td height="20" colspan="3"></td></tr>
			
			<tr>
				<td height="15" width="5"></td>
				<td height="15" align="center">
					<div data-rel="back" onClick="bmtReturnPage()" style="border-radius:5px;width:80%; height:38px; background-color:#D7AA28;">
                        <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
                            <tr><td width="100%" height="100%" align="center">
                                <span style="color:#fff ;font: 12px/1.5 '微软雅黑',tahoma, Srial, helvetica, sans-serif; font-size:16px;text-shadow:none;">返 回</span>                            	
                            </td></tr>
                        </table>
                    </div>
				</td>
				<td height="15" width="5"></td>
			</tr>
			
		</table>

	</article>

</section>


<script type="text/javascript">
	var intHadLinkFlag=0;
	var strSelBarCode;
	var strGetItemNo;
	var strGetItemName;
	strSelBarCode="";
	strGetItemNo="";
	strGetItemName="";
	
	//alert('<?php echo $url;?>');
	wx.config({
			debug: false,
			appId: '<?php echo $js_sign['appid']; ?>', // 必填，公众号的唯一标识
			timestamp: <?php echo $js_sign['timestamp']; ?>, // 必填，生成签名的时间戳，切记时间戳是整数型，别加引号
			nonceStr: '<?php echo $js_sign['noncestr']; ?>', // 必填，生成签名的随机串
			signature: '<?php echo $js_sign['signature']; ?>', // 必填，签名，见附录1
			jsApiList: [
				'checkJsApi',
				'onMenuShareTimeline',
				'onMenuShareAppMessage',
				'onMenuShareQQ',
				'onMenuShareWeibo',
				'hideMenuItems',
				'showMenuItems',
				'hideAllNonBaseMenuItem',
				'showAllNonBaseMenuItem',
				'translateVoice',
				'startRecord',
				'stopRecord',
				'onRecordEnd',
				'playVoice',
				'pauseVoice',
				'stopVoice',
				'uploadVoice',
				'downloadVoice',
				'chooseImage',
				'previewImage',
				'uploadImage',
				'downloadImage',
				'getNetworkType',
				'openLocation',
				'getLocation',
				'hideOptionMenu',
				'showOptionMenu',
				'closeWindow',
				'scanQRCode',
				'chooseWXPay',
				'openProductSpecificView',
				'addCard',
				'chooseCard',
				'openCard'
			  ]
	});
	
	wx.ready(
		function () {
			// 在这里调用 API
			//alert(1);
			wx.checkJsApi(
			{
				jsApiList: ['scanQRCode'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
				success: function(res) 
				{
					// 以键值对的形式返回，可用的api值true，不可用为false
					// 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
					//alert(JSON.stringify(res)); //如果调用成功显示消息
					SetBarAPIState(1);
					scaleCodePage();	//自动启动扫码
					intHadLinkFlag=1;
				},
				fail: function (res)
				{
					SetBarAPIState(0);
				}
			});
		}		
	);
			
		function scaleCodePage()
		{
			wx.scanQRCode(
			{
				needResult: 1,
				scanType: ["qrCode"],
				desc: 'scanQRCode desc',
				success: function (res) {
					//alert(JSON.stringify(res));
					//alert(res.resultStr);
					//localStorage["wx"] = res.resultStr;
					if(res.resultStr=="")
					{
						SetBarAPIState(3);	//扫码失败
					}
					else
					{
						//显示扫码信息
						//alert(res.resultStr);
						strSelBarCode=res.resultStr;
						SetBarAPIState(2);	//扫码成功
					}
				},
				cancel: function (res) 
				{
					SetBarAPIState(4);	//取消
				},
				fail: function (res) 
				{
					//alert("扫码失败");
					SetBarAPIState(3);	//扫码失败
				}
			});
		}
		
		function SubClick()
		{
			if(intHadLinkFlag>0)
			{
				scaleCodePage();
			}
		}
		
		
		function SetBarAPIState(intState)
		{
			if(intState==0)
			{
				//加载失败
				$("#SubDiv1").html('<span style="color: #000;font-size:18px;text-shadow:none;">加载扫码接口失败！！</span>');
				bmtReturnPage();
			}
			else if(intState==1)
			{
				//加载成功
				$("#SubDiv1").html('<span style="color: #000;font-size:18px;text-shadow:none;">正在加载扫码设备。。。</span>');
			}
			else if(intState==2)
			{
				//扫码成功
				$("#SubDiv1").html('<span style="color: #000;font-size:18px;text-shadow:none;">扫码成功！</span>');
				
				//如果二维码是URL: https://ty112.com/weixin/posscana.php?dlistid=1&tableno=102
				var intPos= strSelBarCode.indexOf("dlistid=");
				if(intPos>0)
				{
					var strNewBarCode=strSelBarCode.substring(intPos+8);
					//alert(strNewBarCode);
					strSelBarCode=strNewBarCode.replace("&tableno=", "*");
					
				}
				//alert(strSelBarCode);
				
				//获取该条码的项目
				//window.location="ordermodify.php?selbillno=<?php echo $strselBillNo;?>";
				//开始更新桌台信息
				//桌台二维码格式  2*TableNo
				var intpos;
				intpos = strSelBarCode.indexOf("*");
				if( intpos > 0 )
				{
					var strnWebdlistno;
					var strTableNo;
					var strDListNo;	
					strnWebdlistno = '<?php echo $strNowDListNo;?>';
					strDListNo=strSelBarCode.substring(0,intpos);
					strTableNo=strSelBarCode.substring(intpos+1);
					//alert(strDListNo);
					//alert(strnWebdlistno);
					
					if(strnWebdlistno==strDListNo)
					{
						//用ajax的“同步方式”调用一般处理程序
						$.ajax({
							url: "ordersavetable.php?selbillno=<?php echo $strselBillNo;?>&DList=" + strDListNo + "&TabNo=" +strTableNo,
							async: false,//改为同步方式
							type: "POST",
							//data: {doid:4, rmid: "<?php echo $roomno;?>" },
							success: function (RetData) 
							{
								var strret;
								strret = RetData;
								strret=strret.trim();
								//alert(strret);
								if(strret=='*')
								{
									bmtReturnPage();
								}
								else
								{
									$("#SubDiv1").html('<span style="color: #000;font-size:18px;text-shadow:none;">二维码信息不正确！</span>');
								}
							}
						});
					}
					else
					{
						$("#SubDiv1").html('<span style="color: #000;font-size:18px;text-shadow:none;">二维码信息不正确！</span>');
					}
				}
				else
				{
					$("#SubDiv1").html('<span style="color: #000;font-size:18px;text-shadow:none;">二维码信息不正确！</span>');
				}		
				
								
			}
			else if(intState==3)
			{
				//扫码失败
				$("#SubDiv1").html('<span style="color: #000;font-size:18px;text-shadow:none;">  扫码失败！</span>');
				bmtReturnPage();
			}
			else if(intState==4)
			{
				//扫码失败
				$("#SubDiv1").html('<span style="color: #000;font-size:18px;text-shadow:none;"> 扫码用户取消！</span>');
				bmtReturnPage();
			}
		}
		
	</script>
	
	 <script type="text/javascript">
	 	
		function bmtReturnPage()
		{
			window.location="ordermodify.php?selbillno=<?php echo $strselBillNo;?>";
		};
	</script>
	
</body>
</html>
