index.js 3.2 KB

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