uniapp获取用户登录信息的实现

用户登录需要拿到以下参数,因为getUserInfo已经不再有弹出层了,使用我们改用getUserProfile获取数据
在这里插入图片描述

1.点击登录按钮获取微信用户的基本信息:

<button type="default" v-on:click="dianji()">一键登录</button>

2.在methods节点中声明getUserInfo事件处理函数

methods:{
			dianji(){
				
				uni.getUserProfile({
					desc: '用于完善用户资料', 
					success(e) {
						
						console.log(e.userInfo);
					},
					fail() {
						uni.showToast({
							icon:null,
							title:'您取消了登录授权'
						})
					}
				})
			}
		}

在这里插入图片描述

3.获取code参数:

const [err,res] = await uni.login().catch(err=>err);
if(res.errMsg !== 'login:ok'){
	return uni.showToast({
		title:'登录失败!'
	})
}
console.log(res.code);

在这里插入图片描述

4.把5个参数放在data的一个对象当中

data() {
	return {
		query:{
			code:'',
			encryptedData:'',
			iv:'',
			signature:''
		},
		userInfo:{
			
		}
	};
},
methods:{
	dianji(){
		var that = this;
		uni.getUserProfile({
			desc: '用于完善用户资料', 
			success(e) {
				//保存参数信息
				that.get();
				that.query.encryptedData = e.encryptedData;
				that.query.iv = e.iv;
				that.query.signature = e.signature;
				that.query.rawData = e.rawData;
				
				//保存用户信息
				that.userInfo = e.userInfo;
			},
			fail() {
				uni.showToast({
					icon:null,
					title:'您取消了登录授权'
				})
			},
		});
		
		
	},
	async get(){
		const [err,res] = await uni.login().catch(err=>err);
		
		if(err || res.errMsg !== 'login:ok'){
			return uni.showToast({
				title:'登录失败!'
			})
		}
		console.log(res.code);
		this.query.code = res.code;
	}
	}

5个参数已经全部保存在data下的query对象当中
在这里插入图片描述

5.将code传给后台处理:

methods:{
dianji(){
	var that = this;
	uni.getUserProfile({
		desc: '用于完善用户资料', 
		success(e) {
			//保存参数信息
			
			that.get().then(res=>{
				that.query.encryptedData = e.encryptedData;
				that.query.iv = e.iv;
				that.query.signature = e.signature;
				that.query.rawData = e.rawData;
				
				//保存用户信息
				that.userInfo = e.userInfo;
				
				console.log('发送请求');
				uni.request({
					url:'http://localhost:3000/test?code='+that.query.code,
					method:'GET',
					success(res) {
						console.log(res);
						
					}
				})
			})
			
			
		},
		fail() {
			uni.showToast({
				icon:null,
				title:'您取消了登录授权'
			})
		},
	});
	
	
},
async get(){
	const [err,res] = await uni.login().catch(err=>err);
	
	if(err || res.errMsg !== 'login:ok'){
		return uni.showToast({
			title:'登录失败!'
		})
	}
	
	this.query.code = res.code;
	console.log('code'+this.query.code);
}
}
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
uniapp实现用户登录的步骤如下: 1. 在页面创建时的created方法中判断用户是否登录,可以通过本地缓存的token来调用服务端接口返回用户信息。如果没有token,则需要进行微信授权。 2. 判断用户是否授权,可以使用微信小程序官方文档提供的API wx.getSetting,返回值中会显示小程序已经向用户请求过的权限。如果用户拒绝授权,则登录授权逻辑结束。 3. 如果用户已经授权,可以调用微信小程序官方API wx.getUserInfo或uniapp集成的微信小程序API uni.getUserInfo来获取用户的头像、昵称、openid等个人信息。注意,如果用户的scope.userInfo权限已经被回收,可以使用昵称和头像填写来进行完善用户信息。 4. 调用服务端的登录注册逻辑,完成登录或注册操作。登录或注册成功后,将服务端返回的token信息放在本地的storage缓存中。 5. 使用vuex的storage来实现用户登录状态的管理。 参考代码如下: ```javascript // 页面创建时的created方法中判断用户是否登录 created() { if (!this.isLogin()) { // 用户未登录,进行微信授权 this.wxAuthorize() } else { // 用户已登录,显示用户登录成功之后的头像昵称信息 this.showUserInfo() } }, methods: { // 判断用户是否登录 isLogin() { // 判断本地缓存中是否存在token信息 const token = uni.getStorageSync('token') return !!token }, // 微信授权 wxAuthorize() { wx.getSetting({ success: (res) => { if (res.authSetting['scope.userInfo']) { // 用户已授权,获取用户信息 this.getUserInfo() } else { // 用户未授权,进行登录授权逻辑结束 // TODO: 处理未授权情况 } } }) }, // 获取用户信息 getUserInfo() { // 调用微信小程序API获取用户信息 wx.getUserInfo({ success: (res) => { const userInfo = res.userInfo // TODO: 处理用户信息 // 调用服务端完成登录注册逻辑 this.loginOrRegister(userInfo) } }) }, // 登录或注册 loginOrRegister(userInfo) { // 调用服务端的登录注册接口 // TODO: 处理登录注册逻辑 // 登录或注册成功后将token信息放在本地storage缓存中 uni.setStorageSync('token', 'xxxxx') // 显示用户登录成功之后的头像昵称信息 this.showUserInfo() }, // 显示用户登录成功之后的头像昵称信息 showUserInfo() { // TODO: 显示用户登录成功之后的头像昵称信息 // 可以通过触发全局自定义事件实现,在me.vue页面加载监听该事件 uni.$emit('meUserLogin') } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值