【功能修复】秒杀装修重构

pull/96/head
卢越 2024-09-08 11:39:28 +08:00
parent 27af994667
commit 9521d4b557
3 changed files with 325 additions and 172 deletions

View File

@ -1,4 +1,4 @@
import request from "@/sheep/request"; import request from '@/sheep/request';
const SeckillApi = { const SeckillApi = {
// 获得秒杀时间段列表 // 获得秒杀时间段列表
@ -16,6 +16,17 @@ const SeckillApi = {
return request({ url: 'promotion/seckill-activity/page', method: 'GET', params }); return request({ url: 'promotion/seckill-activity/page', method: 'GET', params });
}, },
// 获得秒杀活动列表,基于活动编号数组
getSeckillActivityListByIds: (ids) => {
return request({
url: '/promotion/seckill-activity/list-by-ids',
method: 'GET',
params: {
ids,
},
});
},
/** /**
* 获得秒杀活动明细 * 获得秒杀活动明细
* @param {number} id 秒杀活动编号 * @param {number} id 秒杀活动编号
@ -25,9 +36,9 @@ const SeckillApi = {
return request({ return request({
url: 'promotion/seckill-activity/get-detail', url: 'promotion/seckill-activity/get-detail',
method: 'GET', method: 'GET',
params: { id } params: { id },
}); });
} },
} };
export default SeckillApi; export default SeckillApi;

View File

@ -145,7 +145,6 @@
import sheep from '@/sheep'; import sheep from '@/sheep';
import CombinationApi from '@/sheep/api/promotion/combination'; import CombinationApi from '@/sheep/api/promotion/combination';
import SpuApi from '@/sheep/api/product/spu'; import SpuApi from '@/sheep/api/product/spu';
import spu from '@/sheep/api/product/spu';
// //
const LayoutTypeEnum = { const LayoutTypeEnum = {

View File

@ -1,182 +1,325 @@
<!-- 装修组件 - 秒杀 --> <!-- 装修商品组件拼团商品卡片 -->
<template> <template>
<view> <!-- 商品卡片 -->
<!-- 样式一三列 - 上图下文 --> <view>
<view v-if="layoutType === 'threeCol'" class="goods-sm-box ss-flex ss-flex-wrap" :style="[{ margin: '-' + data.space + 'rpx' }]"> <!-- 布局1. 单列大图上图下内容-->
<view v-for="product in productList" :key="product.id" class="goods-card-box" :style="[{padding: data.space + 'rpx',},]"> <view
<s-goods-column v-if="layoutType === LayoutTypeEnum.ONE_COL_BIG_IMG && state.spuList.length"
class="goods-card" size="sm" class="goods-sl-box"
:goodsFields="goodsFields" >
:tagStyle="badge" <view
:data="product" class="goods-box"
:titleColor="data.fields.name?.color" v-for="item in state.spuList"
:topRadius="data.borderRadiusTop" :key="item.id"
:bottomRadius="data.borderRadiusBottom" :style="[{ marginBottom: data.space * 2 + 'rpx' }]"
@click="sheep.$router.go('/pages/goods/seckill', { id: props.data.activityId, })"> >
</s-goods-column> <s-goods-column
</view> class=""
</view> size="sl"
<!-- 样式二一列 - 左图右文 --> :goodsFields="data.fields"
<view class="goods-box" v-if="layoutType === 'oneCol'"> :tagStyle="data.badge"
<view class="goods-list" v-for="(product, index) in productList" :key="index" :data="item"
:style="[{ marginBottom: space + 'px' }]"> :titleColor="data.fields.name?.color"
<s-goods-column :subTitleColor="data.fields.introduction.color"
class="goods-card" :topRadius="data.borderRadiusTop"
size="lg" :bottomRadius="data.borderRadiusBottom"
:goodsFields="goodsFields" @click="sheep.$router.go('/pages/goods/groupon', { id: item.activityId })"
:seckillTag="true" >
:tagStyle="badge" <!-- 购买按钮 -->
:data="product" <template v-slot:cart>
:titleColor="data.fields.name?.color" <button class="ss-reset-button cart-btn" :style="[buyStyle]">
:subTitleColor="data.fields.introduction?.color" {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
:topRadius="data.borderRadiusTop" </button>
:bottomRadius="data.borderRadiusBottom" </template>
@click="sheep.$router.go('/pages/goods/seckill', { id: props.data.activityId, })"> </s-goods-column>
<template v-slot:cart> </view>
<button class="ss-reset-button cart-btn" :style="[buyStyle]"> </view>
{{ btnBuy?.type === 'text' ? btnBuy.text : '立即秒杀' }}
</button> <!-- 布局2. 单列小图左图右内容 -->
</template> <view
</s-goods-column> v-if="layoutType === LayoutTypeEnum.ONE_COL_SMALL_IMG && state.spuList.length"
</view> class="goods-lg-box"
</view> >
</view> <view
class="goods-box"
:style="[{ marginBottom: data.space + 'px' }]"
v-for="item in state.spuList"
:key="item.id"
>
<s-goods-column
class="goods-card"
size="lg"
:goodsFields="data.fields"
:data="item"
:tagStyle="data.badge"
:titleColor="data.fields.name?.color"
:subTitleColor="data.fields.introduction.color"
:topRadius="data.borderRadiusTop"
:bottomRadius="data.borderRadiusBottom"
@tap="sheep.$router.go('/pages/goods/groupon', { id: item.activityId })"
>
<!-- 购买按钮 -->
<template v-slot:cart>
<button class="ss-reset-button cart-btn" :style="[buyStyle]">
{{ btnBuy.type === 'text' ? btnBuy.text : '' }}
</button>
</template>
</s-goods-column>
</view>
</view>
<!-- 布局3. 双列每一列上图下内容-->
<view
v-if="layoutType === LayoutTypeEnum.TWO_COL && state.spuList.length"
class="goods-md-wrap ss-flex ss-flex-wrap ss-col-top"
>
<view class="goods-list-box">
<view
class="left-list"
:style="[{ paddingRight: data.space + 'rpx', marginBottom: data.space + 'px' }]"
v-for="item in state.leftSpuList"
:key="item.id"
>
<s-goods-column
class="goods-md-box"
size="md"
:goodsFields="data.fields"
:tagStyle="data.badge"
:data="item"
:titleColor="data.fields.name?.color"
:subTitleColor="data.fields.introduction.color"
:topRadius="data.borderRadiusTop"
:bottomRadius="data.borderRadiusBottom"
:titleWidth="330 - marginLeft - marginRight"
@click="sheep.$router.go('/pages/goods/groupon', { id: item.activityId })"
@getHeight="calculateGoodsColumn($event, 'left')"
>
<!-- 购买按钮 -->
<template v-slot:cart>
<button class="ss-reset-button cart-btn" :style="[buyStyle]">
{{ btnBuy.type === 'text' ? btnBuy.text : '' }}
</button>
</template>
</s-goods-column>
</view>
</view>
<view class="goods-list-box">
<view
class="right-list"
:style="[{ paddingLeft: data.space + 'rpx', marginBottom: data.space + 'px' }]"
v-for="item in state.rightSpuList"
:key="item.id"
>
<s-goods-column
class="goods-md-box"
size="md"
:goodsFields="data.fields"
:tagStyle="data.badge"
:data="item"
:titleColor="data.fields.name?.color"
:subTitleColor="data.fields.introduction.color"
:topRadius="data.borderRadiusTop"
:bottomRadius="data.borderRadiusBottom"
:titleWidth="330 - marginLeft - marginRight"
@click="sheep.$router.go('/pages/goods/groupon', { id: item.activityId })"
@getHeight="calculateGoodsColumn($event, 'right')"
>
<!-- 购买按钮 -->
<template v-slot:cart>
<button class="ss-reset-button cart-btn" :style="[buyStyle]">
{{ btnBuy.type === 'text' ? btnBuy.text : '' }}
</button>
</template>
</s-goods-column>
</view>
</view>
</view>
</view>
</template> </template>
<script setup> <script setup>
/** /**
* 秒杀商品列表 * 商品卡片
* */
* @property {Array} list 商品列表 import { computed, onMounted, reactive, ref } from 'vue';
*/ import sheep from '@/sheep';
import { import SeckillApi from '@/sheep/api/promotion/seckill';
computed, import SpuApi from '@/sheep/api/product/spu';
onMounted,
reactive,
ref
} from 'vue';
import sheep from '@/sheep';
import SeckillApi from "@/sheep/api/promotion/seckill";
import SpuApi from "@/sheep/api/product/spu";
// //
const props = defineProps({ const LayoutTypeEnum = {
// //
data: { ONE_COL_BIG_IMG: 'oneColBigImg',
type: Object, //
default: () => ({}), TWO_COL: 'twoCol',
}, //
// ONE_COL_SMALL_IMG: 'oneColSmallImg',
styles: { };
type: Object,
default: () => ({}),
},
});
//
const goodsFields = reactive({
//
price: { show: true },
//
stock: { show: true },
//
name: { show: true },
//
introduction: { show: true },
//
marketPrice: { show: true },
//
salesCount: { show: true },
});
let {
layoutType,
badge,
btnBuy,
space,
} = props.data;
let {
marginLeft,
marginRight
} = props.styles;
// const state = reactive({
const buyStyle = computed(() => { spuList: [],
let btnBuy = props.data.btnBuy; leftSpuList: [],
if (btnBuy?.type === 'text') { rightSpuList: [],
return { });
background: `linear-gradient(to right, ${btnBuy.bgBeginColor}, ${btnBuy.bgEndColor})`, const props = defineProps({
}; data: {
} type: Object,
if (btnBuy?.type === 'img') { default() {},
return { },
width: '54rpx', styles: {
height: '54rpx', type: Object,
background: `url(${sheep.$url.cdn(btnBuy.imgUrl)}) no-repeat`, default() {},
backgroundSize: '100% 100%', },
}; });
}
});
// const { layoutType, btnBuy, activityIds } = props.data || {};
const productList = ref([]); const { marginLeft, marginRight } = props.styles || {};
//
onMounted(async () => { //
// todo@owen Yudao const buyStyle = computed(() => {
const { if (btnBuy.type === 'text') {
data: activity // 线
} = await SeckillApi.getSeckillActivity(props.data.activityId); return {
const { background: `linear-gradient(to right, ${btnBuy.bgBeginColor}, ${btnBuy.bgEndColor})`,
data: spu };
} = await SpuApi.getSpuDetail(activity.spuId) }
// if (btnBuy.type === 'img') {
activity.products.forEach((product) => { //
spu.price = Math.min(spu.price, product.seckillPrice); // SPU return {
}); width: '54rpx',
// height: '54rpx',
spu.stock = activity.stock background: `url(${sheep.$url.cdn(btnBuy.imgUrl)}) no-repeat`,
// - = backgroundSize: '100% 100%',
spu.salesCount = activity.totalStock - activity.stock };
productList.value = [spu]; }
}); });
//region
//
let count = 0;
//
let leftHeight = 0;
//
let rightHeight = 0;
/**
* 计算商品在左列还是右列
* @param height 商品的高度
* @param where 添加到哪一列
*/
function calculateGoodsColumn(height = 0, where = 'left') {
//
if (!state.spuList[count]) return;
//
if (where === 'left') leftHeight += height;
if (where === 'right') rightHeight += height;
//
if (leftHeight <= rightHeight) {
state.leftSpuList.push(state.spuList[count]);
} else {
state.rightSpuList.push(state.spuList[count]);
}
//
count++;
}
//endregion
/**
* 根据商品编号列表获取商品列表
* @param ids 商品编号列表
* @return {Promise<undefined>} 商品列表
*/
async function getSeckillActivityDetailList(ids) {
const { data } = await SeckillApi.getSeckillActivityListByIds(ids);
return data;
}
/**
* 根据商品编号获取商品详情
* @param ids 商品编号列表
* @return {Promise<undefined>} 商品列表
*/
async function getSpuDetail(ids) {
const { data: spu } = await SpuApi.getSpuDetail(ids);
return spu;
}
//
onMounted(async () => {
//
const activityList = await getSeckillActivityDetailList(activityIds.join(','));
// SPUspuList
for (const activity of activityList) {
state.spuList.push(await getSpuDetail(activity.spuId));
}
//
activityList.forEach((activity) => {
//
const seckillPrice = activity.seckillPrice || Infinity;
// spu
const spu = state.spuList.find((spu) => activity.spuId === spu.id);
if (spu) {
//
spu.price = Math.min(seckillPrice, spu.price);
// ID
spu.activityId = activity.id;
}
});
//
if (layoutType === LayoutTypeEnum.TWO_COL) {
//
calculateGoodsColumn();
}
});
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.header-box { .goods-md-wrap {
height: 100rpx; width: 100%;
} }
.goods-list { .goods-list-box {
position: relative; width: 50%;
box-sizing: border-box;
&:nth-last-child(1) { .left-list {
margin-bottom: 0 !important; &:nth-last-child(1) {
} margin-bottom: 0 !important;
}
}
.cart-btn { .right-list {
position: absolute; &:nth-last-child(1) {
bottom: 10rpx; margin-bottom: 0 !important;
right: 20rpx; }
z-index: 11; }
height: 50rpx; }
line-height: 50rpx;
padding: 0 20rpx;
border-radius: 25rpx;
font-size: 24rpx;
color: #fff;
background: linear-gradient(90deg, #ff6600 0%, #fe832a 100%);
}
}
.goods-sm-box { .goods-box {
margin: 0 auto; &:nth-last-of-type(1) {
box-sizing: border-box; margin-bottom: 0 !important;
}
}
.goods-card-box { .goods-md-box,
flex-shrink: 0; .goods-sl-box,
overflow: hidden; .goods-lg-box {
width: 33.3%; position: relative;
box-sizing: border-box;
} .cart-btn {
} position: absolute;
</style> bottom: 18rpx;
right: 20rpx;
z-index: 11;
height: 50rpx;
line-height: 50rpx;
padding: 0 20rpx;
border-radius: 25rpx;
font-size: 24rpx;
color: #fff;
}
}
</style>