Jelajahi Sumber

邀请码登录按钮失灵修复

yuwenjun 4 tahun lalu
induk
melakukan
564394fc2e
3 mengubah file dengan 77 tambahan dan 64 penghapusan
  1. 20 8
      pages/authorization/authorization.vue
  2. 3 21
      pages/index/index.vue
  3. 54 35
      pages/login/login.vue

+ 20 - 8
pages/authorization/authorization.vue

@@ -13,9 +13,9 @@
 				class="login-btn use"
 				type="primary"
 				size="small"
-				open-type="getUserInfo"
 				lang="zh_CN"
-				@click="getUserProfile"
+				open-type="getUserInfo"
+				@getuserinfo="getUserInfo"
 			>
 				微信授权
 			</button>
@@ -55,6 +55,7 @@ export default {
 		...mapState(['hasLogin', 'userInfo'])
 	},
 	methods: {
+		...mapMutations(['wxLogin']),
 		getUserProfile() {
 			const _that = this
 			// 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
@@ -62,6 +63,8 @@ export default {
 				desc: '用于完善会员资料',
 				success(res) {
 					console.log(res)
+					console.log('新API', res)
+					_that.wxLogin(res.userInfo)
 					// _that.$store.commit('wxLogin',e.detail.userInfo)
 					uni.login({
 						provider: 'weixin',
@@ -79,14 +82,23 @@ export default {
 				}
 			})
 		},
-		getuserinfo(e) {
-			//微信授权
+		getUserInfo(e) {
+			const _that = this
 			if (e.detail.userInfo) {
-				this.checkedUser()
-				// 授权成功后保存微信用户信息
-				this.$store.commit('wxLogin', e.detail.userInfo)
+				// 缓存用户授权状态
+				this.wxLogin(e.detail.userInfo)
+				uni.login({
+					provider: 'weixin',
+					scopes: 'auth_user',
+					success() {
+						_that.checkedUser()
+					},
+					fail(err) {
+						_that.$util.msg(err, 2000)
+					}
+				})
 			} else {
-				this.$util.msg('授权失败', 2000)
+				console.log('授权失败!')
 			}
 		},
 		undGetuserinfo() {

+ 3 - 21
pages/index/index.vue

@@ -85,10 +85,7 @@
 			</view>
 			<!-- 订单状态区域END -->
 			<!-- 帮机构下单 -->
-			<view
-				class="place-order"
-				@click="navigator(`/pages/user/club/club-list`)"
-			>
+			<view class="place-order" @click="navigator(`/pages/user/club/club-list`)">
 				<view class="place-left">
 					<text class="iconfont icon-bangjigouxiadan"></text>
 					<text class="title">帮机构下单</text>
@@ -164,7 +161,7 @@ export default {
 		// })
 	},
 	computed: {
-		...mapState(['hasLogin', 'userInfo'])
+		...mapState(['hasLogin', 'userInfo', 'isWxAuthorize'])
 	},
 	filters: {
 		countNum(value) {
@@ -243,23 +240,8 @@ export default {
 				.catch(err => {
 					_that.$api.navigateTo(`/pages/authorization/authorization`)
 				})
-			// authorize.getSetting().then(res => {
-			// 	console.log('是否已授权',res);
-			// 	//0:为取消授权 1:为已授权 2:为未操作
-			// 	if (res == 1) {
-			// 		if (this.hasLogin) {
-			// 			// 已登录
-			// 			this.$api.navigateTo(url)
-			// 		} else {
-			// 			// 未登录
-			// 			this.$api.navigateTo(`/pages/login/login`)
-			// 		}
-			// 	} else {
-			// 		this.$api.navigateTo(`/pages/authorization/authorization`)
-			// 	}
-			// })
 		},
-		
+
 		onShareAppMessage(res) {
 			//分享转发
 			this.isShareModal = false

+ 54 - 35
pages/login/login.vue

@@ -38,7 +38,7 @@ export default {
 	data() {
 		return {
 			invitationCode: '', //获取用户登录的邀请码
-			loginMessage: '' ,//登录信息反馈
+			loginMessage: '' //登录信息反馈
 		}
 	},
 	onLoad(option) {},
@@ -48,45 +48,64 @@ export default {
 		isCodeEmpty() {
 			return this.invitationCode.trim().length < 6
 		},
-		hasError(){
-			return this.loginMessage.length>0
+		hasError() {
+			return this.loginMessage.length > 0
 		}
 	},
 	methods: {
-		...mapMutations(['login']),
+		...mapMutations(['login', 'wxLogin']),
+		getUserProfile() {
+			const _that = this
+			// 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
+			return new Promise((resolve, reject) => {
+				wx.getUserProfile({
+					desc: '用于完善会员资料',
+					success(res) {
+						// 更新用户信息
+						_that.wxLogin(res.userInfo)
+						uni.setStorageSync('profileFlag', true)
+						resolve(res.userInfo)
+					},
+					fail() {
+						_that.$util.msg('授权失败', 2000)
+					}
+				})
+			})
+		},
 		// 登录
-		goLogin() {
-			// 获取用户微信信息
-			wx.getUserInfo({
-				success: res => {
-					this.isUserInfo = false
-					this.userInfo = res.userInfo
-					// 验证邀请码
-					this.UserService.userInvitation({
-						invitationCode: this.invitationCode,
-						nickName: res.userInfo.nickName,
-						openid: uni.getStorageSync('openid')
-					})
-					.then(response => {
-						console.log(response)
-						// 保存用户信息
-						this.login(response.data)
-						this.$store.commit('updateStatus', response.data)
-						// 登录成功提示
-						this.$util.msg('登录成功', 1500, false, 'success')
-						setTimeout(() => {
-							this.$api.navigateTo(`/pages/index/index`)
-						}, 1500)
-					})
-					.catch(error => {
-						this.loginMessage = error.msg
-						this.isUserInfo = false
-						setTimeout(()=>{
-							this.loginMessage = ''
-						},2000)
-					})
-				}
+		async goLogin() {
+			const _that = this
+			let wechatUserInfo = uni.getStorageSync('wechatUserInfo')
+			let profileFlag = uni.getStorageSync('profileFlag')
+			console.log(profileFlag);
+			if (!profileFlag) {
+				wechatUserInfo = await this.getUserProfile()
+			}
+			// 验证邀请码
+			console.log(this.isWxAuthorize)
+			this.UserService.userInvitation({
+				invitationCode: this.invitationCode,
+				nickName: wechatUserInfo.nickName,
+				openid: uni.getStorageSync('openid')
 			})
+				.then(response => {
+					console.log(response)
+					// 保存用户信息
+					_that.login(response.data)
+					_that.$store.commit('updateStatus', response.data)
+					// 登录成功提示
+					_that.$util.msg('登录成功', 1500, false, 'success')
+					setTimeout(() => {
+						_that.$api.navigateTo(`/pages/index/index`)
+					}, 1500)
+				})
+				.catch(error => {
+					_that.loginMessage = error.msg
+					_that.isUserInfo = false
+					setTimeout(() => {
+						_that.loginMessage = ''
+					}, 2000)
+				})
 		},
 		// 输入框输入介绍
 		vcodeInput(val) {