index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <view>
  3. <cart-container ref="cartContainer"></cart-container>
  4. <policy-consent-host />
  5. </view>
  6. </template>
  7. <script>
  8. import PolicyConsentHost from '@/components/policy-consent-host.vue'
  9. import tabbarBlockMixin from '@/utils/tabbar-block-mixin.js'
  10. import { eventBus } from '@/utils/event-bus.js'
  11. export default {
  12. mixins: [tabbarBlockMixin],
  13. components: {
  14. PolicyConsentHost,
  15. },
  16. data() {
  17. return {
  18. }
  19. },
  20. created() {
  21. this.cartLoadDoneHandler = () => {
  22. uni.stopPullDownRefresh();
  23. };
  24. eventBus.on('cartPageLoadDone', this.cartLoadDoneHandler);
  25. },
  26. onUnload() {
  27. if (this.cartLoadDoneHandler) {
  28. eventBus.off('cartPageLoadDone', this.cartLoadDoneHandler);
  29. }
  30. },
  31. onShow() {
  32. // 每次显示页面时更新角标;通过事件通知子组件刷新,避免访问 $refs 触发小程序占位组件报错
  33. this.$updateCartBadge();
  34. eventBus.emit('cartPageRefresh');
  35. },
  36. onPullDownRefresh() {
  37. this.$updateCartBadge();
  38. eventBus.emit('cartPageRefresh');
  39. },
  40. // 分享配置
  41. onShareAppMessage(res) {
  42. console.log(res, '分享');
  43. if (res.from === "button" && res.target && res.target.dataset && res.target.dataset.shareType === "reduce") {
  44. let reduceCode = uni.getStorageSync("reduceCodeShare");
  45. // 调用分享接口
  46. // 使用 uni.$u 确保可用性,避免 this.$u 在某些上下文不可用
  47. if (this.$u && this.$u.api && this.$u.api.goToReduceShareAjax) {
  48. this.$u.api.goToReduceShareAjax({
  49. reduceCode: reduceCode
  50. });
  51. } else {
  52. // 降级尝试
  53. uni.$u && uni.$u.http && uni.$u.http.get('/token/shop/order/goToShare', { reduceCode: reduceCode });
  54. }
  55. return {
  56. title: "快来帮我减钱买书吧!",
  57. path: "/pages/sell/index?reduceCode=" + reduceCode,
  58. imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
  59. desc: "书嗨,专注于书籍交易的平台,提供新书和二手书的买卖服务",
  60. };
  61. } else {
  62. return {
  63. title: "书嗨",
  64. page: "/pages/home/index",
  65. };
  66. }
  67. },
  68. // 分享到朋友圈
  69. onShareTimeline(res) {
  70. if (res.from === "button") {
  71. let reduceCode = uni.getStorageSync("reduceCodeShare");
  72. // 调用分享接口
  73. if (this.$u && this.$u.api && this.$u.api.goToReduceShareAjax) {
  74. this.$u.api.goToReduceShareAjax({
  75. reduceCode: reduceCode
  76. });
  77. } else {
  78. uni.$u && uni.$u.http && uni.$u.http.get('/token/shop/order/goToShare', { reduceCode: reduceCode });
  79. }
  80. return {
  81. title: "快来帮我减钱买书吧!",
  82. path: "/pages/sell/index?reduceCode=" + reduceCode,
  83. imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
  84. desc: "书嗨,专注于书籍交易的平台,提供新书和二手书的买卖服务",
  85. };
  86. } else {
  87. return {
  88. title: "书嗨",
  89. page: "/pages/home/index",
  90. };
  91. }
  92. },
  93. methods: {
  94. }
  95. }
  96. </script>
  97. <style>
  98. /* Empty style as content is in the component */
  99. </style>