0235699曾露 пре 1 дан
родитељ
комит
80369ee040
1 измењених фајлова са 267 додато и 18 уклоњено
  1. 267 18
      index.js

+ 267 - 18
index.js

@@ -308,6 +308,9 @@ Page({
           
           // 检查是否有待记录的扫码信息
           await this.checkAndRecordPendingScan();
+          
+          // 如果用户没有来源信息,且当前有扫码参数,记录来源
+          await this.checkAndRecordUserSourceOnLogin();
         }
       } else {
         console.log('✅ 用户已登录,跳过 checkAuth');
@@ -318,8 +321,12 @@ Page({
           console.log('✅ 已确认 userLogin:', currentUser.id);
         }
         this.updateUser(currentUser.id);
+        
         // 用户已登录,检查是否有待记录的扫码信息
         await this.checkAndRecordPendingScan();
+        
+        // 如果用户没有来源信息,且当前有扫码参数,记录来源
+        await this.checkAndRecordUserSourceOnLogin();
       }
       getApp().Parse = Parse
       getApp().checkAuth = checkAuth
@@ -468,6 +475,7 @@ Page({
   
   /**
    * 检查并记录待处理的扫码统计
+   * 同时记录用户来源信息到 _User 表
    */
   async checkAndRecordPendingScan() {
     try {
@@ -493,6 +501,9 @@ Page({
         return;
       }
       
+      // 记录用户来源信息
+      await this.recordUserSource(pendingScan);
+      
       // 记录扫码统计
       await this.recordScanStatistics({
         storeId: pendingScan.storeId,
@@ -514,6 +525,238 @@ Page({
       console.error('❌ 记录待处理扫码信息失败:', error);
     }
   },
+  
+  /**
+   * 记录用户来源信息到 _User 表
+   * 根据扫码参数判断来源类型并保存到用户的 source 字段
+   * 
+   * 来源类型:
+   * 1. 渠道xxx→异业xxx(老板后台添加的异业)
+   * 2. 渠道xxx→异业xxx→员工xxx(员工后台添加的异业)
+   * 3. 员工xxx
+   * 4. 老板
+   * 5. 自主进入(无任何推荐)
+   */
+  async recordUserSource(scanInfo) {
+    try {
+      const currentUser = Parse.User.current();
+      if (!currentUser) {
+        console.log('⚠️ 用户未登录,无法记录来源');
+        return;
+      }
+      
+      // 如果用户已经有来源信息,不再覆盖(首次来源原则)
+      const existingSource = currentUser.get('source');
+      if (existingSource) {
+        console.log('ℹ️ 用户已有来源信息,不覆盖:', existingSource);
+        return;
+      }
+      
+      console.log('📊 ===========================================');
+      console.log('📊 [记录来源] 开始记录用户来源信息');
+      console.log('📊 用户ID:', currentUser.id);
+      console.log('📊 ===========================================');
+      
+      const { partnerId, employeeId, ownerId, userId, storeId } = scanInfo;
+      
+      let sourceInfo = {
+        type: 'self_entry',  // 默认为自主进入
+        label: '自主进入',
+        timestamp: new Date(),
+        storeId: storeId
+      };
+      
+      // 判断来源类型
+      if (partnerId && employeeId) {
+        // 情况2: 渠道xxx→异业xxx→员工xxx(员工后台添加的异业)
+        console.log('🔍 [来源类型] 渠道→异业→员工');
+        
+        try {
+          // 查询异业信息
+          const partnerQuery = new Parse.Query('Partner');
+          const partner = await partnerQuery.get(partnerId);
+          const partnerName = partner.get('name') || '未知异业';
+          const channelName = partner.get('channelName') || partner.get('category') || '未知渠道';
+          
+          // 查询员工信息
+          const employeeQuery = new Parse.Query('Employee');
+          const employee = await employeeQuery.get(employeeId);
+          const employeeName = employee.get('name') || '未知员工';
+          
+          sourceInfo = {
+            type: 'channel_partner_employee',
+            label: `${channelName},${partnerName},员工${employeeName}`,
+            channelName: channelName,
+            partnerId: partnerId,
+            partnerName: partnerName,
+            employeeId: employeeId,
+            employeeName: employeeName,
+            timestamp: new Date(),
+            storeId: storeId
+          };
+          
+          console.log('✅ [来源信息] 渠道→异业→员工:', sourceInfo.label);
+        } catch (error) {
+          console.error('❌ 查询异业或员工信息失败:', error);
+        }
+        
+      } else if (partnerId && !employeeId) {
+        // 情况1: 渠道xxx→异业xxx(老板后台添加的异业)
+        console.log('🔍 [来源类型] 渠道→异业');
+        
+        try {
+          // 查询异业信息
+          const partnerQuery = new Parse.Query('Partner');
+          const partner = await partnerQuery.get(partnerId);
+          const partnerName = partner.get('name') || '未知异业';
+          const channelName = partner.get('channelName') || partner.get('category') || '未知渠道';
+          
+          sourceInfo = {
+            type: 'channel_partner',
+            label: `${channelName},${partnerName}`,
+            channelName: channelName,
+            partnerId: partnerId,
+            partnerName: partnerName,
+            timestamp: new Date(),
+            storeId: storeId
+          };
+          
+          console.log('✅ [来源信息] 渠道→异业:', sourceInfo.label);
+        } catch (error) {
+          console.error('❌ 查询异业信息失败:', error);
+        }
+        
+      } else if (employeeId && !partnerId) {
+        // 情况3: 员工xxx
+        console.log('🔍 [来源类型] 员工');
+        
+        try {
+          // 查询员工信息
+          const employeeQuery = new Parse.Query('Employee');
+          const employee = await employeeQuery.get(employeeId);
+          const employeeName = employee.get('name') || '未知员工';
+          
+          sourceInfo = {
+            type: 'employee',
+            label: employeeName,
+            employeeId: employeeId,
+            employeeName: employeeName,
+            timestamp: new Date(),
+            storeId: storeId
+          };
+          
+          console.log('✅ [来源信息] 员工:', sourceInfo.label);
+        } catch (error) {
+          console.error('❌ 查询员工信息失败:', error);
+        }
+        
+      } else if (ownerId) {
+        // 情况4: 老板
+        console.log('🔍 [来源类型] 老板');
+        
+        sourceInfo = {
+          type: 'owner',
+          label: '老板',
+          ownerId: ownerId,
+          timestamp: new Date(),
+          storeId: storeId
+        };
+        
+        console.log('✅ [来源信息] 老板');
+        
+      } else if (userId) {
+        // 推广员(如果有的话)
+        console.log('🔍 [来源类型] 推广员');
+        
+        try {
+          // 查询推广员信息
+          const userQuery = new Parse.Query('_User');
+          const promoter = await userQuery.get(userId);
+          const promoterName = promoter.get('username') || promoter.get('mobile') || '未知推广员';
+          
+          sourceInfo = {
+            type: 'promoter',
+            label: `推广员${promoterName}`,
+            userId: userId,
+            promoterName: promoterName,
+            timestamp: new Date(),
+            storeId: storeId
+          };
+          
+          console.log('✅ [来源信息] 推广员:', sourceInfo.label);
+        } catch (error) {
+          console.error('❌ 查询推广员信息失败:', error);
+        }
+        
+      } else {
+        // 情况5: 自主进入(无任何推荐)
+        console.log('🔍 [来源类型] 自主进入');
+        console.log('✅ [来源信息] 自主进入');
+      }
+      
+      // 保存来源信息到用户
+      currentUser.set('source', sourceInfo);
+      await currentUser.save();
+      
+      console.log('✅ [保存成功] 用户来源信息已保存');
+      console.log('   - 来源类型:', sourceInfo.type);
+      console.log('   - 来源标签:', sourceInfo.label);
+      console.log('📊 ===========================================');
+      
+    } catch (error) {
+      console.error('❌ 记录用户来源失败:', error);
+    }
+  },
+  
+  /**
+   * 用户登录后检查并记录来源信息
+   * 如果用户是首次登录且没有来源信息,根据当前的扫码参数记录来源
+   */
+  async checkAndRecordUserSourceOnLogin() {
+    try {
+      const currentUser = Parse.User.current();
+      if (!currentUser) {
+        return;
+      }
+      
+      // 如果用户已经有来源信息,不再处理
+      const existingSource = currentUser.get('source');
+      if (existingSource) {
+        console.log('ℹ️ 用户已有来源信息,跳过记录');
+        return;
+      }
+      
+      // 检查是否有扫码参数
+      const scanStoreId = wx.getStorageSync('scan_storeId');
+      const scanPartnerId = wx.getStorageSync('scan_partnerId');
+      const scanEmployeeId = wx.getStorageSync('scan_employeeId');
+      const scanOwnerId = wx.getStorageSync('scan_ownerId');
+      const scanUserId = wx.getStorageSync('scan_userId');
+      
+      // 如果有任何扫码参数,记录来源
+      if (scanStoreId || scanPartnerId || scanEmployeeId || scanOwnerId || scanUserId) {
+        console.log('📊 检测到扫码参数,记录用户来源');
+        
+        await this.recordUserSource({
+          storeId: scanStoreId,
+          partnerId: scanPartnerId,
+          employeeId: scanEmployeeId,
+          ownerId: scanOwnerId,
+          userId: scanUserId
+        });
+      } else {
+        // 没有任何扫码参数,标记为自主进入
+        console.log('📊 无扫码参数,标记为自主进入');
+        
+        await this.recordUserSource({
+          storeId: wx.getStorageSync('storeId') || null
+        });
+      }
+      
+    } catch (error) {
+      console.error('❌ 检查并记录用户来源失败:', error);
+    }
+  },
 
   async updateUser(id) {
     try {
@@ -621,7 +864,7 @@ Page({
    * 6. 员工邀请二维码(移动端): ?scanCount=0&storeId=xxx&employeeId=xxx
    * 7. 我的二维码(老板): ?scanCount=0&storeId=xxx&ownerId=xxx
    * 8. 我的二维码(员工): ?scanCount=0&storeId=xxx&employeeId=xxx
-   * 9. 方案分享二维码: ?scanCount=0&storeId=xxx&schemeId=xxx&shareUserId=xxx
+   * 9. 方案分享二维码: ?scanCount=0&storeId=xxx&planId=xxx&shareUserId=xxx
    */
   checkAndHandleScan(options) {
     console.log('🔍🔍🔍 ========================================');
@@ -639,11 +882,15 @@ Page({
       caseId,
       userId,  // 推广员二维码使用userId
       activityId,
-      schemeId,  // 方案ID
+      schemeId,  // 方案ID(旧参数名)
+      planId,    // 方案ID(新参数名,优先使用)
       shareUserId,  // 分享方案的用户ID
       q  // 扫码链接
     } = options;
     
+    // planId 和 schemeId 兼容处理,优先使用 planId
+    const finalPlanId = planId || schemeId;
+    
     console.log('📋 [扫码参数] 提取的参数:');
     console.log('   - scanCount:', scanCount || '无');
     console.log('   - storeId:', storeId || '无');
@@ -654,7 +901,9 @@ Page({
     console.log('   - productId:', productId || '无');
     console.log('   - caseId:', caseId || '无');
     console.log('   - activityId:', activityId || '无');
+    console.log('   - planId:', planId || '无');
     console.log('   - schemeId:', schemeId || '无');
+    console.log('   - finalPlanId (最终使用):', finalPlanId || '无');
     console.log('   - shareUserId:', shareUserId || '无');
     console.log('   - q (扫码链接):', q || '无');
     
@@ -717,8 +966,8 @@ Page({
         console.log('📸 [目标内容] 案例ID (caseId):', caseId);
       }
       
-      if (schemeId) {
-        console.log('📋 [目标内容] 方案ID (schemeId):', schemeId);
+      if (finalPlanId) {
+        console.log('📋 [目标内容] 方案ID (planId/schemeId):', finalPlanId);
         console.log('👤 [分享人] 分享用户ID (shareUserId):', shareUserId || '无');
       }
       
@@ -740,8 +989,8 @@ Page({
       if (caseId) {
         wx.setStorageSync('scan_caseId', caseId);
       }
-      if (schemeId) {
-        wx.setStorageSync('scan_schemeId', schemeId);
+      if (finalPlanId) {
+        wx.setStorageSync('scan_planId', finalPlanId);
         if (shareUserId) {
           wx.setStorageSync('scan_shareUserId', shareUserId);
         }
@@ -788,7 +1037,7 @@ Page({
       const userId = wx.getStorageSync('scan_userId');
       const productId = wx.getStorageSync('scan_productId');
       const caseId = wx.getStorageSync('scan_caseId');
-      const schemeId = wx.getStorageSync('scan_schemeId');
+      const planId = wx.getStorageSync('scan_planId');
       const shareUserId = wx.getStorageSync('scan_shareUserId');
       
       console.log('📖 [读取存储] 从本地存储读取扫码信息:');
@@ -802,7 +1051,7 @@ Page({
       console.log('   - userId:', userId || '无');
       console.log('   - productId:', productId || '无');
       console.log('   - caseId:', caseId || '无');
-      console.log('   - schemeId:', schemeId || '无');
+      console.log('   - planId:', planId || '无');
       console.log('   - shareUserId:', shareUserId || '无');
       
       // 清除临时存储
@@ -817,7 +1066,7 @@ Page({
       wx.removeStorageSync('scan_userId');
       wx.removeStorageSync('scan_productId');
       wx.removeStorageSync('scan_caseId');
-      wx.removeStorageSync('scan_schemeId');
+      wx.removeStorageSync('scan_planId');
       wx.removeStorageSync('scan_shareUserId');
       wx.removeStorageSync('need_scan_redirect');
       console.log('✅ [清理完成] 临时存储已清除');
@@ -839,8 +1088,8 @@ Page({
       if (userId) console.log('📊 推广员 ID:', userId);
       if (productId) console.log('📊 产品 ID:', productId);
       if (caseId) console.log('📊 案例 ID:', caseId);
-      if (schemeId) {
-        console.log('📊 方案 ID:', schemeId);
+      if (planId) {
+        console.log('📊 方案 ID:', planId);
         console.log('📊 分享用户 ID:', shareUserId || '无');
       }
       console.log('📊 ===========================================');
@@ -912,9 +1161,9 @@ Page({
       }
       
       // 如果有方案ID,跳转到门店首页并传递方案ID
-      if (schemeId) {
+      if (planId) {
         console.log('🎯 检测到方案ID,跳转到门店首页展示方案');
-        await this.redirectToStoreWithScheme(storeId, schemeId, shareUserId, partnerId);
+        await this.redirectToStoreWithPlan(storeId, planId, shareUserId, partnerId);
         return;
       }
       
@@ -1140,16 +1389,16 @@ Page({
   /**
    * 跳转到门店首页并展示方案
    * @param {string} storeId - 店铺 ID
-   * @param {string} schemeId - 方案 ID
+   * @param {string} planId - 方案 ID
    * @param {string} shareUserId - 分享方案的用户 ID
    * @param {string} partnerId - 可选的合作伙伴 ID
    */
-  async redirectToStoreWithScheme(storeId, schemeId, shareUserId = null, partnerId = null) {
+  async redirectToStoreWithPlan(storeId, planId, shareUserId = null, partnerId = null) {
     try {
       console.log('===========================================');
       console.log('======= 跳转到门店首页展示方案 =======');
       console.log('店铺 ID:', storeId);
-      console.log('方案 ID:', schemeId);
+      console.log('方案 ID:', planId);
       if (shareUserId) console.log('分享用户 ID:', shareUserId);
       if (partnerId) console.log('合作伙伴 ID:', partnerId);
       console.log('===========================================');
@@ -1168,8 +1417,8 @@ Page({
         h5Url += `&guestMode=true`;
       }
       
-      // 添加方案ID
-      h5Url += `&schemeId=${schemeId}`;
+      // 添加方案ID(使用planId参数名)
+      h5Url += `&planId=${planId}`;
       
       // 如果有分享用户ID,也添加到URL中
       if (shareUserId) {