s-groupon-block.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <!-- 装修商品组件:【拼团】商品卡片 -->
  2. <template>
  3. <!-- 商品卡片 -->
  4. <view>
  5. <!-- 布局1. 单列大图(上图,下内容)-->
  6. <view
  7. v-if="layoutType === LayoutTypeEnum.ONE_COL_BIG_IMG && state.spuList.length"
  8. class="goods-sl-box"
  9. >
  10. <view
  11. class="goods-box"
  12. v-for="item in state.spuList"
  13. :key="item.id"
  14. :style="[{ marginBottom: data.space * 2 + 'rpx' }]"
  15. >
  16. <s-goods-column
  17. class=""
  18. size="sl"
  19. :goodsFields="data.fields"
  20. :tagStyle="data.badge"
  21. :data="item"
  22. :titleColor="data.fields.name?.color"
  23. :subTitleColor="data.fields.introduction.color"
  24. :topRadius="data.borderRadiusTop"
  25. :bottomRadius="data.borderRadiusBottom"
  26. @click="sheep.$router.go('/pages/goods/groupon', { id: item.activityId })"
  27. >
  28. <!-- 购买按钮 -->
  29. <template v-slot:cart>
  30. <button class="ss-reset-button cart-btn" :style="[buyStyle]">
  31. {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
  32. </button>
  33. </template>
  34. </s-goods-column>
  35. </view>
  36. </view>
  37. <!-- 布局2. 单列小图(左图,右内容) -->
  38. <view
  39. v-if="layoutType === LayoutTypeEnum.ONE_COL_SMALL_IMG && state.spuList.length"
  40. class="goods-lg-box"
  41. >
  42. <view
  43. class="goods-box"
  44. :style="[{ marginBottom: data.space + 'px' }]"
  45. v-for="item in state.spuList"
  46. :key="item.id"
  47. >
  48. <s-goods-column
  49. class="goods-card"
  50. size="lg"
  51. :goodsFields="data.fields"
  52. :data="item"
  53. :tagStyle="data.badge"
  54. :titleColor="data.fields.name?.color"
  55. :subTitleColor="data.fields.introduction.color"
  56. :topRadius="data.borderRadiusTop"
  57. :bottomRadius="data.borderRadiusBottom"
  58. @tap="sheep.$router.go('/pages/goods/groupon', { id: item.activityId })"
  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. <!-- 布局3. 双列(每一列:上图,下内容)-->
  70. <view
  71. v-if="layoutType === LayoutTypeEnum.TWO_COL && state.spuList.length"
  72. class="goods-md-wrap ss-flex ss-flex-wrap ss-col-top"
  73. >
  74. <view class="goods-list-box">
  75. <view
  76. class="left-list"
  77. :style="[{ paddingRight: data.space + 'rpx', marginBottom: data.space + 'px' }]"
  78. v-for="item in state.leftSpuList"
  79. :key="item.id"
  80. >
  81. <s-goods-column
  82. class="goods-md-box"
  83. size="md"
  84. :goodsFields="data.fields"
  85. :tagStyle="data.badge"
  86. :data="item"
  87. :titleColor="data.fields.name?.color"
  88. :subTitleColor="data.fields.introduction.color"
  89. :topRadius="data.borderRadiusTop"
  90. :bottomRadius="data.borderRadiusBottom"
  91. :titleWidth="330 - marginLeft - marginRight"
  92. @click="sheep.$router.go('/pages/goods/groupon', { id: item.activityId })"
  93. @getHeight="calculateGoodsColumn($event, 'left')"
  94. >
  95. <!-- 购买按钮 -->
  96. <template v-slot:cart>
  97. <button class="ss-reset-button cart-btn" :style="[buyStyle]">
  98. {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
  99. </button>
  100. </template>
  101. </s-goods-column>
  102. </view>
  103. </view>
  104. <view class="goods-list-box">
  105. <view
  106. class="right-list"
  107. :style="[{ paddingLeft: data.space + 'rpx', marginBottom: data.space + 'px' }]"
  108. v-for="item in state.rightSpuList"
  109. :key="item.id"
  110. >
  111. <s-goods-column
  112. class="goods-md-box"
  113. size="md"
  114. :goodsFields="data.fields"
  115. :tagStyle="data.badge"
  116. :data="item"
  117. :titleColor="data.fields.name?.color"
  118. :subTitleColor="data.fields.introduction.color"
  119. :topRadius="data.borderRadiusTop"
  120. :bottomRadius="data.borderRadiusBottom"
  121. :titleWidth="330 - marginLeft - marginRight"
  122. @click="sheep.$router.go('/pages/goods/groupon', { id: item.activityId })"
  123. @getHeight="calculateGoodsColumn($event, 'right')"
  124. >
  125. <!-- 购买按钮 -->
  126. <template v-slot:cart>
  127. <button class="ss-reset-button cart-btn" :style="[buyStyle]">
  128. {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
  129. </button>
  130. </template>
  131. </s-goods-column>
  132. </view>
  133. </view>
  134. </view>
  135. </view>
  136. </template>
  137. <script setup>
  138. /**
  139. * 商品卡片
  140. */
  141. import { computed, onMounted, reactive, ref } from 'vue';
  142. import sheep from '@/sheep';
  143. import CombinationApi from '@/sheep/api/promotion/combination';
  144. import SpuApi 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. styles: {
  165. type: Object,
  166. default() {},
  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.getCombinationActivityListByIds(ids);
  224. return data;
  225. }
  226. /**
  227. * 根据商品编号,获取商品详情
  228. * @param ids 商品编号列表
  229. * @return {Promise<undefined>} 商品列表
  230. */
  231. async function getSpuDetail(ids) {
  232. const { data: spu } = await SpuApi.getSpuDetail(ids);
  233. return spu;
  234. }
  235. // 初始化
  236. onMounted(async () => {
  237. // 加载活动列表
  238. const activityList = await getCombinationActivityDetailList(activityIds.join(','));
  239. // 循环获取活动商品SPU详情并添加到spuList
  240. for (const activity of activityList) {
  241. state.spuList.push(await getSpuDetail(activity.spuId));
  242. }
  243. // 循环活动列表
  244. activityList.forEach((activity) => {
  245. // 提取活动价格
  246. const combinationPrice = activity.combinationPrice || Infinity;
  247. // 查找对应的 spu 并更新价格
  248. const spu = state.spuList.find((spu) => activity.spuId === spu.id);
  249. if (spu) {
  250. // 赋值活动名称
  251. // TODO 芋艿:暂定活动名。会在调研一些类似有赞、淘宝、京东的选择
  252. spu.name = activity.name;
  253. // 赋值最低价格
  254. spu.price = Math.min(combinationPrice, spu.price);
  255. // 赋值活动ID,为了点击跳转详情页
  256. spu.activityId = activity.id;
  257. }
  258. });
  259. // 只有双列布局时需要
  260. if (layoutType === LayoutTypeEnum.TWO_COL) {
  261. // 分列
  262. calculateGoodsColumn();
  263. }
  264. });
  265. </script>
  266. <style lang="scss" scoped>
  267. .goods-md-wrap {
  268. width: 100%;
  269. }
  270. .goods-list-box {
  271. width: 50%;
  272. box-sizing: border-box;
  273. .left-list {
  274. &:nth-last-child(1) {
  275. margin-bottom: 0 !important;
  276. }
  277. }
  278. .right-list {
  279. &:nth-last-child(1) {
  280. margin-bottom: 0 !important;
  281. }
  282. }
  283. }
  284. .goods-box {
  285. &:nth-last-of-type(1) {
  286. margin-bottom: 0 !important;
  287. }
  288. }
  289. .goods-md-box,
  290. .goods-sl-box,
  291. .goods-lg-box {
  292. position: relative;
  293. .cart-btn {
  294. position: absolute;
  295. bottom: 18rpx;
  296. right: 20rpx;
  297. z-index: 11;
  298. height: 50rpx;
  299. line-height: 50rpx;
  300. padding: 0 20rpx;
  301. border-radius: 25rpx;
  302. font-size: 24rpx;
  303. color: #fff;
  304. }
  305. }
  306. </style>