s-groupon-block.vue 9.1 KB

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