Files
kangyuan-mobile/components/orderItem/ExpressListPopup.vue
T

795 lines
21 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="transparent">
<view class="express-popup" :class="`express-popup--${popupType}`" @tap.stop>
<view class="express-popup__header">
<view>
<view class="express-popup__title">查看物流</view>
<view class="express-popup__order-no">订单号:{{ orderNo || '-' }}</view>
<view class="express-popup__tip">点击商品右侧按钮可展开/收起物流轨迹</view>
</view>
<view class="express-popup__close" @tap.stop="handleClose">×</view>
</view>
<view class="express-popup__scroll-wrap">
<scroll-view
scroll-y
scroll-with-animation
enhanced
:show-scrollbar="false"
class="express-popup__content"
>
<view v-if="renderItems.length === 0" class="express-popup__empty">
暂无物流信息
</view>
<view
v-else
class="express-popup__item"
:class="{ 'express-popup__item--expanded': item.expanded }"
v-for="(item, index) in renderItems"
:key="item.itemKey || index"
>
<view class="express-popup__item-top flex-row al-flex-start">
<image class="express-popup__item-image" :src="item.picUrl" mode="aspectFill" />
<view class="express-popup__item-main">
<view class="express-popup__item-name lineTwo">{{ item.spuName }}</view>
<view class="express-popup__item-sku">{{ item.skuText || '-' }}</view>
<view class="express-popup__item-info">
<text class="express-popup__item-count">数量:x{{ item.count }}</text>
<text
v-if="item.logisticsNo"
class="express-popup__item-logistics"
>运单号:{{ item.logisticsNo }}</text>
</view>
<view class="express-popup__item-action">
<view
class="express-popup__btn"
:class="item.expanded ? 'express-popup__btn--ghost' : 'express-popup__btn--primary'"
@tap.stop="handleToggleTrack(item)"
>
<text v-if="item.loading">加载中...</text>
<template v-else>
{{ item.expanded ? '收起物流' : '查看物流' }}
</template>
<text class="express-popup__btn-arrow" :class="{ rotated: item.expanded }">▾</text>
</view>
</view>
</view>
</view>
<view v-if="item.expanded" class="express-popup__track">
<view v-if="item.loading" class="express-popup__track-loading">物流信息加载中...</view>
<view v-else-if="!Array.isArray(item.tracks) || item.tracks.length === 0" class="express-popup__track-empty">
暂无物流轨迹
</view>
<view v-else class="express-popup__track-list">
<view
class="express-popup__track-node"
:class="{ 'is-first': i === 0 }"
v-for="(node, i) in item.tracks"
:key="i"
>
<view class="express-popup__track-dot" />
<view class="express-popup__track-main">
<view class="express-popup__track-content">{{ node.content || node.desc || node.text || '-' }}</view>
<view class="express-popup__track-time">{{ node.time || node.ftime || node.createTime || '' }}</view>
</view>
</view>
</view>
</view>
</view>
</scroll-view>
</view>
</view>
</uni-popup>
</template>
<script setup>
import { computed, reactive, ref, watch } from 'vue';
import sheep from '@/sheep';
import { getAiOrderDelayRecords, getOpenOrderDetail, getOpenOrderExpressTrackList } from '@/sheep/api/ai';
import { getExpressTrackList } from '@/sheep/api/index/index';
const props = defineProps({
popupType: {
type: String,
default: 'bottom',
},
orderNo: {
type: String,
default: '',
},
orderData: {
type: Object,
default: () => ({}),
},
delayRecords: {
type: Array,
default: () => [],
},
orderId: {
type: [Number, String],
default: 0,
},
});
const emit = defineEmits(['close', 'view-express']);
const popupRef = ref();
const innerItems = ref([]);
const loadedRecords = ref([]);
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?.optionName,
property?.propertyKey,
'',
);
if (hasValue(name) && hasValue(value)) return `${name}:${value}`;
if (hasValue(value)) return String(value);
if (hasValue(name)) return String(name);
return '';
};
const formatSkuProperties = (properties) => {
if (!properties) return '';
if (typeof properties === 'string' && properties.trim()) return properties.trim();
if (Array.isArray(properties)) {
return properties.map(formatSkuPropertyItem).filter(Boolean).join(' ');
}
if (typeof properties === 'object') {
return Object.keys(properties)
.map((key) => {
const value = properties[key];
if (typeof value === 'object') return formatSkuPropertyItem(value);
return `${key}:${value}`;
})
.filter(Boolean)
.join(' ');
}
return '';
};
const resolveSpuItems = (data) => {
const items = Array.isArray(data?.items) ? data.items : [];
return items.flatMap((spu) => {
const subItems = Array.isArray(spu?.items) ? spu.items : [];
if (subItems.length > 0) {
return subItems.map((sub) => ({
...sub,
_spuName: getFirstValidValue(
spu?.spuName,
spu?.productName,
spu?.goodsName,
sub?.spuName,
sub?.productName,
sub?.goodsName,
),
_spuPic: getFirstValidValue(
spu?.picUrl,
spu?.imageUrl,
spu?.thumbUrl,
spu?.goodsPic,
sub?.picUrl,
sub?.imageUrl,
sub?.thumbUrl,
sub?.goodsPic,
),
_spuId: getFirstValidValue(spu?.spuId, spu?.productId, sub?.spuId, sub?.productId),
_skuText: getFirstValidValue(
sub?.skuProperties,
sub?.specInfo,
sub?.skuInfo,
sub?.skuText,
formatSkuProperties(sub?.skuProperties || sub?.specs || sub?.properties),
),
}));
}
return [
{
...spu,
_spuName: getFirstValidValue(spu?.spuName, spu?.productName, spu?.goodsName),
_spuPic: getFirstValidValue(
spu?.picUrl,
spu?.imageUrl,
spu?.thumbUrl,
spu?.goodsPic,
),
_spuId: getFirstValidValue(spu?.spuId, spu?.productId),
_skuText: getFirstValidValue(
spu?.skuProperties,
spu?.specInfo,
spu?.skuInfo,
spu?.skuText,
formatSkuProperties(spu?.skuProperties || spu?.specs || spu?.properties),
),
},
];
});
};
const resolveAllDelayRecords = () => {
if (Array.isArray(loadedRecords.value) && loadedRecords.value.length > 0) {
return loadedRecords.value.filter(Boolean);
}
const orderLevelRecords = Array.isArray(props.delayRecords)
? props.delayRecords
: Array.isArray(props.orderData?.delayRecords)
? props.orderData.delayRecords
: Array.isArray(props.orderData?.delayRecordList)
? props.orderData.delayRecordList
: [];
return orderLevelRecords.filter(Boolean);
};
const resolveCount = (record) =>
Number(
getFirstValidValue(
record?.count,
record?.num,
record?.quantity,
record?.number,
record?.totalCount,
1,
),
) || 1;
const resolveRecordOrderItemId = (record) =>
Number(
getFirstValidValue(
record?.orderItemId,
record?.orderItemVO?.id,
record?.orderItem?.id,
record?.itemId,
0,
),
) || 0;
const resolveRecordSkuText = (record) => {
const direct = getFirstValidValue(
record?.skuProperties,
record?.specInfo,
record?.skuText,
record?.skuInfo,
formatSkuProperties(record?.skuProperties || record?.specs || record?.properties),
);
if (direct) return direct;
const itemSpec = getFirstValidValue(
record?.orderItemVO?.skuProperties,
record?.orderItemVO?.specInfo,
record?.orderItem?.skuProperties,
record?.orderItem?.specInfo,
formatSkuProperties(record?.orderItemVO?.skuProperties || record?.orderItem?.specs),
);
return itemSpec;
};
const resolveRecordSpuName = (record) =>
getFirstValidValue(
record?.spuName,
record?.productName,
record?.goodsName,
record?.orderItemVO?.spuName,
record?.orderItem?.spuName,
record?.orderItemVO?.productName,
record?.orderItem?.productName,
);
const resolveRecordPicUrl = (record) =>
getFirstValidValue(
record?.picUrl,
record?.imageUrl,
record?.thumbUrl,
record?.goodsPic,
record?.orderItemVO?.picUrl,
record?.orderItem?.picUrl,
record?.orderItemVO?.imageUrl,
record?.orderItem?.imageUrl,
);
const resolveRecordLogisticsNo = (record) =>
getFirstValidValue(
record?.logisticsNo,
record?.waybillNo,
record?.trackingNo,
record?.expressNo,
record?.orderItemVO?.logisticsNo,
record?.orderItem?.logisticsNo,
);
const buildExpressItemList = () => {
const records = resolveAllDelayRecords();
const expressRecords = records.filter((record) => {
const status = Number(record?.status);
return status === 3 && Number(record?.delayRecordId || record?.id) > 0;
});
if (expressRecords.length === 0) return [];
const spuItems = resolveSpuItems(props.orderData);
const findItemByOrderItemId = (id) =>
spuItems.find(
(sku) => Number(sku?.id || sku?.orderItemId || sku?.itemId || 0) === Number(id),
);
const findItemBySkuText = (text) =>
spuItems.find((sku) => {
const skuText = getFirstValidValue(
sku?.skuProperties,
sku?.specInfo,
sku?.skuInfo,
sku?.skuText,
formatSkuProperties(sku?.skuProperties || sku?.specs || sku?.properties),
);
return !!text && !!skuText && String(text).trim() === String(skuText).trim();
});
const singleSpu =
spuItems.length === 1
? spuItems[0]
: new Set(spuItems.map((s) => s?._spuId || s?._spuName)).size === 1
? spuItems[0]
: null;
return expressRecords.map((record, idx) => {
const orderItemId = resolveRecordOrderItemId(record);
const recordSku = resolveRecordSkuText(record);
let matched = orderItemId ? findItemByOrderItemId(orderItemId) : null;
if (!matched && recordSku) matched = findItemBySkuText(recordSku);
if (!matched && singleSpu) matched = singleSpu;
const count = resolveCount(record);
const spuName = matched
? getFirstValidValue(matched._spuName, matched?.spuName, matched?.productName)
: resolveRecordSpuName(record);
const picUrl = matched
? getFirstValidValue(matched._spuPic, matched?.picUrl, matched?.imageUrl)
: resolveRecordPicUrl(record);
const skuText = matched
? getFirstValidValue(
matched?._skuText,
matched?.skuProperties,
matched?.specInfo,
matched?.skuInfo,
matched?.skuText,
formatSkuProperties(matched?.skuProperties || matched?.specs || matched?.properties),
)
: recordSku;
const delayRecordId = Number(record?.delayRecordId || record?.id) || 0;
const logisticsNo = resolveRecordLogisticsNo(record);
return reactive({
itemKey: `${delayRecordId}-${idx}`,
delayRecordId,
spuName: spuName || '商品信息',
skuText: skuText || '单规格',
picUrl: picUrl || '',
count,
logisticsNo,
expanded: false,
loading: false,
tracks: null,
});
});
};
const renderItems = computed(() => innerItems.value || []);
const open = async () => {
loadedRecords.value = [];
const oid = props.orderId || props.orderData?.id || props.orderData?.orderId;
const isOpenOrder = Number(props.orderData?.type) === 5;
if (oid) {
try {
if (isOpenOrder) {
// 开通店铺订单:用详情接口获取延迟记录和物流数据,不走延迟记录接口
const { code, data } = await getOpenOrderDetail({ id: oid });
if (code === 0) {
// 更新 props.orderData 引用(因为原 props 可能是旧的)
if (data) {
// 把最新的订单详情中的 delayRecords 取出来作为延迟记录
loadedRecords.value = Array.isArray(data?.delayRecords)
? data.delayRecords
: Array.isArray(data?.delayRecordList)
? data.delayRecordList
: [];
// 同时把最新的 orderData 保存下来,供 loadTrackList 查物流轨迹时用
props.orderData._openOrderDetailData = data;
}
}
} else {
// 正常商城/店铺订单:走专门的延迟记录接口
const { code, data } = await getAiOrderDelayRecords({ orderId: oid });
if (code === 0 && Array.isArray(data)) {
loadedRecords.value = data;
}
}
} catch (e) {
loadedRecords.value = [];
}
}
innerItems.value = buildExpressItemList();
popupRef.value?.open?.();
};
const close = () => {
popupRef.value?.close?.();
};
const handleClose = () => {
emit('close');
close();
};
const loadTrackList = async (item) => {
const delayRecordId = Number(item?.delayRecordId) || 0;
const id = props.orderId || props.orderData?.id || props.orderData?.orderId;
if (!delayRecordId || !id) {
uni.showToast({
title: '物流信息异常',
icon: 'none',
});
return [];
}
const isOpenOrder = Number(props.orderData?.type) === 5;
// 开通店铺订单(type===5):物流接口 /aishop/app/open/order/get-express-track-list
const requestApi = isOpenOrder ? getOpenOrderExpressTrackList : getExpressTrackList;
try {
const { code, data } = await requestApi({
id,
code: 2,
delayRecordId,
});
if (code === 0 && Array.isArray(data)) {
return data;
}
return [];
} catch (e) {
return [];
}
};
const handleToggleTrack = async (item) => {
if (!item) return;
if (item.loading) return;
if (!item.expanded) {
const delayRecordId = Number(item?.delayRecordId) || 0;
const id = props.orderId || props.orderData?.id || props.orderData?.orderId;
if (!delayRecordId || !id) {
uni.showToast({
title: '物流信息异常',
icon: 'none',
});
return;
}
emit('view-express', {
id,
delayRecordId,
code: 2,
});
if (!Array.isArray(item.tracks)) {
item.loading = true;
const data = await loadTrackList(item);
item.tracks = data;
item.loading = false;
}
item.expanded = true;
} else {
item.expanded = false;
}
};
defineExpose({ open, close });
</script>
<style lang="scss" scoped>
.express-popup {
background: #ffffff;
box-sizing: border-box;
display: flex;
flex-direction: column;
&--bottom {
width: 100%;
height: 82vh;
max-height: 82vh;
border-radius: 24rpx 24rpx 0 0;
padding: 32rpx 32rpx calc(24rpx + env(safe-area-inset-bottom)) 32rpx;
overflow: hidden;
box-sizing: border-box;
}
&--center {
width: 680rpx;
height: 78vh;
max-height: 78vh;
border-radius: 24rpx;
padding: 32rpx 32rpx 32rpx 32rpx;
margin: 0 auto;
overflow: hidden;
box-sizing: border-box;
}
&--center &__content {
padding-bottom: 0;
}
&--center &__btn {
min-width: 200rpx;
}
&__header {
display: flex;
justify-content: space-between;
align-items: flex-start;
padding-bottom: 24rpx;
border-bottom: 1rpx solid #f2f2f2;
flex-shrink: 0;
flex-grow: 0;
width: 100%;
box-sizing: border-box;
}
&__title {
font-size: 32rpx;
font-weight: 600;
color: #111827;
line-height: 44rpx;
}
&__order-no {
margin-top: 8rpx;
font-size: 24rpx;
color: #6b7280;
line-height: 32rpx;
}
&__tip {
margin-top: 8rpx;
font-size: 24rpx;
color: #f97316;
line-height: 32rpx;
}
&__close {
width: 48rpx;
height: 48rpx;
display: flex;
align-items: center;
justify-content: center;
font-size: 40rpx;
color: #9ca3af;
line-height: 1;
}
&__scroll-wrap {
flex: 1 1 auto;
min-height: 0;
width: 100%;
display: flex;
flex-direction: column;
margin-top: 20rpx;
box-sizing: border-box;
overflow: hidden;
}
&__content {
width: 100%;
flex: 1 1 auto;
height: auto;
min-height: 100%;
max-height: none;
margin: 0;
padding-bottom: 40rpx;
box-sizing: border-box;
}
&__empty {
padding: 80rpx 0;
text-align: center;
color: #9ca3af;
font-size: 26rpx;
}
&__item {
display: flex;
flex-direction: column;
padding: 20rpx 0;
border-bottom: 1rpx solid #f2f2f2;
}
&__item--expanded {
padding-bottom: 24rpx;
}
&__item-top {
width: 100%;
}
&__item-image {
flex-shrink: 0;
width: 140rpx;
height: 140rpx;
border-radius: 12rpx;
background: #f6f7fb;
margin-right: 20rpx;
}
&__item-main {
flex: 1;
min-width: 0;
padding-top: 4rpx;
display: flex;
flex-direction: column;
gap: 10rpx;
}
&__item-name {
font-size: 28rpx;
color: #111827;
line-height: 40rpx;
}
&__item-sku {
font-size: 24rpx;
color: #6b7280;
line-height: 32rpx;
background: #f6f7fb;
padding: 4rpx 12rpx;
border-radius: 8rpx;
align-self: flex-start;
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
&__item-info {
display: flex;
flex-direction: column;
gap: 4rpx;
}
&__item-count {
font-size: 26rpx;
color: #111827;
font-weight: 500;
}
&__item-logistics {
font-size: 22rpx;
color: #6b7280;
}
&__item-action {
margin-top: 10rpx;
display: flex;
justify-content: flex-end;
}
&__btn {
min-width: 200rpx;
height: 60rpx;
line-height: 60rpx;
text-align: center;
border-radius: 30rpx;
font-size: 26rpx;
font-weight: 500;
padding: 0 24rpx;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 6rpx;
}
&__btn-arrow {
font-size: 22rpx;
transition: transform .2s ease;
display: inline-block;
transform-origin: center;
}
&__btn-arrow.rotated {
transform: rotate(180deg);
}
&__btn--ghost {
border: 2rpx solid #e5e7eb;
color: #4b5563;
background: #ffffff;
}
&__btn--primary {
background: linear-gradient(135deg, #fa8735 0%, #fe3b48 100%);
color: #ffffff;
}
&__btn--disabled {
opacity: 0.6;
}
&__track {
margin-top: 20rpx;
margin-left: 160rpx;
padding: 20rpx 20rpx 10rpx 20rpx;
background: #f8fafc;
border-radius: 12rpx;
}
&__track-loading,
&__track-empty {
padding: 28rpx 0;
text-align: center;
font-size: 24rpx;
color: #9ca3af;
}
&__track-list {
position: relative;
padding: 8rpx 0 0 10rpx;
}
&__track-node {
position: relative;
padding-left: 24rpx;
padding-bottom: 20rpx;
&:last-child {
padding-bottom: 10rpx;
}
&:last-child::before {
display: none;
}
}
&__track-node::before {
content: '';
position: absolute;
left: 4rpx;
top: 20rpx;
bottom: 0;
width: 2rpx;
background: #e5e7eb;
}
&__track-dot {
position: absolute;
left: -4rpx;
top: 8rpx;
width: 14rpx;
height: 14rpx;
border-radius: 50%;
background: #d1d5db;
border: 2rpx solid #ffffff;
box-sizing: border-box;
box-shadow: 0 0 0 2rpx #e5e7eb;
}
&__track-node.is-first &__track-dot {
background: linear-gradient(135deg, #fa8735 0%, #fe3b48 100%);
box-shadow: 0 0 0 2rpx rgba(250,135,53,0.25);
width: 16rpx;
height: 16rpx;
left: -5rpx;
}
&__track-content {
font-size: 24rpx;
line-height: 34rpx;
color: #374151;
word-break: break-all;
}
&__track-node.is-first &__track-content {
color: #111827;
font-weight: 600;
font-size: 26rpx;
}
&__track-time {
margin-top: 6rpx;
font-size: 22rpx;
line-height: 30rpx;
color: #9ca3af;
}
}
::v-deep .uni-popup {
background: transparent !important;
}
::v-deep .uni-popup__wrapper {
background: transparent !important;
}
::v-deep .uni-popup__wrapper-box {
background: transparent !important;
}
::v-deep .uni-popup__content {
background: transparent !important;
}
.express-popup--bottom ::v-deep .uni-popup__content,
.express-popup--bottom ::v-deep .uni-popup__wrapper-box {
border-radius: 24rpx 24rpx 0 0 !important;
overflow: hidden !important;
}
.express-popup--center ::v-deep .uni-popup__content,
.express-popup--center ::v-deep .uni-popup__wrapper-box {
border-radius: 24rpx !important;
overflow: hidden !important;
background: transparent !important;
}
</style>