index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const H5_BASE = 'https://www.yuban.co/home';
  2. const STARTUP_SOURCE_PARAMS = {
  3. fromStartup: 'true',
  4. startupWelcome: 'true',
  5. source: 'wechat-miniprogram',
  6. };
  7. Component({
  8. properties: {},
  9. data: {
  10. loading: false,
  11. webViewPath: '',
  12. slides: [1, 2, 3, 5],
  13. activeSlide: 0,
  14. },
  15. lifetimes: {
  16. attached: function () {
  17. this.preloadCommonSubpackage();
  18. this.prepareWebViewPath();
  19. },
  20. },
  21. methods: {
  22. /**
  23. * 点击「立即体验」按钮
  24. * 直接跳转到 H5,不依赖 Parse/fm-plugin,让 H5 自行处理鉴权
  25. */
  26. onEnter() {
  27. if (this.data.loading) return;
  28. this.setData({ loading: true });
  29. try {
  30. const webViewPath = this.data.webViewPath || this.buildWebViewPath();
  31. wx.navigateTo({
  32. url: webViewPath,
  33. fail: (err) => {
  34. console.error('❌ 跳转失败:', err);
  35. wx.showToast({ title: '跳转失败,请重试', icon: 'none' });
  36. },
  37. complete: () => this.setData({ loading: false }),
  38. });
  39. } catch (err) {
  40. console.error('❌ onEnter 异常:', err);
  41. this.setData({ loading: false });
  42. wx.showToast({ title: '加载失败,请重试', icon: 'none' });
  43. }
  44. },
  45. onSwiperChange(e) {
  46. const current = Number(e?.detail?.current ?? 0);
  47. this.setData({ activeSlide: current });
  48. },
  49. preloadCommonSubpackage() {
  50. if (typeof wx.loadSubpackage !== 'function') return;
  51. wx.loadSubpackage({
  52. name: 'common'
  53. });
  54. },
  55. prepareWebViewPath() {
  56. const webViewPath = this.buildWebViewPath();
  57. this.setData({ webViewPath });
  58. },
  59. buildWebViewPath() {
  60. let token = null;
  61. try {
  62. const app = getApp();
  63. if (app && app.Parse) {
  64. const currentUser = app.Parse.User.current();
  65. token = currentUser ? currentUser.getSessionToken() : null;
  66. }
  67. } catch (e) {
  68. // Parse 不可用时直接游客模式进入 H5
  69. }
  70. const h5Url = this.appendQuery(H5_BASE, {
  71. ...STARTUP_SOURCE_PARAMS,
  72. token,
  73. });
  74. const encodedUrl = encodeURIComponent(h5Url);
  75. return `/common-page/pages/web-view/index?path=${encodedUrl}&skipAuth=true`;
  76. },
  77. appendQuery(url, params = {}) {
  78. const query = Object.keys(params)
  79. .filter((key) => params[key] !== null && params[key] !== undefined && params[key] !== '')
  80. .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
  81. .join('&');
  82. if (!query) return url;
  83. return `${url}${url.indexOf('?') === -1 ? '?' : '&'}${query}`;
  84. }
  85. }
  86. })