Files
kangyuan-mobile/components/orderItem/ShipmentPopup.vue
T
2026-08-06 13:21:17 +08:00

519 lines
13 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<uni-popup ref="popupRef" :type="popupType" background-color="#fff">
<view class="shipment-popup" :class="`shipment-popup--${popupType}`" @tap.stop>
<view class="shipment-popup__header">
<view>
<view class="shipment-popup__title">{{ titleText }}</view>
<view class="shipment-popup__order-no">订单号:{{ orderNo || '-' }}</view>
<view class="shipment-popup__order-time">下单时间:{{ orderTime || '-' }}</view>
</view>
<view class="shipment-popup__close" @tap.stop="handleClose">×</view>
</view>
<view class="shipment-popup__desc">{{ descText }}</view>
<scroll-view scroll-y class="shipment-popup__content">
<view v-if="renderItems.length === 0" class="shipment-popup__empty">
{{ emptyText }}
</view>
<view
v-else
class="shipment-popup__item"
v-for="(item, index) in renderItems"
:key="item.itemKey || index"
>
<image class="shipment-popup__item-image" :src="item.picUrl" mode="aspectFill" />
<view class="shipment-popup__item-main">
<view class="shipment-popup__item-name lineTwo">{{ item.spuName }}</view>
<view class="shipment-popup__item-sku">{{ item.skuText || '-' }}</view>
<view class="shipment-popup__item-footer flex-center ju-between">
<view class="shipment-popup__item-count">最多可选 x{{ item.maxCount }}</view>
<su-number-box
v-model="item.selectedCount"
:min="0"
:max="item.maxCount"
:step="1"
background="#F6F7FB"
color="#333333"
@change="onCountChange(index, $event)"
/>
</view>
</view>
</view>
</scroll-view>
<view class="shipment-popup__actions">
<view class="shipment-popup__btn shipment-popup__btn--ghost" @tap.stop="handleClose">
取消
</view>
<view class="shipment-popup__btn shipment-popup__btn--primary" @tap.stop="handleSubmit">
{{ submitText }}
</view>
</view>
</view>
</uni-popup>
</template>
<script setup>
import { computed, ref } from 'vue';
const props = defineProps({
mode: {
type: String,
default: 'delay',
},
popupType: {
type: String,
default: 'bottom',
},
orderNo: {
type: String,
default: '',
},
orderTime: {
type: String,
default: '',
},
orderData: {
type: Object,
default: () => ({}),
},
delayRecords: {
type: Array,
default: () => [],
},
});
const emit = defineEmits(['close', 'submit']);
const popupRef = ref();
const innerItems = ref([]);
const titleText = computed(() => (props.mode === 'now' ? '立即发货' : '延迟发货'));
const submitText = computed(() => (props.mode === 'now' ? '确认立即发货' : '确认延迟发货'));
const descText = computed(() =>
props.mode === 'now' ? '请选择当前延迟商品及立即发货数量' : '请选择当前商品及延迟发货数量',
);
const emptyText = computed(() =>
props.mode === 'now' ? '暂无可立即发货的商品' : '暂无可延迟发货的商品',
);
const getFirstValidValue = (...values) =>
values.find((value) => value !== undefined && value !== null && String(value).trim() !== '') ?? '';
const hasValue = (value) => value !== undefined && value !== null && String(value).trim() !== '';
const formatSkuPropertyItem = (property) => {
const value = getFirstValidValue(
property?.valueName,
property?.value,
property?.specValue,
property?.attrValue,
property?.optionValue,
property?.propertyValue,
typeof property === 'string' ? property : '',
);
const name = getFirstValidValue(
property?.name,
property?.propertyName,
property?.specName,
property?.attrName,
property?.key,
property?.label,
);
if (name && value) {
return `${name} ${value}`;
}
return value || name || '';
};
const formatSkuProperties = (properties) => {
if (typeof properties === 'string' && properties.trim()) {
return properties.trim();
}
const list = Array.isArray(properties) ? properties : [];
if (!list.length) {
return '';
}
return list
.map((property) => formatSkuPropertyItem(property))
.filter(Boolean)
.join(' ');
};
const resolveOrderItemId = (item = {}) =>
getFirstValidValue(item?.id, item?.orderItemId, item?.itemId, item?.orderGoodsId, item?.goodsId, item?.skuId);
const resolveDelayItemCount = (item = {}) => {
const candidates = [
item?.count,
item?.remainingCount,
item?.availableCount,
item?.canCount,
item?.maxCount,
];
const matched = candidates
.map((value) => Number(value))
.find((value) => Number.isFinite(value) && value > 0);
return matched || 0;
};
const resolveDelayRecordId = (item = {}) =>
getFirstValidValue(item?.delayRecordId, item?.recordId, item?.id);
const resolveDelayRecordCount = (item = {}) => {
const candidates = [
item?.count,
item?.delayCount,
item?.recordCount,
item?.remainingCount,
item?.availableCount,
item?.canCount,
item?.maxCount,
];
const matched = candidates
.map((value) => Number(value))
.find((value) => Number.isFinite(value) && value > 0);
return matched || 0;
};
const createDelayShipmentItem = (item, parentItem = {}) => {
const maxCount = resolveDelayItemCount(item);
const orderItemId = resolveOrderItemId(item);
const parentOrderItemId = resolveOrderItemId(parentItem);
const submitOrderItemId = getFirstValidValue(orderItemId, parentOrderItemId);
const skuText = getFirstValidValue(
formatSkuProperties(item?.properties || parentItem?.properties),
formatSkuProperties(item?.skuProperties || parentItem?.skuProperties),
formatSkuProperties(item?.attrs || parentItem?.attrs),
formatSkuProperties(item?.specs || parentItem?.specs),
formatSkuProperties(item?.specifications || parentItem?.specifications),
getFirstValidValue(item?.skuText, parentItem?.skuText),
getFirstValidValue(item?.specText, parentItem?.specText),
getFirstValidValue(item?.propertiesText, parentItem?.propertiesText),
getFirstValidValue(item?.skuDesc, parentItem?.skuDesc),
getFirstValidValue(item?.specDesc, parentItem?.specDesc),
);
return {
itemKey: getFirstValidValue(
submitOrderItemId,
`${item?.spuName || item?.name || parentItem?.spuName || parentItem?.name || ''}_${skuText}_${maxCount}`,
),
orderItemId,
parentOrderItemId,
submitOrderItemId,
picUrl: item?.picUrl || parentItem?.picUrl || '',
spuName: item?.spuName || item?.name || parentItem?.spuName || parentItem?.name || '',
skuText,
maxCount,
selectedCount: maxCount,
};
};
const createDelayShipmentItems = (data) => {
const sourceItems = Array.isArray(data?.items) ? data.items : [];
return sourceItems
.flatMap((item) => {
const children = Array.isArray(item?.items) ? item.items : [];
if (children.length) {
return children.filter(Boolean).map((child) => createDelayShipmentItem(child, item));
}
return [createDelayShipmentItem(item, item)];
})
.filter((item) => item.maxCount > 0);
};
const createNowShipmentItem = (item) => {
const maxCount = resolveDelayRecordCount(item);
const delayRecordId = resolveDelayRecordId(item);
const skuText = getFirstValidValue(
formatSkuProperties(item?.properties),
formatSkuProperties(item?.skuProperties),
formatSkuProperties(item?.attrs),
formatSkuProperties(item?.specs),
formatSkuProperties(item?.specifications),
getFirstValidValue(item?.skuText, item?.specText, item?.propertiesText),
getFirstValidValue(item?.propertiesValue, item?.skuValue),
getFirstValidValue(item?.skuDesc, item?.specDesc, item?.specification),
getFirstValidValue(item?.sku, item?.specInfo, item?.attrText),
);
return {
itemKey: getFirstValidValue(
delayRecordId,
`${item?.spuName || item?.name || item?.title || ''}_${skuText}_${maxCount}`,
),
delayRecordId,
picUrl: getFirstValidValue(
item?.picUrl,
item?.image,
item?.imgUrl,
item?.thumbnail,
item?.pic,
item?.cover,
item?.spuImage,
item?.skuImage,
),
spuName: getFirstValidValue(
item?.spuName,
item?.name,
item?.title,
item?.goodsName,
item?.productName,
item?.spuTitle,
),
skuText,
maxCount,
selectedCount: maxCount,
};
};
const createNowShipmentItems = (data) => {
const rawExternal = Array.isArray(props.delayRecords) && props.delayRecords.length > 0
? props.delayRecords
: [];
const rawFromData = Array.isArray(data?.delayRecords)
? data.delayRecords
: Array.isArray(data?.delayRecordList)
? data.delayRecordList
: [];
const raw = rawExternal.length > 0 ? rawExternal : rawFromData;
return raw
.filter(Boolean)
.filter((item) => {
const status = Number(item?.status);
if (!status) return true;
return status === 1;
})
.map((item) => createNowShipmentItem(item))
.filter((item) => item.maxCount > 0);
};
const renderItems = computed(() => innerItems.value || []);
const open = () => {
const nextItems = props.mode === 'now' ? createNowShipmentItems(props.orderData) : createDelayShipmentItems(props.orderData);
innerItems.value = nextItems;
if (!popupRef.value || typeof popupRef.value.open !== 'function') {
uni.showToast({
title: '弹窗组件初始化失败,请重试',
icon: 'none',
});
return;
}
popupRef.value.open();
};
const close = () => {
if (popupRef.value && typeof popupRef.value.close === 'function') {
popupRef.value.close();
}
};
const handleClose = () => {
emit('close');
close();
};
const onCountChange = (index, value) => {
const current = innerItems.value[index];
if (!current) return;
const next = Number(value);
if (!Number.isFinite(next)) {
current.selectedCount = 0;
return;
}
current.selectedCount = Math.min(current.maxCount, Math.max(0, next));
};
const buildSubmitPayload = () => {
if (props.mode === 'now') {
const hasSelectedCount = innerItems.value.some((item) => Number(item.selectedCount) > 0);
const items = innerItems.value
.filter((item) => Number(item.selectedCount) > 0 && hasValue(item.delayRecordId))
.map((item) => ({
delayRecordId: item.delayRecordId,
count: Number(item.selectedCount),
}));
return {
hasSelectedCount,
items,
emptyTitle: hasSelectedCount ? '订单商品信息异常' : '请选择立即发货数量',
};
}
const hasSelectedCount = innerItems.value.some((item) => Number(item.selectedCount) > 0);
const items = innerItems.value
.filter((item) => Number(item.selectedCount) > 0 && hasValue(item.submitOrderItemId))
.map((item) => ({
orderItemId: item.submitOrderItemId,
count: Number(item.selectedCount),
}));
return {
hasSelectedCount,
items,
emptyTitle: hasSelectedCount ? '订单商品信息异常' : '请选择延迟发货数量',
};
};
const handleSubmit = () => {
const payload = buildSubmitPayload();
if (!payload.items.length) {
uni.showToast({
title: payload.emptyTitle,
icon: 'none',
});
return;
}
emit('submit', {
mode: props.mode,
items: payload.items,
close,
});
};
defineExpose({
open,
close,
});
</script>
<style lang="scss" scoped>
.shipment-popup {
background: #ffffff;
box-sizing: border-box;
}
.shipment-popup--bottom {
border-radius: 24rpx 24rpx 0 0;
padding: 32rpx 24rpx calc(24rpx + env(safe-area-inset-bottom));
}
.shipment-popup--center {
width: 680rpx;
max-height: 78vh;
border-radius: 24rpx;
padding: 32rpx 24rpx;
overflow: hidden;
}
.shipment-popup__header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12rpx;
}
.shipment-popup__title {
font-size: 34rpx;
font-weight: 600;
color: #333333;
}
.shipment-popup__order-no {
margin-top: 8rpx;
font-size: 22rpx;
line-height: 32rpx;
color: #999999;
word-break: break-all;
}
.shipment-popup__order-time {
margin-top: 4rpx;
font-size: 22rpx;
line-height: 32rpx;
color: #999999;
}
.shipment-popup__close {
width: 56rpx;
height: 56rpx;
border-radius: 50%;
background: #f3f4f6;
line-height: 56rpx;
text-align: center;
font-size: 36rpx;
color: #666666;
}
.shipment-popup__desc {
font-size: 24rpx;
color: #999999;
margin-bottom: 24rpx;
}
.shipment-popup__content {
max-height: 720rpx;
}
.shipment-popup__item {
display: flex;
padding: 20rpx 0;
border-bottom: 1rpx solid #f2f2f2;
}
.shipment-popup__item-image {
width: 144rpx;
height: 144rpx;
border-radius: 12rpx;
flex-shrink: 0;
margin-right: 20rpx;
background: #f5f5f5;
}
.shipment-popup__item-main {
flex: 1;
min-width: 0;
}
.shipment-popup__item-name {
font-size: 28rpx;
line-height: 40rpx;
color: #333333;
}
.shipment-popup__item-sku {
margin-top: 10rpx;
font-size: 24rpx;
line-height: 34rpx;
color: #999999;
}
.shipment-popup__item-footer {
margin-top: 20rpx;
}
.shipment-popup__item-count {
font-size: 24rpx;
color: #666666;
}
.shipment-popup__actions {
display: flex;
align-items: center;
gap: 20rpx;
margin-top: 32rpx;
}
.shipment-popup__btn {
flex: 1;
height: 84rpx;
line-height: 84rpx;
text-align: center;
border-radius: 42rpx;
font-size: 28rpx;
font-weight: 500;
}
.shipment-popup__btn--ghost {
background: #f2f3f5;
color: #333333;
}
.shipment-popup__btn--primary {
background: linear-gradient(135deg, #fa8735 0%, #fe3b48 100%);
color: #ffffff;
}
.shipment-popup__empty {
padding: 40rpx 24rpx;
text-align: center;
font-size: 26rpx;
color: #999999;
}
</style>