Explorar o código

解决登录跳转

sunshibo hai 1 mes
pai
achega
d1ed7a45ab
Modificáronse 5 ficheiros con 34 adicións e 61 borrados
  1. 0 42
      App.vue
  2. 3 2
      components/TabBar.vue
  3. 9 3
      pages/index/index.vue
  4. 13 9
      pages/launch/index.vue
  5. 9 5
      routeInterceptor.js

+ 0 - 42
App.vue

@@ -2,8 +2,6 @@
 import initConfig from '@/initConfig.js';
 // #ifdef MP-WEIXIN
 import { SCAN_QR_CACHE } from '@/settings/enums.js';
-import * as diningApi from '@/api/dining.js';
-import { useUserStore } from '@/store/user.js';
 // #endif
 
 /** 从 scene 或 query 中解析:二维码 s=店铺id,t=桌号id */
@@ -26,44 +24,6 @@ function parseSceneToStoreTable(sceneStr) {
 	return { storeId, tableId };
 }
 
-/**
- * 查询桌位就餐状态,若 inDining 为 true 则跳转点餐页或选择人数页(未登录时)
- * @param {Object} opts - { fromTabBar: boolean } 来自 TabBar 扫码时,inDining 为 false 需 switchTab 到 numberOfDiners
- */
-async function checkTableDiningStatus(opts = {}) {
-	const tableid = uni.getStorageSync('currentTableId') || '';
-
-	try {
-		const res = await diningApi.GetTableDiningStatus(tableid);
-		const raw = (res && typeof res === 'object') ? res : {};
-		const inDining =
-			raw?.inDining === true ||
-			raw?.inDining === 'true' ||
-			raw?.data?.inDining === true ||
-			raw?.data?.inDining === 'true';
-		const dinerCount =
-			raw?.dinerCount ?? raw?.diner ?? raw?.data?.dinerCount ?? raw?.data?.diner ?? uni.getStorageSync('currentDiners') ?? 1;
-		if (!inDining) {
-			if (opts.fromTabBar) uni.switchTab({ url: '/pages/numberOfDiners/index' });
-			return;
-		}
-		uni.setStorageSync('currentDiners', dinerCount);
-		const userStore = useUserStore();
-		if (!userStore.getToken) {
-			uni.reLaunch({
-				url: `/pages/numberOfDiners/index?inDining=1&tableid=${encodeURIComponent(tableid)}&diners=${encodeURIComponent(dinerCount)}`
-			});
-			return;
-		}
-		uni.reLaunch({
-			url: `/pages/orderFood/index?tableid=${encodeURIComponent(tableid)}&diners=${encodeURIComponent(dinerCount)}`
-		});
-	} catch (err) {
-		console.warn('查询桌位就餐状态失败', err);
-		if (opts.fromTabBar) uni.switchTab({ url: '/pages/numberOfDiners/index' });
-	}
-}
-
 export default {
 	onLaunch: function (e) {
 		// 只有小程序执行
@@ -86,8 +46,6 @@ export default {
 		} catch (err) {
 			console.warn('缓存二维码启动参数失败', err);
 		}
-		// 冷启动时由 pages/launch/index 根据接口结果跳转;TabBar 扫码后由 App 统一调用接口并跳转
-		uni.$on('checkTableDiningStatus', () => checkTableDiningStatus({ fromTabBar: true }));
 		// #endif
 	},
 	onShow: function (res) {

+ 3 - 2
components/TabBar.vue

@@ -77,8 +77,9 @@ function handleScanOrder() {
 			uni.setStorageSync(SCAN_QR_CACHE, JSON.stringify(payload));
 			if (storeId) uni.setStorageSync('currentStoreId', storeId);
 			if (tableId) uni.setStorageSync('currentTableId', tableId);
-			// 由 App.vue 统一调用 table-dining-status 接口,根据结果跳转点餐页或选择人数页
-			uni.$emit('checkTableDiningStatus');
+			if (unref(getPath) !== menus[1].link) {
+				uni.switchTab({ url: menus[1].link });
+			}
 		},
 		fail: (err) => {
 			if (err?.errMsg && !String(err.errMsg).includes('cancel')) {

+ 9 - 3
pages/index/index.vue

@@ -133,9 +133,15 @@ const getPriceDecimal = (price) => {
   return dotIndex > -1 ? priceStr.substring(dotIndex + 1) : '00';
 };
 
-// 登录成功回调
-const handleLoginSuccess = (res) => {
-  console.log('登录成功:', res);
+// 登录成功回调:跳转点餐页
+const handleLoginSuccess = () => {
+  const tableid = uni.getStorageSync('currentTableId') || '';
+  const diners = uni.getStorageSync('currentDiners') || '1';
+  const q = [];
+  if (tableid) q.push(`tableid=${encodeURIComponent(tableid)}`);
+  if (diners) q.push(`diners=${encodeURIComponent(diners)}`);
+  const url = q.length ? `/pages/orderFood/index?${q.join('&')}` : '/pages/orderFood/index';
+  go(url);
 };
 
 // 取消登录回调

+ 13 - 9
pages/launch/index.vue

@@ -9,6 +9,7 @@
 import { onLoad } from '@dcloudio/uni-app';
 import * as diningApi from '@/api/dining.js';
 import { useUserStore } from '@/store/user.js';
+import { TOKEN } from '@/settings/enums.js';
 
 const userStore = useUserStore();
 
@@ -21,27 +22,30 @@ async function doRedirect() {
   try {
     const res = await diningApi.GetTableDiningStatus(tableid);
     const raw = (res && typeof res === 'object') ? res : {};
+    // 兼容多种返回:{ inDining: true }、{ data: { inDining: true } }、直接返回 true
     const inDining =
+      res === true ||
+      res === 'true' ||
       raw?.inDining === true ||
       raw?.inDining === 'true' ||
       raw?.data?.inDining === true ||
       raw?.data?.inDining === 'true';
+    const token = userStore.getToken || uni.getStorageSync(TOKEN) || '';
     const dinerCount =
       raw?.dinerCount ?? raw?.diner ?? raw?.data?.dinerCount ?? raw?.data?.diner ?? uni.getStorageSync('currentDiners') ?? 1;
     if (inDining) {
-      uni.setStorageSync('currentDiners', dinerCount);
-      if (!userStore.getToken) {
-        uni.reLaunch({
-          url: `/pages/numberOfDiners/index?inDining=1&tableid=${encodeURIComponent(tableid)}&diners=${encodeURIComponent(dinerCount)}`
-        });
-        return;
-      }
+      uni.setStorageSync('currentDiners', dinerCount);    
       uni.reLaunch({
         url: `/pages/orderFood/index?tableid=${encodeURIComponent(tableid)}&diners=${encodeURIComponent(dinerCount)}`
       });
-      return;
+      
     }
-    uni.reLaunch({ url: '/pages/numberOfDiners/index' });
+    else{
+      uni.reLaunch({
+          url: `/pages/numberOfDiners/index?inDining=1&tableid=${encodeURIComponent(tableid)}&diners=${encodeURIComponent(dinerCount)}`
+        });
+    }
+
   } catch (err) {
     console.warn('查询桌位就餐状态失败,进入选择人数页', err);
     uni.reLaunch({ url: '/pages/numberOfDiners/index' });

+ 9 - 5
routeInterceptor.js

@@ -2,9 +2,11 @@ import { computed, unref } from 'vue';
 import { useUserStore } from '@/store/user.js';
 import { REDIRECT_KEY } from '@/settings/enums.js';
 
-// 登录后可进入页面
-const loginList = [
-];
+// 登录后可进入页面(弹窗提示)
+const loginList = [];
+
+// 未登录时直接跳转首页的页面
+const redirectToIndexPaths = ['/pages/orderFood/index'];
 
 const navigateToInterceptor = {
 	invoke({ url }) {
@@ -13,7 +15,10 @@ const navigateToInterceptor = {
 		const path = url.split('?')[0];
 		if (unref(getToken)) return true;
 
-		console.log('出发前', path, loginList.includes(path));
+		if (redirectToIndexPaths.some((p) => path === p || path.endsWith(p))) {
+			uni.reLaunch({ url: '/pages/index/index' });
+			return false;
+		}
 		if (loginList.includes(path)) {
 			uni.showModal({
 				title: '提示',
@@ -21,7 +26,6 @@ const navigateToInterceptor = {
 				success: function (res) {
 					if (res.confirm) {
 						uni.setStorageSync(REDIRECT_KEY, url);
-					} else if (res.cancel) {
 					}
 				}
 			});