瀏覽代碼

用户登录

yuwenjun 4 年之前
父節點
當前提交
c46303048d
共有 3 個文件被更改,包括 144 次插入69 次删除
  1. 36 18
      common/config/authorize.js
  2. 41 14
      pages/authorization/authorization.vue
  3. 67 37
      pages/index/index.vue

+ 36 - 18
common/config/authorize.js

@@ -103,23 +103,23 @@ const getCode = provider => {
 // 是否开启了获取用户名授权 当用户一开始拒绝了 需再次提醒授权
 const getSetting = function() {
 	return new Promise((resolve, reject) => {
-		uni.getSetting({
-			success: function(res) {
-				let authSetting = res.authSetting
-				if (authSetting['scope.userInfo']) {
-					resolve(1)
-					return
-				} //授权成功
-				if (authSetting['scope.userInfo'] === false) {
-					resolve(0)
-					return
-				} //拒绝授权
-				resolve(2) //2未操作
-			},
-			fail: function() {
-				reject('获取用户授权失败')
-			}
-		})
+		// uni.getSetting({
+		// 	success: function(res) {
+		// 		let authSetting = res.authSetting
+		// 		if (authSetting['scope.userInfo']) {
+		// 			resolve(1)
+		// 			return
+		// 		} //授权成功
+		// 		if (authSetting['scope.userInfo'] === false) {
+		// 			resolve(0)
+		// 			return
+		// 		} //拒绝授权
+		// 		resolve(2) //2未操作
+		// 	},
+		// 	fail: function() {
+		// 		reject('获取用户授权失败')
+		// 	}
+		// })
 	})
 }
 
@@ -146,11 +146,29 @@ const getUserInfo = (provider) => {
 	})
 }
 
+// 获取用户登录状态
+const checkLogin = () => {
+	return new Promise((resolve, reject) => {
+		uni.checkSession({
+			success() {
+				resolve(1)
+				console.log('登录成功')
+			},
+			fail(err) {
+				reject(0)
+			}
+		})
+	})
+}
+
+
+
 export default {
 	getProvider,
 	getSetting,
 	checkSessionKey,
 	getCode,
 	login,
-	getUserInfo
+	getUserInfo,
+	checkLogin
 }

+ 41 - 14
pages/authorization/authorization.vue

@@ -4,7 +4,9 @@
 			<image class="logo" src="../../static/ws/logo.png" mode="widthFix"></image>
 		</view>
 		<view class="login-content">
-			<text>您暂未授权维沙采购商城小程序获取你的信息,将无法正常使用小程序的功能。如需正常使用,请点击“授权”按钮,打开头像,昵称等信息的授权</text>
+			<text
+				>您暂未授权维沙采购商城小程序获取你的信息,将无法正常使用小程序的功能。如需正常使用,请点击“授权”按钮,打开头像,昵称等信息的授权</text
+			>
 		</view>
 		<view class="login-form">
 			<button
@@ -13,7 +15,7 @@
 				size="small"
 				open-type="getUserInfo"
 				lang="zh_CN"
-				@getuserinfo="getuserinfo"
+				@click="getUserProfile"
 			>
 				微信授权
 			</button>
@@ -53,12 +55,35 @@ export default {
 		...mapState(['hasLogin', 'userInfo'])
 	},
 	methods: {
+		getUserProfile() {
+			const _that = this
+			// 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
+			wx.getUserProfile({
+				desc: '用于完善会员资料',
+				success(res) {
+					console.log(res)
+					// _that.$store.commit('wxLogin',e.detail.userInfo)
+					uni.login({
+						provider: 'weixin',
+						scopes: 'auth_user',
+						success() {
+							_that.checkedUser()
+						},
+						fail(err) {
+							uni.showToast({
+								title: err
+							})
+						}
+					})
+				}
+			})
+		},
 		getuserinfo(e) {
 			//微信授权
 			if (e.detail.userInfo) {
 				this.checkedUser()
 				// 授权成功后保存微信用户信息
-				this.$store.commit('wxLogin',e.detail.userInfo)
+				this.$store.commit('wxLogin', e.detail.userInfo)
 			} else {
 				this.$util.msg('授权失败', 2000)
 			}
@@ -70,19 +95,21 @@ export default {
 		async checkedUser() {
 			const wechatCode = await authorize.getCode('weixin')
 			// 用户是否填写了邀请码
-			this.UserService.userInfoLogin({ code: wechatCode }).then(res=>{
-				console.log('授权登录成功')
-				uni.reLaunch({
-					url: '/pages/index/index'
+			this.UserService.userInfoLogin({ code: wechatCode })
+				.then(res => {
+					console.log('授权登录成功')
+					uni.reLaunch({
+						url: '/pages/index/index'
+					})
 				})
-			}).catch((error)=>{
-				console.log('游客登录')
-				// 缓存游客openid
-				uni.setStorageSync('openid', error.data.openid)
-				uni.reLaunch({
-					url: '/pages/login/login'
+				.catch(error => {
+					console.log('游客登录')
+					// 缓存游客openid
+					uni.setStorageSync('openid', error.data.openid)
+					uni.reLaunch({
+						url: '/pages/login/login'
+					})
 				})
-			})
 		}
 	}
 }

+ 67 - 37
pages/index/index.vue

@@ -141,14 +141,27 @@ export default {
 	onLoad() {
 		// 获取用户是否授权
 		//0:为取消授权 1:为已授权 2:为未操作
-		authorize.getSetting().then(res => {
-			if (res == 1) {
-				console.log('用户已授权')
+		// if(wx.getUserProfile){
+		// 	this.GetWxAuthorize()
+		// }else{
+		// 	this.isRequest = true
+		// }
+		authorize
+			.checkLogin()
+			.then(res => {
 				this.GetWxAuthorize()
-			} else {
+			})
+			.catch(err => {
 				this.isRequest = true
-			}
-		})
+			})
+		// authorize.getSetting().then(res => {
+		// 	if (res == 1) {
+		// 		console.log('用户已授权')
+		// 		this.GetWxAuthorize()
+		// 	} else {
+		// 		this.isRequest = true
+		// 	}
+		// })
 	},
 	computed: {
 		...mapState(['hasLogin', 'userInfo'])
@@ -164,24 +177,24 @@ export default {
 		async GetWxAuthorize() {
 			// 获取微信code
 			const wechatCode = await authorize.getCode('weixin')
-			this.UserService.userInfoLogin({ 
-				code: wechatCode ,
-			})
-			.then(res => {
-				// 登录成功  将信息保存在userInfo中 保存在本地和store
-				this.login(res.data)
-				this.organizeId = res.data.organizeId
-				this.getUserInfoPersonal(this.organizeId)
-				this.refresh = true
-				this.isRequest = true
-			})
-			.catch(err => {
-				// 缓存游客openid
-				uni.setStorageSync('openid', err.data.openid)
-				this.logout()
-				this.refresh = true
-				this.isRequest = true
+			this.UserService.userInfoLogin({
+				code: wechatCode
 			})
+				.then(res => {
+					// 登录成功  将信息保存在userInfo中 保存在本地和store
+					this.login(res.data)
+					this.organizeId = res.data.organizeId
+					this.getUserInfoPersonal(this.organizeId)
+					this.refresh = true
+					this.isRequest = true
+				})
+				.catch(err => {
+					// 缓存游客openid
+					uni.setStorageSync('openid', err.data.openid)
+					this.logout()
+					this.refresh = true
+					this.isRequest = true
+				})
 		},
 		//初始化个人中心数据
 		getUserInfoPersonal(organizeId) {
@@ -205,21 +218,38 @@ export default {
 		},
 		// 栏目跳转
 		navigator(url) {
-			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`)
+			const _that = this
+			authorize
+				.checkLogin()
+				.then(res => {
+					if (res === 1) {
+						if (this.hasLogin) {
+							// 已登录
+							_that.$api.navigateTo(url)
+						} else {
+							// 未登录
+							_that.$api.navigateTo(`/pages/login/login`)
+						}
 					}
-				} else {
-					this.$api.navigateTo(`/pages/authorization/authorization`)
-				}
-			})
+				})
+				.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`)
+			// 	}
+			// })
 		},
 		//下拉刷新
 		onPullDownRefresh() {