index.vue 8.9 KB

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