| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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}`;
- }
- }
- })
|