goods.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <!-- 分销产品列表 -->
  2. <template>
  3. <s-layout title="推广产品" :onShareAppMessage="state.shareInfo">
  4. <view class="goods-item ss-m-20" v-for="item in state.pagination.list" :key="item.id">
  5. <s-goods-item
  6. size="lg"
  7. :img="item.picUrl"
  8. :title="item.name"
  9. :subTitle="item.introduction"
  10. :price="item.price"
  11. :originPrice="item.marketPrice"
  12. priceColor="#333"
  13. @tap="sheep.$router.go('/pages/goods/index', { id: item.id })"
  14. >
  15. <template #rightBottom>
  16. <view class="ss-flex ss-row-between">
  17. <view class="commission-num" v-if="item.brokerageMinPrice === undefined"
  18. >预计佣金:计算中</view
  19. >
  20. <view
  21. class="commission-num"
  22. v-else-if="item.brokerageMinPrice === item.brokerageMaxPrice"
  23. >
  24. 预计佣金:{{ fen2yuan(item.brokerageMinPrice) }}
  25. </view>
  26. <view class="commission-num" v-else>
  27. 预计佣金:{{ fen2yuan(item.brokerageMinPrice) }} ~
  28. {{ fen2yuan(item.brokerageMaxPrice) }}
  29. </view>
  30. <button
  31. class="ss-reset-button share-btn ui-BG-Main-Gradient"
  32. @tap.stop="onShareGoods(item)"
  33. >
  34. 分享赚
  35. </button>
  36. </view>
  37. </template>
  38. </s-goods-item>
  39. </view>
  40. <s-empty
  41. v-if="state.pagination.total === 0"
  42. icon="/static/goods-empty.png"
  43. text="暂无推广产品"
  44. />
  45. <!-- 加载更多 -->
  46. <uni-load-more
  47. v-if="state.pagination.total > 0"
  48. :status="state.loadStatus"
  49. :content-text="{
  50. contentdown: '上拉加载更多',
  51. }"
  52. @tap="loadMore"
  53. />
  54. </s-layout>
  55. </template>
  56. <script setup>
  57. import sheep from '@/sheep';
  58. import $share from '@/sheep/platform/share';
  59. import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  60. import { reactive } from 'vue';
  61. import _ from 'lodash-es';
  62. import { showShareModal } from '@/sheep/hooks/useModal';
  63. import SpuApi from '@/sheep/api/product/spu';
  64. import BrokerageApi from '@/sheep/api/trade/brokerage';
  65. import { fen2yuan } from '@/sheep/hooks/useGoods';
  66. const state = reactive({
  67. pagination: {
  68. list: [],
  69. total: 0,
  70. pageNo: 1,
  71. pageSize: 8,
  72. },
  73. loadStatus: '',
  74. shareInfo: {},
  75. });
  76. function onShareGoods(goodsInfo) {
  77. state.shareInfo = $share.getShareInfo(
  78. {
  79. title: goodsInfo.name,
  80. image: sheep.$url.cdn(goodsInfo.picUrl),
  81. desc: goodsInfo.introduction,
  82. params: {
  83. page: '2',
  84. query: goodsInfo.id,
  85. },
  86. },
  87. {
  88. type: 'goods', // 产品海报
  89. title: goodsInfo.name, // 产品名称
  90. image: sheep.$url.cdn(goodsInfo.picUrl), // 产品主图
  91. price: fen2yuan(goodsInfo.price), // 产品价格
  92. original_price: fen2yuan(goodsInfo.marketPrice), // 产品原价
  93. },
  94. );
  95. showShareModal();
  96. }
  97. async function getGoodsList() {
  98. state.loadStatus = 'loading';
  99. let { code, data } = await SpuApi.getSpuPage({
  100. pageSize: state.pagination.pageSize,
  101. pageNo: state.pagination.pageNo,
  102. });
  103. if (code !== 0) {
  104. state.loadStatus = 'error'; // 处理错误状态
  105. return;
  106. }
  107. // 使用 Promise.all 来等待所有佣金请求完成
  108. await Promise.all(
  109. data.list.map(async (item) => {
  110. try {
  111. const res = await BrokerageApi.getProductBrokeragePrice(item.id);
  112. item.brokerageMinPrice = res.data.brokerageMinPrice;
  113. item.brokerageMaxPrice = res.data.brokerageMaxPrice;
  114. } catch (error) {
  115. console.error(`获取产品【${item.name}】的佣金时出错:`, error);
  116. }
  117. }),
  118. );
  119. // 在所有请求完成后合并列表和更新状态
  120. state.pagination.list = _.concat(state.pagination.list, data.list);
  121. state.pagination.total = data.total;
  122. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  123. }
  124. onLoad(() => {
  125. getGoodsList();
  126. });
  127. // 加载更多
  128. function loadMore() {
  129. if (state.loadStatus === 'noMore') {
  130. return;
  131. }
  132. state.pagination.pageNo++;
  133. getGoodsList();
  134. }
  135. // 上拉加载更多
  136. onReachBottom(() => {
  137. loadMore();
  138. });
  139. </script>
  140. <style lang="scss" scoped>
  141. .goods-item {
  142. .commission-num {
  143. font-size: 24rpx;
  144. font-weight: 500;
  145. color: $red;
  146. }
  147. .share-btn {
  148. width: 120rpx;
  149. height: 50rpx;
  150. border-radius: 25rpx;
  151. }
  152. }
  153. </style>