order_customer 只能看自己 customer_portal_users 绑定订单;首页内嵌 customer-trace-panel;不返回内部字段。
311 lines
9.8 KiB
JavaScript
311 lines
9.8 KiB
JavaScript
const config = require('../../config');
|
|
const { fetchCustomerTraceOrders, fetchCustomerTraceOrder } = require('../../utils/request');
|
|
const { beginPanelShow, endPanelShow } = require('../../utils/panelShowGate');
|
|
|
|
function pad(n) {
|
|
return String(n).padStart(2, '0');
|
|
}
|
|
|
|
function chinaDay(offsetDays = 0) {
|
|
const now = new Date(Date.now() + 8 * 3600 * 1000 + offsetDays * 86400 * 1000);
|
|
return `${now.getUTCFullYear()}-${pad(now.getUTCMonth() + 1)}-${pad(now.getUTCDate())}`;
|
|
}
|
|
|
|
function absolutizeMediaUrl(url) {
|
|
const raw = String(url || '').trim();
|
|
if (!raw) return raw;
|
|
if (/^https?:\/\//i.test(raw)) return raw;
|
|
if (/^data:/i.test(raw)) return raw;
|
|
const apiBase = String(config.apiBase || '').replace(/\/+$/, '');
|
|
const origin = apiBase.replace(/\/api$/i, '');
|
|
if (raw.startsWith('/')) return `${origin}${raw}`;
|
|
return `${apiBase}/${raw.replace(/^\/+/, '')}`;
|
|
}
|
|
|
|
function isImageUrl(url) {
|
|
return /\.(png|jpe?g|webp|gif|bmp|avif)(?:[?#]|$)/i.test(String(url || ''));
|
|
}
|
|
|
|
/** 链路阶段 → 表格列(服务端节点标题:入库 / 分拣 / 配送拍照 / 送达) */
|
|
const STAGE_COLUMNS = [
|
|
{ key: 'inbound', label: '入库', match: (title) => title.includes('入库') },
|
|
{ key: 'picking', label: '分拣', match: (title) => title.includes('分拣') },
|
|
{ key: 'loading', label: '配送拍照', match: (title) => title.includes('配送') || title.includes('补拍') },
|
|
{ key: 'delivery', label: '送达', match: (title) => title.includes('送达') },
|
|
];
|
|
|
|
/** 详情接口原始字段 → 面板数据(展示口径与原两页逐字一致) */
|
|
function mapDetail(data) {
|
|
const raw = data && typeof data === 'object' ? data : {};
|
|
const items = (raw.items || []).map((item) => {
|
|
// 链路节点由服务端给出(入库 / 分拣 / 配送拍照 / 送达),照片跟随节点 ⇒ 天然按阶段区分
|
|
// 表格只展示图片(2026-10-24 开发者:时间无需展示),不再取节点时间
|
|
const stages = STAGE_COLUMNS.map((col) => {
|
|
const node = (item.timeline || []).find((candidate) =>
|
|
col.match(String(candidate && candidate.title ? candidate.title : ''))
|
|
);
|
|
const photos = node
|
|
? (node.photos || [])
|
|
.filter((photo) => photo && photo.url)
|
|
.map((photo) => absolutizeMediaUrl(photo.url))
|
|
: [];
|
|
return {
|
|
key: col.key,
|
|
label: col.label,
|
|
photos,
|
|
};
|
|
});
|
|
const reports = (item.quality_reports || []).map((report) => ({
|
|
label: report.report_label || report.report_type || '报告',
|
|
files: (report.file_urls || []).map((url) => {
|
|
const clean = String(url || '').split(/[?#]/)[0];
|
|
const matched = clean.match(/\.([a-z0-9]{1,8})$/i);
|
|
return {
|
|
url: absolutizeMediaUrl(url),
|
|
image: isImageUrl(url),
|
|
ext: matched ? matched[1].toUpperCase() : 'FILE',
|
|
};
|
|
}),
|
|
}));
|
|
return {
|
|
trace_code: item.trace_code,
|
|
item_name: item.item_name,
|
|
actual_qty: item.actual_qty == null ? '-' : item.actual_qty,
|
|
unit: item.unit || '',
|
|
stages,
|
|
reports,
|
|
};
|
|
});
|
|
return { order: raw.order || null, items };
|
|
}
|
|
|
|
Component({
|
|
options: {
|
|
addGlobalClass: true,
|
|
},
|
|
|
|
properties: {
|
|
embedded: {
|
|
type: Boolean,
|
|
value: false,
|
|
},
|
|
},
|
|
|
|
lifetimes: {
|
|
attached() {
|
|
this.onPanelShow();
|
|
},
|
|
},
|
|
|
|
pageLifetimes: {
|
|
show() {
|
|
this.onPanelShow();
|
|
},
|
|
},
|
|
|
|
data: {
|
|
loading: false,
|
|
orders: [],
|
|
total: 0,
|
|
dateFrom: chinaDay(-6),
|
|
dateTo: chinaDay(0),
|
|
keyword: '',
|
|
expandedCode: '',
|
|
detailLoadingCode: '',
|
|
detailError: '',
|
|
emptyText: '暂无溯源订单',
|
|
},
|
|
|
|
methods: {
|
|
async onPanelShow() {
|
|
if (!beginPanelShow(this)) return;
|
|
try {
|
|
await this.loadOrders();
|
|
} finally {
|
|
endPanelShow(this);
|
|
}
|
|
},
|
|
|
|
decorateOrders(rows, expandedCode) {
|
|
const cache = this._detailCache || {};
|
|
const code = String(expandedCode || '');
|
|
return (rows || this.data.orders || []).map((row) => {
|
|
const open = Boolean(code) && row.order_code === code;
|
|
return {
|
|
...row,
|
|
open,
|
|
detail: open ? cache[row.order_code] || null : null,
|
|
};
|
|
});
|
|
},
|
|
|
|
async loadOrders() {
|
|
if (this.data.loading) return;
|
|
this.setData({ loading: true });
|
|
try {
|
|
const result = await fetchCustomerTraceOrders({
|
|
q: this.data.keyword,
|
|
date_from: this.data.keyword ? '' : this.data.dateFrom,
|
|
date_to: this.data.keyword ? '' : this.data.dateTo,
|
|
limit: 50,
|
|
offset: 0,
|
|
});
|
|
const rows = (result.data || []).map((row) => ({
|
|
...row,
|
|
delivery_date: row.delivery_date || '-',
|
|
customer_name: row.customer_name || '-',
|
|
}));
|
|
const expandedCode = rows.some((row) => row.order_code === this.data.expandedCode)
|
|
? this.data.expandedCode
|
|
: '';
|
|
this.setData({
|
|
orders: this.decorateOrders(rows, expandedCode),
|
|
total: Number(result.total) || rows.length,
|
|
expandedCode,
|
|
detailError: expandedCode ? this.data.detailError : '',
|
|
});
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '加载失败', icon: 'none' });
|
|
this.setData({ orders: [], total: 0 });
|
|
} finally {
|
|
this.setData({ loading: false });
|
|
}
|
|
},
|
|
|
|
onKeywordInput(e) {
|
|
this.setData({ keyword: String((e.detail && e.detail.value) || '').trim() });
|
|
},
|
|
|
|
onSearch() {
|
|
this.loadOrders();
|
|
},
|
|
|
|
onDateFromChange(e) {
|
|
this.setData({ dateFrom: e.detail.value, keyword: '' });
|
|
this.loadOrders();
|
|
},
|
|
|
|
onDateToChange(e) {
|
|
this.setData({ dateTo: e.detail.value, keyword: '' });
|
|
this.loadOrders();
|
|
},
|
|
|
|
async onOrderTap(e) {
|
|
const code = String(
|
|
(e.currentTarget && e.currentTarget.dataset && e.currentTarget.dataset.code) || ''
|
|
).trim();
|
|
if (!code) return;
|
|
if (this.data.expandedCode === code) {
|
|
this.setData({
|
|
expandedCode: '',
|
|
detailError: '',
|
|
orders: this.decorateOrders(this.data.orders, ''),
|
|
});
|
|
return;
|
|
}
|
|
this.setData({
|
|
expandedCode: code,
|
|
detailError: '',
|
|
orders: this.decorateOrders(this.data.orders, code),
|
|
});
|
|
if (!(this._detailCache && this._detailCache[code])) {
|
|
await this.loadDetail(code);
|
|
}
|
|
},
|
|
|
|
async loadDetail(code) {
|
|
const key = String(code || '').trim();
|
|
if (!key || this.data.detailLoadingCode) return;
|
|
this.setData({ detailLoadingCode: key, detailError: '' });
|
|
try {
|
|
const payload = await fetchCustomerTraceOrder(key);
|
|
this._detailCache = this._detailCache || {};
|
|
this._detailCache[key] = mapDetail(payload && payload.data);
|
|
} catch (error) {
|
|
const message = (error && error.message) || '加载失败';
|
|
this.setData({ detailError: message });
|
|
wx.showToast({ title: message, icon: 'none' });
|
|
} finally {
|
|
this.setData({
|
|
detailLoadingCode: '',
|
|
orders: this.decorateOrders(this.data.orders, this.data.expandedCode),
|
|
});
|
|
}
|
|
},
|
|
|
|
detailOf(code) {
|
|
const key = String(code || '').trim();
|
|
return (this._detailCache && this._detailCache[key]) || null;
|
|
},
|
|
|
|
previewNodePhoto(e) {
|
|
const ds = (e.currentTarget && e.currentTarget.dataset) || {};
|
|
const detail = this.detailOf(ds.code);
|
|
if (!detail) return;
|
|
const stageIndex = Number(ds.stage) || 0;
|
|
const itemIndex = Number(ds.item) || 0;
|
|
const photoIndex = Number(ds.index) || 0;
|
|
// 聚合**整单同一个阶段**的所有图片:预览里左右滑就能看其它商品的同阶段照片
|
|
const urls = [];
|
|
let current = '';
|
|
(detail.items || []).forEach((sku, skuIdx) => {
|
|
const stage = sku && sku.stages ? sku.stages[stageIndex] : null;
|
|
if (!stage || !stage.photos || !stage.photos.length) return;
|
|
stage.photos.forEach((url, idx) => {
|
|
if (skuIdx === itemIndex && idx === photoIndex) current = url;
|
|
urls.push(url);
|
|
});
|
|
});
|
|
if (!urls.length) return;
|
|
wx.previewImage({ urls, current: current || urls[0] });
|
|
},
|
|
|
|
previewReportImage(e) {
|
|
const ds = (e.currentTarget && e.currentTarget.dataset) || {};
|
|
const detail = this.detailOf(ds.code);
|
|
const item = detail && detail.items[Number(ds.item) || 0];
|
|
const report = item && item.reports[Number(ds.report) || 0];
|
|
if (!report) return;
|
|
const images = report.files.filter((file) => file.image).map((file) => file.url);
|
|
if (!images.length) return;
|
|
wx.previewImage({
|
|
urls: images,
|
|
current: images[Number(ds.index) || 0] || images[0],
|
|
});
|
|
},
|
|
|
|
openReportFile(e) {
|
|
const ds = (e.currentTarget && e.currentTarget.dataset) || {};
|
|
const detail = this.detailOf(ds.code);
|
|
const item = detail && detail.items[Number(ds.item) || 0];
|
|
const report = item && item.reports[Number(ds.report) || 0];
|
|
const file = report && report.files[Number(ds.index) || 0];
|
|
if (!file || file.image) return;
|
|
wx.showLoading({ title: '打开中…', mask: true });
|
|
wx.downloadFile({
|
|
url: file.url,
|
|
success(res) {
|
|
if (res.statusCode !== 200) {
|
|
wx.hideLoading();
|
|
wx.showToast({ title: '文件下载失败', icon: 'none' });
|
|
return;
|
|
}
|
|
wx.openDocument({
|
|
filePath: res.tempFilePath,
|
|
complete() {
|
|
wx.hideLoading();
|
|
},
|
|
});
|
|
},
|
|
fail() {
|
|
wx.hideLoading();
|
|
wx.showToast({ title: '文件下载失败', icon: 'none' });
|
|
},
|
|
});
|
|
},
|
|
|
|
/** 展开区吞掉点击,避免点到空白处把卡片收起来 */
|
|
noop() {},
|
|
},
|
|
});
|