Files
kangyuan-mobile/sheep/components/s-select-aiSku/s-select-aiSku.vue
T

645 lines
18 KiB
Vue

<template>
<!-- 规格弹窗 -->
<su-popup :show="show" round="10" @close="emits('close')" backgroundColor="#ffffff">
<!-- SKU 信息 -->
<view class="ss-modal-box bg17 ss-flex-col">
<view class="modal-header ss-flex ss-col-center">
<uni-icons class="colse" type="closeempty" color="" size="20" @click="emits('close')" />
<view class="header-left">
<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.name }}</view>
<view class="stock-text">
{{ formatStock('exact', state.selectedSku.stock || 0) }}
</view>
<view class="header-right-bottom ss-flex ss-col-center ss-row-between">
<view class="ss-flex">
<view class="price-text ui-TC-Main">
<span class="f500">¥</span>
<span class="priceNum">
{{
fen2yuan(
state.selectedSku.discountedPrice
? state.selectedSku.discountedPrice
: state.selectedSku.promotionPrice ||
state.selectedSku.price ||
goodsInfo.price,
)
}}
</span>
<span
style="
color: 20rpx;
color: #777777;
text-decoration: line-through;
font-size: 24rpx;
margin-left: 10rpx;
"
>¥{{
fen2yuan(
state.selectedSku.discountedPrice
? state.selectedSku.price
: state.selectedSku.marketPrice,
)
}}</span
>
<text v-if="state.selectedSku.promotionType > 0">
<text class="iconBox" v-if="state.selectedSku.promotionType === 4">
限时优惠
</text>
<text class="iconBox" v-else-if="state.selectedSku.promotionType === 6">
会员价
</text>
<text class="origin-price-text">
{{ fen2yuan(state.selectedSku.price) }}
</text>
</text>
</view>
</view>
</view>
</view>
</view>
<!-- 属性选择 -->
<view class="modal-content ss-flex-1">
<scroll-view scroll-y="true" class="modal-content-scroll" @touchmove.stop>
<view
class="buy-num-box ss-flex ss-col-center ss-row-between"
style="margin-bottom: 46rpx"
>
<view class="label-text">购买数量</view>
<su-number-box
:min="
state.selectedSku.minSalesCount && props.minSalesCountShow
? state.selectedSku.minSalesCount
: 1
"
:max="state.selectedSku.stock"
:step="1"
v-model="state.selectedSku.goods_num"
@change="onNumberChange($event)"
background="#343434"
color="#ffffff"
/>
</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 bg21 size28"
v-for="value in property.values"
:class="[
{
border3: state.currentPropertyArray[property.id] === value.id,
},
{
border21: state.currentPropertyArray[property.id] !== value.id,
},
{
c3: state.currentPropertyArray[property.id] === value.id,
},
{
bg21: state.currentPropertyArray[property.id] === value.id,
},
{
'disabled-btn': value.disabled === true,
},
]"
style="min-width: 80rpx"
:key="value.id"
:disabled="value.disabled === true"
@tap="onSelectSku(property.id, value.id)"
>
{{ value.name }}
</button>
</view>
</view>
</scroll-view>
</view>
<!-- 操作区 -->
<view class="modal-footer">
<view class="p-x-40">
<view class="ai-button2" @click="onAddCart">确定</view>
</view>
</view>
</view>
</su-popup>
</template>
<script setup>
import { computed, reactive, watch, onMounted } from 'vue';
import sheep from '@/sheep';
import { formatStock, convertProductPropertyList, fen2yuan } from '@/sheep/hooks/useGoods';
const emits = defineEmits(['change', 'addCart', 'buy', 'close']);
const props = defineProps({
goodsInfo: {
type: Object,
default() {},
},
show: {
type: Boolean,
default: false,
},
activeSku: {
//选中的sku的规格信息
type: Object,
default() {},
},
selectSku: {
// 购物车已经选中的sku
type: Array,
default() {
return [];
},
},
activeInfo: {
// 购物车已经选中的sku信息
type: Object,
default() {
return {};
},
},
minSalesCountShow: {
type: Boolean,
default: false,
},
});
const state = reactive({
selectedSku: {}, // 选中的 SKU
currentPropertyArray: {}, // 当前选中的属性,实际是个 Map。key 是 property 编号,value 是 value 编号
});
// 属性列表
const propertyList = convertProductPropertyList(props.goodsInfo.skus);
// SKU 列表
const skuList = computed(() => {
let skuPrices = props.goodsInfo.skus;
for (let price of skuPrices) {
price.value_id_array = price.properties.map((item) => item.valueId);
}
return skuPrices;
});
watch(
() => state.selectedSku,
(newVal) => {
emits('change', newVal);
},
{
immediate: true, // 立即执行
deep: true, // 深度监听
},
);
watch(
() => props.activeSku,
(newVal) => {
if (props.activeSku?.properties && props.activeSku?.properties.length) {
props.activeSku.properties.forEach((item) => {
onSelectSku(item.propertyId, item.valueId, props.activeSku.goods_num);
});
}
},
{
immediate: true,
deep: true,
},
);
// 输入框改变数量
function onNumberChange(e) {
if (e === 0) return;
if (state.selectedSku.goods_num === e) return;
state.selectedSku.goods_num = e;
}
// 加入购物车
function onAddCart() {
let length = Object.keys(state.currentPropertyArray).length;
let length2 = propertyList.length;
if (length != length2) {
sheep.$helper.toast('请选择规格');
return;
}
if (state.selectedSku.stock <= 0) {
sheep.$helper.toast('库存不足');
return;
}
emits('addCart', {
...state.selectedSku,
count: state.selectedSku.goods_num,
});
}
// 立即购买
function onBuy() {
let length = Object.keys(state.currentPropertyArray).length;
let length2 = propertyList.length;
if (length != length2) {
sheep.$helper.toast('请选择规格');
return;
}
if (state.selectedSku.stock <= 0) {
sheep.$helper.toast('库存不足');
return;
}
emits('buy', state.selectedSku);
}
// 当前所选属性下,获取所有有库存的 SKU 们
function getCanUseSkuList() {
let newSkus = [];
for (let sku of skuList.value) {
if (sku.stock <= 0) {
continue;
}
let isOk = true;
Object.entries(state.currentPropertyArray).forEach(([propertyId, valueId]) => {
// valueId 不为空,并且,这个 条 sku 没有被选中,则排除
if (valueId.toString() !== '' && sku.value_id_array.indexOf(valueId) < 0) {
isOk = false;
}
});
if (isOk) {
newSkus.push(sku);
}
}
return newSkus;
}
// 选择规格
function onSelectSku(propertyId, valueId, goods_num = 0) {
// 从购物车选中的sku里边去掉选中的sku对象
let selectArr = JSON.parse(JSON.stringify(props.selectSku || []));
if (selectArr.length) {
let index = selectArr.findIndex((item) => item.id === props.activeSku.id);
if (index !== -1) {
selectArr.splice(index, 1);
}
}
// 清空已选择
let isChecked = true; // 选中 or 取消选中
if (
state.currentPropertyArray[propertyId] !== undefined &&
state.currentPropertyArray[propertyId] === valueId
) {
// 点击已被选中的,删除并填充 ''
// isChecked = false;
// state.currentPropertyArray.splice(propertyId, 1, '');
} else {
//#region 先判断列表里边是否有选中
let length = Object.keys(state.currentPropertyArray).length;
let length2 = propertyList.length - 1;
// length长度为skus.length - 1时判断是否已经选中
if ((length === length2 || length === length2 + 1) && selectArr && selectArr.length) {
try {
selectArr.forEach((item) => {
if (item.properties && item.properties.length) {
let obj = {
...state.currentPropertyArray,
[propertyId]: valueId,
};
let b = true;
item.properties.forEach((item2) => {
if (obj[item2.propertyId] !== item2.valueId) {
b = false;
}
});
if (b) {
throw new Error();
}
}
});
} catch (error) {
uni.showToast({
title: '该规格的商品已存在',
duration: 1000,
icon: 'none',
});
return;
}
}
//#endregion
// 选中
state.currentPropertyArray[propertyId] = valueId;
}
// 选中的 property 大类
let choosePropertyId = [];
Object.entries(state.currentPropertyArray).forEach(([propertyId, currentPropertyId]) => {
if (currentPropertyId !== '') {
// currentPropertyId 为空是反选 填充的
choosePropertyId.push(currentPropertyId);
}
});
// 当前所选 property 下,所有可以选择的 SKU 们
let newSkuList = getCanUseSkuList();
// 判断所有 property 大类是否选择完成
if (choosePropertyId.length === propertyList.length && newSkuList.length) {
newSkuList[0].goods_num =
goods_num ||
state.selectedSku.goods_num ||
(props.minSalesCountShow && getGoodsNum(newSkuList[0].id)
? getGoodsNum(newSkuList[0].id)
: 1);
state.selectedSku = newSkuList[0];
} else {
state.selectedSku = {};
}
// 改变 property 禁用状态
// changeDisabled(isChecked, propertyId, valueId);
}
const getGoodsNum = (id) => {
let num = 0;
if (props.goodsInfo.skus.length) {
props.goodsInfo.skus.forEach((item) => {
if (item.id == id) {
num = item.minSalesCount || 1;
}
});
}
return num;
};
// changeDisabled(false);
onMounted(() => {
if (
props.activeSku &&
Array.isArray(props.activeSku.properties) &&
props.activeSku.properties.length
) {
return;
}
const skus = Array.isArray(props.goodsInfo?.skus) ? props.goodsInfo.skus : [];
if (skus.length === 1) {
const sku = skus[0];
if (Array.isArray(sku.properties)) {
sku.properties.forEach((p) => {
state.currentPropertyArray[p.propertyId] = p.valueId;
});
}
sku.goods_num = props.minSalesCountShow && sku.minSalesCount ? sku.minSalesCount : 1;
state.selectedSku = sku;
return;
}
const allSingle =
Array.isArray(propertyList) &&
propertyList.length &&
propertyList.every((p) => Array.isArray(p.values) && p.values.length) &&
propertyList.length == 1;
if (allSingle) {
try {
props.goodsInfo?.skus.forEach((item) => {
if (item.stock) {
state.currentPropertyArray[item.properties[0].propertyId] = item.properties[0].valueId;
throw new Error();
}
});
} catch (error) {}
// propertyList.forEach((p) => {
// console.log(p)
// const v = p.values[0];
// state.currentPropertyArray[p.id] = v.id;
// });
const list = getCanUseSkuList();
if (list.length) {
list[0].goods_num =
state.selectedSku.goods_num ||
(props.minSalesCountShow && getGoodsNum(list[0].id)
? getGoodsNum(list[0].id)
: 1);
state.selectedSku = list[0];
if (propertyList.length && propertyList[0].values.length) {
const inStockList = skuList.value.filter((sku) => Number(sku?.stock) > 0);
propertyList[0].values.forEach((e) => {
const enabled = inStockList.some((sku) => sku.value_id_array.includes(e.id));
e.disabled = !enabled;
});
}
}
}
});
</script>
<style lang="scss" scoped>
// 购买
.buy-box {
padding: 0rpx 32rpx;
box-sizing: border-box;
.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;
min-height: 700rpx;
.modal-header {
position: relative;
padding: 40rpx 32rpx 40rpx;
padding-bottom: 0;
.colse {
position: absolute;
top: 42rpx;
right: 22rpx;
}
.sku-image {
width: 200rpx;
height: 200rpx;
border-radius: 10rpx;
margin-right: 12rpx;
}
.header-right {
height: 200rpx;
}
.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;
padding-right: 40rpx;
}
.score-img {
width: 36rpx;
height: 36rpx;
margin: 0 4rpx;
}
.score-text {
font-size: 30rpx;
font-weight: 500;
color: $red;
font-family: OPPOSANS;
}
.price-text {
.priceNum {
font-size: 36rpx;
font-weight: 600;
}
// &::before {
// content: '¥';
// font-size: 30rpx;
// font-weight: 500;
// color: $red;
// }
}
.stock-text {
font-size: 24rpx;
color: #999999;
}
}
.modal-content {
padding: 0 32rpx;
.modal-content-scroll {
max-height: 600rpx;
.label-text {
font-size: 28rpx;
font-weight: 500;
}
.buy-num-box {
// height: 100rpx;
margin-top: 30rpx;
margin-bottom: 36rpx;
}
.spec-btn {
height: 54rpx;
padding: 0 10rpx;
box-sizing: border-box;
border-radius: 12rpx;
background-color: #f2f2f2;
font-weight: 500;
font-size: 24rpx;
line-height: 54rpx;
text-align: center;
margin-right: 10rpx;
margin-bottom: 10rpx;
border: 2px solid #f2f2f2;
}
.active-btn {
border: 2rpx solid #fa7e31;
background: #ffefe5;
color: #fa7e31;
}
// .spec-btn {
// height: 60rpx;
// min-width: 100rpx;
// padding: 0 30rpx;
// background: #f4f4f4;
// border-radius: 30rpx;
// color: #434343;
// font-size: 26rpx;
// margin-right: 10rpx;
// margin-bottom: 10rpx;
// }
.disabled-btn {
font-weight: 400;
color: #c6c6c6;
background: #f8f8f8;
}
}
}
}
.iconBox {
width: fit-content;
height: fit-content;
padding: 2rpx 10rpx;
background-color: rgb(255, 242, 241);
color: #ff2621;
font-size: 24rpx;
margin-left: 5rpx;
}
.origin-price-text {
font-size: 26rpx;
font-weight: 400;
text-decoration: line-through;
color: $gray-c;
font-family: OPPOSANS;
&::before {
content: '¥';
}
}
.modal-footer {
padding-bottom: calc(env(safe-area-inset-bottom) + 140rpx);
box-sizing: border-box;
}
.screen_btn {
width: calc(100% - 54rpx);
height: 82rpx;
border-radius: 12rpx;
background-color: #ffefe5;
color: #ff6403;
line-height: 82rpx;
}
.screen_clear {
margin-right: 18rpx;
}
.screen_confirm {
background: linear-gradient(131deg, #f7772e 0%, #ee0a24 100%);
color: #ffffff;
}
</style>