文件
NHCshopro/components/shopro-sku/shopro-sku.vue
可乐加冰 9d2488a631 chore: 纳入小程序完整源码
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-23 20:25:01 +08:00

588 行
14 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<!-- 规格:单层 fixed 容器 + absolute 子层,兼容各机型层叠渲染 -->
<view v-if="showModal && goodsInfo.sku_price" class="sku-modal"
style="position:fixed;top:0;left:0;width:100%;height:100%;z-index:10080;"
@touchmove.stop.prevent="() => {}">
<view class="sku-modal__mask" style="position:absolute;top:0;left:0;width:100%;height:100%;background-color:rgba(0,0,0,0.6);z-index:1;"
@tap="showModal = false"></view>
<view class="sku-modal__panel safe-area-inset-bottom"
style="position:absolute;left:0;right:0;bottom:0;width:100%;z-index:2;background-color:#fff;border-radius:30rpx 30rpx 0 0;overflow:hidden;"
@tap.stop>
<view class="shop-modal page_box" :style="goodsInfo.is_sku == 0 ? 'height:700rpx' : 'height:950rpx'">
<text class="u-iconfont uicon-close-circle-fill close-icon" @tap.stop="showModal = false"></text>
<!-- 商品卡片-->
<view class="top u-flex modal-head__box">
<image class="shop-img" :src="currentSkuPrice.image ? currentSkuPrice.image : goodsInfo.image"
mode="aspectFill"></image>
<view class=" goods-box u-flex-col u-row-between">
<view class="goods-title u-ellipsis-2">{{ goodsInfo.title }}</view>
<view class="u-flex u-row-between goods-bottom">
<view class="price-box u-flex">
<view v-if="goodsType === 'score'">{{ currentSkuPrice.price_text || goodsInfo.price }}
</view>
<view v-else-if="grouponBuyType === 'groupon'">
¥{{ currentSkuPrice.groupon_price || (goodsInfo.activity_type === 'groupon' ? goodsInfo.groupon_price : goodsInfo.price) }}
</view>
<view v-else>¥{{ currentSkuPrice.price || goodsInfo.price }}</view>
</view>
<text class="stock">库存{{ currentSkuPrice.stock || goodsInfo.stock }}件</text>
</view>
</view>
</view>
<!-- 规格选项 -->
<scroll-view scroll-y class="content_box sku-scroll">
<view class="select-box u-flex-col u-row-left" v-for="(s, x) in skuList" :key="s.id">
<view class="type-title u-flex">{{ s.name }}</view>
<view class="tag-box u-flex u-flex-wrap">
<button class="tag u-reset-button" v-for="(sc, y) in s.content" :key="sc.id"
:class="{ 'tag-active': currentSkuArray[sc.pid] == sc.id, 'tag-disabled': sc.disabled == true }"
:disabled="sc.disabled == true" @tap="chooseSku(sc.pid, sc.id)">
{{ sc.name }}
</button>
</view>
</view>
<!-- 计步器 -->
<view class="buy-num-box u-flex u-row-between">
<view class="num-title">购买数量</view>
<u-number-box v-model="goodsNum" :min="1" :step="1" :max="maxStep" @plus="plus"
:long-press="false" @change="changeNum"></u-number-box>
</view>
</scroll-view>
<!-- 功能按钮 -->
<view class="btn-box foot_box u-flex u-row-between" v-if="buyType === 'cart' || buyType === 'buy'">
<button class="u-reset-button cu-btn save-btn"
v-if="(activityRules && activityRules.status === 'ing') || !goodsInfo.activity_type"
@tap="confirm">确认</button>
<button class="u-reset-button cu-btn cancel-btn"
v-if="activityRules && activityRules.status !== 'ing' && goodsInfo.activity_type"
@tap="confirm">确定</button>
</view>
<view class="btn-box foot_box u-flex u-row-between" v-else>
<button class="u-reset-button cu-btn buy-btn" @tap="confirmBuy">立即购买</button>
</view>
</view>
</view>
</view>
</template>
<script>
/**
* 多规格组件
* @property {Object} goodsInfo - 商品数据
* @property {Boolean} value = showModal - 显隐
* @property {String} buyType - 购买方式
* @property {String} goodsType - 商品类别
* @property {String} grouponBuyType -拼团商品购买方式
* @property {Number} grouponId - 拼团ID,分享进入
* @property {Object} activityRules - 活动状态。
*/
import { mapMutations, mapActions, mapState } from 'vuex';
export default {
// #ifdef MP-WEIXIN
options: {
styleIsolation: 'shared',
virtualHost: true
},
// #endif
components: {},
data() {
return {
maxStep: 999,
skuList: [],
currentSkuPrice: {},
currentSkuArray: [],
goodsNum: 1,
confirmGoodsInfo: {},
type: this.buyType
};
},
props: {
goodsInfo: {},
activityRules: {},
value: {},
buyType: {
type: String,
default: 'sku'
},
goodsType: {
type: String,
default: 'goods'
},
grouponBuyType: {
type: String,
default: 'alone'
},
grouponId: {
type: Number,
default: 0
}
},
created() {
this.skuList = this.goodsInfo.sku;
this.changeDisabled(false);
},
mounted() {
if (!this.goodsInfo.is_sku) {
this.currentSkuPrice = this.skuPrice[0];
this.maxStep = this.skuPrice[0].stock > 999 ? 999 : this.skuPrice[0].stock;
}
},
watch: {
type(nweVal, oldVal) {
return newVal;
}
},
computed: {
skuPrice() {
return this.goodsInfo.sku_price;
},
showModal: {
get() {
return this.value;
},
set(val) {
this.$emit('input', val);
return val;
}
},
currentSkuText() {
let that = this;
let str = '';
let currentSkuArray = this.currentSkuArray;
currentSkuArray.forEach(v => {
that.skuList.forEach(s => {
s.content.forEach(u => {
if (u.id === v) {
str += ' ' + u.name;
}
});
});
});
that.$emit('getSkuText', str);
return str;
}
},
methods: {
...mapActions(['addCartGoods', 'getCartList']),
jump(path, parmas) {
this.$Router.push({
path: path,
query: parmas
});
},
chooseSku(pid, skuId) {
let that = this;
let isChecked = true;
this.goodsNum = 1;
this.maxStep = 999;
if (that.currentSkuArray[pid] != undefined && that.currentSkuArray[pid] == skuId) {
isChecked = false;
that.currentSkuArray.splice(pid, 1, '');
} else {
that.$set(that.currentSkuArray, pid, skuId);
}
let chooseSkuId = [];
that.currentSkuArray.forEach(sku => {
if (sku != '') {
chooseSkuId.push(sku);
}
});
let newPrice = this.getCanUseSkuPrice();
if (chooseSkuId.length == that.skuList.length && newPrice.length) {
that.currentSkuPrice = newPrice[0];
} else {
that.currentSkuPrice = {};
}
this.changeDisabled(isChecked, pid, skuId);
},
changeDisabled(isChecked = false, pid = 0, skuId = 0) {
let newPrice = [];
if (isChecked) {
for (let price of this.skuPrice) {
if (price.stock <= 0) {
continue;
}
if (price.goods_sku_id_arr.indexOf(skuId.toString()) >= 0) {
newPrice.push(price);
}
}
} else {
newPrice = this.getCanUseSkuPrice();
}
let noChooseSkuIds = [];
for (let price of newPrice) {
noChooseSkuIds = noChooseSkuIds.concat(price.goods_sku_id_arr);
}
noChooseSkuIds = Array.from(new Set(noChooseSkuIds));
if (isChecked) {
let index = noChooseSkuIds.indexOf(skuId.toString());
noChooseSkuIds.splice(index, 1);
} else {
this.currentSkuArray.forEach(sku => {
if (sku.toString() != '') {
let index = noChooseSkuIds.indexOf(sku.toString());
if (index >= 0) {
noChooseSkuIds.splice(index, 1);
}
}
});
}
let chooseSkuKey = [];
if (!isChecked) {
this.currentSkuArray.forEach((sku, key) => {
if (sku != '') {
chooseSkuKey.push(key);
}
});
} else {
chooseSkuKey = [pid];
}
for (let i in this.skuList) {
if (chooseSkuKey.indexOf(this.skuList[i]['id']) >= 0) {
continue;
}
for (let j in this.skuList[i]['content']) {
if (noChooseSkuIds.indexOf(this.skuList[i]['content'][j]['id'].toString()) >= 0) {
this.skuList[i]['content'][j]['disabled'] = false;
} else {
this.skuList[i]['content'][j]['disabled'] = true;
}
}
}
},
getCanUseSkuPrice() {
let newPrice = [];
for (let price of this.skuPrice) {
if (price.stock <= 0) {
continue;
}
var isOk = true;
this.currentSkuArray.forEach(sku => {
if (sku.toString() != '' && price.goods_sku_id_arr.indexOf(sku.toString()) < 0) {
isOk = false;
}
});
if (isOk) {
newPrice.push(price);
}
}
return newPrice;
},
changeNum(e) {
this.changeDisabled(false);
},
plus(e) {
if (e.value >= this.currentSkuPrice.stock) {
this.maxStep = this.currentSkuPrice.stock;
this.$u.toast('库存不足');
return;
}
if (this.goodsInfo.activity_type === 'seckill' || this.goodsInfo.activity_type === 'groupon' || this.goodsInfo.activity_type === 'robbuy') {
let rules = this.goodsInfo.activity.rules;
if (rules.limit_buy != 0 && e.value >= rules.limit_buy) {
this.maxStep = parseInt(rules.limit_buy);
this.$u.toast('本次活动最多购买' + rules.limit_buy + '件');
return;
}
}
},
confirmCart() {
let that = this;
if (this.confirmSku()) {
let confirmGoodsList = {
list: [that.confirmGoodsInfo],
from: 'goods'
};
that.addCartGoods(confirmGoodsList).then(res => {
if (res.code === 1) {
that.showModal = false;
that.$u.toast(res.msg);
}
});
}
},
confirmBuy() {
let that = this;
that.showModal = false;
if (this.confirmSku()) {
let confirmGoodsList = [];
confirmGoodsList.push(that.confirmGoodsInfo);
that.jump('/pages/order/confirm', {
goodsList: confirmGoodsList,
from: 'goods',
orderType: that.goodsType,
grouponBuyType: that.grouponBuyType,
grouponId: that.grouponId
});
}
},
confirm() {
if (this.confirmSku()) {
switch (this.buyType) {
case 'cart':
this.confirmCart();
break;
case 'buy':
this.confirmBuy();
break;
default:
}
}
},
confirmSku() {
let that = this;
if (that.currentSkuPrice.stock == 0 || that.currentSkuPrice.stock < that.goodsNum) {
that.$u.toast('库存不足');
that.showModal = false;
return false;
} else {
that.currentSkuPrice.goods_num = that.goodsNum;
that.confirmGoodsInfo = {
goods_id: that.currentSkuPrice.goods_id,
goods_num: that.currentSkuPrice.goods_num,
sku_price_id: that.currentSkuPrice.id,
goods_price: that.currentSkuPrice.price
};
if (!that.confirmGoodsInfo.sku_price_id) {
that.$u.toast('请选择规格');
return false;
} else {
that.showModal = false;
return true;
}
}
}
}
};
</script>
<style lang="scss">
.sku-modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10080;
}
.sku-modal__mask {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.6);
z-index: 1;
}
.sku-modal__panel {
position: absolute;
left: 0;
right: 0;
bottom: 0;
width: 100%;
z-index: 2;
background-color: #ffffff;
border-radius: 30rpx 30rpx 0 0;
overflow: hidden;
}
.sku-scroll {
max-height: 560rpx;
}
.size-box {
line-height: 82rpx;
background: #fff;
padding: 0 20rpx;
margin: 20rpx 0;
font-size: 28rpx;
.title {
color: #999;
margin-right: 20rpx;
}
}
.shop-modal {
width: 750rpx;
height: 950rpx;
background: rgba(255, 255, 255, 1);
padding: 20rpx 20rpx 30rpx;
position: relative;
.close-icon {
font-size: 34rpx;
color: #e0e0e0;
position: absolute;
top: 20rpx;
right: 20rpx;
z-index: 2;
}
.top {
margin: 30rpx 0;
border-radius: 20rpx;
padding: 20rpx;
.shop-img {
width: 160upx;
height: 160upx;
border-radius: 6upx;
margin-right: 30upx;
background: #ccc;
}
.goods-box {
height: 160upx;
width: 490rpx;
align-items: flex-start;
.goods-title {
font-size: 28rpx;
font-weight: 500;
color: rgba(51, 51, 51, 1);
line-height: 42rpx;
text-align: left;
}
.goods-bottom {
width: 100%;
}
.price-box {
font-size: 36upx;
font-weight: bold;
color: #e1212b;
.unit {
font-size: 24upx;
font-weight: bold;
color: #e1212b;
}
}
.stock {
font-size: 26rpx;
color: #999;
}
}
}
.select-box {
margin-bottom: 30rpx;
.type-title {
font-size: 26upx;
font-weight: 400;
margin-bottom: 20upx;
}
.tag-box {
flex-wrap: wrap;
}
.tag {
line-height: 62rpx;
background: #f4f4f4;
border-radius: 31rpx;
font-size: 28rpx;
font-weight: 400;
color: #666666;
padding: 0 30rpx;
margin-bottom: 20rpx;
margin-right: 10rpx;
}
.tag-active {
background: linear-gradient(90deg, #4C7AF3, #2e45c2);
font-size: 28rpx;
font-weight: 400;
color: #ffffff;
}
.tag-disabled {
background: #f8f8f8;
color: #cacaca;
}
}
.buy-num-box {
.num-title {
font-size: 26upx;
font-weight: 400;
margin-bottom: 20upx;
}
}
}
.btn-box {
height: 100rpx;
.cu-btn {
width: 340rpx;
line-height: 70rpx;
border-radius: 35rpx;
font-size: 28rpx;
font-weight: 500;
color: rgba(255, 255, 255, 0.9);
padding: 0;
}
.cart-btn {
background: linear-gradient(90deg, #4C7AF3, #4C7AF3);
}
.buy-btn {
background: linear-gradient(90deg, #4C7AF3, #4C7AF3);
}
.save-btn {
width: 710rpx;
height: 70rpx;
background: linear-gradient(90deg, #4C7AF3, #4C7AF3);
font-size: 28rpx;
font-weight: 500;
color: rgba(255, 255, 255, 1);
border-radius: 35rpx;
padding: 0;
}
.cancel-btn {
width: 710rpx;
height: 70rpx;
background: rgba(221, 221, 221, 1);
font-size: 28rpx;
font-weight: 500;
color: #999999;
border-radius: 35rpx;
padding: 0;
}
}
</style>