| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- /* 我的订单:登录门禁 + 订单列表 + 详情(支付状态 / 生产进度 / 物流) */
- (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, page: 1, pageSize: 8, loading: false,
- drawer: null, // 选中的订单(详情)
- detail: null, // 详情数据(含 events)
- detailBusy: false
- };
- const previewCache = new Map(); // "orderNo:key" -> objectURL
- 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 loadList() {
- state.loading = true;
- render();
- PL.api.get('/orders/mine?page=' + state.page + '&page_size=' + state.pageSize).then(function (r) {
- state.items = r.items || [];
- state.total = r.total || 0;
- 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.loading = false;
- if (!err.network) UI.message.error(err.message || '订单加载失败');
- render();
- });
- }
- function openDetail(order) {
- state.drawer = order;
- state.detail = null;
- render();
- 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 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();
- }, 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();
- }, 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 = key ? previewCache.get(order.orderNo + ':' + key) : null;
- 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)),
- 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.payStatus === 'unpaid' && (order.status === 'pending_payment');
- const canCancel = 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,
- (d.previews || []).length ? h('div', { className: 'pl-preview-grid' },
- d.previews.map(function (key) {
- const url = previewCache.get(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),
- 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;
- 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 && !state.items.length
- ? h('div', { style: { padding: 80, textAlign: 'center' } }, h(A.Spin, null))
- : (state.items.length
- ? h('div', null,
- state.items.map(OrderCard),
- 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(); }
- })))
- : 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.total = 0; }
- render();
- });
- document.addEventListener('pl:auth-expired', function () {
- state.items = []; 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();
- })();
|