营销:适配商城装修组件【商品卡片】

pull/16/head
owen 2023-11-17 09:56:50 +08:00
parent 7adf5dd257
commit 99529e3b9e
4 changed files with 157 additions and 67 deletions

36
sheep/api/product/spu.js Normal file
View File

@ -0,0 +1,36 @@
import request from '@/sheep/request';
export default {
// 获得商品 SPU 列表
getSpuList: (recommendType) => {
return request({
url: '/app-api/product/spu/list',
method: 'GET',
params: {recommendType},
});
},
// 获得商品 SPU 列表
getSpuListByIds: (ids) => {
return request({
url: '/app-api/product/spu/list-by-ids',
method: 'GET',
params: {ids},
});
},
// 获得商品 SPU 分页
getSpuPage: (data) => {
return request({
url: '/app-api/product/spu/page',
method: 'GET',
data
});
},
// 查询商品
getSpuDetail: (id) => {
return request({
url: '/app-api/product/spu/get-detail',
method: 'GET',
params: { id },
});
}
};

View File

@ -29,7 +29,7 @@
<s-hotzone-block v-if="type === 'hotzone'" :data="data" :styles="styles" /> <s-hotzone-block v-if="type === 'hotzone'" :data="data" :styles="styles" />
<!-- 商品组件商品卡片 --> <!-- 商品组件商品卡片 -->
<s-goods-card v-if="type === 'goodsCard'" :data="data" :styles="styles" /> <s-goods-card v-if="type === 'ProductCard'" :data="data" :styles="styles" />
<!-- 商品组件商品栏 --> <!-- 商品组件商品栏 -->
<s-goods-shelves v-if="type === 'goodsShelves'" :data="data" :styles="styles" /> <s-goods-shelves v-if="type === 'goodsShelves'" :data="data" :styles="styles" />

View File

@ -1,8 +1,8 @@
<template> <template>
<!-- 商品卡片 --> <!-- 商品卡片 -->
<view> <view>
<!-- 1 100%宽卡片列表--> <!-- 布局1. 单列大图上图下内容-->
<view v-if="mode === 1 && state.goodsList.length" class="goods-sl-box"> <view v-if="layoutType === LayoutTypeEnum.ONE_COL_BIG_IMG && state.goodsList.length" class="goods-sl-box">
<view <view
class="goods-box" class="goods-box"
v-for="item in state.goodsList" v-for="item in state.goodsList"
@ -12,27 +12,28 @@
<s-goods-column <s-goods-column
class="" class=""
size="sl" size="sl"
:goodsFields="goodsFields" :goodsFields="productFields"
:tagStyle="tagStyle" :tagStyle="badge"
:data="item" :data="item"
:titleColor="goodsFields.title?.color" :titleColor="data.fields.name?.color"
:subTitleColor="goodsFields.subtitle.color" :subTitleColor="data.fields.introduction.color"
:topRadius="data.borderRadiusTop" :topRadius="data.borderRadiusTop"
:bottomRadius="data.borderRadiusBottom" :bottomRadius="data.borderRadiusBottom"
@click="sheep.$router.go('/pages/goods/index', { id: item.id })" @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
> >
<!-- 购买按钮 -->
<template v-slot:cart> <template v-slot:cart>
<button class="ss-reset-button cart-btn" :style="[buyStyle]"> <button class="ss-reset-button cart-btn" :style="[buyStyle]">
{{ buyNowStyle.mode === 1 ? buyNowStyle.text : '' }} {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
</button> </button>
</template> </template>
</s-goods-column> </s-goods-column>
</view> </view>
</view> </view>
<!-- 2 双列瀑布流列表--> <!-- 布局2. 双列每一列上图下内容-->
<view <view
v-if="mode === 2 && state.goodsList.length" v-if="layoutType === LayoutTypeEnum.TWO_COL && state.goodsList.length"
class="goods-md-wrap ss-flex ss-flex-wrap ss-col-top" class="goods-md-wrap ss-flex ss-flex-wrap ss-col-top"
> >
<view class="goods-list-box"> <view class="goods-list-box">
@ -45,20 +46,21 @@
<s-goods-column <s-goods-column
class="goods-md-box" class="goods-md-box"
size="md" size="md"
:goodsFields="goodsFields" :goodsFields="productFields"
:tagStyle="tagStyle" :tagStyle="badge"
:data="item" :data="item"
:titleColor="goodsFields.title?.color" :titleColor="data.fields.name?.color"
:subTitleColor="goodsFields.subtitle.color" :subTitleColor="data.fields.introduction.color"
:topRadius="data.borderRadiusTop" :topRadius="data.borderRadiusTop"
:bottomRadius="data.borderRadiusBottom" :bottomRadius="data.borderRadiusBottom"
:titleWidth="330 - marginLeft - marginRight" :titleWidth="330 - marginLeft - marginRight"
@click="sheep.$router.go('/pages/goods/index', { id: item.id })" @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
@getHeight="mountMasonry($event, 'left')" @getHeight="calculateGoodsColumn($event, 'left')"
> >
<!-- 购买按钮 -->
<template v-slot:cart> <template v-slot:cart>
<button class="ss-reset-button cart-btn" :style="[buyStyle]"> <button class="ss-reset-button cart-btn" :style="[buyStyle]">
{{ buyNowStyle.mode === 1 ? buyNowStyle.text : '' }} {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
</button> </button>
</template> </template>
</s-goods-column> </s-goods-column>
@ -74,20 +76,21 @@
<s-goods-column <s-goods-column
class="goods-md-box" class="goods-md-box"
size="md" size="md"
:goodsFields="goodsFields" :goodsFields="productFields"
:tagStyle="tagStyle" :tagStyle="badge"
:data="item" :data="item"
:titleColor="goodsFields.title?.color" :titleColor="data.fields.name?.color"
:subTitleColor="goodsFields.subtitle.color" :subTitleColor="data.fields.introduction.color"
:topRadius="data.borderRadiusTop" :topRadius="data.borderRadiusTop"
:bottomRadius="data.borderRadiusBottom" :bottomRadius="data.borderRadiusBottom"
:titleWidth="330 - marginLeft - marginRight" :titleWidth="330 - marginLeft - marginRight"
@click="sheep.$router.go('/pages/goods/index', { id: item.id })" @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
@getHeight="mountMasonry($event, 'right')" @getHeight="calculateGoodsColumn($event, 'right')"
> >
<!-- 购买按钮 -->
<template v-slot:cart> <template v-slot:cart>
<button class="ss-reset-button cart-btn" :style="[buyStyle]"> <button class="ss-reset-button cart-btn" :style="[buyStyle]">
{{ buyNowStyle.mode === 1 ? buyNowStyle.text : '' }} {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
</button> </button>
</template> </template>
</s-goods-column> </s-goods-column>
@ -95,8 +98,8 @@
</view> </view>
</view> </view>
<!-- 3 30%卡片列表--> <!-- 布局3. 单列小图左图右内容 -->
<view v-if="mode === 3 && state.goodsList.length" class="goods-lg-box"> <view v-if="layoutType === LayoutTypeEnum.ONE_COL_SMALL_IMG && state.goodsList.length" class="goods-lg-box">
<view <view
class="goods-box" class="goods-box"
:style="[{ marginBottom: data.space + 'px' }]" :style="[{ marginBottom: data.space + 'px' }]"
@ -106,18 +109,19 @@
<s-goods-column <s-goods-column
class="goods-card" class="goods-card"
size="lg" size="lg"
:goodsFields="goodsFields" :goodsFields="productFields"
:data="item" :data="item"
:tagStyle="tagStyle" :tagStyle="badge"
:titleColor="goodsFields.title?.color" :titleColor="data.fields.name?.color"
:subTitleColor="goodsFields.subtitle.color" :subTitleColor="data.fields.introduction.color"
:topRadius="data.borderRadiusTop" :topRadius="data.borderRadiusTop"
:bottomRadius="data.borderRadiusBottom" :bottomRadius="data.borderRadiusBottom"
@tap="sheep.$router.go('/pages/goods/index', { id: item.id })" @tap="sheep.$router.go('/pages/goods/index', { id: item.id })"
> >
<!-- 购买按钮 -->
<template v-slot:cart> <template v-slot:cart>
<button class="ss-reset-button cart-btn" :style="[buyStyle]"> <button class="ss-reset-button cart-btn" :style="[buyStyle]">
{{ buyNowStyle.mode === 1 ? buyNowStyle.text : '' }} {{ btnBuy.type === 'text' ? btnBuy.text : '' }}
</button> </button>
</template> </template>
</s-goods-column> </s-goods-column>
@ -128,11 +132,21 @@
<script setup> <script setup>
/** /**
* 商品模板装修商品卡片 * 商品卡片
* @description style 1:带tab 2瀑布流横向两个上图下内容 3大图横向一个
*/ */
import { computed, reactive, onMounted } from 'vue'; import { computed, reactive, onMounted } from 'vue';
import sheep from '@/sheep'; import sheep from '@/sheep';
import SpuApi from '@/sheep/api/product/spu';
//
const LayoutTypeEnum = {
//
ONE_COL_BIG_IMG: 'oneColBigImg',
//
TWO_COL: 'twoCol',
//
ONE_COL_SMALL_IMG: 'oneColSmallImg',
}
const state = reactive({ const state = reactive({
goodsList: [], goodsList: [],
@ -150,55 +164,95 @@
}, },
}); });
const { mode, tagStyle, buyNowStyle, goodsFields, goodsIds } = props.data ?? {}; const { layoutType, btnBuy, spuIds } = props.data ?? {};
const { marginLeft, marginRight } = props.styles ?? {}; const { marginLeft, marginRight } = props.styles ?? {};
async function getGoodsListByIds(ids) { //
let { data } = await sheep.$api.goods.ids({ ids }); const buyStyle = computed(() => {
return data; if (btnBuy.type === 'text') {
} // 线
return {
onMounted(async () => { background: `linear-gradient(to right, ${btnBuy.bgBeginColor}, ${btnBuy.bgEndColor})`,
state.goodsList = await getGoodsListByIds(goodsIds.join(',')); };
if (mode === 2) { } else if (btnBuy.type === 'img') {
mountMasonry(); //
return {
width: '54rpx',
height: '54rpx',
background: `url(${sheep.$url.cdn(btnBuy.imgUrl)}) no-repeat`,
backgroundSize: '100% 100%',
};
} }
}); });
// // todo @owen
const productFields = computed(() => {
return {
title: props.data.fields.name,
subtitle: props.data.fields.introduction,
price: props.data.fields.price,
original_price: props.data.fields.marketPrice,
stock: props.data.fields.stock,
salesCount: props.data.fields.salesCount,
}
})
// todo @owen
const badge = computed(() => {
return {
show: props.data.badge.show,
src: props.data.badge.imgUrl,
}
})
//region
//
let count = 0; let count = 0;
//
let leftHeight = 0; let leftHeight = 0;
//
let rightHeight = 0; let rightHeight = 0;
function mountMasonry(height = 0, where = 'left') { /**
* 计算商品在左列还是右列
* @param height 商品的高度
* @param where 添加到哪一列
*/
function calculateGoodsColumn(height = 0, where = 'left') {
//
if (!state.goodsList[count]) return; if (!state.goodsList[count]) return;
//
if (where === 'left') leftHeight += height; if (where === 'left') leftHeight += height;
if (where === 'right') rightHeight += height; if (where === 'right') rightHeight += height;
//
if (leftHeight <= rightHeight) { if (leftHeight <= rightHeight) {
state.leftGoodsList.push(state.goodsList[count]); state.leftGoodsList.push(state.goodsList[count]);
} else { } else {
state.rightGoodsList.push(state.goodsList[count]); state.rightGoodsList.push(state.goodsList[count]);
} }
//
count++; count++;
} }
//endregion
// /**
const buyStyle = computed(() => { * 根据商品编号列表获取商品列表
if (buyNowStyle.mode == 1) { * @param ids 商品编号列表
// button * @return {Promise<undefined>} 商品列表
return { */
background: `linear-gradient(to right, ${buyNowStyle.color1}, ${buyNowStyle.color2})`, async function getGoodsListByIds(ids) {
}; const { data } = await SpuApi.getSpuListByIds(ids);
} return data;
}
if (buyNowStyle.mode == 2) { //
// image onMounted(async () => {
return { //
width: '54rpx', state.goodsList = await getGoodsListByIds(spuIds.join(','));
height: '54rpx', //
background: `url(${sheep.$url.cdn(buyNowStyle.src)}) no-repeat`, if (layoutType === LayoutTypeEnum.TWO_COL){
backgroundSize: '100% 100%', //
}; calculateGoodsColumn();
} }
}); });
</script> </script>

View File

@ -21,7 +21,7 @@
class="xs-goods-title ss-line-1" class="xs-goods-title ss-line-1"
:style="[{ color: titleColor, width: titleWidth ? titleWidth + 'rpx' : '' }]" :style="[{ color: titleColor, width: titleWidth ? titleWidth + 'rpx' : '' }]"
> >
{{ data.title }} {{ data.title || data.name }}
</view> </view>
<view <view
v-if="goodsFields.price?.show" v-if="goodsFields.price?.show"
@ -47,7 +47,7 @@
:style="[{ color: titleColor, width: titleWidth ? titleWidth + 'rpx' : '' }]" :style="[{ color: titleColor, width: titleWidth ? titleWidth + 'rpx' : '' }]"
> >
<view v-if="goodsFields.title?.show" class="sm-goods-title ss-line-1 ss-m-b-16"> <view v-if="goodsFields.title?.show" class="sm-goods-title ss-line-1 ss-m-b-16">
{{ data.title }} {{ data.title || data.name }}
</view> </view>
<view <view
v-if="goodsFields.price?.show" v-if="goodsFields.price?.show"
@ -75,14 +75,14 @@
class="md-goods-title ss-line-1" class="md-goods-title ss-line-1"
:style="[{ color: titleColor, width: titleWidth ? titleWidth + 'rpx' : '' }]" :style="[{ color: titleColor, width: titleWidth ? titleWidth + 'rpx' : '' }]"
> >
{{ data.title||data.name }} {{ data.title || data.name }}
</view> </view>
<view <view
v-if="goodsFields.subtitle?.show" v-if="goodsFields.subtitle?.show"
class="md-goods-subtitle ss-m-t-16 ss-line-1" class="md-goods-subtitle ss-m-t-16 ss-line-1"
:style="[{ color: subTitleColor, background: subTitleBackground }]" :style="[{ color: subTitleColor, background: subTitleBackground }]"
> >
{{ data.subtitle }} {{ data.subtitle || data.introduction }}
</view> </view>
<slot name="activity"> <slot name="activity">
<view v-if="data.promos?.length" class="tag-box ss-flex-wrap ss-flex ss-col-center"> <view v-if="data.promos?.length" class="tag-box ss-flex-wrap ss-flex ss-col-center">
@ -149,14 +149,14 @@
class="lg-goods-title ss-line-2" class="lg-goods-title ss-line-2"
:style="[{ color: titleColor }]" :style="[{ color: titleColor }]"
> >
{{ data.title||data.name }} {{ data.title || data.name }}
</view> </view>
<view <view
v-if="goodsFields.subtitle?.show" v-if="goodsFields.subtitle?.show"
class="lg-goods-subtitle ss-m-t-10 ss-line-1" class="lg-goods-subtitle ss-m-t-10 ss-line-1"
:style="[{ color: subTitleColor, background: subTitleBackground }]" :style="[{ color: subTitleColor, background: subTitleBackground }]"
> >
{{ data.subtitle }} {{ data.subtitle || data.introduction }}
</view> </view>
</view> </view>
<view> <view>
@ -213,14 +213,14 @@
class="sl-goods-title ss-line-1" class="sl-goods-title ss-line-1"
:style="[{ color: titleColor }]" :style="[{ color: titleColor }]"
> >
{{ data.title||data.name }} {{ data.title || data.name }}
</view> </view>
<view <view
v-if="goodsFields.subtitle?.show" v-if="goodsFields.subtitle?.show"
class="sl-goods-subtitle ss-m-t-16" class="sl-goods-subtitle ss-m-t-16"
:style="[{ color: subTitleColor, background: subTitleBackground }]" :style="[{ color: subTitleColor, background: subTitleBackground }]"
> >
{{ data.subtitle }} {{ data.subtitle || data.introduction }}
</view> </view>
</view> </view>
<view> <view>