1.点击登录按钮

	<button type="primary" class="btn-login" @click="getUserProfile">一键登录</button>

2.触发getUserProfile事件,实现登录第一步(用uni.getUserProfile()获取用户信息并传给登录第二步)

async getUserProfile(e) {
			let that = this;
			const res = await uni.getUserProfile({
				desc: '用于获取您的个人信息',
				success: res => console.log(res),
				fail: res => console.log('取消了授权')
			});
			// console.log();
			// console.log(e.detail.userInfo);
			// 以后都不会再这里储存用户的微信头像和名称了,要么直接用手机号登录,要么用户自己去设置头像和用户名
			that.updateUserInfo(e.detail.userInfo);
			that.getToken(e.detail);
		},

3. 登录第二步(用uni.login()获取code,集齐5个参数调登录接口,获取token)

// 登录第二步(用uni.login()获取code,集齐5个参数调登录接口,获取token)
		async getToken(info) {
			// 获取 code 对应的值
			const [err, res] = await uni.login().catch(err => err);

			if (err || res.errMsg !== 'login:ok') return uni.$showMsg('登录11失败!');

			// 准备参数
			const query = {
				code: res.code,
				encryptedData: info.encryptedData,
				iv: info.iv,
				rawData: info.rawData,
				signature: info.signature
			};
			// 调登录接口(登录接口失效才导致登录失败,这个登录过程是对的,只是接口失效,手动给vuex里面的token随便写个值就可以进行后面的开发了)
			const { data: loginResult } = await uni.$http.post('/api/public/v1/users/wxlogin', query);
			if (loginResult.meta.status !== 200) return uni.$showMsg('登录22失败!');

			// 直接把 token 保存到 vuex 中
			this.updateToken(loginResult.message.token);
            //表示登录成功,跳转到登录详情页
			this.navigateBack();
		},

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐