Browse Source

补充登录时缓存openid

littleblue55 2 days ago
parent
commit
4cc198cfae
4 changed files with 50 additions and 16 deletions
  1. 1 1
      api/edu.js
  2. 1 0
      pages/initial/initial.vue
  3. 31 14
      pages/login/login.vue
  4. 17 1
      store/authStore.js

+ 1 - 1
api/edu.js

@@ -101,4 +101,4 @@ export function loadCourseHistory(data) {
 		'method': 'post',
 		data: data
 	})
-}
+}

+ 1 - 0
pages/initial/initial.vue

@@ -100,6 +100,7 @@
 				const openidRes = await wxLoginOpenid({code : wxcode})
 				if(openidRes?.data){
 					const { openid, sessionKey } = openidRes.data
+					authStore.setOpenid(openid);
 					const phoneRes = await wxloginPhone({ openid, phoneCode: code})
 					const { phone, token, isNewUser } = phoneRes.data;
 					authStore.setPhone(phone);

+ 31 - 14
pages/login/login.vue

@@ -218,18 +218,24 @@
 				uni.showLoading({
 					title: '正在登录...'
 				})
-				loginPhone(this.phoneForm).then(res => {
-					if (res.data.token) {
-						msgSuccess("登录成功!")
-						this.useAuthStore.setAuthToken(res.data.token)
+				let res = await loginPhone(this.phoneForm)
+				if (res.data.token) {
+					msgSuccess("登录成功!")
+					this.useAuthStore.setAuthToken(res.data.token)
+					const loginRes = await wx.login();
+					const code = loginRes.code;
+					wxLoginOpenid({code}).then(resp=>{
+						if(resp?.data){
+							const { openid, sessionKey } = resp.data
+							this.useAuthStore.setOpenid(openid)
+						}
 						setTimeout(() => {
 							uni.switchTab({
 								url: "/pages/index/index"
 							})
 						}, 1000)
-					}
-				})
-
+					})
+				}
 			},
 			// 账号登录
 			async userNameLoginHandle() {
@@ -249,18 +255,29 @@
 					...this.userNameForm
 				}
 				form.password = encryptAESCBC(form.password);
-				usernameLogin(form).then(res => {
-					// console.log(res, "登录")
-					if (res.data.token) {
-						msgSuccess("登录成功!")
-						this.useAuthStore.setAuthToken(res.data.token)
+				let res = await usernameLogin(form)
+				if (res.data.token) {
+					msgSuccess("登录成功!")
+					this.useAuthStore.setAuthToken(res.data.token)
+					const loginRes = await wx.login();
+					const code = loginRes.code;
+					wxLoginOpenid({code}).then(resp=>{
+						if(resp?.data){
+							const { openid, sessionKey } = resp.data
+							this.useAuthStore.setOpenid(openid)
+						}
 						setTimeout(() => {
 							uni.switchTab({
 								url: "/pages/index/index"
 							})
 						}, 1000)
-					}
-				})
+					})
+					// setTimeout(() => {
+					// 	uni.switchTab({
+					// 		url: "/pages/index/index"
+					// 	})
+					// }, 1000)
+				}
 			},
 			toPage(url) {
 				uni.navigateTo({

+ 17 - 1
store/authStore.js

@@ -6,7 +6,8 @@ export const useAuthStore = defineStore('auth', {
 		token: null,
 		userInfo: null,
 		phone: null,
-		creditCard: ''
+		creditCard: '',
+		openid: null,
 	}),
 	actions: {
 		setAuthToken(newToken) {
@@ -55,6 +56,18 @@ export const useAuthStore = defineStore('auth', {
 			this.phone = null
 			uni.removeStorageSync("phone")
 		},
+		setOpenid(data) {
+		  this.openid = data
+		  uni.setStorageSync('openid', data)
+		},
+		// 初始化时从Storage加载
+		loadOpenid() {
+		  this.openid = uni.getStorageSync('openid') || null
+		},
+		cleanOpenid() {
+			this.openid = null
+			uni.removeStorageSync("openid")
+		},
 	},
 	getters: {
 		isAuthenticated(state) {
@@ -62,6 +75,9 @@ export const useAuthStore = defineStore('auth', {
 		},
 		isUserInfo(state){
 			return !!state.userInfo;
+		},
+		isOpenid(state){
+			return !!state.openid;
 		}
 	}
 })