微信授权登陆绑定

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);
})

  

微信授权登陆绑定

上一篇:飞天加速计划初体验


下一篇:解决android:background背景图片被拉伸问题