s-goods-card.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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(item)" 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 = (item) => {
  117. if (item.proType == 0) {
  118. sheep.$router.go('/pages/chat/index')
  119. } else {
  120. sheep.$router.go('/pages/customerService/index', {
  121. data: JSON.stringify({
  122. url: item.qrCodePath,
  123. })
  124. })
  125. }
  126. console.log(item)
  127. // sheep.$router.go('/pages/customerService/index')
  128. }
  129. // 购买按钮样式
  130. const buyStyle = computed(() => {
  131. if (btnBuy.type === 'text') {
  132. // 文字按钮:线性渐变背景颜色
  133. return {
  134. background: `linear-gradient(to right, ${btnBuy.bgBeginColor}, ${btnBuy.bgEndColor})`,
  135. };
  136. }
  137. if (btnBuy.type === 'img') {
  138. // 图片按钮
  139. return {
  140. width: '54rpx',
  141. height: '54rpx',
  142. background: `url(${sheep.$url.cdn(btnBuy.imgUrl)}) no-repeat`,
  143. backgroundSize: '100% 100%',
  144. };
  145. }
  146. });
  147. //region 产品瀑布流布局
  148. // 下一个要处理的产品索引
  149. let count = 0;
  150. // 左列的高度
  151. let leftHeight = 0;
  152. // 右列的高度
  153. let rightHeight = 0;
  154. /**
  155. * 计算产品在左列还是右列
  156. * @param height 产品的高度
  157. * @param where 添加到哪一列
  158. */
  159. function calculateGoodsColumn(height = 0, where = 'left') {
  160. // 处理完
  161. if (!state.goodsList[count]) return;
  162. // 增加列的高度
  163. if (where === 'left') leftHeight += height;
  164. if (where === 'right') rightHeight += height;
  165. // 添加到矮的一列
  166. if (leftHeight <= rightHeight) {
  167. state.leftGoodsList.push(state.goodsList[count]);
  168. } else {
  169. state.rightGoodsList.push(state.goodsList[count]);
  170. }
  171. // 计数
  172. count++;
  173. }
  174. //endregion
  175. /**
  176. * 根据产品编号列表,获取产品列表
  177. * @param ids 产品编号列表
  178. * @return {Promise<undefined>} 产品列表
  179. */
  180. async function getGoodsListByIds(ids) {
  181. const { data } = await SpuApi.getSpuListByIds(ids);
  182. return data;
  183. }
  184. // 初始化
  185. onMounted(async () => {
  186. // 加载产品列表
  187. state.goodsList = await getGoodsListByIds(spuIds.join(','));
  188. // 拼接结算信息(营销)
  189. await OrderApi.getSettlementProduct(state.goodsList.map((item) => item.id).join(',')).then(
  190. (res) => {
  191. if (res.code !== 0) {
  192. return;
  193. }
  194. appendSettlementProduct(state.goodsList, res.data);
  195. },
  196. );
  197. // 只有双列布局时需要
  198. if (layoutType === LayoutTypeEnum.TWO_COL) {
  199. // 分列
  200. calculateGoodsColumn();
  201. }
  202. });
  203. </script>
  204. <style lang="scss" scoped>
  205. .goods-md-wrap {
  206. width: 100%;
  207. }
  208. .goods-list-box {
  209. width: 50%;
  210. box-sizing: border-box;
  211. .left-list {
  212. &:nth-last-child(1) {
  213. margin-bottom: 0 !important;
  214. }
  215. }
  216. .right-list {
  217. &:nth-last-child(1) {
  218. margin-bottom: 0 !important;
  219. }
  220. }
  221. }
  222. .goods-box {
  223. &:nth-last-of-type(1) {
  224. margin-bottom: 0 !important;
  225. }
  226. }
  227. .goods-md-box,
  228. .goods-sl-box,
  229. .goods-lg-box {
  230. position: relative;
  231. .cart-btn {
  232. position: absolute;
  233. bottom: 18rpx;
  234. right: 20rpx;
  235. z-index: 11;
  236. height: 50rpx;
  237. line-height: 50rpx;
  238. padding: 0 20rpx;
  239. border-radius: 25rpx;
  240. font-size: 24rpx;
  241. color: #fff;
  242. }
  243. }
  244. </style>