index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // 直接进入 H5 首页(对应 H5 路由 /home),用于跳过 H5 的启动页。
  2. const H5_BASE = 'https://www.yuban.co/home';
  3. Component({
  4. properties: {},
  5. data: {
  6. loading: false,
  7. webViewPath: '',
  8. },
  9. lifetimes: {
  10. attached: function () {
  11. this.preloadCommonSubpackage();
  12. this.prepareWebViewPath();
  13. },
  14. },
  15. methods: {
  16. /**
  17. * 点击「立即体验」按钮
  18. * 直接跳转到 H5,不依赖 Parse/fm-plugin,让 H5 自行处理鉴权
  19. */
  20. onEnter() {
  21. if (this.data.loading) return;
  22. this.setData({ loading: true });
  23. try {
  24. const webViewPath = this.data.webViewPath || this.buildWebViewPath();
  25. wx.navigateTo({
  26. url: webViewPath,
  27. fail: (err) => {
  28. console.error('❌ 跳转失败:', err);
  29. wx.showToast({ title: '跳转失败,请重试', icon: 'none' });
  30. },
  31. complete: () => this.setData({ loading: false }),
  32. });
  33. } catch (err) {
  34. console.error('❌ onEnter 异常:', err);
  35. this.setData({ loading: false });
  36. wx.showToast({ title: '加载失败,请重试', icon: 'none' });
  37. }
  38. },
  39. preloadCommonSubpackage() {
  40. if (typeof wx.loadSubpackage !== 'function') return;
  41. wx.loadSubpackage({
  42. name: 'common'
  43. });
  44. },
  45. prepareWebViewPath() {
  46. const webViewPath = this.buildWebViewPath();
  47. this.setData({ webViewPath });
  48. },
  49. buildWebViewPath() {
  50. let token = null;
  51. try {
  52. const app = getApp();
  53. if (app && app.Parse) {
  54. const currentUser = app.Parse.User.current();
  55. token = currentUser ? currentUser.getSessionToken() : null;
  56. }
  57. } catch (e) {
  58. // Parse 不可用时直接游客模式进入 H5
  59. }
  60. const h5Url = token ? `${H5_BASE}?token=${token}` : H5_BASE;
  61. const encodedUrl = encodeURIComponent(h5Url);
  62. return `/common-page/pages/web-view/index?path=${encodedUrl}&skipAuth=true`;
  63. }
  64. }
  65. })