2021-05-15 公众号授权登录基本流程

公众号授权登录流程

1.判断登陆情况,直接就是拿本地存储的token去请求接口,401就是未授权或者授权过期

2.未登录:

  •  url地址有code

        直接获取code,请求登陆接口

  •  url地址无code

         缓存当前页面的url,跳转微信公众号授权网址授权

// 请求微信服务器获取code 方法
function getUserCode() {
	const redirectURL = encodeURIComponent(//对url进行encodeURIComponent编码
		window.location.href
	); 
    //获取地址
	const base = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=";
	let state = getUrlParam("parent_id") || "STATE";
	// let surl;
	// let reserveUrl;
	let appid = $mConfig.weixinAppId;
	let scope = "snsapi_userinfo";
	window.location.href =
		base +
		appid +
		"&redirect_uri=" +
		redirectURL +
		"&response_type=code&scope=" +
		scope +
		"&state=" +
		state +
		"#wechat_redirect";
}

3.已登陆:不做讨论,实际需求实际开发

上一篇:极客时间-设计模式之美笔记(4) 面向对象-实战(二)


下一篇:微信小程序入门(一)