/* 我的订单:登录门禁 + 订单列表 + 详情(支付状态 / 生产进度 / 物流) */ (function () { const h = window.React.createElement; const A = window.PL.ui.antd; const UI = window.PL.ui; const dayjs = window.dayjs; const state = { items: [], total: 0, local: [], page: 1, pageSize: 8, loading: false, drawer: null, // 选中的订单(详情) detail: null, // 详情数据(含 events) detailBusy: false }; const previewCache = new Map(); // "orderNo:key" -> objectURL const localPreview = new Map(); // "orderNo:key" -> dataURL(本地订单) const STATUS_OPTS = { pending_payment: { label: '待支付', color: 'orange' }, producing: { label: '生产中', color: 'processing' }, shipped: { label: '已发货', color: 'geekblue' }, completed: { label: '已完成', color: 'success' }, cancelled: { label: '已取消', color: 'default' } }; const PAY_OPTS = { unpaid: { label: '未支付', color: 'orange' }, paid: { label: '已支付', color: 'success' }, refunded: { label: '已退款', color: 'purple' } }; const TYPE_LABEL = { bag: '包装袋', box: '纸盒' }; function fmtTime(ts) { return ts ? dayjs(ts * 1000).format('YYYY-MM-DD HH:mm') : '-'; } function statusTag(s) { const o = STATUS_OPTS[s] || { label: s, color: 'default' }; return h(A.Tag, { color: o.color }, o.label); } function payTag(p) { const o = PAY_OPTS[p] || { label: p, color: 'default' }; return h(A.Tag, { color: o.color }, o.label); } function loadPreview(orderNo, key) { const k = orderNo + ':' + key; if (previewCache.has(k)) return Promise.resolve(previewCache.get(k)); return fetch('/api/orders/mine/' + orderNo + '/preview/' + key, { headers: PL.api.token() ? { Authorization: 'Bearer ' + PL.api.token() } : {} }).then(function (res) { if (!res.ok) throw new Error('preview'); return res.blob(); }).then(function (blob) { const url = URL.createObjectURL(blob); previewCache.set(k, url); return url; }).catch(function () { previewCache.set(k, null); return null; }); } function previewSrc(orderNo, key) { if (!key) return null; return localPreview.get(orderNo + ':' + key) || previewCache.get(orderNo + ':' + key) || null; } /* ---------------- 本地订单(离线下单 / 本机保存) ---------------- */ function bagPreview(no, design, side) { const k = no + ':' + side; if (localPreview.has(k)) return localPreview.get(k); let url = null; try { const R = window.PL.render; const D = window.PL.data; if (R && D) { const prod = D.productById(design.productId) || D.products[0]; const s = design.sizeId === 'custom' && design.customSize ? { id: 'custom', label: '自定义', w: design.customSize.w, h: design.customSize.h } : D.sizeOf(prod, design.sizeId); const paint = design.bagColor ? Object.assign({}, prod, { mockColor: design.bagColor }) : prod; const sd = (design.sides && design.sides[side]) || { bg: null, elements: [] }; const svg = R.bagSVG(paint, side, sd, { size: s, card: true, material: design.material, finish: design.finish }); url = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg); } } catch (e) { url = null; } localPreview.set(k, url); return url; } function bagSummary(design) { if (!design) return '包装袋'; const D = window.PL.data; if (!D) return '包装袋'; try { const p = D.productById(design.productId); const s = design.sizeId === 'custom' && design.customSize ? design.customSize : D.sizeOf(p, design.sizeId); return [p ? p.name : '包装袋', s ? (s.w + '×' + s.h + 'mm') : '', design.qty ? design.qty + ' 只' : ''] .filter(Boolean).join(' · '); } catch (e) { return '包装袋'; } } function bagLocalOrder(raw, key) { let design = raw.design; if (typeof design === 'string') { try { design = JSON.parse(design); } catch (e) { design = null; } } const no = raw.order || key.replace('pouchlab.order.', ''); const qty = (raw.spec && raw.spec.qty) || raw.qty || (design && design.qty) || 0; const total = Number(raw.total || 0); const previews = []; if (design) { ['front', 'back'].forEach(function (side) { if (bagPreview(no, design, side)) previews.push(side); }); } else if (raw.previews) { Object.keys(raw.previews).forEach(function (k) { const v = raw.previews[k]; if (!v) return; localPreview.set(no + ':' + k, /^data:/.test(v) ? v : 'data:image/png;base64,' + v); previews.push(k); }); } return { orderNo: no, type: 'bag', local: true, summary: raw.summary || bagSummary(design), spec: { qty: qty }, total: total, unitPrice: qty ? total / qty : 0, contactName: raw.name || '-', contactPhone: raw.phone || '-', contactEmail: raw.email || '', address: raw.address || '(本机订单未记录地址)', note: raw.note || '', payStatus: 'unpaid', status: 'pending_payment', createdAt: Math.floor((raw.at || Date.now()) / 1000), previews: previews }; } function boxLocalOrder(raw) { const no = raw.no || ('PLX' + (raw.time || Date.now())); const previews = []; Object.keys(raw.previews || {}).forEach(function (k) { const v = raw.previews[k]; if (!v) return; localPreview.set(no + ':' + k, /^data:/.test(v) ? v : 'data:image/png;base64,' + v); previews.push(k); }); // 列表缩略图优先展示 3D 效果 const shot = previews.indexOf('box3d'); if (shot > 0) previews.unshift(previews.splice(shot, 1)[0]); const qty = Number(raw.qty || 0); const total = Number(raw.total || 0); return { orderNo: no, type: 'box', local: true, summary: raw.summary || ('纸盒 ' + (raw.boxNo || '') + ' · ' + qty + ' 个'), spec: { qty: qty }, total: total, unitPrice: qty ? total / qty : 0, contactName: raw.name || '-', contactPhone: '', contactEmail: '', address: '(本机订单未记录地址)', note: '', payStatus: 'unpaid', status: 'pending_payment', createdAt: Math.floor((raw.time || Date.now()) / 1000), previews: previews }; } function buildLocalOrders() { const list = []; try { for (let i = 0; i < localStorage.length; i += 1) { const key = localStorage.key(i); if (!key || key.indexOf('pouchlab.order.') !== 0) continue; try { const raw = JSON.parse(localStorage.getItem(key) || 'null'); if (raw) list.push(bagLocalOrder(raw, key)); } catch (e) {} } } catch (e) {} try { const boxOrders = JSON.parse(localStorage.getItem('pouchlab.box.orders') || '[]'); if (Array.isArray(boxOrders)) boxOrders.forEach(function (raw) { if (raw && raw.no) list.push(boxLocalOrder(raw)); }); } catch (e) {} return list.sort(function (a, b) { return b.createdAt - a.createdAt; }); } function loadList() { state.loading = true; render(); PL.api.get('/orders/mine?page=' + state.page + '&page_size=' + state.pageSize).then(function (r) { state.items = r.items || []; // 本地订单(离线保存 / 盒子本机订单)与服务端订单按单号去重 if (state.page === 1) { state.local = buildLocalOrders().filter(function (o) { return !state.items.some(function (item) { return item.orderNo === o.orderNo; }); }); } state.total = (r.total || 0) + state.local.length; state.loading = false; render(); // 预取每行首图 state.items.forEach(function (o) { if (o.previews && o.previews.length) loadPreview(o.orderNo, o.previews[0]).then(function () { render(); }); }); }, function (err) { state.items = []; if (state.page === 1) state.local = buildLocalOrders(); state.total = state.local.length; state.loading = false; if (!err.network) UI.message.error(err.message || '订单加载失败'); render(); }); } function openDetail(order) { state.drawer = order; state.detail = null; render(); if (order.local) { state.detail = Object.assign({}, order, { events: [{ kind: 'status', title: '订单保存在本机', detail: '服务器未收到该订单,联网后请在设计页重新提交', createdAt: order.createdAt }] }); render(); return; } PL.api.get('/orders/mine/' + order.orderNo).then(function (d) { state.detail = d; render(); (d.previews || []).forEach(function (key) { loadPreview(d.orderNo, key).then(function () { render(); }); }); }, function (err) { UI.message.error(err.message || '详情加载失败'); state.drawer = null; render(); }); } function refreshDetail(order) { PL.api.get('/orders/mine/' + order.orderNo).then(function (full) { if (state.drawer && state.drawer.orderNo === order.orderNo) { state.detail = full; render(); } }, function () {}); } function doPay(order) { state.detailBusy = true; render(); PL.api.post('/orders/mine/' + order.orderNo + '/pay').then(function (d) { state.detailBusy = false; state.detail = d; UI.message.success('支付成功(模拟),订单已进入生产'); loadList(); refreshDetail(order); }, function (err) { state.detailBusy = false; UI.message.error(err.message || '支付失败'); render(); }); } function doCancel(order) { state.detailBusy = true; render(); PL.api.post('/orders/mine/' + order.orderNo + '/cancel').then(function (d) { state.detailBusy = false; state.detail = d; UI.message.success('订单已取消'); loadList(); refreshDetail(order); }, function (err) { state.detailBusy = false; UI.message.error(err.message || '取消失败'); render(); }); } function copyTracking(no) { if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(no).then(function () { UI.message.success('运单号已复制'); }); } } /* ---------------- 视图 ---------------- */ function Header(user) { return h('div', { className: 'pl-orders-header' }, h('div', { className: 'pl-orders-wrap' }, h('a', { className: 'logo', href: 'index.html' }, h('span', { className: 'logo-mark' }, h('svg', { viewBox: '0 0 24 24', fill: 'none' }, h('path', { d: 'M7 8.2h10l1 10.1a2.8 2.8 0 0 1-2.8 3H8.8A2.8 2.8 0 0 1 6 18.3L7 8.2Z', fill: '#fffdf8' }), h('path', { d: 'M7.4 11.1h9.2', stroke: '#17140f', 'stroke-width': 1.4, 'stroke-linecap': 'round' }), h('circle', { cx: 14.2, cy: 11.1, r: 1.15, fill: '#ff5c2b' }), h('path', { d: 'M9.5 14.4h4.9', stroke: '#ff5c2b', 'stroke-width': 1.4, 'stroke-linecap': 'round' }), h('path', { d: 'M17.8 5.2v2.4M16.6 6.4h2.4', stroke: '#17140f', 'stroke-width': 1.2, 'stroke-linecap': 'round' }))), h('span', { className: 'logo-type' }, h('b', null, '袋研社'), h('span', null, 'POUCHLAB'))), h('span', { className: 'spacer' }), h('a', { className: 'pl-nav', href: 'studio.html' }, '包装袋设计'), h('a', { className: 'pl-nav', href: 'box.html' }, '纸盒设计'), user ? h(A.Button, { size: 'small', onClick: function () { window.PL.auth.logout(); } }, '退出登录') : h(A.Button, { size: 'small', onClick: function () { window.PL.auth.open(); } }, '登录') ) ); } function LoginGate() { return h('div', { style: { padding: '90px 0', background: '#fffdf8', borderRadius: 16, border: '1px solid #e2dacb' } }, h(A.Empty, { description: '登录后可查看订单的支付状态、生产进度与物流信息', image: A.Empty.PRESENTED_IMAGE_SIMPLE }), h('div', { style: { textAlign: 'center', marginTop: 18 } }, h(A.Button, { type: 'primary', size: 'large', onClick: function () { window.PL.auth.open(); } }, '手机号登录') ) ); } function OrderCard(order) { const key = order.previews && order.previews.length ? order.previews[0] : null; const thumb = previewSrc(order.orderNo, key); return h('div', { className: 'pl-orders-card', key: order.orderNo, onClick: function () { openDetail(order); } }, h('div', { className: 'pl-orders-thumb' }, thumb ? h('img', { src: thumb }) : null), h('div', { className: 'pl-orders-info' }, h('div', { className: 'pl-orders-no' }, order.orderNo, h(A.Tag, { style: { marginLeft: 10 } }, TYPE_LABEL[order.type] || order.type), order.local ? h(A.Tag, { color: 'orange', style: { marginLeft: 6 } }, '本机保存') : null), h('div', { className: 'pl-orders-summary' }, order.summary || '-'), h('div', { className: 'pl-orders-time' }, '下单时间 ' + fmtTime(order.createdAt)) ), h('div', { className: 'pl-orders-side' }, h('div', { className: 'pl-orders-amount' }, '¥' + Number(order.total).toFixed(2), h('small', null, ' / ' + ((order.spec && order.spec.qty) || '-') + ' 件')), h('div', { className: 'pl-orders-tags' }, payTag(order.payStatus), statusTag(order.status)) ) ); } function DetailDrawer() { const order = state.detail || state.drawer || {}; const d = state.detail; const spec = order.spec || {}; const canPay = !order.local && order.payStatus === 'unpaid' && (order.status === 'pending_payment'); const canCancel = !order.local && order.payStatus === 'unpaid' && ['pending_payment', 'producing'].indexOf(order.status) >= 0; const events = d ? d.events || [] : []; const shipped = d && d.carrier; return h(A.Drawer, { open: !!state.drawer, width: 560, title: '订单详情 ' + (order.orderNo || ''), onClose: function () { state.drawer = null; state.detail = null; render(); }, footer: null }, d ? h('div', null, order.local ? h(A.Alert, { type: 'warning', showIcon: true, message: '该订单保存在本机,尚未同步到服务器', description: '联网后可在设计页重新提交;设计稿与预览图已随订单保存在本机。', style: { marginBottom: 14 } }) : null, (d.previews || []).length ? h('div', { className: 'pl-preview-grid' }, d.previews.map(function (key) { const url = previewSrc(d.orderNo, key); return url ? h('img', { key: key, src: url, title: key }) : null; }) ) : null, h(A.Descriptions, { title: '规格与金额', column: 2, size: 'small', bordered: true, style: { marginTop: 14 } }, h(A.Descriptions.Item, { label: '类型', span: 1 }, TYPE_LABEL[d.type] || d.type), h(A.Descriptions.Item, { label: '数量', span: 1 }, (spec.qty || '-') + ' 件'), h(A.Descriptions.Item, { label: '规格', span: 2 }, d.summary || '-'), h(A.Descriptions.Item, { label: '单价', span: 1 }, '¥' + Number(d.unitPrice || 0).toFixed(2)), h(A.Descriptions.Item, { label: '总额', span: 1 }, h('b', { style: { color: '#ff5c2b' } }, '¥' + Number(d.total || 0).toFixed(2))), h(A.Descriptions.Item, { label: '支付状态', span: 1 }, payTag(d.payStatus)), h(A.Descriptions.Item, { label: '订单状态', span: 1 }, statusTag(d.status)) ), h(A.Descriptions, { title: '收货信息', column: 1, size: 'small', bordered: true, style: { marginTop: 18 } }, h(A.Descriptions.Item, { label: '联系人' }, d.contactName + (d.contactPhone ? ' · ' + d.contactPhone : '')), h(A.Descriptions.Item, { label: '地址' }, d.address || '-'), d.note ? h(A.Descriptions.Item, { label: '备注' }, d.note) : null ), shipped ? h(A.Descriptions, { title: '物流信息', column: 1, size: 'small', bordered: true, style: { marginTop: 18 } }, h(A.Descriptions.Item, { label: '承运商' }, d.carrier), h(A.Descriptions.Item, { label: '运单号' }, h('span', null, h('b', { style: { letterSpacing: '0.04em' } }, d.trackingNo), ' ', h(A.Button, { size: 'small', type: 'link', onClick: function () { copyTracking(d.trackingNo); } }, '复制'))) ) : h(A.Alert, { type: d.status === 'completed' || d.status === 'cancelled' ? 'info' : 'warning', message: d.status === 'shipped' ? '物流信息录入中' : '尚未发货', description: d.status === 'cancelled' ? '订单已取消' : '工厂发货后将在此展示快递公司与运单号,可直接复制查询。', showIcon: true, style: { marginTop: 18 } }), h('div', { style: { margin: '20px 0 6px', fontWeight: 700 } }, '进度时间线'), h(A.Timeline, { items: events.slice().reverse().map(function (e) { return { color: e.kind === 'logistics' ? 'geekblue' : (e.kind === 'status' && e.title.indexOf('取消') >= 0 ? 'gray' : 'green'), children: h('div', null, h('div', { style: { fontWeight: 600 } }, e.title), e.detail ? h('div', { style: { fontSize: 12, color: '#7d7466' } }, e.detail) : null, h('div', { style: { fontSize: 11, color: '#a79d8d' } }, fmtTime(e.createdAt)) ) }; }) }), (canPay || canCancel) ? h('div', { style: { display: 'flex', gap: 10, marginTop: 16 } }, canPay ? h(A.Popconfirm, { title: '模拟支付', description: '确认支付 ¥' + Number(order.total).toFixed(2) + '?(演示环境不会真实扣款)', onConfirm: function () { doPay(order); } }, h(A.Button, { type: 'primary', loading: state.detailBusy }, '立即支付')) : null, canCancel ? h(A.Popconfirm, { title: '取消订单', description: '确定取消该订单吗?', onConfirm: function () { doCancel(order); } }, h(A.Button, { danger: true }, '取消订单')) : null ) : null ) : h('div', { style: { padding: 60, textAlign: 'center' } }, h(A.Spin, null)) ); } function App() { const user = window.PL.auth ? window.PL.auth.user() : null; const locals = state.page === 1 ? state.local : []; const list = locals.concat(state.items); return h('div', null, Header(user), h('main', { className: 'pl-orders-main' }, !user ? LoginGate() : h('div', null, h('div', { style: { display: 'flex', alignItems: 'center', marginBottom: 16 } }, h('h2', { style: { margin: 0, fontSize: 22, letterSpacing: '-0.02em' } }, '我的订单'), h('span', { style: { color: '#a79d8d', fontSize: 12.5, marginLeft: 10 } }, '共 ' + state.total + ' 单')), state.loading && !list.length ? h('div', { style: { padding: 80, textAlign: 'center' } }, h(A.Spin, null)) : (list.length ? h('div', null, list.map(OrderCard), state.total > state.pageSize ? h('div', { style: { textAlign: 'center', marginTop: 20 } }, h(A.Pagination, { current: state.page, total: state.total, pageSize: state.pageSize, showSizeChanger: false, onChange: function (p) { state.page = p; loadList(); } })) : null) : h(A.Empty, { description: '还没有订单,去设计一款包装吧', image: A.Empty.PRESENTED_IMAGE_SIMPLE }, h(A.Button, { type: 'primary', href: 'studio.html' }, '开始设计'))), DetailDrawer() ) ) ); } function render() { UI.render(document.getElementById('root'), App()); } document.addEventListener('pl:auth', function () { const user = window.PL.auth ? window.PL.auth.user() : null; if (user) { state.page = 1; loadList(); } else { state.items = []; state.local = []; state.total = 0; } render(); }); document.addEventListener('pl:auth-expired', function () { state.items = []; state.local = []; state.total = 0; state.drawer = null; state.detail = null; UI.message.warning('登录已失效,请重新登录'); render(); }); const user = window.PL.auth ? window.PL.auth.user() : null; if (user) loadList(); render(); })();