营销:适配商城装修组件【优惠券】

pull/18/head
owen 2023-11-22 16:57:59 +08:00
parent 04b0b87126
commit 92e164747a
7 changed files with 239 additions and 142 deletions

View File

@ -0,0 +1,12 @@
import request from '@/sheep/request';
export default {
// 获得优惠劵模板列表
getCouponTemplateListByIds: (ids) => {
return request({
url: '/app-api/promotion/coupon-template/list-by-ids',
method: 'GET',
params: { ids }
});
}
}

View File

@ -42,7 +42,7 @@
<!-- 营销组件小程序直播 -->
<s-live-block v-if="type === 'mplive'" :data="data" :styles="styles" />
<!-- 营销组件优惠券 -->
<s-coupon-block v-if="type === 'coupon'" :data="data" :styles="styles" />
<s-coupon-block v-if="type === 'CouponCard'" :data="data" :styles="styles" />
<!-- 会员组件会员卡片 -->
<s-user-card v-if="type === 'userCard'" />

View File

@ -1,126 +1,54 @@
<!-- 优惠券组 -->
<template>
<view>
<!-- 样式1 -->
<view class="lg-coupon-wrap" v-if="mode == 1">
<scroll-view class="scroll-box" scroll-x scroll-anchoring>
<view class="coupon-box ss-flex">
<view
class="coupon-item"
:style="[couponBg, { marginLeft: data.space + 'px' }]"
v-for="(item, index) in couponList"
:key="index"
>
<su-coupon
size="lg"
:textColor="data.fill.color"
background=""
:couponId="item.id"
:title="item.amount_text"
:value="item.amount"
:surplus="item.stock"
:type="item.type"
:sellBy="`${item.get_start_time.substring(0, 10)}${item.get_end_time.substring(
0,
10,
)}`"
<scroll-view class="scroll-box" scroll-x scroll-anchoring>
<view class="coupon-box ss-flex">
<view
class="coupon-item"
:style="[couponBg, { marginLeft: `${data.space}px` }]"
v-for="(item, index) in couponList"
: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
v-if="columns === 2"
@click.stop="onGetCoupon(item.id)"
class="ss-reset-button card-btn vertical"
:style="[btnStyles]"
>
<template v-slot:btn>
<button
class="ss-reset-button card-btn"
:style="[btnStyles]"
@click.stop="onGetCoupon(item.id)"
>
{{ item.get_status_text }}
</button>
</template>
</su-coupon>
</view>
</view>
</scroll-view>
</view>
<!-- 样式2 -->
<view class="md-coupon-wrap" v-if="mode == 2">
<scroll-view class="scroll-box" scroll-x scroll-anchoring>
<view class="coupon-box ss-flex">
<view
class="coupon-item"
:style="[couponBg, { marginLeft: data.space + 'px' }]"
v-for="(item, index) in couponList"
:key="index"
>
<su-coupon
size="md"
:textColor="data.fill.color"
background=""
:title="item.amount_text"
:value="item.amount"
:surplus="item.stock"
:couponId="item.id"
:type="item.type"
:sellBy="`${item.get_start_time.substring(0, 10)}${item.get_end_time.substring(
0,
10,
)}`"
<view class="btn-text">立即领取</view>
</button>
<button
v-else
class="ss-reset-button card-btn"
:style="[btnStyles]"
@click.stop="onGetCoupon(item.id)"
>
<template v-slot:btn>
<button
@click.stop="onGetCoupon(item.id)"
class="ss-reset-button card-btn ss-flex ss-row-center ss-col-center"
:style="[btnStyles]"
>
<view class="btn-text">{{ item.get_status_text }}</view>
</button>
</template>
</su-coupon>
</view>
</view>
</scroll-view>
立即领取
</button>
</template>
</su-coupon>
</view>
</view>
<!-- 样式3 -->
<view class="xs-coupon-wrap" v-if="mode == 3">
<scroll-view class="scroll-box" scroll-x scroll-anchoring>
<view class="coupon-box ss-flex">
<view
class="coupon-item"
:style="[couponBg, { marginLeft: data.space + 'px' }]"
v-for="(item, index) in couponList"
:key="index"
>
<su-coupon
size="xs"
:textColor="data.fill.color"
background=""
:title="item.amount_text"
:value="item.amount"
:surplus="item.stock"
:couponId="item.id"
:type="item.type"
:sellBy="`${item.get_start_time.substring(0, 10)}${item.get_end_time.substring(
0,
10,
)}`"
>
<template v-slot:btn>
<button
class="ss-reset-button card-btn"
:style="[btnStyles]"
@click.stop="onGetCoupon(item.id)"
>
{{ item.get_status_text }}
</button>
</template>
</su-coupon>
</view>
</view>
</scroll-view>
</view>
</view>
</scroll-view>
</template>
<script setup>
import sheep from '@/sheep';
import TemplateApi from '@/sheep/api/promotion/coupon';
import { ref, onMounted } from 'vue';
import {CouponTemplateValidityTypeEnum, PromotionDiscountTypeEnum} from "@/sheep/util/const";
import {floatToFixed2, formatDate} from "@/sheep/util";
const props = defineProps({
data: {
@ -132,14 +60,48 @@
default: () => ({}),
},
});
const { mode, button } = props.data;
const { columns, button } = props.data;
const SIZE_LIST = ['lg', 'md', 'xs']
const couponBg = {
background: `url(${sheep.$url.cdn(props.data.fill.bgImage)}) no-repeat top center / 100% 100%`,
background: `url(${sheep.$url.cdn(props.data.bgImg)}) no-repeat top center / 100% 100%`,
};
const btnStyles = {
background: button.bgColor,
color: button.color,
};
//
const formatCouponDiscountType = (coupon) => {
if(coupon.discountType === PromotionDiscountTypeEnum.PRICE.type) {
return 'reduce'
}
if(coupon.discountType === PromotionDiscountTypeEnum.PERCENT.type) {
return 'percent'
}
return `未知【${coupon.discountType}`
}
//
const formatCouponDiscountValue = (coupon) => {
if(coupon.discountType === PromotionDiscountTypeEnum.PRICE.type) {
return floatToFixed2(coupon.discountPrice)
}
if(coupon.discountType === PromotionDiscountTypeEnum.PERCENT.type) {
return coupon.discountPercent
}
return `未知【${coupon.discountType}`
}
//
const formatValidityType = (row) => {
if (row.validityType === CouponTemplateValidityTypeEnum.DATE.type) {
return `${formatDate(row.validStartTime)}${formatDate(row.validEndTime)}`
}
if (row.validityType === CouponTemplateValidityTypeEnum.TERM.type) {
return `领取后第 ${row.fixedStartTerm} - ${row.fixedEndTerm} 天内可用`
}
return '未知【' + row.validityType + '】'
}
const couponList = ref([]);
//
async function onGetCoupon(id) {
@ -149,15 +111,16 @@
title: msg,
icon: 'none',
});
} else {
let { data } = await sheep.$api.coupon.list({ ids: props.data.couponIds.join(',') });
couponList.value = [...data.data];
return
}
await getCouponTemplateList()
}
onMounted(async () => {
let { data } = await sheep.$api.coupon.list({ ids: props.data.couponIds.join(',') });
// couponList.value = [...data.data, ...data.data, ...data.data, ...data.data];
couponList.value = [...data.data];
const getCouponTemplateList = async () => {
const { data } = await TemplateApi.getCouponTemplateListByIds(props.data.couponIds.join(','));
couponList.value = data;
}
onMounted(() => {
getCouponTemplateList()
});
</script>
@ -168,25 +131,21 @@
border-radius: 25rpx;
font-size: 24rpx;
line-height: 50rpx;
&.vertical {
width: 50rpx;
height: 140rpx;
margin: auto 20rpx auto 0;
.btn-text {
font-size: 24rpx;
text-align: center;
writing-mode: vertical-lr;
}
}
}
.coupon-item {
&:nth-of-type(1) {
margin-left: 0 !important;
}
}
.md-coupon-wrap {
.card-btn {
width: 50rpx;
height: 140rpx;
margin: auto 0;
margin-right: 20rpx;
.btn-text {
font-size: 24rpx;
text-align: center;
writing-mode: vertical-lr;
writing-mode: tb-lr;
}
}
}
</style>

View File

@ -1,5 +1,5 @@
<template>
<view class="u-page__item" v-if="tabbar?.items.length > 0">
<view class="u-page__item" v-if="tabbar?.items?.length > 0">
<su-tabbar
:value="path"
:fixed="true"

View File

@ -43,7 +43,7 @@
</view>
<view class="ss-m-b-28">
<view class="title-text ss-m-b-10">{{ props.title }}</view>
<view class="surplus-text">仅剩{{ props.surplus }}</view>
<view class="surplus-text" v-if="props.surplus">{{ props.surplus }}</view>
</view>
</view>
<view class="card-right ss-flex ss-row-center">
@ -82,7 +82,7 @@
{{ state.stateMap[props.state] }}
</button>
</slot>
<view class="surplus-text ss-m-t-24">仅剩{{ props.surplus }}</view>
<view class="surplus-text ss-m-t-24" v-if="props.surplus">{{ props.surplus }}</view>
</view>
</view>
</view>
@ -159,7 +159,7 @@
},
surplus: {
type: [Number, String],
default: 1000,
default: 0,
},
type: {
type: String,

47
sheep/util/const.js Normal file
View File

@ -0,0 +1,47 @@
// ========== MALL - 营销模块 ==========
/**
* 优惠类型枚举
*/
export const PromotionDiscountTypeEnum = {
PRICE: {
type: 1,
name: '满减'
},
PERCENT: {
type: 2,
name: '折扣'
}
}
/**
* 优惠劵模板的有限期类型的枚举
*/
export const CouponTemplateValidityTypeEnum = {
DATE: {
type: 1,
name: '固定日期可用'
},
TERM: {
type: 2,
name: '领取之后可用'
}
}
/**
* 营销的商品范围枚举
*/
export const PromotionProductScopeEnum = {
ALL: {
scope: 1,
name: '通用劵'
},
SPU: {
scope: 2,
name: '商品劵'
},
CATEGORY: {
scope: 3,
name: '品类劵'
}
}

79
sheep/util/index.js Normal file
View File

@ -0,0 +1,79 @@
import dayjs from "dayjs";
/**
* 将一个整数转换为分数保留两位小数
* @param {number | string | undefined} num 整数
* @return {number} 分数
*/
export const formatToFraction = (num) => {
if (typeof num === 'undefined') return 0
const parsedNumber = typeof num === 'string' ? parseFloat(num) : num
return parseFloat((parsedNumber / 100).toFixed(2))
}
/**
* 将一个数转换为 1.00 这样
* 数据呈现的时候使用
*
* @param {number | string | undefined} num 整数
* @return {string} 分数
*/
export const floatToFixed2 = (num) => {
let str = '0.00'
if (typeof num === 'undefined') {
return str
}
const f = formatToFraction(num)
const decimalPart = f.toString().split('.')[1]
const len = decimalPart ? decimalPart.length : 0
switch (len) {
case 0:
str = f.toString() + '.00'
break
case 1:
str = f.toString() + '.0'
break
case 2:
str = f.toString()
break
}
return str
}
/**
* 将一个分数转换为整数
*
* @param {number | string | undefined} num 分数
* @return {number} 整数
*/
export const convertToInteger = (num) => {
if (typeof num === 'undefined') return 0
const parsedNumber = typeof num === 'string' ? parseFloat(num) : num
// TODO 分转元后还有小数则四舍五入
return Math.round(parsedNumber * 100)
}
/**
* 时间日期转换
* @param {dayjs.ConfigType} date 当前时间new Date() 格式
* @param {string} format 需要转换的时间格式字符串
* @description format 字符串随意 `YYYY-mm、YYYY-mm-dd`
* @description format 季度"YYYY-mm-dd HH:MM:SS QQQQ"
* @description format 星期"YYYY-mm-dd HH:MM:SS WWW"
* @description format 几周"YYYY-mm-dd HH:MM:SS ZZZ"
* @description format 季度 + 星期 + 几周"YYYY-mm-dd HH:MM:SS WWW QQQQ ZZZ"
* @returns {string} 返回拼接后的时间字符串
*/
export function formatDate(date, format) {
// 日期不存在,则返回空
if (!date) {
return ''
}
// 日期存在,则进行格式化
if (format === undefined) {
format = 'YYYY-MM-DD HH:mm:ss'
}
return dayjs(date).format(format)
}