430 行
12 KiB
Vue
430 行
12 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">{{ goodsInfo.title }}</view>
|
||
<view class="header-right-bottom ss-flex ss-col-center ss-row-between">
|
||
<format-price :price="displaygoods.price" type="range" color="#FF4104" :decimal-scale="1" />
|
||
<view class="tig ss-flex ss-col-center">
|
||
<view class="tig-icon ss-flex ss-col-center ss-row-center">
|
||
<text class="cicon-alarm"></text>
|
||
</view>
|
||
<view class="tig-title">秒杀价</view>
|
||
</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 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"
|
||
activity="seckill"></su-number-box>
|
||
</view>
|
||
</scroll-view>
|
||
</view>
|
||
<view class="modal-footer">
|
||
<view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center">
|
||
<button class="ss-reset-button buy-btn" @tap="onBuyNow">确认</button>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</su-popup>
|
||
</template>
|
||
|
||
<script setup>
|
||
/**
|
||
* 1. 根据spus获得skus: 后台已算好
|
||
* 2. 用路径字典,生成路径map
|
||
* 3. 计算用户选择后,其他路径是否可用
|
||
*/
|
||
// 按钮状态: active,nostock
|
||
import { computed, ref, watch } from 'vue';
|
||
import sheep from '@/sheep';
|
||
import { formatStock } from '@/sheep/hooks/useGoods';
|
||
import FormatPrice from '@/pages/goods/components/format-price.vue';
|
||
|
||
|
||
const emits = defineEmits(['change', 'buyNow', 'close']);
|
||
const props = defineProps({
|
||
goods: {
|
||
type: Object,
|
||
default() { },
|
||
},
|
||
show: {
|
||
type: Boolean,
|
||
default: false,
|
||
},
|
||
});
|
||
|
||
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(() => {
|
||
console.log('selectedSkuPrice.value', selectedSkuPrice.value)
|
||
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)) : [],
|
||
}))
|
||
})
|
||
|
||
// 监听商品变化
|
||
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 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-box {
|
||
padding: 10rpx 20rpx;
|
||
|
||
.buy-btn {
|
||
width: 100%;
|
||
height: 80rpx;
|
||
border-radius: 40rpx;
|
||
background: linear-gradient(90deg, #ff5854, #ff2621);
|
||
color: #fff;
|
||
}
|
||
}
|
||
|
||
.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;
|
||
}
|
||
|
||
.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, #ff5854, #ff2621);
|
||
font-weight: 500;
|
||
color: #ffffff;
|
||
}
|
||
|
||
.disabled-btn {
|
||
font-weight: 400;
|
||
color: #c6c6c6;
|
||
background: #f8f8f8;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
.tig {
|
||
border: 2rpx solid #ff5854;
|
||
border-radius: 4rpx;
|
||
width: 126rpx;
|
||
height: 38rpx;
|
||
|
||
.tig-icon {
|
||
width: 40rpx;
|
||
height: 40rpx;
|
||
background: #ff5854;
|
||
border-radius: 4rpx 0 0 4rpx;
|
||
|
||
.cicon-alarm {
|
||
font-size: 32rpx;
|
||
color: #fff;
|
||
}
|
||
}
|
||
|
||
.tig-title {
|
||
font-size: 24rpx;
|
||
font-weight: 500;
|
||
line-height: normal;
|
||
color: #ff6000;
|
||
width: 86rpx;
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: center;
|
||
}
|
||
}
|
||
</style>
|