商品详情:SKU 的选择完善

pull/26/head^2
YunaiV 2023-12-10 11:57:24 +08:00
parent 2ee3c77249
commit e3264329e7
3 changed files with 191 additions and 276 deletions

View File

@ -1,10 +1,10 @@
<template>
<detail-cell v-if="skus.length > 0" label="选择" :value="value"></detail-cell>
<!-- SKU 选择的提示框 -->
<detail-cell label="选择" :value="value" />
</template>
<script setup>
import { computed } from 'vue';
import detailCell from './detail-cell.vue';
const props = defineProps({
@ -14,22 +14,18 @@
return [];
},
},
skus: {
type: Array,
default() {
return [];
},
},
sku: {
type: Object
}
});
const value = computed(() => {
let str = '';
if (props.modelValue.length > 0) {
props.modelValue.forEach((item, index) => {
str += props.skus[index].name + ':' + item + ' ';
});
} else {
str = '请选择商品规格';
if (!props.sku.id) {
return '请选择商品规格';
}
let str = '';
props.sku.properties.forEach(property => {
str += property.propertyName + ':' + property.valueName + ' ';
});
return str;
});
</script>

View File

@ -12,24 +12,25 @@
<block v-else>
<view class="detail-swiper-selector">
<!-- 商品轮播图 -->
<su-swiper class="ss-m-b-14" isPreview :list="state.goodsSwiper" dotStyle="tag" imageMode="widthFix"
dotCur="bg-mask-40" :seizeHeight="750" />
<su-swiper class="ss-m-b-14" isPreview :list="formatGoodsSwiper(state.goodsInfo.sliderPicUrls)"
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="ss-flex ss-row-between ss-col-center ss-m-b-26">
<view class="price-box ss-flex ss-col-bottom">
<view class="price-text ss-m-r-16">
{{ state.selectedSkuPrice.price || formatPrice(state.goodsInfo.price) }}
{{ fen2yuan(state.selectedSku.price || state.goodsInfo.price) }}
</view>
<view class="origin-price-text" v-if="state.goodsInfo.original_price > 0">
{{ state.selectedSkuPrice.original_price || state.goodsInfo.original_price }}
<view class="origin-price-text" v-if="state.goodsInfo.marketPrice > 0">
{{ fen2yuan(state.selectedSku.marketPrice || state.goodsInfo.marketPrice) }}
</view>
</view>
<view class="sales-text">
{{ formatSales(state.goodsInfo.sales_show_type, state.goodsInfo.sales) }}
{{ formatSales('exact', state.goodsInfo.salesCount) }}
</view>
</view>
<!-- TODO 芋艿promos 未写 -->
<view class="discounts-box ss-flex ss-row-between ss-m-b-28">
<div class="tag-content">
<view class="tag-box ss-flex">
@ -40,27 +41,29 @@
</view>
</div>
<!-- TODO 芋艿couponInfo 优惠劵 -->
<view class="get-coupon-box ss-flex ss-col-center ss-m-l-20" @tap="state.showModel = true"
v-if="state.couponInfo.length">
<view class="discounts-title ss-m-r-8">领券</view>
<text class="cicon-forward"></text>
</view>
</view>
<view class="title-text ss-line-2 ss-m-b-6">{{ state.goodsInfo.title }}</view>
<view class="subtitle-text ss-line-1">{{ state.goodsInfo.subtitle }}</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.introduction }}</view>
</view>
<!-- 功能卡片 -->
<view class="detail-cell-card detail-card ss-flex-col">
<detail-cell-sku v-model="state.selectedSkuPrice.goods_sku_text" :skus="state.goodsInfo.skus"
@tap="state.showSelectSku = true" />
<detail-cell-sku v-model="state.selectedSku.goods_sku_text" :sku="state.selectedSku"
@tap="state.showSelectSku = true" />
<!-- TODO 芋艿可能暂时不考虑使用 -->
<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" />
</view>
<!-- 规格与数量弹框 -->
<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>
<!-- 评价 -->
@ -73,7 +76,6 @@
<!-- 详情tabbar -->
<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">
<button class="ss-reset-button add-btn ui-Shadow-Main" @tap="state.showSelectSku = true">
加入购物车
@ -87,29 +89,19 @@
</view>
</detail-tabbar>
<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"
@close="state.showActivityModel = false" />
@close="state.showActivityModel = false" />
</block>
</s-layout>
</view>
</template>
<script setup>
import {
reactive,
computed
} from 'vue';
import {
onLoad,
onPageScroll
} from '@dcloudio/uni-app';
import { reactive, computed } from 'vue';
import { onLoad, onPageScroll } from '@dcloudio/uni-app';
import sheep from '@/sheep';
import {
formatSales,
formatGoodsSwiper,
formatPrice
} from '@/sheep/hooks/useGoods';
import { formatSales, formatGoodsSwiper, fen2yuan, } from '@/sheep/hooks/useGoods';
import detailNavbar from './components/detail/detail-navbar.vue';
import detailCellSku from './components/detail/detail-cell-sku.vue';
import detailCellService from './components/detail/detail-cell-service.vue';
@ -130,29 +122,27 @@
const state = reactive({
goodsId: 0,
skeletonLoading: true,
goodsInfo: {},
showSelectSku: false,
goodsSwiper: [],
selectedSkuPrice: {},
skeletonLoading: true, // SPU
goodsInfo: {}, // SPU
showSelectSku: false, // SKU
selectedSku: {}, // SKU
showModel: false,
total: 0,
couponInfo: [],
showActivityModel: false,
activityInfo: [],
});
//
// TODO
function onSkuChange(e) {
state.selectedSkuPrice = e;
state.selectedSku = e;
}
//
// TODO
function onAddCart(e) {
sheep.$store('cart').add(e);
}
//
// TODO
function onBuy(e) {
sheep.$router.go('/pages/order/confirm', {
data: JSON.stringify({
@ -165,13 +155,14 @@
}),
});
}
//
// TODO
function onActivity() {
state.activityInfo = state.goodsInfo.promos;
state.showActivityModel = true;
}
//
// TODO
async function onGet(id) {
const {
error,
@ -187,10 +178,11 @@
}
}
// TODO
const shareInfo = computed(() => {
if (isEmpty(state.goodsInfo)) return {};
return sheep.$platform.share.getShareInfo({
title: state.goodsInfo.title,
title: state.goodsInfo.name,
image: sheep.$url.cdn(state.goodsInfo.image),
desc: state.goodsInfo.subtitle,
params: {
@ -199,7 +191,7 @@
},
}, {
type: 'goods', //
title: state.goodsInfo.title, //
title: state.goodsInfo.name, //
image: sheep.$url.cdn(state.goodsInfo.image), //
price: state.goodsInfo.price[0], //
original_price: state.goodsInfo.original_price, //
@ -207,41 +199,29 @@
});
onLoad(async (options) => {
console.log('页面被访问')
//
if (!options.id) {
state.goodsInfo = null;
return;
}
state.goodsId = options.id;
//
// 1.
sheep.$api.goods.detail(state.goodsId).then((res) => {
//
// let arr = [];
// res.skus = res.data.skus.map(item => {
// arr = [...arr, ...item.properties];
// 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, '');
//
if (res.code !== 0 || !res.data) {
state.goodsInfo = null;
return;
}
//
state.skeletonLoading = false;
if (res.code === 0) {
//
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;
}
state.goodsInfo = res.data;
});
// TODO
if (true) {
return
}
// 2.
const {
error,
data

View File

@ -1,70 +1,56 @@
<template>
<!-- 规格弹窗 -->
<su-popup :show="show" round="10" @close="emits('close')">
<!-- SKU 信息 -->
<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="state.selectedSkuPrice.image || goodsInfo.image"
mode="aspectFill"></image>
<image class="sku-image" :src="state.selectedSku.picUrl || goodsInfo.picUrl" mode="aspectFill" />
</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="goods-title ss-line-2">{{ goodsInfo.name }}</view>
<view class="header-right-bottom ss-flex ss-col-center ss-row-between">
<view class="ss-flex">
<view v-if="goodsPrice.price > 0" class="price-text">
{{ goodsPrice.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 class="price-text">
{{ fen2yuan( state.selectedSku.price || goodsInfo.price) }}
</view>
</view>
<view class="stock-text ss-m-l-20">
{{
state.selectedSkuPrice.stock
? formatStock(goodsInfo.stock_show_type, state.selectedSkuPrice.stock)
: formatStock(goodsInfo.stock_show_type, goodsInfo.stock)
}}
{{ formatStock('exact', state.selectedSku.stock || goodsInfo.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="sku1 in skuList" :key="sku1.id">
<view class="label-text ss-m-b-20">{{ sku1.name }}</view>
<view class="sku-item ss-m-b-20" v-for="property in propertyList" :key="property.id">
<view class="label-text ss-m-b-20">{{ property.name }}</view>
<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)">
{{ sku2.name }}
]" :key="value.id" :disabled="value.disabled === true" @tap="onSelectSku(property.id, value.id)">
{{ value.name }}
</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="state.selectedSkuPrice.stock" :step="1"
v-model="state.selectedSkuPrice.goods_num" @change="onNumberChange($event)" />
<su-number-box :min="1" :max="state.selectedSku.stock" :step="1"
v-model="state.selectedSku.goods_num" @change="onNumberChange($event)" />
</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="onBuy"></button>
</view>
<view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center" v-else>
<view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center">
<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>
</view>
@ -74,20 +60,9 @@
</template>
<script setup>
import {
computed,
reactive,
watch
} from 'vue';
import { computed, reactive, watch } from 'vue';
import sheep from '@/sheep';
import {
formatStock,
formatPrice,
convertProductPropertyList
} from '@/sheep/hooks/useGoods';
import {
isEmpty
} from 'lodash';
import { formatStock, convertProductPropertyList, fen2yuan } from '@/sheep/hooks/useGoods';
const emits = defineEmits(['change', 'addCart', 'buy', 'close']);
const props = defineProps({
@ -98,48 +73,27 @@
show: {
type: Boolean,
default: false,
},
isScore: {
type: Boolean,
default: false,
},
}
});
const state = reactive({
selectedSkuPrice: {},
currentSkuArray: [],
selectedSku: {}, // SKU
currentPropertyArray: [], // Mapkey 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);
//
const skuPrices = computed(() => {
// SKU
const skuList = computed(() => {
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) {
price.goods_sku_id_arr = price.properties.map((item) => item.valueId).join(',')
price.value_id_array = price.properties.map((item) => item.valueId)
}
return skuPrices;
});
watch(
() => state.selectedSkuPrice,
() => state.selectedSku,
(newVal) => {
emits('change', newVal);
}, {
@ -148,189 +102,174 @@
},
);
const goodsPrice = computed(() => {
let price, score;
if (isEmpty(state.selectedSkuPrice)) {
price = props.goodsInfo.price[0];
score = props.goodsInfo.score || 0;
} else {
price = state.selectedSkuPrice.price;
score = state.selectedSkuPrice.score || 0;
}
return {
price,
score,
};
});
//
function onNumberChange(e) {
if (e === 0) return;
if (state.selectedSku.goods_num === e) return;
state.selectedSku.goods_num = e;
}
//
function onAddCart() {
if (state.selectedSkuPrice.goods_id) {
if (state.selectedSkuPrice.stock <= 0) {
sheep.$helper.toast('库存不足');
} else {
emits('addCart', state.selectedSkuPrice);
}
} else {
sheep.$helper.toast('请选择规格');
if (state.selectedSku.id <= 0) {
sheep.$helper.toast('请选择规格');
return;
}
if (state.selectedSku.stock <= 0) {
sheep.$helper.toast('库存不足');
return;
}
emits('addCart', state.selectedSku);
}
//
function onBuy() {
if (state.selectedSkuPrice.goods_id) {
if (state.selectedSkuPrice.stock <= 0) {
sheep.$helper.toast('库存不足');
} else {
emits('buy', state.selectedSkuPrice);
}
} else {
sheep.$helper.toast('请选择规格');
}
if (state.selectedSku.id <= 0) {
sheep.$helper.toast('请选择规格');
return;
}
if (state.selectedSku.stock <= 0) {
sheep.$helper.toast('库存不足');
return;
}
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) {
//
// skuPrice
for (let price of skuPrices.value) {
// property
// property SKU
for (let price of skuList.value) {
if (price.stock <= 0) {
// this.goodsNum uni-number-box stock goods_num
continue;
}
console.log(price.goods_sku_id_arr, '=======')
if (price.goods_sku_id_arr.indexOf(skuId.toString()) >= 0) {
newPrice.push(price);
if (price.value_id_array.indexOf(valueId) >= 0) {
newSkus.push(price);
}
}
} else {
//
// skuPrice
newPrice = getCanUseSkuPrice();
// property
// property SKU
newSkus = getCanUseSkuList();
}
// id
let noChooseSkuIds = [];
for (let price of newPrice) {
noChooseSkuIds = noChooseSkuIds.concat(price.goods_sku_id_arr);
// SKU value id
let noChooseValueIds = [];
for (let price of newSkus) {
noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
}
//
noChooseSkuIds = Array.from(new Set(noChooseSkuIds));
noChooseValueIds = Array.from(new Set(noChooseValueIds)); //
if (isChecked) {
//
let index = noChooseSkuIds.indexOf(skuId.toString());
noChooseSkuIds.splice(index, 1);
// value id
let index = noChooseValueIds.indexOf(valueId);
noChooseValueIds.splice(index, 1);
} else {
//
state.currentSkuArray.forEach((sku) => {
if (sku.toString() != '') {
// sku
let index = noChooseSkuIds.indexOf(sku.toString());
if (index >= 0) {
// sku noChooseSkuIds
noChooseSkuIds.splice(index, 1);
}
// value id
state.currentPropertyArray.forEach((currentPropertyId) => {
if (currentPropertyId.toString() !== '') {
return;
}
// currentPropertyId
let index = noChooseValueIds.indexOf(currentPropertyId);
if (index >= 0) {
// currentPropertyId noChooseValueIds
noChooseValueIds.splice(index, 1);
}
});
}
//
let chooseSkuKey = [];
// property
let choosePropertyIds = [];
if (!isChecked) {
//
state.currentSkuArray.forEach((sku, key) => {
if (sku != '') {
// sku
chooseSkuKey.push(key);
// property
state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
if (currentPropertyId !== '') {
// currentPropertyId
choosePropertyIds.push(currentValueId);
}
});
} else {
//
chooseSkuKey = [pid];
// property
choosePropertyIds = [propertyId];
}
for (let i in skuList) {
//
if (chooseSkuKey.indexOf(skuList[i]['id']) >= 0) {
for (let propertyIndex in propertyList) {
// property property
if (choosePropertyIds.indexOf(propertyList[propertyIndex]['id']) >= 0) {
continue;
}
for (let j in skuList[i]['children']) {
// id
if (noChooseSkuIds.indexOf(skuList[i]['children'][j]['id'].toString()) >= 0) {
skuList[i]['children'][j]['disabled'] = false;
} else {
skuList[i]['children'][j]['disabled'] = true;
}
// property id SKU
for (let valueIndex in propertyList[propertyIndex]['values']) {
propertyList[propertyIndex]['values'][valueIndex]['disabled'] =
noChooseValueIds.indexOf(propertyList[propertyIndex]['values'][valueIndex]['id']) < 0; // true or false
}
}
}
// skuPrice
function getCanUseSkuPrice() {
// debugger
let newPrice = [];
for (let price of skuPrices.value) {
if (price.stock <= 0) {
// || price.stock < this.goodsNum uni-number-box stock goods_num
// SKU
function getCanUseSkuList() {
let newSkus = [];
for (let sku of skuList.value) {
if (sku.stock <= 0) {
continue;
}
var isOk = true;
state.currentSkuArray.forEach((sku) => {
// sku skuPrice ,
if (sku.toString() != '' && price.goods_sku_id_arr.indexOf(sku.toString()) < 0) {
let isOk = true;
state.currentPropertyArray.forEach((propertyId) => {
// propertyId sku
if (propertyId.toString() !== '' && sku.value_id_array.indexOf(propertyId) < 0) {
isOk = false;
}
});
if (isOk) {
newPrice.push(price);
newSkus.push(sku);
}
}
return newPrice;
return newSkus;
}
//
function onSelectSku(pid, skuId) {
// debugger
function onSelectSku(propertyId, valueId) {
//
let isChecked = true; // or
if (state.currentSkuArray[pid] != undefined && state.currentSkuArray[pid] == skuId) {
if (state.currentPropertyArray[propertyId] !== undefined && state.currentPropertyArray[propertyId] === valueId) {
// ''
isChecked = false;
state.currentSkuArray.splice(pid, 1, '');
state.currentPropertyArray.splice(propertyId, 1, '');
} else {
//
state.currentSkuArray[pid] = skuId;
state.currentPropertyArray[propertyId] = valueId;
}
let chooseSkuId = []; //
state.currentSkuArray.forEach((sku) => {
if (sku != '') {
// sku
chooseSkuId.push(sku);
// property
let choosePropertyId = [];
state.currentPropertyArray.forEach((currentPropertyId) => {
if (currentPropertyId !== '') {
// currentPropertyId
choosePropertyId.push(currentPropertyId);
}
});
// skuPrice
let newPrice = getCanUseSkuPrice();
// property SKU
let newSkuList = getCanUseSkuList();
//
if (chooseSkuId.length == skuList.length && newPrice.length) {
newPrice[0].goods_num = state.selectedSkuPrice.goods_num || 1;
state.selectedSkuPrice = newPrice[0];
// property
if (choosePropertyId.length === propertyList.length && newSkuList.length) {
newSkuList[0].goods_num = state.selectedSku.goods_num || 1;
state.selectedSku = newSkuList[0];
} else {
state.selectedSkuPrice = {};
state.selectedSku = {};
}
//
changeDisabled(isChecked, pid, skuId);
// property
changeDisabled(isChecked, propertyId, valueId);
}
changeDisabled(false);
// TODO 12
</script>
<style lang="scss" scoped>