| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- const H5_BASE = 'https://www.yuban.co/home';
- const STARTUP_SOURCE_PARAMS = {
- fromStartup: 'true',
- startupWelcome: 'true',
- 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;
- try {
- const app = getApp();
- if (app && app.Parse) {
- const currentUser = app.Parse.User.current();
- token = currentUser ? currentUser.getSessionToken() : null;
- }
- } catch (e) {
- // Parse 不可用时直接游客模式进入 H5
- }
- const h5Url = this.appendQuery(H5_BASE, {
- ...STARTUP_SOURCE_PARAMS,
- token,
- });
- 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}`;
- }
- }
- })
|