商品详情:SKU 的选择完善

This commit is contained in:
YunaiV
2023-12-10 11:57:24 +08:00
parent 2ee3c77249
commit e3264329e7
3 changed files with 191 additions and 276 deletions
@@ -1,10 +1,10 @@
<template> <template>
<detail-cell v-if="skus.length > 0" label="选择" :value="value"></detail-cell> <!-- SKU 选择的提示框 -->
<detail-cell label="选择" :value="value" />
</template> </template>
<script setup> <script setup>
import { computed } from 'vue'; import { computed } from 'vue';
import detailCell from './detail-cell.vue'; import detailCell from './detail-cell.vue';
const props = defineProps({ const props = defineProps({
@@ -14,22 +14,18 @@
return []; return [];
}, },
}, },
skus: { sku: {
type: Array, type: Object
default() { }
return [];
},
},
}); });
const value = computed(() => { const value = computed(() => {
let str = ''; if (!props.sku.id) {
if (props.modelValue.length > 0) { return '请选择商品规格';
props.modelValue.forEach((item, index) => {
str += props.skus[index].name + ':' + item + ' ';
});
} else {
str = '请选择商品规格';
} }
let str = '';
props.sku.properties.forEach(property => {
str += property.propertyName + ':' + property.valueName + ' ';
});
return str; return str;
}); });
</script> </script>
+47 -67
View File
@@ -12,24 +12,25 @@
<block v-else> <block v-else>
<view class="detail-swiper-selector"> <view class="detail-swiper-selector">
<!-- 商品轮播图 --> <!-- 商品轮播图 -->
<su-swiper class="ss-m-b-14" isPreview :list="state.goodsSwiper" dotStyle="tag" imageMode="widthFix" <su-swiper class="ss-m-b-14" isPreview :list="formatGoodsSwiper(state.goodsInfo.sliderPicUrls)"
dotCur="bg-mask-40" :seizeHeight="750" /> dotStyle="tag" imageMode="widthFix" dotCur="bg-mask-40" :seizeHeight="750" />
<!-- 价格+标题 --> <!-- 价格+标题 -->
<view class="title-card detail-card ss-p-y-40 ss-p-x-20"> <view class="title-card detail-card ss-p-y-40 ss-p-x-20">
<view class="ss-flex ss-row-between ss-col-center ss-m-b-26"> <view class="ss-flex ss-row-between ss-col-center ss-m-b-26">
<view class="price-box ss-flex ss-col-bottom"> <view class="price-box ss-flex ss-col-bottom">
<view class="price-text ss-m-r-16"> <view class="price-text ss-m-r-16">
{{ state.selectedSkuPrice.price || formatPrice(state.goodsInfo.price) }} {{ fen2yuan(state.selectedSku.price || state.goodsInfo.price) }}
</view> </view>
<view class="origin-price-text" v-if="state.goodsInfo.original_price > 0"> <view class="origin-price-text" v-if="state.goodsInfo.marketPrice > 0">
{{ state.selectedSkuPrice.original_price || state.goodsInfo.original_price }} {{ fen2yuan(state.selectedSku.marketPrice || state.goodsInfo.marketPrice) }}
</view> </view>
</view> </view>
<view class="sales-text"> <view class="sales-text">
{{ formatSales(state.goodsInfo.sales_show_type, state.goodsInfo.sales) }} {{ formatSales('exact', state.goodsInfo.salesCount) }}
</view> </view>
</view> </view>
<!-- TODO 芋艿promos 未写 -->
<view class="discounts-box ss-flex ss-row-between ss-m-b-28"> <view class="discounts-box ss-flex ss-row-between ss-m-b-28">
<div class="tag-content"> <div class="tag-content">
<view class="tag-box ss-flex"> <view class="tag-box ss-flex">
@@ -40,27 +41,29 @@
</view> </view>
</div> </div>
<!-- TODO 芋艿couponInfo 优惠劵 -->
<view class="get-coupon-box ss-flex ss-col-center ss-m-l-20" @tap="state.showModel = true" <view class="get-coupon-box ss-flex ss-col-center ss-m-l-20" @tap="state.showModel = true"
v-if="state.couponInfo.length"> v-if="state.couponInfo.length">
<view class="discounts-title ss-m-r-8">领券</view> <view class="discounts-title ss-m-r-8">领券</view>
<text class="cicon-forward"></text> <text class="cicon-forward"></text>
</view> </view>
</view> </view>
<view class="title-text ss-line-2 ss-m-b-6">{{ state.goodsInfo.title }}</view> <view class="title-text ss-line-2 ss-m-b-6">{{ state.goodsInfo.name }}</view>
<view class="subtitle-text ss-line-1">{{ state.goodsInfo.subtitle }}</view> <view class="subtitle-text ss-line-1">{{ state.goodsInfo.introduction }}</view>
</view> </view>
<!-- 功能卡片 --> <!-- 功能卡片 -->
<view class="detail-cell-card detail-card ss-flex-col"> <view class="detail-cell-card detail-card ss-flex-col">
<detail-cell-sku v-model="state.selectedSkuPrice.goods_sku_text" :skus="state.goodsInfo.skus" <detail-cell-sku v-model="state.selectedSku.goods_sku_text" :sku="state.selectedSku"
@tap="state.showSelectSku = true" /> @tap="state.showSelectSku = true" />
<!-- TODO 芋艿可能暂时不考虑使用 -->
<detail-cell-service v-if="state.goodsInfo.service" v-model="state.goodsInfo.service" /> <detail-cell-service v-if="state.goodsInfo.service" v-model="state.goodsInfo.service" />
<detail-cell-params v-if="state.goodsInfo.params" v-model="state.goodsInfo.params" /> <detail-cell-params v-if="state.goodsInfo.params" v-model="state.goodsInfo.params" />
</view> </view>
<!-- 规格与数量弹框 --> <!-- 规格与数量弹框 -->
<s-select-sku :goodsInfo="state.goodsInfo" :show="state.showSelectSku" @addCart="onAddCart" <s-select-sku :goodsInfo="state.goodsInfo" :show="state.showSelectSku" @addCart="onAddCart"
@buy="onBuy" @change="onSkuChange" @close="state.showSelectSku = false" /> @buy="onBuy" @change="onSkuChange" @close="state.showSelectSku = false" />
</view> </view>
<!-- 评价 --> <!-- 评价 -->
@@ -73,7 +76,6 @@
<!-- 详情tabbar --> <!-- 详情tabbar -->
<detail-tabbar v-model="state.goodsInfo"> <detail-tabbar v-model="state.goodsInfo">
<!-- TODO: 缺货中 已售罄 判断 设计-->
<view class="buy-box ss-flex ss-col-center ss-p-r-20" v-if="state.goodsInfo.stock > 0"> <view class="buy-box ss-flex ss-col-center ss-p-r-20" v-if="state.goodsInfo.stock > 0">
<button class="ss-reset-button add-btn ui-Shadow-Main" @tap="state.showSelectSku = true"> <button class="ss-reset-button add-btn ui-Shadow-Main" @tap="state.showSelectSku = true">
加入购物车 加入购物车
@@ -87,29 +89,19 @@
</view> </view>
</detail-tabbar> </detail-tabbar>
<s-coupon-get v-model="state.couponInfo" :show="state.showModel" @close="state.showModel = false" <s-coupon-get v-model="state.couponInfo" :show="state.showModel" @close="state.showModel = false"
@get="onGet" /> @get="onGet" />
<s-activity-pop v-model="state.activityInfo" :show="state.showActivityModel" <s-activity-pop v-model="state.activityInfo" :show="state.showActivityModel"
@close="state.showActivityModel = false" /> @close="state.showActivityModel = false" />
</block> </block>
</s-layout> </s-layout>
</view> </view>
</template> </template>
<script setup> <script setup>
import { import { reactive, computed } from 'vue';
reactive, import { onLoad, onPageScroll } from '@dcloudio/uni-app';
computed
} from 'vue';
import {
onLoad,
onPageScroll
} from '@dcloudio/uni-app';
import sheep from '@/sheep'; import sheep from '@/sheep';
import { import { formatSales, formatGoodsSwiper, fen2yuan, } from '@/sheep/hooks/useGoods';
formatSales,
formatGoodsSwiper,
formatPrice
} from '@/sheep/hooks/useGoods';
import detailNavbar from './components/detail/detail-navbar.vue'; import detailNavbar from './components/detail/detail-navbar.vue';
import detailCellSku from './components/detail/detail-cell-sku.vue'; import detailCellSku from './components/detail/detail-cell-sku.vue';
import detailCellService from './components/detail/detail-cell-service.vue'; import detailCellService from './components/detail/detail-cell-service.vue';
@@ -130,29 +122,27 @@
const state = reactive({ const state = reactive({
goodsId: 0, goodsId: 0,
skeletonLoading: true, skeletonLoading: true, // SPU 加载中
goodsInfo: {}, goodsInfo: {}, // SPU 信息
showSelectSku: false, showSelectSku: false, // 是否展示 SKU 选择弹窗
goodsSwiper: [], selectedSku: {}, // 选中的 SKU
selectedSkuPrice: {},
showModel: false, showModel: false,
total: 0,
couponInfo: [], couponInfo: [],
showActivityModel: false, showActivityModel: false,
activityInfo: [], activityInfo: [],
}); });
// 规格变更 // 规格变更 TODO 芋艿:待测试
function onSkuChange(e) { function onSkuChange(e) {
state.selectedSkuPrice = e; state.selectedSku = e;
} }
// 添加购物车 // 添加购物车 TODO 芋艿:待测试
function onAddCart(e) { function onAddCart(e) {
sheep.$store('cart').add(e); sheep.$store('cart').add(e);
} }
// 立即购买 // 立即购买 TODO 芋艿:待测试
function onBuy(e) { function onBuy(e) {
sheep.$router.go('/pages/order/confirm', { sheep.$router.go('/pages/order/confirm', {
data: JSON.stringify({ data: JSON.stringify({
@@ -165,13 +155,14 @@
}), }),
}); });
} }
//营销活动
// 营销活动 TODO 芋艿:待测试
function onActivity() { function onActivity() {
state.activityInfo = state.goodsInfo.promos; state.activityInfo = state.goodsInfo.promos;
state.showActivityModel = true; state.showActivityModel = true;
} }
//立即领取 //立即领取 TODO 芋艿:待测试
async function onGet(id) { async function onGet(id) {
const { const {
error, error,
@@ -187,10 +178,11 @@
} }
} }
// TODO 芋艿:待测试
const shareInfo = computed(() => { const shareInfo = computed(() => {
if (isEmpty(state.goodsInfo)) return {}; if (isEmpty(state.goodsInfo)) return {};
return sheep.$platform.share.getShareInfo({ return sheep.$platform.share.getShareInfo({
title: state.goodsInfo.title, title: state.goodsInfo.name,
image: sheep.$url.cdn(state.goodsInfo.image), image: sheep.$url.cdn(state.goodsInfo.image),
desc: state.goodsInfo.subtitle, desc: state.goodsInfo.subtitle,
params: { params: {
@@ -199,7 +191,7 @@
}, },
}, { }, {
type: 'goods', // 商品海报 type: 'goods', // 商品海报
title: state.goodsInfo.title, // 商品标题 title: state.goodsInfo.name, // 商品标题
image: sheep.$url.cdn(state.goodsInfo.image), // 商品主图 image: sheep.$url.cdn(state.goodsInfo.image), // 商品主图
price: state.goodsInfo.price[0], // 商品价格 price: state.goodsInfo.price[0], // 商品价格
original_price: state.goodsInfo.original_price, // 商品原价 original_price: state.goodsInfo.original_price, // 商品原价
@@ -207,41 +199,29 @@
}); });
onLoad(async (options) => { onLoad(async (options) => {
console.log('页面被访问')
// 非法参数 // 非法参数
if (!options.id) { if (!options.id) {
state.goodsInfo = null; state.goodsInfo = null;
return; return;
} }
state.goodsId = options.id; state.goodsId = options.id;
// 加载商品信息 // 1. 加载商品信息
sheep.$api.goods.detail(state.goodsId).then((res) => { sheep.$api.goods.detail(state.goodsId).then((res) => {
// 处理数据适配 // 未找到商品
// let arr = []; if (res.code !== 0 || !res.data) {
// res.skus = res.data.skus.map(item => { state.goodsInfo = null;
// arr = [...arr, ...item.properties]; return;
// item.children = item.properties; }
// item.goods_id = res.data.id; // 加载到商品
// item.name = item.children[0].propertyName;
// return item;
// })
// console.log(arr, '合并');
// console.log(res.data, '替换后订单数据');
state.skeletonLoading = false; state.skeletonLoading = false;
if (res.code === 0) { state.goodsInfo = res.data;
// 在此处对数据做出转换
res.data.sales = res.data.salesCount
res.data.original_price = res.data.marketPrice / 100
res.data.subtitle = res.data.introduction
res.data.title = res.data.name
res.data.price = [res.data.price / 100]
state.goodsInfo = res.data;
state.goodsSwiper = formatGoodsSwiper(state.goodsInfo.sliderPicUrls);
} else {
// 未找到商品
state.goodsInfo = null;
}
}); });
// TODO 芋艿:下面接口的调整
if (true) {
return
}
// 2. 加载优惠劵信息
const { const {
error, error,
data data
+133 -194
View File
@@ -1,70 +1,56 @@
<template> <template>
<!-- 规格弹窗 --> <!-- 规格弹窗 -->
<su-popup :show="show" round="10" @close="emits('close')"> <su-popup :show="show" round="10" @close="emits('close')">
<!-- SKU 信息 -->
<view class="ss-modal-box bg-white ss-flex-col"> <view class="ss-modal-box bg-white ss-flex-col">
<view class="modal-header ss-flex ss-col-center"> <view class="modal-header ss-flex ss-col-center">
<view class="header-left ss-m-r-30"> <view class="header-left ss-m-r-30">
<image class="sku-image" :src="state.selectedSkuPrice.image || goodsInfo.image" <image class="sku-image" :src="state.selectedSku.picUrl || goodsInfo.picUrl" mode="aspectFill" />
mode="aspectFill"></image>
</view> </view>
<view class="header-right ss-flex-col ss-row-between ss-flex-1"> <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="goods-title ss-line-2">{{ goodsInfo.name }}</view>
<view class="header-right-bottom ss-flex ss-col-center ss-row-between"> <view class="header-right-bottom ss-flex ss-col-center ss-row-between">
<view class="ss-flex"> <view class="ss-flex">
<view v-if="goodsPrice.price > 0" class="price-text"> <view class="price-text">
{{ goodsPrice.price }} {{ fen2yuan( state.selectedSku.price || goodsInfo.price) }}
</view>
<view class="ss-flex">
<view v-if="goodsPrice.price > 0 && goodsPrice.score > 0" class="score-text ss-m-l-4">+
</view>
<image v-if="goodsPrice.score > 0"
:src="sheep.$url.static('/static/img/shop/goods/score1.svg')" class="score-img">
</image>
<view v-if="goodsPrice.score > 0" class="score-text">
{{ goodsPrice.score }}
</view>
</view> </view>
</view> </view>
<view class="stock-text ss-m-l-20"> <view class="stock-text ss-m-l-20">
{{ {{ formatStock('exact', state.selectedSku.stock || goodsInfo.stock) }}
state.selectedSkuPrice.stock
? formatStock(goodsInfo.stock_show_type, state.selectedSkuPrice.stock)
: formatStock(goodsInfo.stock_show_type, goodsInfo.stock)
}}
</view> </view>
</view> </view>
</view> </view>
</view> </view>
<!-- 属性选择 -->
<view class="modal-content ss-flex-1"> <view class="modal-content ss-flex-1">
<scroll-view scroll-y="true" class="modal-content-scroll" @touchmove.stop> <scroll-view scroll-y="true" class="modal-content-scroll" @touchmove.stop>
<view class="sku-item ss-m-b-20" v-for="sku1 in skuList" :key="sku1.id"> <view class="sku-item ss-m-b-20" v-for="property in propertyList" :key="property.id">
<view class="label-text ss-m-b-20">{{ sku1.name }}</view> <view class="label-text ss-m-b-20">{{ property.name }}</view>
<view class="ss-flex ss-col-center ss-flex-wrap"> <view class="ss-flex ss-col-center ss-flex-wrap">
<button class="ss-reset-button spec-btn" v-for="sku2 in sku1.values" :class="[ <button class="ss-reset-button spec-btn" v-for="value in property.values" :class="[
{ {
'ui-BG-Main-Gradient': state.currentSkuArray[sku1.id] == sku2.id, 'ui-BG-Main-Gradient': state.currentPropertyArray[property.id] === value.id,
}, },
{ {
'disabled-btn': sku2.disabled === true, 'disabled-btn': value.disabled === true,
}, },
]" :key="sku2.id" :disabled="sku2.disabled === true" @tap="onSelectSku(sku1.id, sku2.id)"> ]" :key="value.id" :disabled="value.disabled === true" @tap="onSelectSku(property.id, value.id)">
{{ sku2.name }} {{ value.name }}
</button> </button>
</view> </view>
</view> </view>
<view class="buy-num-box ss-flex ss-col-center ss-row-between ss-m-b-40"> <view class="buy-num-box ss-flex ss-col-center ss-row-between ss-m-b-40">
<view class="label-text">购买数量</view> <view class="label-text">购买数量</view>
<su-number-box :min="1" :max="state.selectedSkuPrice.stock" :step="1" <su-number-box :min="1" :max="state.selectedSku.stock" :step="1"
v-model="state.selectedSkuPrice.goods_num" @change="onNumberChange($event)" /> v-model="state.selectedSku.goods_num" @change="onNumberChange($event)" />
</view> </view>
</scroll-view> </scroll-view>
</view> </view>
<!-- 操作区 -->
<view class="modal-footer border-top"> <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"> <view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center">
<button class="ss-reset-button score-btn ui-Shadow-Main" @tap="onBuy">立即兑换</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 add-btn ui-Shadow-Main" @tap="onAddCart">加入购物车</button>
<button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="onBuy">立即购买</button> <button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="onBuy">立即购买</button>
</view> </view>
@@ -74,20 +60,9 @@
</template> </template>
<script setup> <script setup>
import { import { computed, reactive, watch } from 'vue';
computed,
reactive,
watch
} from 'vue';
import sheep from '@/sheep'; import sheep from '@/sheep';
import { import { formatStock, convertProductPropertyList, fen2yuan } from '@/sheep/hooks/useGoods';
formatStock,
formatPrice,
convertProductPropertyList
} from '@/sheep/hooks/useGoods';
import {
isEmpty
} from 'lodash';
const emits = defineEmits(['change', 'addCart', 'buy', 'close']); const emits = defineEmits(['change', 'addCart', 'buy', 'close']);
const props = defineProps({ const props = defineProps({
@@ -98,48 +73,27 @@
show: { show: {
type: Boolean, type: Boolean,
default: false, default: false,
}, }
isScore: {
type: Boolean,
default: false,
},
}); });
const state = reactive({ const state = reactive({
selectedSkuPrice: {}, selectedSku: {}, // 选中的 SKU
currentSkuArray: [], currentPropertyArray: [], // 当前选中的属性,实际是个 Map。key 是 property 编号,value 是 value 编号
}); });
// 输入框改变数量
function onNumberChange(e) {
if (e === 0) return;
if (state.selectedSkuPrice.goods_num === e) return;
state.selectedSkuPrice.goods_num = e;
}
// TODO 芋艿:去除单规格
// 默认单规格
// if (!props.goodsInfo.is_sku) {
// state.selectedSkuPrice = props.goodsInfo.sku_prices[0];
// }
const skuList = convertProductPropertyList(props.goodsInfo.skus); const propertyList = convertProductPropertyList(props.goodsInfo.skus);
// 可选规格 // SKU 列表
const skuPrices = computed(() => { const skuList = computed(() => {
let skuPrices = props.goodsInfo.skus; let skuPrices = props.goodsInfo.skus;
// TODO 芋艿:去除单规格
// if (props.goodsInfo.is_sku) {
// skuPrices.forEach((item) => {
// item.goods_sku_id_arr = item.goods_sku_ids.split(',');
// });
// }
for (let price of skuPrices) { for (let price of skuPrices) {
price.goods_sku_id_arr = price.properties.map((item) => item.valueId).join(',') price.value_id_array = price.properties.map((item) => item.valueId)
} }
return skuPrices; return skuPrices;
}); });
watch( watch(
() => state.selectedSkuPrice, () => state.selectedSku,
(newVal) => { (newVal) => {
emits('change', newVal); emits('change', newVal);
}, { }, {
@@ -148,189 +102,174 @@
}, },
); );
const goodsPrice = computed(() => { // 输入框改变数量
let price, score; function onNumberChange(e) {
if (isEmpty(state.selectedSkuPrice)) { if (e === 0) return;
price = props.goodsInfo.price[0]; if (state.selectedSku.goods_num === e) return;
score = props.goodsInfo.score || 0; state.selectedSku.goods_num = e;
} else { }
price = state.selectedSkuPrice.price;
score = state.selectedSkuPrice.score || 0;
}
return {
price,
score,
};
});
// 加入购物车
function onAddCart() { function onAddCart() {
if (state.selectedSkuPrice.goods_id) { if (state.selectedSku.id <= 0) {
if (state.selectedSkuPrice.stock <= 0) { sheep.$helper.toast('请选择规格');
sheep.$helper.toast('库存不足'); return;
} else {
emits('addCart', state.selectedSkuPrice);
}
} else {
sheep.$helper.toast('请选择规格');
} }
if (state.selectedSku.stock <= 0) {
sheep.$helper.toast('库存不足');
return;
}
emits('addCart', state.selectedSku);
} }
// 立即购买
function onBuy() { function onBuy() {
if (state.selectedSkuPrice.goods_id) { if (state.selectedSku.id <= 0) {
if (state.selectedSkuPrice.stock <= 0) { sheep.$helper.toast('请选择规格');
sheep.$helper.toast('库存不足'); return;
} else { }
emits('buy', state.selectedSkuPrice); if (state.selectedSku.stock <= 0) {
} sheep.$helper.toast('库存不足');
} else { return;
sheep.$helper.toast('请选择规格'); }
} emits('buy', state.selectedSku);
} }
// 改变禁用状态
function changeDisabled(isChecked = false, pid = 0, skuId = 0) {
let newPrice = []; // 所有可以选择的 skuPrice
// 改变禁用状态:计算每个 property 属性值的按钮,是否禁用
function changeDisabled(isChecked = false, propertyId = 0, valueId = 0) {
let newSkus = []; // 所有可以选择的 sku 数组
if (isChecked) { if (isChecked) {
// 选中规格 // 情况一:选中 property
// 当前点击选中规格下的 所有可用 skuPrice // 获得当前点击选中 property 的、所有可用 SKU
for (let price of skuPrices.value) { for (let price of skuList.value) {
if (price.stock <= 0) { if (price.stock <= 0) {
// this.goodsNum 不判断是否大于当前选择数量,在 uni-number-box 判断,并且 取 stock 和 goods_num 的小值
continue; continue;
} }
console.log(price.goods_sku_id_arr, '=======') if (price.value_id_array.indexOf(valueId) >= 0) {
if (price.goods_sku_id_arr.indexOf(skuId.toString()) >= 0) { newSkus.push(price);
newPrice.push(price);
} }
} }
} else { } else {
// 取消选中 // 情况二:取消选中 property
// 当前所选规格下,所有可以选择的 skuPrice // 当前所选 property 下,所有可以选择的 SKU
newPrice = getCanUseSkuPrice(); newSkus = getCanUseSkuList();
} }
// 所有存在并且有库存未选择的规格项 的 子项 id // 所有存在并且有库存未选择的 SKU 的 value 属性值 id
let noChooseSkuIds = []; let noChooseValueIds = [];
for (let price of newPrice) { for (let price of newSkus) {
noChooseSkuIds = noChooseSkuIds.concat(price.goods_sku_id_arr); noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
} }
noChooseValueIds = Array.from(new Set(noChooseValueIds)); // 去重
// 去重
noChooseSkuIds = Array.from(new Set(noChooseSkuIds));
if (isChecked) { if (isChecked) {
// 去除当前选中的规格项 // 去除当前选中的 value 属性值 id
let index = noChooseSkuIds.indexOf(skuId.toString()); let index = noChooseValueIds.indexOf(valueId);
noChooseSkuIds.splice(index, 1); noChooseValueIds.splice(index, 1);
} else { } else {
// 循环去除当前已选择的规格项 // 循环去除当前已选择的 value 属性值 id
state.currentSkuArray.forEach((sku) => { state.currentPropertyArray.forEach((currentPropertyId) => {
if (sku.toString() != '') { if (currentPropertyId.toString() !== '') {
// sku 为空是反选 填充的 return;
let index = noChooseSkuIds.indexOf(sku.toString());
if (index >= 0) {
// sku 存在于 noChooseSkuIds
noChooseSkuIds.splice(index, 1);
}
} }
// currentPropertyId 为空是反选 填充的
let index = noChooseValueIds.indexOf(currentPropertyId);
if (index >= 0) {
// currentPropertyId 存在于 noChooseValueIds
noChooseValueIds.splice(index, 1);
}
}); });
} }
// 当前已选择的规格大类 // 当前已选择的 property 数组
let chooseSkuKey = []; let choosePropertyIds = [];
if (!isChecked) { if (!isChecked) {
// 当前已选择的规格大类 // 当前已选择的 property
state.currentSkuArray.forEach((sku, key) => { state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
if (sku != '') { if (currentPropertyId !== '') {
// sku 为空是反选 填充的 // currentPropertyId 为空是反选 填充的
chooseSkuKey.push(key); choosePropertyIds.push(currentValueId);
} }
}); });
} else { } else {
// 当前点击选择的规格大类 // 当前点击选择的 property
chooseSkuKey = [pid]; choosePropertyIds = [propertyId];
} }
for (let i in skuList) { for (let propertyIndex in propertyList) {
// 当前点击的规格,或者取消选择时候 已选中的规格 不进行处理 // 当前点击的 property、或者取消选择时候已选中的 property 不进行处理
if (chooseSkuKey.indexOf(skuList[i]['id']) >= 0) { if (choosePropertyIds.indexOf(propertyList[propertyIndex]['id']) >= 0) {
continue; continue;
} }
// 如果当前 property id 不存在于有库存的 SKU 中,则禁用
for (let j in skuList[i]['children']) { for (let valueIndex in propertyList[propertyIndex]['values']) {
// 如果当前规格项 id 不存在于有库存的规格项中,则禁用 propertyList[propertyIndex]['values'][valueIndex]['disabled'] =
if (noChooseSkuIds.indexOf(skuList[i]['children'][j]['id'].toString()) >= 0) { noChooseValueIds.indexOf(propertyList[propertyIndex]['values'][valueIndex]['id']) < 0; // true 禁用 or false 不禁用
skuList[i]['children'][j]['disabled'] = false;
} else {
skuList[i]['children'][j]['disabled'] = true;
}
} }
} }
} }
// 当前所选规格下,获取所有有库存的 skuPrice
function getCanUseSkuPrice() {
// debugger
let newPrice = [];
for (let price of skuPrices.value) { // 当前所选属性下,获取所有有库存的 SKU 们
if (price.stock <= 0) { function getCanUseSkuList() {
// || price.stock < this.goodsNum 不判断是否大于当前选择数量,在 uni-number-box 判断,并且 取 stock 和 goods_num 的小值 let newSkus = [];
for (let sku of skuList.value) {
if (sku.stock <= 0) {
continue; continue;
} }
var isOk = true; let isOk = true;
state.currentPropertyArray.forEach((propertyId) => {
state.currentSkuArray.forEach((sku) => { // propertyId 不为空,并且,这个 条 sku 没有被选中,则排除
// sku 不为空,并且,这个 条 skuPrice 没有被选中,则排除 if (propertyId.toString() !== '' && sku.value_id_array.indexOf(propertyId) < 0) {
if (sku.toString() != '' && price.goods_sku_id_arr.indexOf(sku.toString()) < 0) {
isOk = false; isOk = false;
} }
}); });
if (isOk) { if (isOk) {
newPrice.push(price); newSkus.push(sku);
} }
} }
return newSkus;
return newPrice;
} }
// 选择规格 // 选择规格
function onSelectSku(pid, skuId) { function onSelectSku(propertyId, valueId) {
// debugger
// 清空已选择 // 清空已选择
let isChecked = true; // 选中 or 取消选中 let isChecked = true; // 选中 or 取消选中
if (state.currentSkuArray[pid] != undefined && state.currentSkuArray[pid] == skuId) { if (state.currentPropertyArray[propertyId] !== undefined && state.currentPropertyArray[propertyId] === valueId) {
// 点击已被选中的,删除并填充 '' // 点击已被选中的,删除并填充 ''
isChecked = false; isChecked = false;
state.currentSkuArray.splice(pid, 1, ''); state.currentPropertyArray.splice(propertyId, 1, '');
} else { } else {
// 选中 // 选中
state.currentSkuArray[pid] = skuId; state.currentPropertyArray[propertyId] = valueId;
} }
let chooseSkuId = []; // 选中的规格大类 // 选中的 property 大类
state.currentSkuArray.forEach((sku) => { let choosePropertyId = [];
if (sku != '') { state.currentPropertyArray.forEach((currentPropertyId) => {
// sku 为空是反选 填充的 if (currentPropertyId !== '') {
chooseSkuId.push(sku); // currentPropertyId 为空是反选 填充的
choosePropertyId.push(currentPropertyId);
} }
}); });
// 当前所选规格下,所有可以选择的 skuPrice // 当前所选 property 下,所有可以选择的 SKU 们
let newPrice = getCanUseSkuPrice(); let newSkuList = getCanUseSkuList();
// 判断所有规格大类是否选择完成 // 判断所有 property 大类是否选择完成
if (chooseSkuId.length == skuList.length && newPrice.length) { if (choosePropertyId.length === propertyList.length && newSkuList.length) {
newPrice[0].goods_num = state.selectedSkuPrice.goods_num || 1; newSkuList[0].goods_num = state.selectedSku.goods_num || 1;
state.selectedSkuPrice = newPrice[0]; state.selectedSku = newSkuList[0];
} else { } else {
state.selectedSkuPrice = {}; state.selectedSku = {};
} }
// 改变规格项禁用状态 // 改变 property 禁用状态
changeDisabled(isChecked, pid, skuId); changeDisabled(isChecked, propertyId, valueId);
} }
changeDisabled(false); changeDisabled(false);
// TODO 芋艿:待讨论的优化点:1)单规格,要不要默认选中;2)默认要不要选中第一个规格
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>