微信小程序授权登录

1. 基本功能

点击授权按钮,就可使用自己的微信账号进行登录,第三方网站或应用就可以获取用户基本信息(如昵称、头像等),简化账号密码登录流程

2. 三种授权方式:

  • PC版扫码授权
  • H5版授权
  • 小程序插件版授权

3. 第三方网站获取微信小程序授权流程:

4.小程序的登录授权流程:

5. 实现代码:

// Promise封装用户的数据信息
const wxgetUserInfo = function () {
	return new Promise((resolve, reject) => {
		wx.getUserInfo({
			lang: 'zh_CN',
			success(res) {
				resolve(res);
			},
			fail(res) {
				reject(res);
			}
		})
	});
}

// 判断用户是否登录
const checkLogin = function () {
	let res = getApp().globalData.userSession.token ? true : false;
	let res1 = getApp().globalData.isLog;
	let res2 = res && res1;
	if (res2) {
		let newTime = Math.round(new Date() / 1000);
		if (getApp().globalData.expiresTime < newTime) return false;
	}
	return res2;
}

// 注销
const logout = function () {
	getApp().globalData.userSession.token = '';
	getApp().globalData.isLog = false;
}

//检查用户是否授权了用户信息(scope.userInfo)-存入缓存
const chekWxLogin = function () {
	return new Promise((resolve, reject) => {
		wx.getSetting({
			success(res) {
				if (!res.authSetting['scope.userInfo']) {
					return reject({
						authSetting: false
					});
				} else {
					wx.getStorage({
						key: 'cache_key',
						success(res) {
							if (checkLogin()) {
								return resolve({
									userinfo: getApp().globalData.userInfo,
									isLogin: true
								});
							} else {
								wxgetUserInfo().then(userInfo => {
									userInfo.cache_key = res.data;
									return resolve({
										userInfo: userInfo,
										isLogin: false
									});
								}).catch(res => {
									return reject(res);
								})
							}
						},
						fail() {
							getCodeLogin((code) => {
								wxgetUserInfo().then(userInfo => {
									userInfo.code = code;
									return resolve({
										userInfo: userInfo,
										isLogin: false
									});
								}).catch(res => {
									return reject(res);
								})
							});
						}
					})

				}
			},
			fail(res) {
				return reject(res);
			}
		})
	})
}


//授权过后自动登录
const autoLogin = function () {
	return new Promise((resolve, reject) => {
		wx.getStorage({
			key: 'cache_key',
			success(res) {
				wxgetUserInfo().then(userInfo => {
					userInfo.cache_key = res.data;
					return resolve(userInfo);
				}).catch(res => {
					return reject(res);
				})
			},
			fail() {
				getCodeLogin((code) => {
					wxgetUserInfo().then(userInfo => {
						userInfo.code = code;
						return resolve(userInfo);
					}).catch(res => {
						return reject(res);
					})
				});
			}
		});
	})
}

// 获取登录时需要的 code
const getCodeLogin = function (successFn) {
	wx.login({
		success(res) {
			successFn(res);
		}
	})
}

注: wx.login()方法:调用接口获取登录凭证(code)。

通过凭证进而换取用户登录态信息,包括用户的唯一标识(openid)及本次登录的会话密钥(session_key)等。用户数据的加解密通讯需依赖会话密钥完成。

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值