1. 程式人生 > >微信授權登陸綁定

微信授權登陸綁定

rip 計時 move ring 登錄 img info type dir

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4a1da83332cf1cb6&redirect_uri=https://www.grande-health.com/mp/1.html&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect 
1.獲取用戶唯一識別碼code(後臺獲取)

// 獲取用戶唯一識別碼 code
function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return unescape(r[2]);
	return null;
}
// 調用方法
var code = GetQueryString("code");


2.根據code獲取微信openid
/*
 * 獲取用戶 openid
 * 傳入用戶唯一識別碼 code
 * https://tsch.fromfuture.cn:7712/GZ/v1/weixin/getOpenid?code=071df1AX0zVnb12FJvxX0FHNzX0df1Ab
 */
if(code == null) {
	alert(‘獲取code失敗‘)
}			 

function gainOpenid() {

	var paramJson = {
		‘code‘: code
	}
	try {
		ajaxUtil(‘POST‘, WEIXIN_GETOPENID, paramJson, function(result) {
			if(result.code == ‘000‘) {
				if(typeof(result.data.openid) == ‘undefined‘ || result.data.openid == "" || result.data.openid == null) {
					alert("獲取失敗")
				} else {
					window.sessionStorage.WEIXIN_OPENID = result.data.openid;
					ajaxUtil(‘POST‘, WEIXIN_CHECKOPENID, {
						‘openid‘: window.sessionStorage.WEIXIN_OPENID
					}, function(result2) {
						if(result2.code == ‘000‘) {
							if(result2.data.code == ‘1‘) {
								G_APP_USRGUID = result2.data.usrguid;
								window.sessionStorage.WEIXIN_USRGUID = result2.data.usrguid;

								window.sessionStorage.WEIXIN_PHONENUM = result2.data.acounnt;
								mui.openWindow({
									url: ‘myProfile.html‘,
									id: ‘myProfile.html‘
								});
							} else if(result2.data.code == ‘2‘) {
								mui.openWindow({
									url: ‘myRecord.html‘,
									id: ‘myRecord.html‘
								});
							}

						} else if(result2.code == ‘001‘) {
							mui.toast(‘請求出錯‘);
						}

					});

				}
			} else if(result.code == ‘001‘) {
				mui.toast(‘請求出錯‘);
			}
		});

	} catch(e) {
		mui.toast(‘請檢查網絡連接‘);
	}
};
gainOpenid();

 如果未綁定則跳轉:

/**
 * 獲取微信用戶資料 
 */
function userDatum() {
	try{
		ajaxUtil(‘POST‘,WEIXIN_USERINFO,{‘openid‘:window.sessionStorage.WEIXIN_OPENID},function(result){
			if(result.code == ‘000‘) {
				$(‘#usrImg‘).attr(‘src‘, result.data.headimgurl); // 用戶頭像
				$(‘#usrName‘).html(result.data.nickname); // 用戶微信名稱
			} else if(result.code == ‘001‘) {
				mui.toast(‘請求出錯‘);
			}
		});
	}catch(e){
		mui.toast(‘請檢查網絡連接‘);
	}
}
userDatum();


/**
 * 註冊登錄賬號
 * 1、手機號碼正則表達式
 * 2、驗證用戶輸入手機號碼是否符合正則mobileNum();
 */
function isReg(str) {
	var reg = /^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))[0-9]{8}$/;
	return reg.test(str);
}

function mobileNum(phone) {

	if(phone == "" || phone == null) {

		//	手機號為空時提示:手機號碼不能為空
		mui.toast(‘手機號不能為空‘);
		return false;

	} else if(!isReg(phone)) {

		mui.toast(‘請輸入正確的手機號‘);
		return false;
	}
	return true;
}

// 獲取驗證碼
mui(‘body‘).on(‘tap‘, ‘#getbtn‘, function() {

	var iphone = $("#userphone").val(); //	獲取用戶填寫的手機號
	
	if(mobileNum(iphone) == true) {
		getYzm();
	}
})

function getYzm() {
	var iphone = $("#userphone").val(); //	獲取用戶填寫的手機號
	try {
	    ajaxUtil(‘GET‘,WEIXIN_GETYZM,{‘phone‘:iphone},function(result){
			if(result.code == ‘000‘) {
				  window.sessionStorage.WEIXIN_PHONENUM=iphone;
				if(result.data.msg != ‘發送成功‘) {
					time(); //開始倒計時
				} else {
					mui.toast("驗證碼已發送,請註意查收");
					$(‘#yzmtwo‘).val(result.data.yzm);
					window.sessionStorage.WEIXIN_YZM=result.data.yzm;
					console.log(window.sessionStorage.WEIXIN_YZM)
					time();
				}

			} else {
				//  返回結果為1時表示用戶手機號碼已註冊
				mui.toast(‘手機號已註冊,您可以直接登錄‘);
			}
		});
		
	} catch(error) {
		mui.toast("請檢查網絡連接");
	}
}

/**
 * 提交
 */
mui(‘body‘).on(‘tap‘, ‘#input_check‘, function() {
	var phone = mobileNum(window.sessionStorage.WEIXIN_PHONENUM);
	if(phone != true) {
		return;
	}
	
	var yzm = verify(window.sessionStorage.WEIXIN_YZM);
	if(yzm != true) {
		return;
	}

	if(phone == true && yzm == true) {

	    var paramData={
	    	‘name‘:window.sessionStorage.WEIXIN_PHONENUM,
	    	‘openid‘:window.sessionStorage.WEIXIN_OPENID,
	    	‘yzm‘:window.sessionStorage.WEIXIN_YZM,
	    	‘password‘:‘1‘,
	    	‘type‘:‘3‘
	    };
		try {
			//提交按鈕
			ajaxUtil(‘POST‘,WEIXIN_CHECK,paramData,function(result){
	      		if(result.code == ‘000‘) {
					//window.localStorage.getItem("FROMFUTURE_APP_USRGUID", result.data.usrguid);
					if(result.data.code == ‘1‘) {
						window.sessionStorage.WEIXIN_USRGUID=result.data.usrguid;
						G_APP_USRGUID=result.data.usrguid;
						window.sessionStorage.WEIXIN_PHONENUM=result.data.acounnt;
						mui.toast(‘綁定過微信‘);
						mui.openWindow({
							url: ‘myProfile.html‘,
							id: ‘myProfile.html‘
						});
					}
					if(result.data.code == ‘2‘) {
						mui.toast(‘沒註冊過用戶,需要重新註冊‘);
						mui.openWindow({

							url: ‘login.html‘,
							id: ‘login.html‘
						});
					}
					if(result.data.code == ‘3‘) {
						window.sessionStorage.WEIXIN_USRGUID=result.data.usrguid;
						window.sessionStorage.WEIXIN_PHONENUM=result.data.acounnt;
							G_APP_USRGUID=result.data.usrguid;
						mui.openWindow({
							url: ‘registered.html‘,
							id: ‘registered.html‘
						});
					}
				}
									
			});
		} catch(e) {
			mui.toast("接口錯誤,請聯系客服" + e);
		}
			
	}
});

/**
 * 全局變量聲明 
 */
var iphone;

/**
 * 點擊獲取驗證碼後顯示倒數時間
 */
function time() {

	GZ("getbtn").disabled = false; //讓按鈕可以點擊
	var btn = GZ("getbtn");
	btn.removeAttribute("disabled"); //	讓按鈕可以點擊
	btn.value = "獲取驗證碼";
	window.sessionStorage.FROMFUTURE_APP_USRTIME = 60;
	timeMain = setInterval(‘djs()‘, 1000)
}
/**
 * 按鈕倒計時顯示
 * @param {Object} num
 */
function djs() {

	var btn = GZ("getbtn");
	GZ("getbtn").disabled = true; //讓按鈕不可以點擊
	var num = window.sessionStorage.FROMFUTURE_APP_USRTIME;
	num--;
	window.sessionStorage.FROMFUTURE_APP_USRTIME = num;
	if(num <= 0) {
		clearTimeout(timeMain);
		GZ("getbtn").disabled = false; //讓按鈕可以點擊
		btn.value = "獲取驗證碼";
	} else {
		btn.value = "重新發送(" + num + ")";
	}
}

/**
 * 驗證用戶輸入驗證碼是否正確
 */
function verify(yzm) {
	var yzmtwo = $(‘#yzmtwo‘).val();
	if(yzm == "" || yzm == null) {
		//	手機號為空時提示:手機號碼不能為空
		mui.toast(‘驗證碼不能為空‘);
		return false;
	} else if(yzm != 	yzmtwo) {
		mui.toast("驗證碼錯誤,請重新輸入!");
		return false;
	}
	return true;
}

 如果綁定則跳轉:

$(function() {
	var imgParam = USR_GETIMG + "?guid=" + window.sessionStorage.WEIXIN_USRGUID;
	$(‘#headerImg‘).attr(‘src‘, imgParam);
	$(‘.iphone‘).html(window.sessionStorage.WEIXIN_PHONENUM);
})

  

微信授權登陸綁定