Browse Source

:update login

0235699曾露 8 hours ago
parent
commit
0b4dc36773
4 changed files with 261 additions and 48 deletions
  1. 86 4
      index.js
  2. 107 1
      nova-pbf/components/home/index.js
  3. 23 3
      nova-pbf/components/home/index.wxml
  4. 45 40
      utils/login.js

+ 86 - 4
index.js

@@ -194,12 +194,37 @@ Page({
       console.log('force 参数:', force);
       console.log('===========================================');
       
-      // 修改:不强制登录,允许游客访问
+      // 查询 Company 的 isPublishing 字段
+      let isPublishing = false;
+      try {
+        const companyQuery = new Parse.Query('Company');
+        companyQuery.equalTo('objectId', getApp().globalData.company);
+        companyQuery.select('isPublishing');
+        const companyObj = await companyQuery.first();
+        
+        if (companyObj) {
+          isPublishing = companyObj.get('isPublishing') === true;
+          console.log('📋 Company isPublishing:', isPublishing);
+        }
+        
+        // 保存到全局,供其他页面使用
+        getApp().globalData.isPublishing = isPublishing;
+        wx.setStorageSync('isPublishing', isPublishing);
+      } catch (error) {
+        console.error('❌ 查询 Company isPublishing 失败:', error);
+        // 查询失败时,默认为 false(强制登录)
+        isPublishing = false;
+      }
+      
+      // 根据 isPublishing 决定是否强制登录
       if (!currentUser || force) {
         console.log('🔄 开始调用 checkAuth...');
         
-        // 尝试静默登录(不强制授权)
-        let r = await checkAuth(false)  // 改为 false,不强制授权
+        // isPublishing == true 时不强制授权,否则强制授权
+        const forceAuth = !isPublishing;
+        console.log('🔐 是否强制登录:', forceAuth);
+        
+        let r = await checkAuth(forceAuth);
         
         console.log('===========================================');
         console.log('======= checkAuth 返回结果 =======');
@@ -212,7 +237,64 @@ Page({
         console.log('checkAuth 后的 Session Token:', currentUser?.getSessionToken()?.substring(0, 20) || '无');
         console.log('===========================================');
         
-        // 即使登录失败,也允许继续访问
+        // 如果强制登录但用户未登录,不允许继续访问
+        if (forceAuth && !r) {
+          console.log('❌ 强制登录模式,用户未登录,停止访问');
+          return;
+        }
+        
+        // 即使登录失败,也允许继续访问(仅在非强制登录模式下)
+        if(!r) {
+          console.log('⚠️ 用户未登录或拒绝授权,允许游客访问');
+          // 不要 return,继续执行后面的跳转逻辑
+        } else {
+          // 登录成功,设置 userLogin
+          if (currentUser && currentUser.get('mobile')) {
+            wx.setStorageSync("userLogin", currentUser.id);
+            console.log('✅ 授权登录成功,已设置 userLogin:', currentUser.id);
+            console.log('✅ 用户手机号:', currentUser.get('mobile'));
+          }
+          
+          // 检查是否有待记录的扫码信息
+          await this.checkAndRecordPendingScan();
+        }
+      } else {
+        // 用户已登录,确保 userLogin 已设置
+        if (currentUser.get('mobile')) {
+          wx.setStorageSync("userLogin", currentUser.id);
+        }
+        this.updateUser(currentUser.id)
+        // 用户已登录,检查是否有待记录的扫码信息
+        await this.checkAndRecordPendingScan();
+      }
+        // 游客模式(isPublishing == true)
+        if (!currentUser || force) {
+          console.log('🔄 游客模式,开始调用 checkAuth(false)...');
+          
+          // 尝试静默登录(不强制授权)
+          let r = await checkAuth(false);  // 不强制授权
+          
+          console.log('===========================================');
+          console.log('======= checkAuth 返回结果 =======');
+          console.log('返回值:', r);
+          
+          // 重新获取用户信息
+          currentUser = Parse.User.current();
+          console.log('checkAuth 后的用户:', currentUser ? currentUser.id : '无');
+          console.log('checkAuth 后的手机号:', currentUser?.get('mobile') || '无');
+          console.log('checkAuth 后的 Session Token:', currentUser?.getSessionToken()?.substring(0, 20) || '无');
+          console.log('===========================================');
+          
+          // 即使登录失败,也允许继续访问
+          if(!r) {
+            console.log('⚠️ 用户未登录或拒绝授权,允许游客访问');
+            // 不要 return,继续执行后面的跳转逻辑
+          } else {
+            // 登录成功,设置 userLogin❌ 强制登录模式,用户未登录,停止访问');
+          return;
+        }
+        
+        // 即使登录失败,也允许继续访问(仅在非强制登录模式下)
         if(!r) {
           console.log('⚠️ 用户未登录或拒绝授权,允许游客访问');
           // 不要 return,继续执行后面的跳转逻辑

+ 107 - 1
nova-pbf/components/home/index.js

@@ -14,6 +14,8 @@ Component({
   data: {
     // 轮播图相关数据
     currentSwiperIndex: 0,
+    // 是否显示 AI 相关内容(根据 Company.isPublishing 字段)
+    showAI: false,
   },
 
   lifetimes: {
@@ -27,14 +29,20 @@ Component({
       
       // 在控制台显示当前登录用户信息
       this.showCurrentUser();
+      
+      // 检查 isPublishing 状态
+      await this.checkPublishingStatus();
     },
   },
 
   pageLifetimes: {
-    show: function() {
+    show: async function() {
       console.log('===========================================');
       console.log('======= 页面显示 (pageLifetimes.show) =======');
       
+      // 重新检查 isPublishing 状态
+      await this.checkPublishingStatus();
+      
       // 延迟检查登录状态,等待 Parse 更新
       setTimeout(() => {
         const currentUser = Parse.User.current();
@@ -105,6 +113,53 @@ Component({
       }
     },
 
+    /**
+     * 检查 Company 的 isPublishing 状态
+     * 只有 isPublishing == true 时才显示 AI 相关内容
+     */
+    async checkPublishingStatus() {
+      try {
+        // 先尝试从全局或缓存读取
+        let isPublishing = getApp().globalData.isPublishing;
+        
+        if (isPublishing === undefined) {
+          isPublishing = wx.getStorageSync('isPublishing');
+        }
+        
+        // 如果缓存中没有,查询数据库
+        if (isPublishing === undefined || isPublishing === '') {
+          const companyQuery = new Parse.Query('Company');
+          companyQuery.equalTo('objectId', getApp().globalData.company);
+          companyQuery.select('isPublishing');
+          const companyObj = await companyQuery.first();
+          
+          if (companyObj) {
+            isPublishing = companyObj.get('isPublishing') === true;
+          } else {
+            isPublishing = false;
+          }
+          
+          // 保存到全局和缓存
+          getApp().globalData.isPublishing = isPublishing;
+          wx.setStorageSync('isPublishing', isPublishing);
+        }
+        
+        console.log('📋 isPublishing 状态:', isPublishing);
+        console.log('🤖 是否显示 AI:', isPublishing);
+        
+        // 更新组件数据
+        this.setData({
+          showAI: isPublishing === true
+        });
+      } catch (error) {
+        console.error('❌ 检查 isPublishing 状态失败:', error);
+        // 出错时默认不显示 AI
+        this.setData({
+          showAI: false
+        });
+      }
+    },
+
     /**
      * 在控制台显示当前登录用户信息
      */
@@ -148,6 +203,57 @@ Component({
       }
     },
 
+    /**
+     * 检查 Company 的 isPublishing 状态
+     * 用于控制是否显示 AI 相关内容
+     */
+    async checkPublishingStatus() {
+      try {
+        console.log('🔍 开始检查 isPublishing 状态...');
+        
+        // 先尝试从缓存读取
+        let isPublishing = wx.getStorageSync('isPublishing');
+        
+        if (isPublishing === '' || isPublishing === null || isPublishing === undefined) {
+          // 缓存中没有,查询 Company 表
+          const companyQuery = new Parse.Query('Company');
+          companyQuery.equalTo('objectId', getApp().globalData.company);
+          companyQuery.select('isPublishing');
+          const companyObj = await companyQuery.first();
+          
+          if (companyObj) {
+            isPublishing = companyObj.get('isPublishing') === true;
+            console.log('📋 从数据库查询 isPublishing:', isPublishing);
+            
+            // 保存到缓存
+            wx.setStorageSync('isPublishing', isPublishing);
+            getApp().globalData.isPublishing = isPublishing;
+          } else {
+            console.warn('⚠️ 未找到 Company 记录');
+            isPublishing = false;
+          }
+        } else {
+          console.log('📦 从缓存读取 isPublishing:', isPublishing);
+        }
+        
+        // 更新组件状态
+        this.setData({
+          showAI: isPublishing === true
+        });
+        
+        console.log('✅ isPublishing 状态检查完成');
+        console.log('   - isPublishing:', isPublishing);
+        console.log('   - showAI:', isPublishing === true);
+        console.log('===========================================');
+      } catch (error) {
+        console.error('❌ 检查 isPublishing 状态失败:', error);
+        // 出错时默认不显示 AI
+        this.setData({
+          showAI: false
+        });
+      }
+    },
+
     /**
      * 点击"立即开始探索"按钮
      */

+ 23 - 3
nova-pbf/components/home/index.wxml

@@ -42,8 +42,8 @@
         </view>
       </swiper-item>
       
-      <!-- 第3屏:AI专属顾问 -->
-      <swiper-item>
+      <!-- 第3屏:AI专属顾问(仅在 isPublishing == true 时显示) -->
+      <swiper-item wx:if="{{showAI}}">
         <view class="swiper-item-content swiper-item-3">
           <view class="feature-icon">🤖</view>
           <view class="feature-title">AI专属顾问 智能推荐</view>
@@ -57,6 +57,18 @@
           </view>
         </view>
       </swiper-item>
+      
+      <!-- 第3屏:专业服务(isPublishing != true 时显示,替代 AI) -->
+      <swiper-item wx:if="{{!showAI}}">
+        <view class="swiper-item-content swiper-item-3">
+          <view class="feature-icon">👨‍💼</view>
+          <view class="feature-title">专业顾问 贴心服务</view>
+          <view class="feature-desc">
+            <text>专业团队为您服务</text>
+            <text>量身定制最佳方案</text>
+          </view>
+        </view>
+      </swiper-item>
     </swiper>
   </view>
 
@@ -82,11 +94,19 @@
         <view class="card-desc">专属设计</view>
       </view>
       
-      <view class="value-card" bindtap="navigateToConsultation">
+      <!-- AI推荐卡片(仅在 isPublishing == true 时显示) -->
+      <view class="value-card" bindtap="navigateToConsultation" wx:if="{{showAI}}">
         <view class="card-icon">🤖</view>
         <view class="card-title">AI推荐</view>
         <view class="card-desc">智能匹配</view>
       </view>
+      
+      <!-- 专业咨询卡片(isPublishing != true 时显示,替代 AI) -->
+      <view class="value-card" bindtap="navigateToConsultation" wx:if="{{!showAI}}">
+        <view class="card-icon">💬</view>
+        <view class="card-title">专业咨询</view>
+        <view class="card-desc">贴心服务</view>
+      </view>
     </view>
   </view>
 

+ 45 - 40
utils/login.js

@@ -1,6 +1,6 @@
 var Parse = getApp().Parse;
 
-function loginNow(forceAuth = true) {
+function loginNow(authPage = 'plugin://fm-plugin/fm-auth') {
   console.log('===========================================');
   console.log('======= 开始登录流程 =======');
   
@@ -28,51 +28,56 @@ function loginNow(forceAuth = true) {
     wx.removeStorageSync('userLogin');
   }
   
-  // 使用插件的 checkAuth 方法进行授权
-  console.log('🔄 调用插件的 checkAuth 方法...');
+  // 跳转到授权页面
+  console.log('🔄 准备跳转到授权页面:', authPage);
   
-  const checkAuth = getApp().checkAuth;
-  if (checkAuth) {
-    // 调用插件的授权方法
-    checkAuth(forceAuth).then((result) => {
-      console.log('✅ checkAuth 成功:', result);
-      
-      // 授权成功后,更新本地存储
-      const newUser = Parse.User.current();
-      if (newUser && newUser.get('mobile')) {
-        wx.setStorageSync("userLogin", newUser.id);
-        console.log('✅ 已设置 userLogin:', newUser.id);
-        console.log('✅ 用户手机号:', newUser.get('mobile'));
-        
-        // 提示用户授权成功
+  // 检查页面栈
+  const pages = getCurrentPages();
+  console.log('当前页面栈层数:', pages.length);
+  
+  // 如果页面栈接近满了(>= 9层),使用 redirectTo
+  if (pages.length >= 9) {
+    console.log('⚠️ 页面栈接近满,使用 redirectTo');
+    wx.redirectTo({
+      url: authPage,
+      success: () => {
+        console.log('✅ redirectTo 跳转成功');
+      },
+      fail: (err) => {
+        console.error('❌ redirectTo 失败:', err);
         wx.showToast({
-          title: '登录成功',
-          icon: 'success',
-          duration: 2000
+          title: '跳转失败,请重试',
+          icon: 'none'
         });
-        
-        // 返回上一页或刷新当前页
-        setTimeout(() => {
-          const pages = getCurrentPages();
-          if (pages.length > 1) {
-            wx.navigateBack();
-          }
-        }, 2000);
       }
-    }).catch((err) => {
-      console.error('❌ checkAuth 失败:', err);
-      wx.showToast({
-        title: '授权失败,请重试',
-        icon: 'none',
-        duration: 2000
-      });
     });
   } else {
-    console.error('❌ 找不到 checkAuth 方法');
-    wx.showToast({
-      title: '登录功能异常,请重启小程序',
-      icon: 'none',
-      duration: 3000
+    // 使用 navigateTo
+    console.log('📱 使用 navigateTo 跳转');
+    wx.navigateTo({
+      url: authPage,
+      success: () => {
+        console.log('✅ navigateTo 跳转成功');
+      },
+      fail: (err) => {
+        console.error('❌ navigateTo 失败:', err);
+        console.log('⚠️ 尝试使用 redirectTo');
+        
+        // 降级使用 redirectTo
+        wx.redirectTo({
+          url: authPage,
+          success: () => {
+            console.log('✅ redirectTo 跳转成功');
+          },
+          fail: (err2) => {
+            console.error('❌ redirectTo 也失败:', err2);
+            wx.showToast({
+              title: '跳转失败,请重试',
+              icon: 'none'
+            });
+          }
+        });
+      }
     });
   }