s-groupon-block.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <!-- 装修商品组件:商品卡片 -->
  2. <!-- 商品卡片 -->
  3. <view>
  4. <!-- 布局1. 单列大图(上图,下内容)-->
  5. <view v-if="layoutType === LayoutTypeEnum.ONE_COL_BIG_IMG && state.spuList.length" class="goods-sl-box">
  6. <view
  7. class="goods-box"
  8. v-for="item in state.spuList"
  9. :key="item.id"
  10. :style="[{ marginBottom: data.space * 2 + 'rpx' }]"
  11. >
  12. <s-goods-column
  13. class=""
  14. size="sl"
  15. :goodsFields="data.fields"
  16. :tagStyle="data.badge"
  17. :data="item"
  18. :titleColor="data.fields.name?.color"
  19. :subTitleColor="data.fields.introduction.color"
  20. :topRadius="data.borderRadiusTop"
  21. :bottomRadius="data.borderRadiusBottom"
  22. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  23. >
  24. <!-- 购买按钮 -->
  25. <template v-slot:cart>
  26. <button class="ss-reset-button cart-btn" :style="[buyStyle]">
  27. {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
  28. </button>
  29. </template>
  30. </s-goods-column>
  31. </view>
  32. </view>
  33. <!-- 布局2. 双列(每一列:上图,下内容)-->
  34. <view
  35. v-if="layoutType === LayoutTypeEnum.TWO_COL && state.spuList.length"
  36. class="goods-md-wrap ss-flex ss-flex-wrap ss-col-top"
  37. >
  38. <view class="goods-list-box">
  39. <view
  40. class="left-list"
  41. :style="[{ paddingRight: data.space + 'rpx', marginBottom: data.space + 'px' }]"
  42. v-for="item in state.leftSpuList"
  43. :key="item.id"
  44. >
  45. <s-goods-column
  46. class="goods-md-box"
  47. size="md"
  48. :goodsFields="data.fields"
  49. :tagStyle="data.badge"
  50. :data="item"
  51. :titleColor="data.fields.name?.color"
  52. :subTitleColor="data.fields.introduction.color"
  53. :topRadius="data.borderRadiusTop"
  54. :bottomRadius="data.borderRadiusBottom"
  55. :titleWidth="330 - marginLeft - marginRight"
  56. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  57. @getHeight="calculateGoodsColumn($event, 'left')"
  58. >
  59. <!-- 购买按钮 -->
  60. <template v-slot:cart>
  61. <button class="ss-reset-button cart-btn" :style="[buyStyle]">
  62. {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
  63. </button>
  64. </template>
  65. </s-goods-column>
  66. </view>
  67. </view>
  68. <view class="goods-list-box">
  69. <view
  70. class="right-list"
  71. :style="[{ paddingLeft: data.space + 'rpx', marginBottom: data.space + 'px' }]"
  72. v-for="item in state.rightSpuList"
  73. :key="item.id"
  74. >
  75. <s-goods-column
  76. class="goods-md-box"
  77. size="md"
  78. :goodsFields="data.fields"
  79. :tagStyle="data.badge"
  80. :data="item"
  81. :titleColor="data.fields.name?.color"
  82. :subTitleColor="data.fields.introduction.color"
  83. :topRadius="data.borderRadiusTop"
  84. :bottomRadius="data.borderRadiusBottom"
  85. :titleWidth="330 - marginLeft - marginRight"
  86. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  87. @getHeight="calculateGoodsColumn($event, 'right')"
  88. >
  89. <!-- 购买按钮 -->
  90. <template v-slot:cart>
  91. <button class="ss-reset-button cart-btn" :style="[buyStyle]">
  92. {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
  93. </button>
  94. </template>
  95. </s-goods-column>
  96. </view>
  97. </view>
  98. </view>
  99. <!-- 布局3. 单列小图(左图,右内容) -->
  100. <view v-if="layoutType === LayoutTypeEnum.ONE_COL_SMALL_IMG && state.spuList.length" class="goods-lg-box">
  101. <view
  102. class="goods-box"
  103. :style="[{ marginBottom: data.space + 'px' }]"
  104. v-for="item in state.spuList"
  105. :key="item.id"
  106. >
  107. <s-goods-column
  108. class="goods-card"
  109. size="lg"
  110. :goodsFields="data.fields"
  111. :data="item"
  112. :tagStyle="data.badge"
  113. :titleColor="data.fields.name?.color"
  114. :subTitleColor="data.fields.introduction.color"
  115. :topRadius="data.borderRadiusTop"
  116. :bottomRadius="data.borderRadiusBottom"
  117. @tap="sheep.$router.go('/pages/goods/index', { id: item.id })"
  118. >
  119. <!-- 购买按钮 -->
  120. <template v-slot:cart>
  121. <button class="ss-reset-button cart-btn" :style="[buyStyle]">
  122. {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
  123. </button>
  124. </template>
  125. </s-goods-column>
  126. </view>
  127. </view>
  128. </view>
  129. </template>
  130. <script setup>
  131. /**
  132. * 商品卡片
  133. */
  134. import {
  135. computed,
  136. onMounted,
  137. reactive,
  138. ref,
  139. } from 'vue';
  140. import sheep from '@/sheep';
  141. import CombinationApi from '@/sheep/api/promotion/combination';
  142. import SpuApi from '@/sheep/api/product/spu';
  143. // 布局类型
  144. const LayoutTypeEnum = {
  145. // 单列大图
  146. ONE_COL_BIG_IMG: 'oneColBigImg',
  147. // 双列
  148. TWO_COL: 'twoCol',
  149. // 单列小图
  150. ONE_COL_SMALL_IMG: 'oneColSmallImg',
  151. };
  152. const state = reactive({
  153. spuList: [],
  154. leftSpuList: [],
  155. rightSpuList: [],
  156. });
  157. const props = defineProps({
  158. data: {
  159. type: Object,
  160. default() {
  161. },
  162. },
  163. styles: {
  164. type: Object,
  165. default() {
  166. },
  167. },
  168. });
  169. const { layoutType, btnBuy, activityIds } = props.data || {};
  170. const { marginLeft, marginRight } = props.styles || {};
  171. // 购买按钮样式
  172. const buyStyle = computed(() => {
  173. if (btnBuy.type === 'text') {
  174. // 文字按钮:线性渐变背景颜色
  175. return {
  176. background: `linear-gradient(to right, ${btnBuy.bgBeginColor}, ${btnBuy.bgEndColor})`,
  177. };
  178. }
  179. if (btnBuy.type === 'img') {
  180. // 图片按钮
  181. return {
  182. width: '54rpx',
  183. height: '54rpx',
  184. background: `url(${sheep.$url.cdn(btnBuy.imgUrl)}) no-repeat`,
  185. backgroundSize: '100% 100%',
  186. };
  187. }
  188. });
  189. //region 商品瀑布流布局
  190. // 下一个要处理的商品索引
  191. let count = 0;
  192. // 左列的高度
  193. let leftHeight = 0;
  194. // 右列的高度
  195. let rightHeight = 0;
  196. /**
  197. * 计算商品在左列还是右列
  198. * @param height 商品的高度
  199. * @param where 添加到哪一列
  200. */
  201. function calculateGoodsColumn(height = 0, where = 'left') {
  202. // 处理完
  203. if (!state.spuList[count]) return;
  204. // 增加列的高度
  205. if (where === 'left') leftHeight += height;
  206. if (where === 'right') rightHeight += height;
  207. // 添加到矮的一列
  208. if (leftHeight <= rightHeight) {
  209. state.leftSpuList.push(state.spuList[count]);
  210. } else {
  211. state.rightSpuList.push(state.spuList[count]);
  212. }
  213. // 计数
  214. count++;
  215. }
  216. //endregion
  217. /**
  218. * 根据商品编号列表,获取商品列表
  219. * @param ids 商品编号列表
  220. * @return {Promise<undefined>} 商品列表
  221. */
  222. async function getCombinationActivityDetailList(ids) {
  223. const { data } = await CombinationApi.getCombinationActivityDetailList(ids);
  224. return data;
  225. }
  226. /**
  227. * 根据商品编号,获取商品详情
  228. * @param ids 商品编号列表
  229. * @return {Promise<undefined>} 商品列表
  230. */
  231. async function getSpuDetail(spuId) {
  232. const { data: spu } = await SpuApi.getSpuDetail(spuId)
  233. return spu;
  234. }
  235. // 初始化
  236. onMounted(async () => {
  237. // 加载商品列表
  238. const activityList = await getCombinationActivityDetailList(activityIds.join(','));
  239. // 创建 SPU 请求的 Promise 数组
  240. const spuPromises = activityList.map(activity => getSpuDetail(activity.spuId));
  241. // 等待所有 SPU 请求完成
  242. // 处理 spuList
  243. state.spuList = await Promise.all(spuPromises);
  244. // 只有双列布局时需要
  245. if (layoutType === LayoutTypeEnum.TWO_COL) {
  246. // 分列
  247. calculateGoodsColumn();
  248. }
  249. });
  250. </script>
  251. <style lang="scss" scoped>
  252. .goods-md-wrap {
  253. width: 100%;
  254. }
  255. .goods-list-box {
  256. width: 50%;
  257. box-sizing: border-box;
  258. .left-list {
  259. &:nth-last-child(1) {
  260. margin-bottom: 0 !important;
  261. }
  262. }
  263. .right-list {
  264. &:nth-last-child(1) {
  265. margin-bottom: 0 !important;
  266. }
  267. }
  268. }
  269. .goods-box {
  270. &:nth-last-of-type(1) {
  271. margin-bottom: 0 !important;
  272. }
  273. }
  274. .goods-md-box,
  275. .goods-sl-box,
  276. .goods-lg-box {
  277. position: relative;
  278. .cart-btn {
  279. position: absolute;
  280. bottom: 18rpx;
  281. right: 20rpx;
  282. z-index: 11;
  283. height: 50rpx;
  284. line-height: 50rpx;
  285. padding: 0 20rpx;
  286. border-radius: 25rpx;
  287. font-size: 24rpx;
  288. color: #fff;
  289. }
  290. }
  291. </style>