index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <view>
  3. <cart-container ref="cartContainer"></cart-container>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. }
  11. },
  12. onShow() {
  13. // 每次显示页面时,更新购物车角标并调用子组件的刷新方法
  14. this.$updateCartBadge();
  15. this.$nextTick(() => {
  16. if (this.$refs.cartContainer) {
  17. this.$refs.cartContainer.loadData();
  18. }
  19. });
  20. },
  21. onPullDownRefresh() {
  22. // 更新购物车角标并调用子组件的刷新方法
  23. this.$updateCartBadge();
  24. if (this.$refs.cartContainer) {
  25. // 如果 cartContainer 的 loadData 返回 Promise,可以通过 await 等待结束后停止下拉刷新,这里暂直接调用并停止
  26. this.$refs.cartContainer.loadData();
  27. }
  28. setTimeout(() => {
  29. uni.stopPullDownRefresh();
  30. }, 1000);
  31. },
  32. // 分享配置
  33. onShareAppMessage(res) {
  34. console.log(res, '分享');
  35. if (res && res.from === 'button' && res.target && res.target.dataset && res.target.dataset.shareType === 'product') {
  36. const dataset = res.target.dataset;
  37. return {
  38. title: dataset.title || '',
  39. path: `/pages-sell/pages/detail?isbn=${dataset.isbn}`,
  40. imageUrl: dataset.image || ''
  41. };
  42. }
  43. if (res.from === "button") {
  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/home/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/home/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>