index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.from === "button") {
  36. let reduceCode = uni.getStorageSync("reduceCodeShare");
  37. // 调用分享接口
  38. // 使用 uni.$u 确保可用性,避免 this.$u 在某些上下文不可用
  39. if (this.$u && this.$u.api && this.$u.api.goToReduceShareAjax) {
  40. this.$u.api.goToReduceShareAjax({
  41. reduceCode: reduceCode
  42. });
  43. } else {
  44. // 降级尝试
  45. uni.$u && uni.$u.http && uni.$u.http.get('/token/shop/order/goToShare', { reduceCode: reduceCode });
  46. }
  47. return {
  48. title: "快来帮我减钱买书吧!",
  49. path: "/pages/home/index?reduceCode=" + reduceCode,
  50. imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
  51. desc: "书嗨,专注于书籍交易的平台,提供新书和二手书的买卖服务",
  52. };
  53. } else {
  54. return {
  55. title: "书嗨",
  56. page: "/pages/home/index",
  57. };
  58. }
  59. },
  60. // 分享到朋友圈
  61. onShareTimeline(res) {
  62. if (res.from === "button") {
  63. let reduceCode = uni.getStorageSync("reduceCodeShare");
  64. // 调用分享接口
  65. if (this.$u && this.$u.api && this.$u.api.goToReduceShareAjax) {
  66. this.$u.api.goToReduceShareAjax({
  67. reduceCode: reduceCode
  68. });
  69. } else {
  70. uni.$u && uni.$u.http && uni.$u.http.get('/token/shop/order/goToShare', { reduceCode: reduceCode });
  71. }
  72. return {
  73. title: "快来帮我减钱买书吧!",
  74. path: "/pages/home/index?reduceCode=" + reduceCode,
  75. imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
  76. desc: "书嗨,专注于书籍交易的平台,提供新书和二手书的买卖服务",
  77. };
  78. } else {
  79. return {
  80. title: "书嗨",
  81. page: "/pages/home/index",
  82. };
  83. }
  84. },
  85. methods: {
  86. }
  87. }
  88. </script>
  89. <style>
  90. /* Empty style as content is in the component */
  91. </style>