Jelajahi Sumber

:update login

0235699曾露 10 jam lalu
induk
melakukan
662e3d0c79
3 mengubah file dengan 134 tambahan dan 56 penghapusan
  1. 3 1
      index.js
  2. 109 53
      nova-pbf/components/home/index.js
  3. 22 2
      nova-pbf/pages/index/index.js

+ 3 - 1
index.js

@@ -137,9 +137,11 @@ Page({
     activityId && wx.setStorageSync("activityId", activityId);
     activityId && wx.setStorageSync("activityId", activityId);
     inviteHost && wx.setStorageSync("inviteHost", true);
     inviteHost && wx.setStorageSync("inviteHost", true);
     if (storeId) {
     if (storeId) {
+      // 扫码进入时,强制设置店铺 ID,标记为扫码来源
       wx.setStorageSync('storeId', storeId)
       wx.setStorageSync('storeId', storeId)
       getApp().globalData.storeId = storeId
       getApp().globalData.storeId = storeId
-      console.log('✅ 入口页已设置店铺 ID:', storeId)
+      wx.setStorageSync('storeId_from_scan', true)
+      console.log('✅ 入口页扫码进入,已设置店铺 ID(优先级最高):', storeId)
     }
     }
     if (company) getApp().globalData.toCompany = true;
     if (company) getApp().globalData.toCompany = true;
     
     

+ 109 - 53
nova-pbf/components/home/index.js

@@ -242,33 +242,87 @@ Component({
 
 
     /**
     /**
      * 获取店铺 ID
      * 获取店铺 ID
+     * 优先级:
+     * 1. 扫码进入的 storeId(最高优先级)
+     * 2. 用户历史访问的 storeId
+     * 3. 超级门店 ID(默认值)
+     * 
+     * 注意:如果需要切换店铺(非扫码),请先调用:
+     * wx.removeStorageSync('storeId_from_scan')
      */
      */
     async getStoreId() {
     async getStoreId() {
       try {
       try {
         console.log('🏪 开始获取店铺 ID...');
         console.log('🏪 开始获取店铺 ID...');
         const defaultStoreId = 'pkPdAnLAUZ';  // 超级门店ID
         const defaultStoreId = 'pkPdAnLAUZ';  // 超级门店ID
-        const configuredStoreId =
-          wx.getStorageSync('storeId') ||
-          getApp().globalData.storeId ||
-          defaultStoreId;
-        console.log('✅ 使用店铺 ID:', configuredStoreId);
+        
+        // 检查是否是扫码进入
+        const isFromScan = wx.getStorageSync('storeId_from_scan');
+        const storedStoreId = wx.getStorageSync('storeId');
+        const globalStoreId = getApp().globalData.storeId;
+        
+        let configuredStoreId;
+        
+        if (isFromScan && storedStoreId) {
+          // 优先级1:扫码进入的店铺(最高优先级)
+          configuredStoreId = storedStoreId;
+          console.log('🎯 使用扫码进入的店铺 ID:', configuredStoreId);
+        } else if (storedStoreId) {
+          // 优先级2:用户历史访问的店铺
+          configuredStoreId = storedStoreId;
+          console.log('📍 使用历史访问的店铺 ID:', configuredStoreId);
+        } else if (globalStoreId) {
+          // 优先级3:全局设置的店铺
+          configuredStoreId = globalStoreId;
+          console.log('🌐 使用全局店铺 ID:', configuredStoreId);
+        } else {
+          // 优先级4:默认超级门店
+          configuredStoreId = defaultStoreId;
+          console.log('⭐ 使用默认超级门店 ID:', configuredStoreId);
+        }
+        
+        console.log('✅ 最终使用店铺 ID:', configuredStoreId);
         return configuredStoreId;
         return configuredStoreId;
       } catch (error) {
       } catch (error) {
         console.error('❌ 获取店铺 ID 失败:', error);
         console.error('❌ 获取店铺 ID 失败:', error);
-        return null;
+        return 'pkPdAnLAUZ';
       }
       }
     },
     },
 
 
     /**
     /**
      * 获取店铺信息(id 与 名称)
      * 获取店铺信息(id 与 名称)
+     * 优先级:
+     * 1. 扫码进入的 storeId(最高优先级)
+     * 2. 用户历史访问的 storeId
+     * 3. 超级门店 ID(默认值)
      */
      */
     async getStoreInfo() {
     async getStoreInfo() {
       try {
       try {
         const defaultStoreId = 'pkPdAnLAUZ';  // 超级门店ID
         const defaultStoreId = 'pkPdAnLAUZ';  // 超级门店ID
-        const effectiveStoreId =
-          wx.getStorageSync('storeId') ||
-          getApp().globalData.storeId ||
-          defaultStoreId;
+        
+        // 检查是否是扫码进入
+        const isFromScan = wx.getStorageSync('storeId_from_scan');
+        const storedStoreId = wx.getStorageSync('storeId');
+        const globalStoreId = getApp().globalData.storeId;
+        
+        let effectiveStoreId;
+        
+        if (isFromScan && storedStoreId) {
+          // 优先级1:扫码进入的店铺(最高优先级)
+          effectiveStoreId = storedStoreId;
+          console.log('🎯 使用扫码进入的店铺 ID:', effectiveStoreId);
+        } else if (storedStoreId) {
+          // 优先级2:用户历史访问的店铺
+          effectiveStoreId = storedStoreId;
+          console.log('📍 使用历史访问的店铺 ID:', effectiveStoreId);
+        } else if (globalStoreId) {
+          // 优先级3:全局设置的店铺
+          effectiveStoreId = globalStoreId;
+          console.log('🌐 使用全局店铺 ID:', effectiveStoreId);
+        } else {
+          // 优先级4:默认超级门店
+          effectiveStoreId = defaultStoreId;
+          console.log('⭐ 使用默认超级门店 ID:', effectiveStoreId);
+        }
 
 
         const query = new Parse.Query('ShopStore');
         const query = new Parse.Query('ShopStore');
         query.equalTo('objectId', effectiveStoreId);
         query.equalTo('objectId', effectiveStoreId);
@@ -284,7 +338,8 @@ Component({
         }
         }
       } catch (e) {
       } catch (e) {
         console.error('获取店铺信息失败:', e);
         console.error('获取店铺信息失败:', e);
-        return { id: wx.getStorageSync('storeId') || 'pkPdAnLAUZ', name: '' };
+        const fallbackId = wx.getStorageSync('storeId') || 'pkPdAnLAUZ';
+        return { id: fallbackId, name: '' };
       }
       }
     },
     },
 
 
@@ -296,17 +351,40 @@ Component({
       console.log('======= navigateToHome 被调用 =======');
       console.log('======= navigateToHome 被调用 =======');
       console.log('===========================================');
       console.log('===========================================');
       
       
-      const currentUser = Parse.User.current();
+      let currentUser = Parse.User.current();
       let userInfo = wx.getStorageSync("userLogin");
       let userInfo = wx.getStorageSync("userLogin");
       
       
-      console.log('当前用户:', currentUser ? '已登录' : '未登录');
-      console.log('userInfo:', userInfo);
+      console.log('当前用户:', currentUser ? currentUser.id : '无');
+      console.log('用户手机号:', currentUser?.get('mobile') || '无');
+      console.log('userInfo:', userInfo || '无');
       
       
-      // 如果用户未登录,直接进入游客模式(简化流程)
-      if (!currentUser || userInfo == '') {
-        console.log('⚠️ 用户未登录,直接进入游客模式');
+      // 如果有 Parse 用户但没有 userInfo,可能是刚登录,等待同步
+      if (currentUser && currentUser.get('mobile') && !userInfo) {
+        console.log('⏳ 检测到刚登录,等待状态同步...');
+        
+        // 等待 500ms
+        await new Promise(resolve => setTimeout(resolve, 500));
         
         
-        // 直接进入游客模式,不显示提示框
+        // 重新获取
+        currentUser = Parse.User.current();
+        userInfo = wx.getStorageSync("userLogin");
+        
+        // 如果还是没有,手动设置
+        if (currentUser && currentUser.get('mobile') && !userInfo) {
+          wx.setStorageSync("userLogin", currentUser.id);
+          userInfo = currentUser.id;
+          console.log('✅ 手动设置 userLogin:', userInfo);
+        }
+      }
+      
+      // 重新检查登录状态
+      const isLoggedIn = currentUser && currentUser.get('mobile') && userInfo;
+      
+      console.log('最终登录状态:', isLoggedIn ? '已登录' : '未登录');
+      
+      // 如果用户未登录,进入游客模式
+      if (!isLoggedIn) {
+        console.log('⚠️ 用户未登录,进入游客模式');
         this.navigateToHomeAsGuest();
         this.navigateToHomeAsGuest();
         return;
         return;
       }
       }
@@ -603,42 +681,20 @@ Component({
             if (res.confirm) {
             if (res.confirm) {
               console.log('用户选择:立即登录');
               console.log('用户选择:立即登录');
               
               
-              // 显示加载提示
-              wx.showLoading({
-                title: '准备登录...',
-                mask: true
-              });
+              // 不要清除登录状态,因为可能用户刚登录成功只是状态没同步
+              // 直接跳转到登录页面
+              console.log('准备调用 login.loginNow()');
               
               
-              try {
-                // 强制清除所有登录相关的缓存
-                console.log('🧹 清除旧的登录状态...');
-                await clearLogin.clearAllLoginState();
-                
-                // 等待一下,确保状态清除完成
-                await new Promise(resolve => setTimeout(resolve, 300));
-                
-                wx.hideLoading();
-                
-                console.log('准备调用 login.loginNow()');
-                
-                // 直接调用登录方法
-                const loginResult = login.loginNow();
-                console.log('login.loginNow() 返回值:', loginResult);
-                
-                // 如果返回 false,说明已经跳转到登录页面
-                if (!loginResult) {
-                  console.log('✅ 已跳转到登录页面');
-                  // 设置待处理的跳转,登录成功后自动跳转到咨询页面
-                  wx.setStorageSync('pendingNavigation', 'consultation');
-                  console.log('📌 已设置待处理跳转: consultation');
-                }
-              } catch (error) {
-                wx.hideLoading();
-                console.error('❌ 登录准备失败:', error);
-                wx.showToast({
-                  title: '登录准备失败,请重试',
-                  icon: 'none'
-                });
+              // 直接调用登录方法
+              const loginResult = login.loginNow();
+              console.log('login.loginNow() 返回值:', loginResult);
+              
+              // 如果返回 false,说明已经跳转到登录页面
+              if (!loginResult) {
+                console.log('✅ 已跳转到登录页面');
+                // 设置待处理的跳转,登录成功后自动跳转到咨询页面
+                wx.setStorageSync('pendingNavigation', 'consultation');
+                console.log('📌 已设置待处理跳转: consultation');
               }
               }
             } else {
             } else {
               console.log('用户选择:取消');
               console.log('用户选择:取消');

+ 22 - 2
nova-pbf/pages/index/index.js

@@ -19,6 +19,9 @@ Page({
 
 
     try {
     try {
       let finalStoreId = options && options.storeId ? options.storeId : '';
       let finalStoreId = options && options.storeId ? options.storeId : '';
+      
+      // 标记是否是扫码进入(有明确的 storeId 参数)
+      const isFromScan = !!finalStoreId;
 
 
       // 支持从 scene 读取(小程序码 getwxacodeunlimit 传参)
       // 支持从 scene 读取(小程序码 getwxacodeunlimit 传参)
       if (!finalStoreId && options && options.scene) {
       if (!finalStoreId && options && options.scene) {
@@ -37,11 +40,28 @@ Page({
       }
       }
 
 
       if (finalStoreId) {
       if (finalStoreId) {
+        // 扫码进入时,强制设置店铺 ID,不被历史记录覆盖
         wx.setStorageSync('storeId', finalStoreId);
         wx.setStorageSync('storeId', finalStoreId);
         getApp().globalData.storeId = finalStoreId;
         getApp().globalData.storeId = finalStoreId;
-        console.log('✅ 已设置店铺 ID:', finalStoreId);
+        
+        // 标记这是扫码进入的店铺,优先级最高
+        if (isFromScan) {
+          wx.setStorageSync('storeId_from_scan', true);
+          console.log('✅ 扫码进入,已设置店铺 ID(优先级最高):', finalStoreId);
+        } else {
+          console.log('✅ 已设置店铺 ID:', finalStoreId);
+        }
       } else {
       } else {
-        console.log('ℹ️ 未传入店铺 ID,继续使用默认值');
+        // 没有传入 storeId,检查是否有扫码标记
+        const isFromScanBefore = wx.getStorageSync('storeId_from_scan');
+        
+        if (!isFromScanBefore) {
+          // 不是扫码进入,可以使用历史店铺
+          console.log('ℹ️ 未传入店铺 ID,使用历史店铺或默认值');
+        } else {
+          // 之前是扫码进入的,保持扫码的店铺
+          console.log('ℹ️ 保持扫码进入的店铺 ID');
+        }
       }
       }
     } catch (e) {
     } catch (e) {
       console.error('设置店铺 ID 失败:', e);
       console.error('设置店铺 ID 失败:', e);