index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <!-- 指定满减送的活动列表 -->
  2. <template>
  3. <s-layout class="activity-wrap" :title="state.activityInfo.title">
  4. <!-- 活动信息 -->
  5. <su-sticky bgColor="#fff">
  6. <view class="ss-flex ss-col-top tip-box">
  7. <view class="type-text ss-flex ss-row-center">满减:</view>
  8. <view class="ss-flex-1">
  9. <view class="tip-content" v-for="item in state.activityInfo.rules" :key="item">
  10. {{ formatRewardActivityRule(state.activityInfo, item) }}
  11. </view>
  12. </view>
  13. <image class="activity-left-image" src="/static/activity-left.png" />
  14. <image class="activity-right-image" src="/static/activity-right.png" />
  15. </view>
  16. </su-sticky>
  17. <!-- 商品信息 -->
  18. <view class="ss-flex ss-flex-wrap ss-p-x-20 ss-m-t-20 ss-col-top">
  19. <view class="goods-list-box">
  20. <view class="left-list" v-for="item in state.leftGoodsList" :key="item.id">
  21. <s-goods-column class="goods-md-box" size="md" :data="item"
  22. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  23. @getHeight="mountMasonry($event, 'left')">
  24. <template v-slot:cart>
  25. <button class="ss-reset-button cart-btn"> </button>
  26. </template>
  27. </s-goods-column>
  28. </view>
  29. </view>
  30. <view class="goods-list-box">
  31. <view class="right-list" v-for="item in state.rightGoodsList" :key="item.id">
  32. <s-goods-column class="goods-md-box" size="md" :data="item"
  33. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  34. @getHeight="mountMasonry($event, 'right')">
  35. <template v-slot:cart>
  36. <button class="ss-reset-button cart-btn" />
  37. </template>
  38. </s-goods-column>
  39. </view>
  40. </view>
  41. </view>
  42. <uni-load-more v-if="state.pagination.total > 0" :status="state.loadStatus" :content-text="{
  43. contentdown: '上拉加载更多',
  44. }" @tap="loadMore" />
  45. </s-layout>
  46. </template>
  47. <script setup>
  48. import {
  49. reactive,
  50. toRaw,
  51. ref
  52. } from 'vue';
  53. import {
  54. onLoad,
  55. onReachBottom
  56. } from '@dcloudio/uni-app';
  57. import sheep from '@/sheep';
  58. import _ from 'lodash-es';
  59. import RewardActivityApi from '@/sheep/api/promotion/rewardActivity';
  60. import {
  61. formatRewardActivityRule
  62. } from '@/sheep/hooks/useGoods';
  63. import SpuApi from '@/sheep/api/product/spu';
  64. const state = reactive({
  65. activityId: 0, // 获得编号
  66. activityInfo: {}, // 获得信息
  67. pagination: {
  68. list: [],
  69. total: 1,
  70. pageNo: 1,
  71. pageSize: 8,
  72. },
  73. loadStatus: '',
  74. leftGoodsList: [],
  75. rightGoodsList: [],
  76. });
  77. // 加载瀑布流
  78. let count = 0;
  79. let leftHeight = 0;
  80. let rightHeight = 0;
  81. function mountMasonry(height = 0, where = 'left') {
  82. if (!state.pagination.list[count]) return;
  83. if (where === 'left') {
  84. leftHeight += height;
  85. } else {
  86. rightHeight += height;
  87. }
  88. if (leftHeight <= rightHeight) {
  89. state.leftGoodsList.push(state.pagination.list[count]);
  90. } else {
  91. state.rightGoodsList.push(state.pagination.list[count]);
  92. }
  93. count++;
  94. }
  95. // 加载商品信息
  96. async function getList() {
  97. // state.loadStatus = 'loading';
  98. // 处理拓展参数
  99. const params = {}
  100. if (state.activityInfo.productScope === 2) {
  101. // const params = toRaw(state.activityInfo.productScopeValues)
  102. // 请求数据
  103. const {
  104. code,
  105. data
  106. } = await SpuApi.getSpuListByIds(state.activityInfo.productScopeValues.join(','));
  107. if (code !== 0) {
  108. return;
  109. }
  110. // 使用 map 提取每个对象的 id 属性
  111. const ids = data.map(item => item.id);
  112. // 使用 join 方法将 id 数组连接成一个用逗号分隔的字符串
  113. const idsString = ids.join(',');
  114. // 获取结算信息
  115. settleData.value = await getSettlementByIds(idsString)
  116. // 处理获得的数据
  117. const ms = enrichDataWithSkus(data, settleData.value)
  118. state.pagination.list = ms;
  119. // state.pagination.total = data.total;
  120. // state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  121. } else if (state.activityInfo.productScope === 3) {
  122. params.categoryIds = state.activityInfo.productScopeValues.join(',');
  123. state.loadStatus = 'loading';
  124. const {
  125. code,
  126. data
  127. } = await SpuApi.getSpuPage({
  128. pageNo: state.pagination.pageNo,
  129. pageSize: state.pagination.pageSize,
  130. ...params,
  131. });
  132. if (code !== 0) {
  133. return;
  134. }
  135. // 使用 map 提取每个对象的 id 属性
  136. const ids = data.list.map(item => item.id);
  137. // 使用 join 方法将 id 数组连接成一个用逗号分隔的字符串
  138. const idsString = ids.join(',');
  139. // 获取结算信息
  140. settleData.value = await getSettlementByIds(idsString)
  141. // 处理获得的数据
  142. const ms = enrichDataWithSkus(data.list, settleData.value)
  143. state.pagination.list = _.concat(state.pagination.list, ms);
  144. state.pagination.total = data.total;
  145. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  146. } else {
  147. state.loadStatus = 'loading';
  148. const {
  149. code,
  150. data
  151. } = await SpuApi.getSpuPage({
  152. pageNo: state.pagination.pageNo,
  153. pageSize: state.pagination.pageSize,
  154. });
  155. if (code !== 0) {
  156. return;
  157. }
  158. // 使用 map 提取每个对象的 id 属性
  159. const ids = data.list.map(item => item.id);
  160. // 使用 join 方法将 id 数组连接成一个用逗号分隔的字符串
  161. const idsString = ids.join(',');
  162. // 获取结算信息
  163. settleData.value = await getSettlementByIds(idsString)
  164. // 处理获得的数据
  165. const ms = enrichDataWithSkus(data.list, settleData.value)
  166. state.pagination.list = _.concat(state.pagination.list, ms);
  167. state.pagination.total = data.total;
  168. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  169. }
  170. mountMasonry();
  171. }
  172. //获取结算信息
  173. const settleData = ref()
  174. async function getSettlementByIds(ids) {
  175. const {
  176. data
  177. } = await SpuApi.getSettlementProduct(ids);
  178. return data;
  179. }
  180. //计算展示价格的函数
  181. function enrichDataWithSkus(data, array) {
  182. // 创建一个映射,以 id 为键,存储 data 数组中的对象
  183. const dataMap = new Map(data.map(item => [item.id, {
  184. ...item
  185. }]));
  186. // 遍历 array 数组
  187. array.forEach(item => {
  188. // 初始化 discountPrice 和 vipPrice 为 null
  189. let discountPrice = null;
  190. let vipPrice = null;
  191. let foundType4 = false;
  192. let foundType6 = false;
  193. // 遍历 skus 数组,寻找 type 为 4 和 6 的首个条目
  194. item.skus.forEach(sku => {
  195. if (!foundType4 && sku.type === 4) {
  196. discountPrice = sku.price;
  197. foundType4 = true;
  198. }
  199. if (!foundType6 && sku.type === 6) {
  200. vipPrice = sku.price;
  201. foundType6 = true;
  202. }
  203. // 如果已经找到 type 为 4 和 6 的条目,则不需要继续遍历
  204. if (foundType4 && foundType6) {
  205. return;
  206. }
  207. });
  208. // 更新 dataMap 中对应的对象
  209. if (dataMap.has(item.id)) {
  210. dataMap.get(item.id).discountPrice = discountPrice;
  211. dataMap.get(item.id).vipPrice = vipPrice;
  212. dataMap.get(item.id).reward = item.reward;
  213. }
  214. });
  215. // 返回更新后的数据数组
  216. return Array.from(dataMap.values());
  217. }
  218. // 加载活动信息
  219. async function getActivity(id) {
  220. const {
  221. code,
  222. data
  223. } = await RewardActivityApi.getRewardActivity(id);
  224. if (code === 0) {
  225. state.activityInfo = data;
  226. }
  227. }
  228. // 加载更多
  229. function loadMore() {
  230. if (state.loadStatus === 'noMore') {
  231. return;
  232. }
  233. state.pagination.pageNo++;
  234. getList();
  235. }
  236. // 上拉加载更多
  237. onReachBottom(() => {
  238. loadMore();
  239. });
  240. onLoad(async (options) => {
  241. state.activityId = options.activityId;
  242. await getActivity(state.activityId);
  243. await getList();
  244. });
  245. </script>
  246. <style lang="scss" scoped>
  247. .goods-list-box {
  248. width: 50%;
  249. box-sizing: border-box;
  250. .left-list {
  251. margin-right: 10rpx;
  252. margin-bottom: 20rpx;
  253. }
  254. .right-list {
  255. margin-left: 10rpx;
  256. margin-bottom: 20rpx;
  257. }
  258. }
  259. .tip-box {
  260. background: #fff0e7;
  261. padding: 20rpx;
  262. width: 100%;
  263. position: relative;
  264. box-sizing: border-box;
  265. .activity-left-image {
  266. position: absolute;
  267. bottom: 0;
  268. left: 0;
  269. width: 58rpx;
  270. height: 36rpx;
  271. }
  272. .activity-right-image {
  273. position: absolute;
  274. top: 0;
  275. right: 0;
  276. width: 72rpx;
  277. height: 50rpx;
  278. }
  279. .type-text {
  280. font-size: 26rpx;
  281. font-weight: 500;
  282. color: #ff6000;
  283. line-height: 42rpx;
  284. }
  285. .tip-content {
  286. font-size: 26rpx;
  287. font-weight: 500;
  288. color: #ff6000;
  289. line-height: 42rpx;
  290. }
  291. }
  292. </style>