 //localStorage.removeItem("mpInfo");
//alert("-----");
function GetQueryString(name) { //获取URL参数值
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null)
			return unescape(r[2]);
		return null;
	}
	var daySeconds = 86400000; //一天的秒数
	var appNumber = GetQueryString("accountsnumber"); //公众号ID
	var appid = GetQueryString("accountsid"); //公众号ID
	// alert(appid);
	//http://stevenlisw.imwork.net/cfwechat/mobile/oauth2/register?appid=wxa53abdf500a8ee6d
	var Data = {
		"appid" : appid
	}
	function oauth2() {
		var oauth2Url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
				+ appNumber
				+ "&redirect_uri="
				+ encodeURIComponent(window.location.href)
				+ "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
		window.location.href = oauth2Url;
	}

	$(function() {
		validate();
	//	var mpInfo = localStorage.getItem("mpInfo");
	//	alert(mpInfo)
	});

	function validate() {
		var code = GetQueryString("code");
	//	alert(code);
		if (code != null && code != "") { //微信授权后回调到此页面
			if (validateflag()) {
				var mpInfo = localStorage.getItem("mpInfo"); /* 获取本地微信用户信息 */
				var mpInfoJson = JSON.parse(mpInfo);
				showBody();
			} else {
				Data.code = code;
			//alert("---");
				var infoUrl = "http://faye5.iask.in/pay/mobile/api/getOpenId?qq=3"
						+"&code="+code;
				$.get(infoUrl, function(data) {

					data =  eval('(' + data + ')');
			//	alert(data.openid);
					if (data.openid) {
						var mpInfoJson = {
							"oauthDate" : new Date().getTime(),
							"openId" : data.openid
						}
						localStorage.setItem("mpInfo", JSON
								.stringify(mpInfoJson));
						showBody();
					}  else {
						return null;//其他异常
					}
				});
			}
		} else {
			if (validateflag()) {
				// 				var mpInfo = localStorage.getItem("mpInfo"); /* 获取本地微信用户信息 */
				// 				var mpInfoJson = JSON.parse(mpInfo);
				showBody();
			} else {
				oauth2(); //进行授权
			}
		}
	}

	function validateflag() { //判断localStorage中是否已存在信息，并判断信息是否已经过期 。过期时长为24小时
	
		var mpInfo = localStorage.getItem("mpInfo");
	//	alert(mpInfo);
		if (mpInfo == null || mpInfo == "") {
			return false;
		} else {
			var mpInfoJson = JSON.parse(mpInfo);
			var openid = mpInfoJson.openId;
			if (openid == null || openid == "") { //信息中是否存在openid
				return false;
			} else {
				var oauthDate = mpInfoJson.oauthDate;
				var nowTime = new Date().getTime();
				if (nowTime - parseInt(oauthDate) > daySeconds) {
					return false;
				}
			}
		}
		return true;
	}

	function showBody() {
		$(".showData").show();
		doAction();
	}
