580 行
16 KiB
Vue
580 行
16 KiB
Vue
<template>
|
|
<!-- 拼团商品规格弹窗 -->
|
|
<su-popup :show="show" round="10" @close="emits('close')">
|
|
<view class="ss-modal-box bg-white ss-flex-col">
|
|
<view class="modal-header ss-flex ss-col-center">
|
|
<view class="header-left ss-m-r-30">
|
|
<image class="sku-image" :src="sheep.$url.cdn(displaygoods.image || goodsInfo.image)" mode="aspectFill">
|
|
</image>
|
|
</view>
|
|
<view class="header-right ss-flex-col ss-row-between ss-flex-1">
|
|
<view class="goods-title ss-line-2">
|
|
<view class="tig ss-flex ss-col-center">
|
|
<view class="tig-icon ss-flex ss-col-center ss-row-center">
|
|
<view class="groupon-tag">
|
|
<image :src="sheep.$url.static('/assets/addons/shopro/uniapp/goods/groupon-tag-white.png')">
|
|
</image>
|
|
</view>
|
|
</view>
|
|
<view class="tig-title">拼团价</view>
|
|
</view>
|
|
<view class="info-title">
|
|
{{ goodsInfo.title }}
|
|
</view>
|
|
</view>
|
|
<view class="header-right-bottom ss-flex ss-col-center ss-row-between">
|
|
<view class="price-text"> {{ goodsPrice }}</view>
|
|
|
|
<view class="stock-text ss-m-l-20">
|
|
{{ formatStock(goodsInfo.stock_show_type, displaygoods.stock) }}
|
|
</view>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
<view class="modal-content ss-flex-1">
|
|
<scroll-view scroll-y="true" class="modal-content-scroll">
|
|
<view v-if="grouponAction === 'create' && goodsInfo.activity_type === 'groupon_ladder'"
|
|
class="sku-item ss-m-b-20">
|
|
<view class="label-text ss-m-b-20">拼团人数</view>
|
|
<view class="ss-flex ss-col-center ss-flex-wrap">
|
|
<button v-for="(ladder, key) in goodsInfo.activity.rules.ladders" :key="key"
|
|
class="ss-reset-button spec-btn" :class="[
|
|
{
|
|
'checked-btn': grouponNum == ladder,
|
|
},
|
|
]" @tap="onSelectLadder(ladder)">
|
|
{{ ladder }}人团
|
|
</button>
|
|
</view>
|
|
</view>
|
|
<view class="sku-item ss-m-b-20" v-for="(sku, skuIndex) in skuTree" :key="sku.id">
|
|
<view class="label-text ss-m-b-20">{{ sku.name }}</view>
|
|
<view class="ss-flex ss-col-center ss-flex-wrap">
|
|
<button class="ss-reset-button spec-btn" v-for="option in sku.children" :class="[
|
|
{
|
|
'checked-btn': selectedSkuMap[skuIndex] == option.id,
|
|
},
|
|
{
|
|
'disabled-btn': option.disabled == true,
|
|
},
|
|
]" :key="option.id" :disabled="option.disabled == true" @tap="onSelectSkuOption(skuIndex, option)">
|
|
<view class="ss-flex ss-w-100">
|
|
<image v-if="option.image" class="spec-img" :src="sheep.$url.cdn(option.image)" />
|
|
<view class="ss-m-x-20 ss-flex-1 spec-name">
|
|
{{ option.name }}
|
|
</view>
|
|
</view>
|
|
</button>
|
|
</view>
|
|
</view>
|
|
<view class="buy-num-box ss-flex ss-col-center ss-row-between">
|
|
<view class="label-text">购买数量</view>
|
|
<su-number-box :min="1" :max="displaygoods.stock" :step="1" v-model="goodsNum"
|
|
@change="onNumberChange($event)" activity="groupon">
|
|
</su-number-box>
|
|
</view>
|
|
</scroll-view>
|
|
</view>
|
|
<view class="modal-footer ss-p-y-20">
|
|
<view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center">
|
|
<view class="ss-flex">
|
|
<button class="ss-reset-button origin-price-btn ss-flex-col">
|
|
<view class="btn-title">{{
|
|
grouponNum === 0 ? '阶梯团' : grouponNum + '人团'
|
|
}}</view>
|
|
</button>
|
|
<button class="ss-reset-button btn-tox ss-flex-col" @tap="onBuyNow">
|
|
<view class="btn-price">
|
|
{{
|
|
grouponAction === 'create' && goodsInfo.activity.rules.is_leader_discount == 1
|
|
? leaderPrice
|
|
: goodsPrice
|
|
}}
|
|
</view>
|
|
<view v-if="
|
|
grouponAction === 'create' && goodsInfo.activity.rules.is_leader_discount == 0
|
|
">立即开团</view>
|
|
<view v-else-if="
|
|
grouponAction === 'create' && goodsInfo.activity.rules.is_leader_discount == 1
|
|
">团长立减价</view>
|
|
<view v-else-if="grouponAction === 'join'">参与拼团</view>
|
|
</button>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
</su-popup>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { computed, ref, watch } from 'vue';
|
|
import sheep from '@/sheep';
|
|
import { formatPrice, formatStock } from '@/sheep/hooks/useGoods';
|
|
import { isEmpty } from 'lodash';
|
|
|
|
const headerBg = sheep.$url.css('/assets/addons/shopro/uniapp/goods/groupon-btn-long.png');
|
|
const emits = defineEmits(['change', 'buyNow', 'close', 'ladder']);
|
|
const props = defineProps({
|
|
show: {
|
|
type: Boolean,
|
|
default: false,
|
|
},
|
|
goods: {
|
|
type: Object,
|
|
default() { },
|
|
},
|
|
grouponAction: {
|
|
type: String,
|
|
default: 'create',
|
|
},
|
|
grouponNum: {
|
|
type: [Number, String],
|
|
default: 0,
|
|
},
|
|
});
|
|
|
|
const goodsInfo = ref({})
|
|
const skuTree = ref([]) // 规格选项列表
|
|
const selectedSkuMap = ref([]) // 已选规格 ID 索引
|
|
const isSelectedAllSkus = ref(false) // 是否已选完所有规格
|
|
const isNotSelectAnySkus = ref(true) // 是否未选择任何规格
|
|
const selectedSkuPrice = ref(null) // 已选规格对应的价格项
|
|
const selectedSkuText = ref('') // 已选规格文本标签
|
|
|
|
const goodsNum = ref(1) // 购买数量
|
|
const unitPrice = ref(0) // 单价
|
|
const totalPrice = computed(() => { // 总价
|
|
return (unitPrice.value * goodsNum.value).toFixed(2)
|
|
})
|
|
|
|
const displaygoods = computed(() => {
|
|
return selectedSkuPrice.value || goodsInfo.value
|
|
})
|
|
|
|
// 预处理规格价格列表,添加辅助字段
|
|
const skuPriceList = computed(() => {
|
|
const skuPrices = goodsInfo.value.sku_prices || []
|
|
return skuPrices.map((item) => ({
|
|
...item,
|
|
// 预先转为数组并转为数字/字符串统一格式,方便后续 includes 对比
|
|
skuIds: item.goods_sku_ids ? item.goods_sku_ids.split(',').map((id) => Number(id)) : [],
|
|
}))
|
|
})
|
|
|
|
const goodsPrice = computed(() => {
|
|
if (isEmpty(selectedSkuPrice.value)) {
|
|
return formatPrice(goodsInfo.value.price);
|
|
}
|
|
if (props.grouponNum === 0 && goodsInfo.value.activity_type === 'groupon_ladder') {
|
|
return formatPrice(goodsInfo.value.price);
|
|
}
|
|
|
|
if (goodsInfo.value.activity_type === 'groupon') {
|
|
return selectedSkuPrice.value.groupon_price;
|
|
}
|
|
if (goodsInfo.value.activity_type === 'groupon_ladder') {
|
|
const ladder = getSkuPriceByLadder();
|
|
selectedSkuPrice.value.ladder_price = ladder.ladder_price;
|
|
return ladder.ladder_price;
|
|
}
|
|
});
|
|
|
|
// 获取阶梯价
|
|
function getSkuPriceByLadder() {
|
|
return selectedSkuPrice.value.ladders.find((item) => item.ladder == props.grouponNum);
|
|
}
|
|
|
|
// 团长优惠
|
|
const leaderPrice = computed(() => {
|
|
if (isEmpty(selectedSkuPrice.value)) {
|
|
return formatPrice(goodsInfo.value.price);
|
|
}
|
|
|
|
if (props.grouponNum === 0 && goodsInfo.value.activity_type === 'groupon_ladder') {
|
|
return formatPrice(goodsInfo.value.price);
|
|
}
|
|
|
|
if (goodsInfo.value.activity_type === 'groupon') {
|
|
return selectedSkuPrice.value.leader_price;
|
|
}
|
|
if (goodsInfo.value.activity_type === 'groupon_ladder') {
|
|
const ladder = getSkuPriceByLadder();
|
|
return ladder.leader_ladder_price;
|
|
}
|
|
});
|
|
|
|
|
|
// 监听商品变化
|
|
watch(
|
|
() => props.goods,
|
|
() => {
|
|
init()
|
|
},
|
|
{ immediate: true, deep: true },
|
|
)
|
|
|
|
// 初始化商品信息
|
|
function init() {
|
|
goodsInfo.value = JSON.parse(JSON.stringify(props.goods))
|
|
goodsNum.value = 1
|
|
if (goodsInfo.value.is_sku === 0) {
|
|
selectedSkuPrice.value = goodsInfo.value.sku_prices[0]
|
|
selectedSkuMap.value = [goodsInfo.value.sku_prices[0].id]
|
|
}
|
|
if (goodsInfo.value.is_sku === 1) {
|
|
skuTree.value = goodsInfo.value.skus || []
|
|
selectedSkuMap.value = Array.from({ length: skuTree.value.length }).fill(0)
|
|
updateOptionStatus() // 初始化时计算一次禁用状态
|
|
}
|
|
calculate()
|
|
}
|
|
|
|
// 选择阶梯拼团人数
|
|
function onSelectLadder(ladder) {
|
|
emits('ladder', ladder);
|
|
}
|
|
|
|
// 点击选择规格(可正选/反选)
|
|
function onSelectSkuOption(skuIndex, skuOption) {
|
|
if (skuOption.disabled) {
|
|
return
|
|
}
|
|
if (selectedSkuMap.value[skuIndex] === skuOption.id) {
|
|
selectedSkuMap.value[skuIndex] = 0 // 取消选中
|
|
}
|
|
else {
|
|
selectedSkuMap.value[skuIndex] = skuOption.id // 选中
|
|
}
|
|
|
|
// 更新所有规格选项禁用状态
|
|
updateOptionStatus()
|
|
// 选完所有规格后,查找对应的 SKU 价格项
|
|
if (checkIsSelectedAllSkus()) {
|
|
// 把 selectedSkuMap 按照由小到大排序
|
|
const sortedSelectedSkuMap = [...selectedSkuMap.value].sort((a, b) => a - b)
|
|
const selectedSkuKey = sortedSelectedSkuMap.join(',')
|
|
selectedSkuPrice.value = skuPriceList.value.find((sku) =>
|
|
sku.skuIds.join(',') === selectedSkuKey) || null
|
|
}
|
|
else {
|
|
selectedSkuPrice.value = null
|
|
}
|
|
// 计算价格和规格标签
|
|
calculate()
|
|
// 抛出变更事件
|
|
emits('change', selectedSkuPrice.value)
|
|
}
|
|
|
|
// 判断某个选项是否可选 ---
|
|
/**
|
|
* @param skuIndex 规格所在的行索引
|
|
* @param optionId 规格值的 ID
|
|
*/
|
|
function isSkuOptionSelectable(skuIndex, optionId) {
|
|
// 1. 复制当前已选的规格数组
|
|
const tempSelectedSkuMap = [...selectedSkuMap.value]
|
|
// 2. 假设选中当前这个选项
|
|
tempSelectedSkuMap[skuIndex] = optionId
|
|
|
|
// 3. 过滤掉未选中的项,得到有效的条件集合
|
|
const conditions = tempSelectedSkuMap.filter(id => id !== 0)
|
|
|
|
// 4. 遍历所有有效的 SKU 组合
|
|
// 只要找到 至少一个SKU 满足以下条件,则该选项可选:
|
|
// A. 可选库存 > 0
|
|
// B. 该 SKU 包含所有 `conditions` 中的 ID
|
|
return skuPriceList.value.some((sku) => {
|
|
if (sku.stock <= 0) {
|
|
return false
|
|
}
|
|
else {
|
|
// 检查 sku._skuIds 是否包含 conditions 中的所有 ID
|
|
return conditions.every(condId => sku.skuIds.includes(condId))
|
|
}
|
|
})
|
|
}
|
|
|
|
// 更新所有规格按钮的状态
|
|
function updateOptionStatus() {
|
|
skuTree.value.forEach((sku, skuIndex) => {
|
|
sku.children.forEach((option) => {
|
|
// 判断是否可选
|
|
option.disabled = !isSkuOptionSelectable(skuIndex, option.id)
|
|
})
|
|
})
|
|
}
|
|
|
|
// 判断是否已选完所有规格
|
|
function checkIsSelectedAllSkus() {
|
|
isSelectedAllSkus.value = selectedSkuMap.value.includes(0) === false
|
|
return isSelectedAllSkus.value
|
|
}
|
|
|
|
// 判断未选择任何规格
|
|
function checkIsNotSelectAnySkus() {
|
|
isNotSelectAnySkus.value = selectedSkuMap.value.every(id => id === 0)
|
|
return isNotSelectAnySkus.value
|
|
}
|
|
|
|
// 计算价格、规格、属性信息
|
|
function calculate() {
|
|
unitPrice.value = 0
|
|
selectedSkuText.value = ''
|
|
if (checkIsNotSelectAnySkus()) {
|
|
return
|
|
}
|
|
let unitSkuPrice = 0 // 规格单价
|
|
|
|
if (selectedSkuPrice.value) {
|
|
unitSkuPrice = Number(selectedSkuPrice.value.price)
|
|
}
|
|
const selectedSkuTextList = []
|
|
selectedSkuMap.value.forEach((optionId, skuIndex) => {
|
|
if (optionId === 0) {
|
|
selectedSkuTextList.push('--')
|
|
}
|
|
else if (goodsInfo.value.sku_type === 'multiple') {
|
|
const sku = skuTree.value[skuIndex]
|
|
const skuOption = sku.children.find((option) => option.id === optionId)
|
|
if (skuOption) {
|
|
selectedSkuTextList.push(skuOption.name)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
|
|
async function onBuyNow() {
|
|
let orderData
|
|
try {
|
|
orderData = validate()
|
|
}
|
|
catch (e) {
|
|
sheep.$helper.toast(e.message || '商品信息变更,请返回重试')
|
|
return
|
|
}
|
|
emits('buyNow', orderData)
|
|
}
|
|
|
|
function validate() {
|
|
// 检测规格是否全部选择完成
|
|
if (!selectedSkuPrice.value)
|
|
throw new Error('请选择商品规格')
|
|
const orderData = {
|
|
goods_id: goodsInfo.value.id,
|
|
goods_sku_price: selectedSkuPrice.value,
|
|
goods_sku_price_id: selectedSkuPrice.value.id,
|
|
goods_num: goodsNum.value,
|
|
price: totalPrice.value,
|
|
}
|
|
return orderData
|
|
}
|
|
|
|
</script>
|
|
|
|
<style lang="scss" scoped>
|
|
// 购买
|
|
.buy-btn {
|
|
margin: 0 20rpx;
|
|
width: 100%;
|
|
height: 80rpx;
|
|
border-radius: 40rpx;
|
|
background: linear-gradient(90deg, #ff6000, #fe832a);
|
|
color: #fff;
|
|
}
|
|
|
|
.btn-tox {
|
|
width: 382rpx;
|
|
height: 80rpx;
|
|
font-size: 24rpx;
|
|
font-weight: 600;
|
|
margin-left: -50rpx;
|
|
background-image: v-bind(headerBg);
|
|
background-repeat: no-repeat;
|
|
background-size: 100% 100%;
|
|
color: #ffffff;
|
|
line-height: normal;
|
|
border-radius: 0px 40rpx 40rpx 0px;
|
|
|
|
.btn-price {
|
|
font-family: OPPOSANS;
|
|
|
|
&::before {
|
|
content: '¥';
|
|
}
|
|
}
|
|
}
|
|
|
|
.origin-price-btn {
|
|
width: 370rpx;
|
|
height: 80rpx;
|
|
background: rgba(#ff5651, 0.1);
|
|
color: #ff6000;
|
|
border-radius: 40rpx 0px 0px 40rpx;
|
|
line-height: normal;
|
|
font-size: 24rpx;
|
|
font-weight: 500;
|
|
|
|
.btn-price {
|
|
font-family: OPPOSANS;
|
|
|
|
&::before {
|
|
content: '¥';
|
|
}
|
|
}
|
|
|
|
.btn-title {
|
|
font-size: 28rpx;
|
|
}
|
|
}
|
|
|
|
.ss-modal-box {
|
|
border-radius: 30rpx 30rpx 0 0;
|
|
max-height: 1000rpx;
|
|
|
|
.modal-header {
|
|
position: relative;
|
|
padding: 80rpx 20rpx 40rpx;
|
|
|
|
.sku-image {
|
|
width: 160rpx;
|
|
height: 160rpx;
|
|
border-radius: 10rpx;
|
|
}
|
|
|
|
.header-right {
|
|
height: 160rpx;
|
|
}
|
|
|
|
.close-icon {
|
|
position: absolute;
|
|
top: 10rpx;
|
|
right: 20rpx;
|
|
font-size: 46rpx;
|
|
opacity: 0.2;
|
|
}
|
|
|
|
.goods-title {
|
|
font-size: 28rpx;
|
|
font-weight: 500;
|
|
line-height: 42rpx;
|
|
position: relative;
|
|
|
|
.tig {
|
|
border: 2rpx solid #ff6000;
|
|
border-radius: 4rpx;
|
|
width: 126rpx;
|
|
height: 38rpx;
|
|
position: absolute;
|
|
left: 0;
|
|
top: 0;
|
|
|
|
.tig-icon {
|
|
width: 40rpx;
|
|
height: 40rpx;
|
|
background: #ff6000;
|
|
margin-left: -2rpx;
|
|
border-radius: 4rpx 0 0 4rpx;
|
|
|
|
.groupon-tag {
|
|
width: 32rpx;
|
|
height: 32rpx;
|
|
}
|
|
}
|
|
|
|
.tig-title {
|
|
font-size: 24rpx;
|
|
font-weight: 500;
|
|
line-height: normal;
|
|
color: #ff6000;
|
|
width: 86rpx;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
}
|
|
|
|
.info-title {
|
|
text-indent: 132rpx;
|
|
}
|
|
}
|
|
|
|
.price-text {
|
|
font-size: 30rpx;
|
|
font-weight: 500;
|
|
color: $red;
|
|
font-family: OPPOSANS;
|
|
|
|
&::before {
|
|
content: '¥';
|
|
font-size: 24rpx;
|
|
}
|
|
}
|
|
|
|
.stock-text {
|
|
font-size: 26rpx;
|
|
color: #999999;
|
|
}
|
|
}
|
|
|
|
.modal-content {
|
|
padding: 0 20rpx;
|
|
|
|
.modal-content-scroll {
|
|
max-height: 600rpx;
|
|
|
|
.label-text {
|
|
font-size: 26rpx;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.buy-num-box {
|
|
height: 100rpx;
|
|
}
|
|
|
|
.spec-btn {
|
|
height: 68rpx;
|
|
min-width: 100rpx;
|
|
background: #f8f8f8;
|
|
border-radius: 10rpx;
|
|
color: #666;
|
|
font-size: 24rpx;
|
|
margin-right: 20rpx;
|
|
margin-bottom: 20rpx;
|
|
|
|
.spec-img {
|
|
margin-left: 2rpx;
|
|
height: 60rpx;
|
|
width: 60rpx;
|
|
border-radius: 10rpx;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.spec-name {
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
}
|
|
.checked-btn {
|
|
background: linear-gradient(90deg, #ff6000, #fe832a);
|
|
font-weight: 500;
|
|
color: #ffffff;
|
|
}
|
|
|
|
.disabled-btn {
|
|
font-weight: 400;
|
|
color: #c6c6c6;
|
|
background: #f8f8f8;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
image {
|
|
width: 100%;
|
|
height: 100%;
|
|
}
|
|
</style>
|