s-goods-card.vue 7.7 KB

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