const H5_BASE = 'https://www.yuban.co/home'; const STARTUP_SOURCE_PARAMS = { fromStartup: '1', source: 'wechat-miniprogram', }; Component({ properties: {}, data: { loading: false, webViewPath: '', slides: [1, 2, 3, 5], activeSlide: 0, }, lifetimes: { attached: function () { this.preloadCommonSubpackage(); this.prepareWebViewPath(); }, }, methods: { /** * 点击「立即体验」按钮 * 直接跳转到 H5,不依赖 Parse/fm-plugin,让 H5 自行处理鉴权 */ onEnter() { if (this.data.loading) return; this.setData({ loading: true }); try { const webViewPath = this.data.webViewPath || this.buildWebViewPath(); wx.navigateTo({ url: webViewPath, fail: (err) => { console.error('❌ 跳转失败:', err); wx.showToast({ title: '跳转失败,请重试', icon: 'none' }); }, complete: () => this.setData({ loading: false }), }); } catch (err) { console.error('❌ onEnter 异常:', err); this.setData({ loading: false }); wx.showToast({ title: '加载失败,请重试', icon: 'none' }); } }, onSwiperChange(e) { const current = Number(e?.detail?.current ?? 0); this.setData({ activeSlide: current }); }, preloadCommonSubpackage() { if (typeof wx.loadSubpackage !== 'function') return; wx.loadSubpackage({ name: 'common' }); }, prepareWebViewPath() { const webViewPath = this.buildWebViewPath(); this.setData({ webViewPath }); }, buildWebViewPath() { let token = null; let userId = null; let mobile = null; try { const app = getApp(); if (app && app.Parse) { const currentUser = app.Parse.User.current(); if (currentUser) { token = currentUser.getSessionToken() || null; userId = currentUser.id || null; mobile = currentUser.get('mobile') || null; } } } catch (e) { // Parse 不可用时直接游客模式进入 H5 } // 兜底:storage 里可能已有登录态 try { if (!token) token = wx.getStorageSync('sessionToken') || null; if (!userId) userId = wx.getStorageSync('userLogin') || null; if (!mobile) { const userInfo = wx.getStorageSync('userInfo') || {}; mobile = userInfo.mobile || null; } } catch (e) { // ignore } // 约定:直达 /home,勿进 /startup;fromStartup=1 触发 H5 欢迎语桥接 const h5Url = this.appendQuery(H5_BASE, { ...STARTUP_SOURCE_PARAMS, token, userId, mobile, }); const encodedUrl = encodeURIComponent(h5Url); return `/common-page/pages/web-view/index?path=${encodedUrl}&skipAuth=true`; }, appendQuery(url, params = {}) { const query = Object.keys(params) .filter((key) => params[key] !== null && params[key] !== undefined && params[key] !== '') .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`) .join('&'); if (!query) return url; return `${url}${url.indexOf('?') === -1 ? '?' : '&'}${query}`; } } })