Explorar o código

修改登录逻辑

yangg hai 2 meses
pai
achega
fc657553a1

+ 6 - 7
pages/index/index.vue

@@ -113,7 +113,6 @@ import { apiBaseUrl } from '@/common/config.js';
 			}
 		},
 		onLoad() {
-			this.checkLogin();
 			this.checkUserInfo();
 			this.fetchJobList();
 		},
@@ -193,9 +192,9 @@ import { apiBaseUrl } from '@/common/config.js';
 				this.formData.relation = this.relationOptions[this.relationIndex];
 			},
 			checkUserInfo() {
-				if (!this.checkLogin()) {
-					return;
-				}
+				// if (!this.checkLogin()) {
+				// 	return;
+				// }
 				
 				uni.showLoading({
 					title: '加载中...'
@@ -251,9 +250,9 @@ import { apiBaseUrl } from '@/common/config.js';
 						title: '获取用户信息失败',
 						icon: 'none'
 					});
-					uni.navigateTo({
-						url: '/pages/login/login'
-					});
+					// uni.navigateTo({
+					// 	url: '/pages/login/login'
+					// });
 				}
 			},
 			fetchJobList() {

+ 53 - 3
pages/my/my.vue

@@ -9,7 +9,7 @@
 			</view>
 		</view>
 		
-		<view class="user-info" v-else @click="wxLogin">
+		<view class="user-info" v-else @click="goLogin">
 			<image class="avatar" src="/static/avatar.png"></image>
 			<view class="user-details">
 				<text class="username">点击登录</text>
@@ -78,11 +78,54 @@ export default {
 			wxLoginCode: '' // 存储微信登录code
 		}
 	},
+	onLoad() {
+		// 每次显示页面时检查登录状态
+		this.checkLogin();
+	},
 	onShow() {
 		// 每次显示页面时检查登录状态
 		this.checkLoginStatus();
 	},
 	methods: {
+		checkLogin() {
+				const userInfo = uni.getStorageSync('userInfo');
+				if (!userInfo) {
+					// 未登录时跳转到身份验证登录页面
+					uni.reLaunch({
+						url: '/pages/login/login',
+						success: () => {
+							console.log('跳转到身份验证登录页面成功');
+						},
+						fail: (err) => {
+							console.error('跳转到身份验证登录页面失败:', err);
+							uni.showToast({
+								title: '跳转失败,请重试',
+								icon: 'none'
+							});
+						}
+					});
+					return false;
+				}
+				
+				try {
+					const parsedUserInfo = JSON.parse(userInfo);
+					if (!parsedUserInfo.openid) {
+						// 没有openid时跳转到身份验证登录页面
+						uni.navigateTo({
+							url: '/pages/login/login'
+						});
+						return false;
+					}
+					return true;
+				} catch (e) {
+					console.error('解析用户信息失败:', e);
+					uni.removeStorageSync('userInfo');
+					uni.navigateTo({
+						url: '/pages/login/login'
+					});
+					return false;
+				}
+			},
 		// 检查登录状态
 		checkLoginStatus() {
 			try {
@@ -127,6 +170,13 @@ export default {
 		
 		// 前往登录页
 		goLogin() {
+			// 直接跳转到登录页面,不显示选择对话框
+			uni.navigateTo({
+				url: '/pages/login/login'
+			});
+			
+			// 如果您想保留选择功能,可以取消注释下面的代码
+			/*
 			// 显示登录方式选择
 			uni.showActionSheet({
 				itemList: ['账号密码登录', '微信登录'],
@@ -142,6 +192,7 @@ export default {
 					}
 				}
 			});
+			*/
 		},
 		
 		// 微信登录
@@ -589,8 +640,7 @@ export default {
 					icon: 'none'
 				});
 				setTimeout(() => {
-					this.wxLogin()
-					//this.goLogin();
+					this.goLogin();
 				}, 1500);
 				return;
 			}

+ 0 - 7
unpackage/dist/dev/mp-weixin/pages/index/index.js

@@ -36,7 +36,6 @@ const _sfc_main = {
     };
   },
   onLoad() {
-    this.checkLogin();
     this.checkUserInfo();
     this.fetchJobList();
   },
@@ -98,9 +97,6 @@ const _sfc_main = {
       this.formData.relation = this.relationOptions[this.relationIndex];
     },
     checkUserInfo() {
-      if (!this.checkLogin()) {
-        return;
-      }
       common_vendor.index.showLoading({
         title: "加载中..."
       });
@@ -149,9 +145,6 @@ const _sfc_main = {
           title: "获取用户信息失败",
           icon: "none"
         });
-        common_vendor.index.navigateTo({
-          url: "/pages/login/login"
-        });
       }
     },
     fetchJobList() {

+ 43 - 13
unpackage/dist/dev/mp-weixin/pages/my/my.js

@@ -28,10 +28,49 @@ const _sfc_main = {
       // 存储微信登录code
     };
   },
+  onLoad() {
+    this.checkLogin();
+  },
   onShow() {
     this.checkLoginStatus();
   },
   methods: {
+    checkLogin() {
+      const userInfo = common_vendor.index.getStorageSync("userInfo");
+      if (!userInfo) {
+        common_vendor.index.reLaunch({
+          url: "/pages/login/login",
+          success: () => {
+            console.log("跳转到身份验证登录页面成功");
+          },
+          fail: (err) => {
+            console.error("跳转到身份验证登录页面失败:", err);
+            common_vendor.index.showToast({
+              title: "跳转失败,请重试",
+              icon: "none"
+            });
+          }
+        });
+        return false;
+      }
+      try {
+        const parsedUserInfo = JSON.parse(userInfo);
+        if (!parsedUserInfo.openid) {
+          common_vendor.index.navigateTo({
+            url: "/pages/login/login"
+          });
+          return false;
+        }
+        return true;
+      } catch (e) {
+        console.error("解析用户信息失败:", e);
+        common_vendor.index.removeStorageSync("userInfo");
+        common_vendor.index.navigateTo({
+          url: "/pages/login/login"
+        });
+        return false;
+      }
+    },
     // 检查登录状态
     checkLoginStatus() {
       try {
@@ -66,17 +105,8 @@ const _sfc_main = {
     },
     // 前往登录页
     goLogin() {
-      common_vendor.index.showActionSheet({
-        itemList: ["账号密码登录", "微信登录"],
-        success: (res) => {
-          if (res.tapIndex === 0) {
-            common_vendor.index.navigateTo({
-              url: "/pages/login/login"
-            });
-          } else if (res.tapIndex === 1) {
-            this.wxLogin();
-          }
-        }
+      common_vendor.index.navigateTo({
+        url: "/pages/login/login"
       });
     },
     // 微信登录
@@ -432,7 +462,7 @@ const _sfc_main = {
           icon: "none"
         });
         setTimeout(() => {
-          this.wxLogin();
+          this.goLogin();
         }, 1500);
         return;
       }
@@ -534,7 +564,7 @@ function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
     e: common_vendor.t($options.formatPhone($data.userInfo.phone))
   } : {}) : {
     f: common_assets._imports_0$1,
-    g: common_vendor.o((...args) => $options.wxLogin && $options.wxLogin(...args))
+    g: common_vendor.o((...args) => $options.goLogin && $options.goLogin(...args))
   }, {
     h: common_vendor.f($data.menuItems, (item, index, i0) => {
       return {