index.js 1.8 KB

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