s-goods-card copy.vue 8.4 KB

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