index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view>
  3. <sell-container ref="sellContainer"></sell-container>
  4. <ActivityHost ref="activityHost" :enable-upsell="false"></ActivityHost>
  5. <policy-consent-host />
  6. </view>
  7. </template>
  8. <script>
  9. import ActivityHost from "@/components/activity-host.vue";
  10. import PolicyConsentHost from "@/components/policy-consent-host.vue";
  11. import tabbarBlockMixin from "@/utils/tabbar-block-mixin.js";
  12. import { eventBus } from "@/utils/event-bus";
  13. import { consumeHomeRefreshAfterPolicy } from "@/utils/policy-consent-manager.js";
  14. export default {
  15. mixins: [tabbarBlockMixin],
  16. components: {
  17. ActivityHost,
  18. PolicyConsentHost,
  19. },
  20. data() {
  21. return {
  22. pendingActivityOptions: {},
  23. pendingPolicyRefresh: false,
  24. policyAcceptedHandler: null,
  25. loginSuccessHandler: null,
  26. }
  27. },
  28. onLoad(options) {
  29. this.pendingActivityOptions = options || {};
  30. this.bindPolicyRefreshListeners();
  31. },
  32. onUnload() {
  33. this.unbindPolicyRefreshListeners();
  34. },
  35. onReady() {
  36. this.$nextTick(() => {
  37. if (this.hasActivityEntryOptions(this.pendingActivityOptions)) {
  38. this.$refs.activityHost?.handlePageOptions(this.pendingActivityOptions);
  39. }
  40. this.pendingActivityOptions = {};
  41. });
  42. },
  43. onShow() {
  44. this.$updateCartBadge();
  45. // 在其他 Tab 同意协议后切回首页时补刷数据
  46. if (consumeHomeRefreshAfterPolicy() && uni.getStorageSync("token")) {
  47. this.refreshHomePageData();
  48. }
  49. },
  50. onShareAppMessage(res) {
  51. return this.$refs.activityHost?.getShareAppMessage(res) || {
  52. title: "书嗨",
  53. path: "/pages/sell/index",
  54. };
  55. },
  56. onShareTimeline(res) {
  57. return this.$refs.activityHost?.getShareTimeline(res) || {
  58. title: "书嗨",
  59. path: "/pages/sell/index",
  60. };
  61. },
  62. onPullDownRefresh() {
  63. if (this.$refs.sellContainer) {
  64. this.$refs.sellContainer.hasShareList();
  65. this.$refs.sellContainer.getIndexCateInfo();
  66. }
  67. setTimeout(() => {
  68. uni.stopPullDownRefresh();
  69. }, 1000);
  70. },
  71. onPageScroll(e) {
  72. const comp = this.$refs.sellContainer;
  73. if (!comp) return;
  74. comp.scrollTop = e.scrollTop;
  75. },
  76. methods: {
  77. bindPolicyRefreshListeners() {
  78. if (!this.policyAcceptedHandler) {
  79. this.policyAcceptedHandler = () => {
  80. this.pendingPolicyRefresh = true;
  81. };
  82. eventBus.on("policyAccepted", this.policyAcceptedHandler);
  83. }
  84. if (!this.loginSuccessHandler) {
  85. this.loginSuccessHandler = () => {
  86. if (!this.pendingPolicyRefresh) return;
  87. this.pendingPolicyRefresh = false;
  88. consumeHomeRefreshAfterPolicy();
  89. this.refreshHomePageData();
  90. };
  91. eventBus.on("loginSuccess", this.loginSuccessHandler);
  92. }
  93. },
  94. unbindPolicyRefreshListeners() {
  95. if (this.policyAcceptedHandler) {
  96. eventBus.off("policyAccepted", this.policyAcceptedHandler);
  97. this.policyAcceptedHandler = null;
  98. }
  99. if (this.loginSuccessHandler) {
  100. eventBus.off("loginSuccess", this.loginSuccessHandler);
  101. this.loginSuccessHandler = null;
  102. }
  103. },
  104. refreshHomePageData() {
  105. this.$nextTick(() => {
  106. this.$refs.sellContainer?.refreshPageData?.();
  107. });
  108. },
  109. hasActivityEntryOptions(options = {}) {
  110. return !!(options.scene || options.reduceCode || options.upsellCode);
  111. }
  112. }
  113. }
  114. </script>
  115. <style>
  116. /* Empty style as content is in the component */
  117. </style>