const Parse = getApp().Parse; const company = getApp().globalData.company; const login = require("../../../utils/login"); Component({ /** * 组件的属性列表 */ properties: { }, data: { // 轮播图相关数据 currentSwiperIndex: 0, }, lifetimes: { created() {}, attached: function () { // 页面加载时检查是否首次访问 this.checkFirstVisit(); // 在控制台显示当前登录用户信息 this.showCurrentUser(); }, }, /** * 组件的方法列表 */ methods: { /** * 在控制台显示当前登录用户信息 */ showCurrentUser() { const currentUser = Parse.User.current(); console.log('==========================================='); console.log('======= 当前登录用户信息 ======='); console.log('==========================================='); if (currentUser) { console.log('✅ 用户已登录'); console.log('📱 用户ID:', currentUser.id); console.log('📞 手机号:', currentUser.get('mobile') || '未设置'); console.log('👤 用户名:', currentUser.get('username') || '未设置'); console.log('🔑 Session Token:', currentUser.getSessionToken()); console.log('📧 邮箱:', currentUser.get('email') || '未设置'); console.log('📦 完整用户对象:', currentUser); console.log('📋 所有属性:', currentUser.attributes); } else { console.log('❌ 当前没有登录用户'); } console.log('==========================================='); }, /** * 检查是否首次访问 */ checkFirstVisit() { try { const hasVisited = wx.getStorageSync('hasVisitedWelcome'); if (!hasVisited) { console.log('首次访问,显示引导页'); } else { console.log('已访问过,可选择是否直接跳转'); // 如果需要自动跳转到主页,可以在这里调用 this.navigateToHome() } } catch (e) { console.error('检查访问状态失败:', e); } }, /** * 点击"立即开始探索"按钮 */ onStart() { // 标记已访问 wx.setStorageSync('hasVisitedWelcome', true); // 跳转到 H5 主页 this.navigateToHome(); }, /** * 点击"跳过引导"按钮 */ onSkip() { // 标记已访问 wx.setStorageSync('hasVisitedWelcome', true); // 跳转到 H5 主页 this.navigateToHome(); }, /** * 点击"查看新手指南" */ onGuide() { wx.showToast({ title: '新手指南开发中', icon: 'none', duration: 2000 }); // TODO: 后续可以跳转到新手指南页面 // wx.navigateTo({ // url: '/pages/guide/index' // }); }, /** * 验证 token 是否有效 */ async validateToken(token) { try { console.log('🔍 开始验证 token 有效性...'); // 使用 Parse.User.become() 验证 token const user = await Parse.User.become(token); if (user) { console.log('✅ Token 有效!用户:', user.id); return true; } console.log('⚠️ Token 验证返回空用户'); return false; } catch (error) { console.error('❌ Token 验证失败:', error.message); return false; } }, /** * 刷新用户 session token */ async refreshToken() { try { console.log('🔄 开始刷新 token...'); const currentUser = Parse.User.current(); if (!currentUser) { console.error('❌ 当前没有登录用户,无法刷新 token'); return null; } // 保存用户信息以触发 session token 刷新 await currentUser.fetch(); const newToken = currentUser.getSessionToken(); console.log('✅ Token 刷新成功'); console.log('🔑 新 Token (前20字符):', newToken.substring(0, 20)); return newToken; } catch (error) { console.error('❌ Token 刷新失败:', error.message); return null; } }, /** * 获取店铺 ID */ async getStoreId() { try { console.log('🏪 开始获取店铺 ID...'); // 查询 ShopStore 表,获取第一个店铺 const storeQuery = new Parse.Query('ShopStore'); storeQuery.equalTo('company', company); storeQuery.ascending('score'); storeQuery.limit(1); const store = await storeQuery.first(); if (store) { const storeId = store.id; console.log('✅ 获取店铺 ID 成功:', storeId); return storeId; } else { console.log('⚠️ 未找到店铺,将不带 storeId 参数'); return null; } } catch (error) { console.error('❌ 获取店铺 ID 失败:', error); return null; } }, /** * 获取店铺信息(id 与 名称) */ async getStoreInfo() { try { const query = new Parse.Query('ShopStore'); query.equalTo('company', company); query.ascending('score'); query.limit(1); const store = await query.first(); if (!store) return null; return { id: store.id, name: store.get('storeName') || '' }; } catch (e) { console.error('获取店铺信息失败:', e); return null; } }, /** * 跳转到 H5 主页 */ async navigateToHome() { console.log('==========================================='); console.log('======= 小程序跳转 H5 ======='); console.log('==========================================='); const currentUser = Parse.User.current(); console.log('📱 当前用户:', currentUser); const isAuth = login.loginNow() if (!currentUser || !isAuth) { console.error('❌ 当前没有登录用户!'); wx.showToast({ title: '请先登录', icon: 'none' }); // 跳转到登录页 wx.navigateTo({ url: 'plugin://fm-plugin/fm-auth' }); return; } let token = currentUser.getSessionToken(); console.log('🔑 当前 Session Token:', token); console.log(' - Token 长度:', token ? token.length : 0); console.log(' - Token 前20个字符:', token ? token.substring(0, 20) : 'null'); if (!token) { console.error('❌ 无法获取 Session Token!'); wx.showToast({ title: '获取登录信息失败', icon: 'none' }); return; } // 验证 token 是否有效 console.log('🔍 验证 token 有效性...'); const isValid = await this.validateToken(token); if (!isValid) { console.log('⚠️ Token 已失效,尝试刷新...'); wx.showLoading({ title: '正在刷新登录...' }); // 尝试刷新 token const newToken = await this.refreshToken(); wx.hideLoading(); if (newToken) { token = newToken; console.log('✅ 使用新的 token:', token.substring(0, 20)); } else { console.error('❌ Token 刷新失败,需要重新登录'); wx.showModal({ title: '提示', content: '登录已过期,请重新登录', showCancel: false, success: () => { wx.navigateTo({ url: 'plugin://fm-plugin/fm-auth' }); } }); return; } } else { console.log('✅ Token 验证通过'); } // 获取店铺信息 const store = await this.getStoreInfo(); const storeId = store && store.id ? store.id : null; const storeName = store && store.name ? store.name : ''; // 构建 H5 URL(重要:storeId 和 token 作为查询参数) let h5Url = `https://app.fmode.cn/dev/pobingfeng/owner/nav/home?`; // 如果有 storeId,添加到 URL 中 if (storeId) { h5Url += `storeId=${storeId}&`; } h5Url += `token=${token}`; console.log('🌐 H5 URL:', h5Url); console.log(' - URL 长度:', h5Url.length); if (storeId) { console.log(' - 店铺 ID:', storeId); } // 编码后的 URL const encodedUrl = encodeURIComponent(h5Url); console.log('📦 编码后的 URL:', encodedUrl.substring(0, 100) + '...'); // 最终的小程序页面路径 // 传递店铺信息给 web-view 页面,优先用于设置标题 let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}`; if (storeId) { webViewPath += `&storeId=${storeId}`; } if (storeName) { webViewPath += `&storeName=${encodeURIComponent(storeName)}`; } console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...'); console.log('==========================================='); wx.navigateTo({ url: webViewPath, success: () => { console.log('✅ 跳转成功'); }, fail: (err) => { console.error('❌ 跳转失败:', err); wx.showToast({ title: '跳转失败,请重试', icon: 'none' }); } }); }, /** * 轮播图变化事件 */ onSwiperChange(e) { this.setData({ currentSwiperIndex: e.detail.current }); } } })