s-point-card.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <!-- 装修商品组件:【积分商城】商品卡片 -->
  2. <template>
  3. <!-- 商品卡片 -->
  4. <view>
  5. <!-- 布局1. 单列大图(上图,下内容)-->
  6. <view
  7. v-if="state.property.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: state.property.space * 2 + 'rpx' }]"
  15. >
  16. <s-goods-column
  17. class=""
  18. size="sl"
  19. :goodsFields="state.property.fields"
  20. :tagStyle="state.property.badge"
  21. :data="item"
  22. :titleColor="state.property.fields.name?.color"
  23. :subTitleColor="state.property.fields.introduction.color"
  24. :topRadius="state.property.borderRadiusTop"
  25. :bottomRadius="state.property.borderRadiusBottom"
  26. @click="sheep.$router.go('/pages/goods/point', { id: item.activityId })"
  27. >
  28. <!-- 购买按钮 -->
  29. <template v-slot:cart>
  30. <button class="ss-reset-button cart-btn" :style="[buyStyle]">
  31. {{ state.property.btnBuy.type === 'text' ? state.property.btnBuy.text : '' }}
  32. </button>
  33. </template>
  34. </s-goods-column>
  35. </view>
  36. </view>
  37. <!-- 布局2. 单列小图(左图,右内容) -->
  38. <view
  39. v-if="state.property.layoutType === LayoutTypeEnum.ONE_COL_SMALL_IMG && state.spuList.length"
  40. class="goods-lg-box"
  41. >
  42. <view
  43. class="goods-box"
  44. :style="[{ marginBottom: state.property.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="state.property.fields"
  52. :data="item"
  53. :tagStyle="state.property.badge"
  54. :titleColor="state.property.fields.name?.color"
  55. :subTitleColor="state.property.fields.introduction.color"
  56. :topRadius="state.property.borderRadiusTop"
  57. :bottomRadius="state.property.borderRadiusBottom"
  58. @tap="sheep.$router.go('/pages/goods/point', { id: item.activityId })"
  59. >
  60. <!-- 购买按钮 -->
  61. <template v-slot:cart>
  62. <button class="ss-reset-button cart-btn" :style="[buyStyle]">
  63. {{ state.property.btnBuy.type === 'text' ? state.property.btnBuy.text : '' }}
  64. </button>
  65. </template>
  66. </s-goods-column>
  67. </view>
  68. </view>
  69. <!-- 布局3. 双列(每一列:上图,下内容)-->
  70. <view
  71. v-if="state.property.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: state.property.space + 'rpx', marginBottom: state.property.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="state.property.fields"
  85. :tagStyle="state.property.badge"
  86. :data="item"
  87. :titleColor="state.property.fields.name?.color"
  88. :subTitleColor="state.property.fields.introduction.color"
  89. :topRadius="state.property.borderRadiusTop"
  90. :bottomRadius="state.property.borderRadiusBottom"
  91. :titleWidth="330 - marginLeft - marginRight"
  92. @click="sheep.$router.go('/pages/goods/point', { 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. {{ state.property.btnBuy.type === 'text' ? state.property.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: state.property.space + 'rpx', marginBottom: state.property.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="state.property.fields"
  115. :tagStyle="state.property.badge"
  116. :data="item"
  117. :titleColor="state.property.fields.name?.color"
  118. :subTitleColor="state.property.fields.introduction.color"
  119. :topRadius="state.property.borderRadiusTop"
  120. :bottomRadius="state.property.borderRadiusBottom"
  121. :titleWidth="330 - marginLeft - marginRight"
  122. @click="sheep.$router.go('/pages/goods/point', { 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. {{ state.property.btnBuy.type === 'text' ? state.property.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, nextTick, onMounted, reactive, watch } from 'vue';
  142. import sheep from '@/sheep';
  143. import SpuApi from '@/sheep/api/product/spu';
  144. import { PromotionActivityTypeEnum } from '@/sheep/util/const';
  145. import { isEmpty } from '@/sheep/helper/utils';
  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. spuList: [],
  157. leftSpuList: [],
  158. rightSpuList: [],
  159. property: {
  160. 'layoutType': 'oneColBigImg',
  161. 'fields': {
  162. 'name': {
  163. 'show': true,
  164. 'color': '#000',
  165. },
  166. 'introduction': {
  167. 'show': true,
  168. 'color': '#999',
  169. },
  170. 'price': {
  171. 'show': true,
  172. 'color': '#ff3000',
  173. },
  174. 'marketPrice': {
  175. 'show': true,
  176. 'color': '#c4c4c4',
  177. },
  178. 'salesCount': {
  179. 'show': true,
  180. 'color': '#c4c4c4',
  181. },
  182. 'stock': {
  183. 'show': true,
  184. 'color': '#c4c4c4',
  185. },
  186. },
  187. 'badge': {
  188. 'show': false,
  189. 'imgUrl': '',
  190. },
  191. 'btnBuy': {
  192. 'type': 'text',
  193. 'text': '立即兑换',
  194. 'bgBeginColor': '#FF6000',
  195. 'bgEndColor': '#FE832A',
  196. 'imgUrl': '',
  197. },
  198. 'borderRadiusTop': 8,
  199. 'borderRadiusBottom': 8,
  200. 'space': 8,
  201. 'style': {
  202. 'bgType': 'color',
  203. 'bgColor': '',
  204. 'marginLeft': 8,
  205. 'marginRight': 8,
  206. 'marginBottom': 8,
  207. },
  208. },
  209. });
  210. const props = defineProps({
  211. property: {
  212. type: Object,
  213. default: () => ({}),
  214. },
  215. });
  216. // 动态更新 property
  217. watch(() => props.property, (newVal) => {
  218. state.property = { ...state.property, ...newVal };
  219. }, { immediate: true, deep: true });
  220. const { marginLeft, marginRight } = state.property.styles || {};
  221. // 购买按钮样式
  222. const buyStyle = computed(() => {
  223. if (state.property.btnBuy.type === 'text') {
  224. // 文字按钮:线性渐变背景颜色
  225. return {
  226. background: `linear-gradient(to right, ${state.property.btnBuy.bgBeginColor}, ${state.property.btnBuy.bgEndColor})`,
  227. };
  228. }
  229. if (state.property.btnBuy.type === 'img') {
  230. // 图片按钮
  231. return {
  232. width: '54rpx',
  233. height: '54rpx',
  234. background: `url(${sheep.$url.cdn(state.property.btnBuy.imgUrl)}) no-repeat`,
  235. backgroundSize: '100% 100%',
  236. };
  237. }
  238. });
  239. //region 商品瀑布流布局
  240. // 下一个要处理的商品索引
  241. let count = 0;
  242. // 左列的高度
  243. let leftHeight = 0;
  244. // 右列的高度
  245. let rightHeight = 0;
  246. /**
  247. * 计算商品在左列还是右列
  248. * @param height 商品的高度
  249. * @param where 添加到哪一列
  250. */
  251. function calculateGoodsColumn(height = 0, where = 'left') {
  252. // 处理完
  253. if (!state.spuList[count]) return;
  254. // 增加列的高度
  255. if (where === 'left') leftHeight += height;
  256. if (where === 'right') rightHeight += height;
  257. // 添加到矮的一列
  258. if (leftHeight <= rightHeight) {
  259. state.leftSpuList.push(state.spuList[count]);
  260. } else {
  261. state.rightSpuList.push(state.spuList[count]);
  262. }
  263. // 计数
  264. count++;
  265. }
  266. //endregion
  267. /**
  268. * 根据商品编号,获取商品详情
  269. * @param ids 商品编号列表
  270. * @return {Promise<undefined>} 商品列表
  271. */
  272. async function getSpuDetail(ids) {
  273. const { data: spu } = await SpuApi.getSpuDetail(ids);
  274. return spu;
  275. }
  276. async function concatActivity(list) {
  277. if (isEmpty(list)) {
  278. return;
  279. }
  280. // 循环获取活动商品SPU详情并添加到spuList
  281. for (const activity of list) {
  282. state.spuList.push(await getSpuDetail(activity.spuId));
  283. }
  284. // 循环活动列表
  285. list.forEach((activity) => {
  286. // 查找对应的 spu 并更新价格
  287. const spu = state.spuList.find((spu) => activity.spuId === spu.id);
  288. if (spu) {
  289. spu.pointStock = activity.stock;
  290. spu.pointTotalStock = activity.totalStock;
  291. spu.point = activity.point;
  292. spu.pointPrice = activity.price;
  293. // 赋值活动ID,为了点击跳转详情页
  294. spu.activityId = activity.id;
  295. // 赋值活动类型
  296. spu.activityType = PromotionActivityTypeEnum.POINT.type;
  297. }
  298. });
  299. // 只有双列布局时需要
  300. if (state.property.layoutType === LayoutTypeEnum.TWO_COL) {
  301. // 分列
  302. calculateGoodsColumn();
  303. }
  304. }
  305. function getActivityCount() {
  306. return state.spuList.length;
  307. }
  308. defineExpose({ concatActivity,getActivityCount,calculateGoodsColumn });
  309. </script>
  310. <style lang="scss" scoped>
  311. .goods-md-wrap {
  312. width: 100%;
  313. }
  314. .goods-list-box {
  315. width: 50%;
  316. box-sizing: border-box;
  317. .left-list {
  318. &:nth-last-child(1) {
  319. margin-bottom: 0 !important;
  320. }
  321. }
  322. .right-list {
  323. &:nth-last-child(1) {
  324. margin-bottom: 0 !important;
  325. }
  326. }
  327. }
  328. .goods-box {
  329. &:nth-last-of-type(1) {
  330. margin-bottom: 0 !important;
  331. }
  332. }
  333. .goods-md-box,
  334. .goods-sl-box,
  335. .goods-lg-box {
  336. position: relative;
  337. .cart-btn {
  338. position: absolute;
  339. bottom: 18rpx;
  340. right: 20rpx;
  341. z-index: 11;
  342. height: 50rpx;
  343. line-height: 50rpx;
  344. padding: 0 20rpx;
  345. border-radius: 25rpx;
  346. font-size: 24rpx;
  347. color: #fff;
  348. }
  349. }
  350. </style>