s-goods-card.vue 8.7 KB

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