文件
xionggeshop/xgsc.gynhc.com_wx/components/shopro-goods-card/shopro-goods-card.vue
2026-09-14 17:24:53 +08:00

716 行
17 KiB
Vue

<template>
<!-- m -->
<view class="goods-box" :class="'goods-box--' + layout" @tap="click">
<view v-if="layout === 'compact'" class="compact-inner">
<view class="compact-img-box">
<image class="compact-img" lazy-load fade-show :src="image" mode="aspectFit"></image>
</view>
<view class="compact-body u-flex-1">
<view class="compact-main">
<view class="compact-title u-ellipsis-1">{{ displayTitle }}</view>
<view class="compact-spec u-ellipsis-1" v-if="compactSpecLine1">{{ compactSpecLine1 }}</view>
<view class="compact-spec u-ellipsis-1" v-if="compactSpecLine2">{{ compactSpecLine2 }}</view>
</view>
<view class="compact-footer u-flex u-row-between u-col-center">
<view class="price-box compact-price-box">
<view class="price">{{ price }}</view>
</view>
<view class="compact-add" @tap.stop>
<view v-if="!detail.is_sku">
<button class="u-reset-button cart-btn u-flex u-col-center u-row-center"
v-if="!isCart(detail.id)" @tap.stop="addCart(detail.sku_price[0])">
<text class="u-iconfont uicon-plus"></text>
</button>
<view class="num-step compact-num-step" v-else>
<u-number-box :value="checkCart[detail.id].num" :min="0" :step="1" :long-press="false"
:max="detail.sku_price[0].stock > 999 ? 999 : detail.sku_price[0].stock"
@min="onMin" @plus="plus($event, detail.sku_price[0])"
@change="onChangeNum($event, detail.sku_price[0])"></u-number-box>
</view>
</view>
<button class="u-reset-button cart-btn u-flex u-col-center u-row-center"
@tap.stop="selSku(detail)" v-else>
<text class="u-iconfont uicon-plus"></text>
</button>
</view>
</view>
</view>
</view>
<view v-else-if="layout === 'grid'" class="grid-inner">
<view class="grid-img-box">
<image class="grid-img" lazy-load fade-show :src="image" mode="aspectFit"></image>
</view>
<view class="grid-body">
<view class="grid-title u-ellipsis-2">{{ displayTitle }}</view>
<view class="grid-sub u-ellipsis-1" v-if="gridSubtitle">{{ gridSubtitle }}</view>
<view class="grid-footer u-flex u-row-between u-col-end">
<view class="grid-price-wrap">
<view class="price">{{ price }}</view>
<view class="origin-price" v-if="showOriginPrice">¥{{ originPrice }}</view>
</view>
<view class="grid-add" @tap.stop>
<view v-if="!detail.is_sku">
<button class="u-reset-button cart-btn u-flex u-col-center u-row-center"
v-if="!isCart(detail.id)" @tap.stop="addCart(detail.sku_price[0])">
<text class="u-iconfont uicon-shopping-cart-fill"></text>
</button>
<view class="num-step grid-num-step" v-else>
<u-number-box :value="checkCart[detail.id].num" :min="0" :step="1" :long-press="false"
:max="detail.sku_price[0].stock > 999 ? 999 : detail.sku_price[0].stock"
@min="onMin" @plus="plus($event, detail.sku_price[0])"
@change="onChangeNum($event, detail.sku_price[0])"></u-number-box>
</view>
</view>
<button class="u-reset-button cart-btn u-flex u-col-center u-row-center"
@tap.stop="selSku(detail)" v-else>
<text class="u-iconfont uicon-shopping-cart-fill"></text>
</button>
</view>
</view>
</view>
</view>
<block v-else>
<view class="img-box">
<image class="goods-img" lazy-load fade-show :src="image" mode="aspectFill"></image>
</view>
<view class="goods-bottom u-p-14"
:style="type ? 'background-image: url(' + $IMG_URL + typeMap[type].goodsBg + ')' : ''">
<view class="title u-ellipsis-2 u-m-b-10">
<view class=" sm cu-tag radius title-tag u-m-r-10"
:style="{ backgroundColor: typeMap[type].tagBg, color: '#fff' }" v-if="type">
{{ typeMap[type].text }}
</view>
{{ title }}
</view>
<view class="sub-title u-ellipsis-1 u-m-b-10" v-show="subtitle">{{ subtitle }}</view>
<view class="u-m-b-20">
<view class="cu-tag line-red sm radius" v-for="(item, index) in tagTextList" :key="index">{{ item }}
</view>
</view>
<slot name="cardBottom">
<view class="u-flex u-col-center u-row-between">
<view class="price-box">
<view class="price u-m-b-10 neutra-font">{{ price }}</view>
<view class="origin-price neutra-font">¥{{ originPrice }}</view>
</view>
<!-- 加入购物车 -->
<view class="cart-box">
<!-- 单规格 -->
<view class="" v-if="!detail.is_sku">
<button class="u-reset-button cart-btn u-flex u-col-center u-row-center"
v-if="!isCart(detail.id)" @tap.stop="addCart(detail.sku_price[0])">
<view class="u-iconfont uicon-shopping-cart-fill" style="color: #fff;"></view>
</button>
<view class="num-step" @tap.stop v-else>
<u-number-box :value="checkCart[detail.id].num" :min="0" :step="1" :long-press="false"
:max="detail.sku_price[0].stock > 999 ? 999 : detail.sku_price[0].stock"
@min="onMin" @plus="plus($event, detail.sku_price[0])"
@change="onChangeNum($event, detail.sku_price[0])"></u-number-box>
</view>
</view>
<!-- 多规格 -->
<button class="u-reset-button item-btn cart-btn u-flex u-col-center u-row-center"
@tap.stop="selSku(detail)" v-else>
<view class="u-iconfont uicon-shopping-cart-fill" style="color: #fff;"></view>
</button>
</view>
</view>
</slot>
</view>
</block>
<!-- 规格弹窗 -->
<shopro-sku v-if="showSku && goodsInfo.id" v-model="showSku" :goodsInfo="goodsInfo" buyType="cart"></shopro-sku>
</view>
</template>
<script>
/**
*shoproGoodsCard - 商品列表卡片
* @property {Object} detail - 商品详情
* @property {String} type - 商品类型
* @property {String} image - 商品图片
* @property {String} title - 商品标题
* @property {String} subtitle - 商品副标题
* @property {String | Number} price - 商品价格
* @property {String | Number} originPrice - 商品原价
* @property {String | Number} sales - 商品销量
* @property {Array} tagTextList - 活动标签
* @event {Function} click 商品被点击
*/
import { mapMutations, mapActions, mapState, mapGetters } from 'vuex';
export default {
components: {},
data() {
return {
showSku: false,
goodsInfo: {}, //商品详情
typeMap: {
seckill: {
text: '秒杀',
tagBg: '#FF5854',
goodsBg: '/imgs/tag/seckill_y_bg.png'
},
timer: {
text: '限时秒杀',
tagBg: '#FF5854',
goodsBg: '/imgs/tag/seckill_y_bg.png'
},
robbuy: {
text: '预约抢购',
tagBg: '#FF5854',
goodsBg: '/imgs/tag/seckill_y_bg.png'
},
promote: {
text: '限时促销',
tagBg: '#FFE2BC',
goodsBg: '/imgs/tag/promote_y_bg.png'
},
groupon: {
text: '拼团',
tagBg: '#FE832A',
goodsBg: '/imgs/tag/groupon_y_bg.png'
}
}
};
},
computed: {
...mapGetters(['cartList', 'checkCart']),
showOriginPrice() {
return this.originPrice && Number(this.originPrice) > Number(this.price);
},
displayTitle() {
return String(this.title || '').replace(/^【演示】/, '');
},
compactSpecLine1() {
const params = Array.isArray(this.detail?.params) ? this.detail.params : [];
const alcohol = this.findParamValue(params, ['酒精度', '酒精']);
const volume = this.findParamValue(params, ['净含量', '容量', '规格']);
const parts = [];
if (alcohol) parts.push(alcohol);
if (volume) parts.push(volume);
if (parts.length) return parts.join(' ');
const subtitle = String(this.subtitle || '').trim();
if (subtitle && !subtitle.startsWith('—')) return subtitle;
return '';
},
compactSpecLine2() {
const params = Array.isArray(this.detail?.params) ? this.detail.params : [];
const aroma = this.findParamValue(params, ['香型', '类型']);
if (aroma) return aroma;
const subtitle = String(this.subtitle || '').trim();
if (subtitle.startsWith('—')) return subtitle;
if (this.compactSpecLine1) return '酱香型白酒';
return subtitle || '酱香型白酒';
},
gridSubtitle() {
const line1 = this.compactSpecLine1;
const line2 = this.compactSpecLine2;
if (line1 && line2 && line1 !== line2) {
return line2;
}
return line2 || line1 || '';
}
},
props: {
layout: {
type: String,
default: 'vertical'
},
detail: {
type: Object,
default: () => {
return {};
}
},
type: {
type: [String, null],
default: ''
},
image: {
type: String,
default: ''
},
title: {
type: String,
default: ''
},
subtitle: {
type: String,
default: ''
},
price: {
type: [String, Number],
default: ''
},
originPrice: {
type: [String, Number],
default: ''
},
sales: {
type: [String, Number],
default: ''
},
tagTextList: {
type: Array,
default: () => []
}
},
methods: {
...mapActions(['getCartList', 'changeCartList', 'addCartGoods']),
findParamValue(params, keys) {
const item = params.find(param => keys.some(key => String(param?.title || '').includes(key)));
if (!item) return '';
const content = String(item.content || '').trim();
const title = String(item.title || '').trim();
if (!content) return title;
if (title.includes('酒精') || content.includes('vol') || content.includes('%')) {
return content.includes('vol') || content.includes('%') ? content : `${content}%vol`;
}
return content;
},
//点击商品
click() {
this.$emit('click');
},
// 检测是否为购物车商品
isCart(id) {
return Object.keys(this.checkCart).includes(id + '');
},
// 检测商品在购物车中的下标
checkGoodsIndex(id) {
let cIndex = 0;
this.cartList.forEach((item, index) => {
if (id == item.goods_id) {
cIndex = index;
}
});
return cIndex;
},
// 更改商品数
async onChangeNum(e, sku) {
let gIndex = this.checkGoodsIndex(sku.goods_id);
if (e.value != this.checkCart[sku.goods_id].num) {
uni.showLoading({
mask: true
});
this.$set(this.cartList[gIndex], 'goods_num', +e.value);
await this.changeCartList({
ids: [this.checkCart[sku.goods_id].cartOrderId],
goodsNum: +e.value,
art: 'change'
});
await uni.hideLoading();
}
},
// 到达最小值
onMin() {
const that = this;
let cartGoodId = 0;
cartGoodId = this.cartList.filter(item => item.goods_id === that.detail.id)[0].id;
uni.showModal({
title: '删除提示',
confirmColor: '#1677FF',
content: `是否确认从购物车中删除此商品?`,
success: res => {
res.confirm && this.changeCartList({ ids: [cartGoodId], art: 'delete' });
}
});
},
// 增加
plus(e, sku) {
if (e.value >= sku.stock) {
this.$u.toast('库存不足');
return;
}
if (this.detail.activity_type === 'seckill' || this.detail.activity_type === 'timer' || this.detail.activity_type === 'groupon') {
let rules = this.detail.activity.rules;
if (rules.limit_buy != 0 && e.value >= rules.limit_buy) {
this.$u.toast('本次活动最多购买' + rules.limit_buy + '件');
return;
}
}
},
// 添加购物车,多规格
async selSku(info) {
if (this.detail.activity_type) {
this.$Router.push({ path: '/pages/goods/detail', query: { id: this.detail.id } });
return;
}
this.goodsInfo = {};
this.getGoodsDetail(info.id);
this.showSku = true;
},
// 商品详情
getGoodsDetail(id) {
let that = this;
that.$http('goods.detail', {
id: id
}).then(res => {
if (res.code === 1) {
that.goodsInfo = res.data;
}
});
},
// 加入购物车
addCart(sku) {
if (sku.stock <= 0) {
this.$u.toast('库存不足');
return;
}
if (this.detail.activity_type) {
this.$Router.push({ path: '/pages/goods/detail', query: { id: this.detail.id } });
return;
}
let confirmGoodsList = {
list: [{
goods_id: sku.goods_id,
goods_num: 1,
sku_price_id: sku.id,
goods_price: sku.price
}],
from: 'goods'
};
this.addCartGoods(confirmGoodsList).then(res => {
if (res.code === 1) {
this.$u.toast(res.msg);
}
});
}
}
};
</script>
<style lang="scss">
.goods-box {
width: 100%;
background: #fff;
border-radius: $brand-card-radius;
overflow: hidden;
box-shadow: $brand-shadow;
&--compact {
padding: 20rpx 24rpx;
box-sizing: border-box;
border-radius: 20rpx;
}
.goods-bottom {
background-size: 100% 100%;
background-position: bottom center;
background-repeat: no-repeat;
}
.img-box {
width: 100%;
height: 0;
padding-bottom: 100%;
overflow: hidden;
position: relative;
background-color: #fff;
border-radius: 6rpx;
.goods-img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #ccc;
}
}
.title {
width: 100%;
vertical-align: center;
font-size: 28rpx;
font-weight: bold;
line-height: 40rpx;
color: $brand-text;
padding-top: 6rpx;
.title-tag {
position: relative;
top: -6rpx;
}
}
.sub-title {
font-size: 24rpx;
font-weight: 400;
width: 100%;
color: $brand-text-muted;
}
.price-box {
.price {
font-size: 36rpx;
color: $brand-price;
font-weight: 600;
&::before {
content: '¥';
font-size: 24rpx;
}
}
.origin-price {
font-size: 26rpx;
font-weight: 400;
text-decoration: line-through;
color: #c4c4c4;
}
.sales-box {
font-size: 18rpx;
font-weight: 400;
color: #c4c4c4;
line-height: 20rpx;
}
}
// 购物车
.cart-box {
.cart-btn {
width: 54rpx;
height: 54rpx;
border-radius: 50%;
padding: 0;
background: $brand-primary;
box-shadow: 0 4rpx 12rpx rgba(22, 119, 255, 0.35);
}
}
}
.goods-box--compact {
height: 100%;
.compact-inner {
display: flex;
flex-direction: row;
align-items: center;
width: 100%;
height: 176rpx;
box-sizing: border-box;
}
.compact-img-box {
width: 176rpx;
height: 176rpx;
flex-shrink: 0;
border-radius: 12rpx;
overflow: hidden;
background: #f8fafc;
}
.compact-img {
width: 176rpx;
height: 176rpx;
display: block;
}
.compact-body {
flex: 1;
min-width: 0;
height: 176rpx;
margin-left: 20rpx;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.compact-main {
min-width: 0;
}
.compact-title {
font-size: 28rpx;
font-weight: 600;
color: $brand-text;
line-height: 1.35;
}
.compact-spec {
margin-top: 6rpx;
font-size: 22rpx;
color: #9CA3AF;
line-height: 1.35;
}
.compact-footer {
width: 100%;
padding-top: 8rpx;
}
.compact-price-box {
flex: 1;
min-width: 0;
padding-right: 12rpx;
.price {
font-size: 32rpx;
font-weight: 700;
color: $brand-price;
line-height: 1.2;
white-space: nowrap;
&::before {
content: '¥ ';
font-size: 22rpx;
font-weight: 600;
}
}
}
.compact-add {
flex-shrink: 0;
}
.compact-add .cart-btn {
width: 52rpx;
height: 52rpx;
border-radius: 50%;
padding: 0;
background: $brand-primary;
box-shadow: 0 6rpx 16rpx rgba(22, 119, 255, 0.35);
.uicon-plus {
font-size: 28rpx;
font-weight: 700;
color: #fff;
}
}
.compact-num-step {
transform: scale(0.78);
transform-origin: right center;
}
}
.goods-box--grid {
display: flex;
flex-direction: column;
height: 100%;
overflow: hidden;
border-radius: 20rpx;
.grid-inner {
display: flex;
flex-direction: column;
height: 100%;
}
.grid-img-box {
position: relative;
width: 100%;
height: 0;
padding-bottom: 100%;
background: #f8fafc;
overflow: hidden;
}
.grid-img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.grid-body {
flex: 1;
display: flex;
flex-direction: column;
padding: 16rpx 16rpx 14rpx;
box-sizing: border-box;
min-height: 168rpx;
}
.grid-title {
font-size: 26rpx;
font-weight: 600;
color: $brand-text;
line-height: 1.35;
min-height: 70rpx;
}
.grid-sub {
margin-top: 4rpx;
font-size: 22rpx;
color: #9CA3AF;
line-height: 1.3;
}
.grid-footer {
margin-top: auto;
padding-top: 10rpx;
width: 100%;
}
.grid-price-wrap {
flex: 1;
min-width: 0;
padding-right: 8rpx;
.price {
font-size: 32rpx;
font-weight: 700;
color: $brand-price;
line-height: 1.2;
white-space: nowrap;
&::before {
content: '¥';
font-size: 22rpx;
margin-right: 2rpx;
}
}
.origin-price {
margin-top: 2rpx;
font-size: 20rpx;
color: #c4c4c4;
text-decoration: line-through;
line-height: 1.2;
white-space: nowrap;
}
}
.grid-add {
flex-shrink: 0;
}
.grid-add .cart-btn {
width: 52rpx;
height: 52rpx;
border-radius: 50%;
padding: 0;
background: $brand-primary;
box-shadow: 0 6rpx 16rpx rgba(22, 119, 255, 0.35);
.uicon-shopping-cart-fill {
font-size: 28rpx;
color: #fff;
}
}
.grid-num-step {
transform: scale(0.78);
transform-origin: right center;
}
}
</style>