uniapp获取小程序的用户信息

loginGlobal(e) { // 小程序登录
			//登录
			let _this = this
			// 获取用户信息
			uni.getUserInfo({
				provider: 'weixin',
				success: function(infoRes) {
					uni.showLoading({
						title: '正在登录...',
						mask: false
					})
					console.log('微信获得用户信息:', infoRes);
					let params = {
						"functionName": "smallProgramService",
						"methodName": "outGetId",
						"originSource": {
							"OS": "tiny",
							"version": "AppVersion_V2.0"
						},
						"data": {
							openId: _this.$store.state.openId
						}
					}
					_this.$http(JSON.stringify(params)).then(res => {
						if (res.data.errorCode === "ERRORCODE0000") {
							console.log('登录获得', res)
							let userId = res.data.data.userInfo.id
							console.log('查看资料完整度:', res.data.data.goldInfo.userInfoPercentage)
							if (res.data.data.goldInfo.userInfoPercentage === '0') { // 新用户
								let params = {
									"functionName": "smallProgramService",
									"methodName": "updateUserInfo",
									"originSource": {
										"OS": "tiny",
										"version": "AppVersion_V2.0"
									},
									"data": {
										"userInfo": {
											id: userId,
											name: infoRes.userInfo.nickName,
											headPortrait: infoRes.userInfo.avatarUrl,
											gender: infoRes.userInfo.gender,

										}
									}
								}
								_this.$http(JSON.stringify(params)).then(result => { //更新用户信息
									if (res.data.errorCode === "ERRORCODE0000") {
										uni.hideLoading()
										console.log('新用户初始化资料', result)
										_this.isLogin = true
										uni.setStorageSync('isLogin', 'true'); // 缓存本地
										uni.setStorageSync('userId', userId)
										uni.setStorageSync('hasPhone', res.data.data.userInfo.phone)
										_this.getUserInfo(userId)
									}
								})
							} else { // 非新用户
								uni.hideLoading()
								_this.isLogin = true
								uni.setStorageSync('isLogin', 'true'); // 缓存本地
								uni.setStorageSync('userId', userId)
								uni.setStorageSync('hasPhone', res.data.data.userInfo.phone)
								if (res.data.data.userInfo.phone != null) {
									_this.hasPhone = true
								}
								console.log('手机号为:', uni.getStorageSync('hasPhone'))
								_this.getUserInfo(userId)
							}
						}
					})

				},
				fail: function() {
					uni.hideLoading()
					uni.showToast({
						title: '未授权成功,登录失败',
						icon: 'none'
					});

				}
			});



		}

 

你可能感兴趣的:(uniapp获取小程序的用户信息)