459 行
13 KiB
Vue
459 行
13 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">
|
|
<view class="ss-flex">
|
|
<format-price :price="displaygoods.price" type="range" color="#FF4104" :decimal-scale="1" />
|
|
|
|
<view class="ss-flex">
|
|
<view v-if="displaygoods.score > 0" class="score-text ss-m-l-4">+</view>
|
|
<image v-if="displaygoods.score > 0"
|
|
:src="sheep.$url.static('/assets/addons/shopro/uniapp/goods/score1.svg')" class="score-img"></image>
|
|
<view v-if="displaygoods.score > 0" class="score-text">
|
|
{{ displaygoods.score }}
|
|
</view>
|
|
</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" @touchmove.stop>
|
|
<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="[
|
|
{
|
|
'ui-BG-Main-Gradient': 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 ss-m-b-40">
|
|
<view class="label-text">购买数量</view>
|
|
<su-number-box :min="1" :max="displaygoods.stock" :step="1" v-model="goodsNum"
|
|
@change="onGoodsNumChange($event)"></su-number-box>
|
|
</view>
|
|
</scroll-view>
|
|
</view>
|
|
<view class="modal-footer border-top">
|
|
<view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center" v-if="isScore">
|
|
<button class="ss-reset-button score-btn ui-Shadow-Main" @tap="onBuyNow">立即兑换</button>
|
|
</view>
|
|
<view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center" v-else>
|
|
<button class="ss-reset-button add-btn ui-Shadow-Main" @tap="onAddCart">加入购物车</button>
|
|
<button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="onBuyNow">立即购买</button>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
</su-popup>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { computed, reactive, watch, ref } from 'vue';
|
|
import sheep from '@/sheep';
|
|
import { formatStock } from '@/sheep/hooks/useGoods';
|
|
import { isArray, isEmpty } from 'lodash';
|
|
import FormatPrice from '@/pages/goods/components/format-price.vue';
|
|
|
|
const emits = defineEmits(['change', 'addCart', 'buyNow', 'close']);
|
|
const props = defineProps({
|
|
goods: {
|
|
type: Object,
|
|
default() { },
|
|
},
|
|
show: {
|
|
type: Boolean,
|
|
default: false,
|
|
},
|
|
isScore: {
|
|
type: Boolean,
|
|
default: false,
|
|
},
|
|
mode: { type: String, default: 'buyNow' }, // addCart | buyNow
|
|
});
|
|
|
|
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 unitScore = ref(0) // 单价积分
|
|
const totalPrice = computed(() => { // 总价
|
|
return (unitPrice.value * goodsNum.value).toFixed(2)
|
|
})
|
|
|
|
const totalScore = computed(() => { // 总价
|
|
return (unitScore.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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
// 商品数量更新
|
|
function onGoodsNumChange(val) {
|
|
// goodsNum.value = val
|
|
}
|
|
|
|
async function onBuyNow() {
|
|
let orderData
|
|
try {
|
|
orderData = validate()
|
|
}
|
|
catch (e) {
|
|
sheep.$helper.toast(e.message || '商品信息变更,请返回重试')
|
|
return
|
|
}
|
|
emits('buyNow', orderData)
|
|
}
|
|
|
|
async function onAddCart() {
|
|
let orderData
|
|
try {
|
|
orderData = validate()
|
|
}
|
|
catch (e) {
|
|
sheep.$helper.toast(e.message || '商品信息变更,请返回重试')
|
|
return
|
|
}
|
|
emits('addCart', 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,
|
|
score: totalScore.value,
|
|
}
|
|
return orderData
|
|
}
|
|
</script>
|
|
|
|
<style lang="scss" scoped>
|
|
// 购买
|
|
.buy-box {
|
|
padding: 10rpx 0;
|
|
|
|
.add-btn {
|
|
width: 356rpx;
|
|
height: 80rpx;
|
|
border-radius: 40rpx 0 0 40rpx;
|
|
background-color: var(--ui-BG-Main-light);
|
|
color: var(--ui-BG-Main);
|
|
}
|
|
|
|
.buy-btn {
|
|
width: 356rpx;
|
|
height: 80rpx;
|
|
border-radius: 0 40rpx 40rpx 0;
|
|
background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
|
|
color: #fff;
|
|
}
|
|
|
|
.score-btn {
|
|
width: 100%;
|
|
margin: 0 20rpx;
|
|
height: 80rpx;
|
|
border-radius: 40rpx;
|
|
background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
|
|
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;
|
|
}
|
|
|
|
.score-img {
|
|
width: 36rpx;
|
|
height: 36rpx;
|
|
margin: 0 4rpx;
|
|
}
|
|
|
|
.score-text {
|
|
font-size: 30rpx;
|
|
font-weight: 500;
|
|
color: $red;
|
|
font-family: OPPOSANS;
|
|
}
|
|
|
|
.price-text {
|
|
font-size: 30rpx;
|
|
font-weight: 500;
|
|
color: $red;
|
|
font-family: OPPOSANS;
|
|
|
|
&::before {
|
|
content: '¥';
|
|
font-size: 30rpx;
|
|
font-weight: 500;
|
|
color: $red;
|
|
}
|
|
}
|
|
|
|
.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;
|
|
}
|
|
}
|
|
|
|
.disabled-btn {
|
|
font-weight: 400;
|
|
color: #ccc;
|
|
background: #f8f8f8;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</style>
|