s-coupon-block.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!-- 装修营销组件:优惠券 -->
  2. <template>
  3. <scroll-view class="scroll-box" scroll-x scroll-anchoring>
  4. <view class="coupon-box ss-flex">
  5. <view
  6. class="coupon-item"
  7. :style="[couponBg, { marginLeft: `${data.space}px` }]"
  8. v-for="(item, index) in couponList"
  9. :key="index"
  10. >
  11. <su-coupon
  12. :size="SIZE_LIST[columns - 1]"
  13. :textColor="data.textColor"
  14. background=""
  15. :couponId="item.id"
  16. :title="item.name"
  17. :type="formatCouponDiscountType(item)"
  18. :value="formatCouponDiscountValue(item)"
  19. :sellBy="formatValidityType(item)"
  20. >
  21. <template v-slot:btn>
  22. <!-- 两列时,领取按钮坚排 -->
  23. <button
  24. v-if="columns === 2"
  25. @click.stop="onGetCoupon(item.id)"
  26. class="ss-reset-button card-btn vertical"
  27. :style="[btnStyles]"
  28. >
  29. <view class="btn-text">立即领取</view>
  30. </button>
  31. <button
  32. v-else
  33. class="ss-reset-button card-btn"
  34. :style="[btnStyles]"
  35. @click.stop="onGetCoupon(item.id)"
  36. >
  37. 立即领取
  38. </button>
  39. </template>
  40. </su-coupon>
  41. </view>
  42. </view>
  43. </scroll-view>
  44. </template>
  45. <script setup>
  46. import sheep from '@/sheep';
  47. import CouponApi from '@/sheep/api/promotion/coupon';
  48. import { ref, onMounted } from 'vue';
  49. import { CouponTemplateValidityTypeEnum, PromotionDiscountTypeEnum } from "@/sheep/util/const";
  50. import { floatToFixed2, formatDate } from "@/sheep/util";
  51. const props = defineProps({
  52. data: {
  53. type: Object,
  54. default: () => ({}),
  55. },
  56. styles: {
  57. type: Object,
  58. default: () => ({}),
  59. },
  60. });
  61. const { columns, button } = props.data;
  62. const SIZE_LIST = ['lg', 'md', 'xs']
  63. const couponBg = {
  64. background: `url(${sheep.$url.cdn(props.data.bgImg)}) no-repeat top center / 100% 100%`,
  65. };
  66. const btnStyles = {
  67. background: button.bgColor,
  68. color: button.color,
  69. };
  70. // 格式化【折扣类型】
  71. const formatCouponDiscountType = (coupon) => {
  72. if(coupon.discountType === PromotionDiscountTypeEnum.PRICE.type) {
  73. return 'reduce'
  74. }
  75. if(coupon.discountType === PromotionDiscountTypeEnum.PERCENT.type) {
  76. return 'percent'
  77. }
  78. return `未知【${coupon.discountType}】`
  79. }
  80. // 格式化【折扣】
  81. const formatCouponDiscountValue = (coupon) => {
  82. if(coupon.discountType === PromotionDiscountTypeEnum.PRICE.type) {
  83. return floatToFixed2(coupon.discountPrice)
  84. }
  85. if(coupon.discountType === PromotionDiscountTypeEnum.PERCENT.type) {
  86. return coupon.discountPercent
  87. }
  88. return `未知【${coupon.discountType}】`
  89. }
  90. // 格式化【有效期限】
  91. const formatValidityType = (row) => {
  92. if (row.validityType === CouponTemplateValidityTypeEnum.DATE.type) {
  93. return `${formatDate(row.validStartTime)} 至 ${formatDate(row.validEndTime)}`
  94. }
  95. if (row.validityType === CouponTemplateValidityTypeEnum.TERM.type) {
  96. return `领取后第 ${row.fixedStartTerm} - ${row.fixedEndTerm} 天内可用`
  97. }
  98. return '未知【' + row.validityType + '】'
  99. }
  100. const couponList = ref([]);
  101. // 立即领取优惠券
  102. async function onGetCoupon(id) {
  103. const { error, msg } = await CouponApi.takeCoupon(id);
  104. if (error === 0) {
  105. uni.showToast({
  106. title: msg,
  107. icon: 'none',
  108. });
  109. return
  110. }
  111. await getCouponTemplateList()
  112. }
  113. const getCouponTemplateList = async () => {
  114. const { data } = await CouponApi.getCouponTemplateListByIds(props.data.couponIds.join(','));
  115. couponList.value = data;
  116. }
  117. onMounted(() => {
  118. getCouponTemplateList()
  119. });
  120. </script>
  121. <style lang="scss" scoped>
  122. .card-btn {
  123. width: 140rpx;
  124. height: 50rpx;
  125. border-radius: 25rpx;
  126. font-size: 24rpx;
  127. line-height: 50rpx;
  128. &.vertical {
  129. width: 50rpx;
  130. height: 140rpx;
  131. margin: auto 20rpx auto 0;
  132. .btn-text {
  133. font-size: 24rpx;
  134. text-align: center;
  135. writing-mode: vertical-lr;
  136. }
  137. }
  138. }
  139. .coupon-item {
  140. &:nth-of-type(1) {
  141. margin-left: 0 !important;
  142. }
  143. }
  144. </style>