zyejMAll-mobile/sheep/components/s-coupon-block/s-coupon-block.vue

171 lines
4.7 KiB
Vue
Raw Normal View History

2024-08-07 10:31:42 +08:00
<!-- 装修营销组件优惠券 -->
<template>
<scroll-view class="scroll-box" scroll-x scroll-anchoring>
<view class="coupon-box ss-flex">
2024-09-27 18:36:26 +08:00
<!-- couponList -->
2024-08-07 10:31:42 +08:00
<view
2024-09-27 18:36:26 +08:00
class="coupon-item new-class"
2024-10-11 16:07:43 +08:00
:style="[item.canTake ? couponBg : couponBgFalse, { marginLeft: `${data.space}px` }]"
2024-09-30 19:00:13 +08:00
v-for="(item, index) in couponList"
2024-08-07 10:31:42 +08:00
:key="index"
>
<su-coupon
:size="SIZE_LIST[columns - 1]"
:textColor="data.textColor"
background=""
:couponId="item.id"
:title="item.name"
:type="formatCouponDiscountType(item)"
:value="formatCouponDiscountValue(item)"
:sellBy="formatValidityType(item)"
>
<template v-slot:btn>
<!-- 两列时领取按钮坚排 -->
<button
2024-10-11 16:07:43 +08:00
v-if="item.canTake"
2024-08-07 10:31:42 +08:00
@click.stop="onGetCoupon(item.id)"
class="ss-reset-button card-btn vertical"
:style="[btnStyles]"
>
<view class="btn-text">立即领取</view>
</button>
<button
v-else
class="ss-reset-button card-btn"
:style="[btnStyles]"
>
2024-10-11 16:07:43 +08:00
已领取
2024-08-07 10:31:42 +08:00
</button>
</template>
</su-coupon>
</view>
</view>
</scroll-view>
</template>
<script setup>
import sheep from '@/sheep';
import CouponApi from '@/sheep/api/promotion/coupon';
import { ref, onMounted } from 'vue';
2024-08-07 21:40:27 +08:00
import { CouponTemplateValidityTypeEnum, PromotionDiscountTypeEnum } from '@/sheep/util/const';
import { floatToFixed2, formatDate } from '@/sheep/util';
2024-08-07 10:31:42 +08:00
const props = defineProps({
data: {
type: Object,
default: () => ({}),
},
styles: {
type: Object,
default: () => ({}),
},
});
const { columns, button } = props.data;
2024-08-07 21:40:27 +08:00
const SIZE_LIST = ['lg', 'md', 'xs'];
2024-08-07 10:31:42 +08:00
const couponBg = {
background: `url(${sheep.$url.cdn(props.data.bgImg)}) no-repeat top center / 100% 100%`,
};
2024-10-11 16:07:43 +08:00
const couponBgFalse = {
background: `url('https://zysc.fjptzykj.com:3000/shangcheng/94dfaa69257a102da3e28aa9b82cc7e04be5cea38555941972f6b5a3c2fc47a3.png') no-repeat top center / 100% 100%`,
};
2024-08-07 10:31:42 +08:00
const btnStyles = {
background: button.bgColor,
color: button.color,
};
// 格式化【折扣类型】
const formatCouponDiscountType = (coupon) => {
2024-08-07 21:40:27 +08:00
if (coupon.discountType === PromotionDiscountTypeEnum.PRICE.type) {
return 'reduce';
2024-08-07 10:31:42 +08:00
}
2024-08-07 21:40:27 +08:00
if (coupon.discountType === PromotionDiscountTypeEnum.PERCENT.type) {
return 'percent';
2024-08-07 10:31:42 +08:00
}
2024-08-07 21:40:27 +08:00
return `未知【${coupon.discountType}`;
};
2024-08-07 10:31:42 +08:00
// 格式化【折扣】
const formatCouponDiscountValue = (coupon) => {
2024-08-07 21:40:27 +08:00
if (coupon.discountType === PromotionDiscountTypeEnum.PRICE.type) {
return floatToFixed2(coupon.discountPrice);
2024-08-07 10:31:42 +08:00
}
2024-08-07 21:40:27 +08:00
if (coupon.discountType === PromotionDiscountTypeEnum.PERCENT.type) {
return coupon.discountPercent;
2024-08-07 10:31:42 +08:00
}
2024-08-07 21:40:27 +08:00
return `未知【${coupon.discountType}`;
};
2024-08-07 10:31:42 +08:00
// 格式化【有效期限】
const formatValidityType = (row) => {
if (row.validityType === CouponTemplateValidityTypeEnum.DATE.type) {
2024-08-07 21:40:27 +08:00
return `${formatDate(row.validStartTime)}${formatDate(row.validEndTime)}`;
2024-08-07 10:31:42 +08:00
}
if (row.validityType === CouponTemplateValidityTypeEnum.TERM.type) {
2024-08-07 21:40:27 +08:00
return `领取后第 ${row.fixedStartTerm} - ${row.fixedEndTerm} 天内可用`;
2024-08-07 10:31:42 +08:00
}
2024-08-07 21:40:27 +08:00
return '未知【' + row.validityType + '】';
};
2024-08-07 10:31:42 +08:00
const couponList = ref([]);
// 立即领取优惠券
async function onGetCoupon(id) {
const { error, msg } = await CouponApi.takeCoupon(id);
if (error === 0) {
uni.showToast({
title: msg,
icon: 'none',
});
2024-08-07 21:40:27 +08:00
return;
2024-08-07 10:31:42 +08:00
}
2024-08-07 21:40:27 +08:00
await getCouponTemplateList();
2024-08-07 10:31:42 +08:00
}
2024-09-30 19:00:13 +08:00
// 旧接口 一行一个
// const getCouponTemplateList = async () => {
// const { data } = await CouponApi.getCouponTemplateListByIds(props.data.couponIds.join(','));
// couponList.value = data;
// };
// 新接口 一行三个
2024-08-07 10:31:42 +08:00
const getCouponTemplateList = async () => {
2024-09-30 19:00:13 +08:00
const { data } = await CouponApi.getCouponTemplateList();
2024-08-07 10:31:42 +08:00
couponList.value = data;
2024-08-07 21:40:27 +08:00
};
2024-09-30 19:00:13 +08:00
2024-08-07 10:31:42 +08:00
onMounted(() => {
2024-08-07 21:40:27 +08:00
getCouponTemplateList();
2024-08-07 10:31:42 +08:00
});
</script>
<style lang="scss" scoped>
2024-09-27 18:36:26 +08:00
.new-class{
margin: 0 10px !important;
margin-right: 2px !important;
}
2024-08-07 10:31:42 +08:00
.card-btn {
2024-10-11 16:07:43 +08:00
writing-mode: tb-rl;
width: 35px;
height: auto;
2024-08-07 10:31:42 +08:00
border-radius: 25rpx;
font-size: 24rpx;
line-height: 50rpx;
&.vertical {
width: 50rpx;
height: 140rpx;
2024-10-11 16:07:43 +08:00
margin: 0;
2024-08-07 10:31:42 +08:00
.btn-text {
font-size: 24rpx;
text-align: center;
writing-mode: vertical-lr;
}
}
}
.coupon-item {
2024-09-27 18:36:26 +08:00
// &:nth-of-type(1) {
// margin-left: 0 !important;
// }
2024-08-07 10:31:42 +08:00
}
</style>