orders.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* 我的订单:登录门禁 + 订单列表 + 详情(支付状态 / 生产进度 / 物流) */
  2. (function () {
  3. const h = window.React.createElement;
  4. const A = window.PL.ui.antd;
  5. const UI = window.PL.ui;
  6. const dayjs = window.dayjs;
  7. const state = {
  8. items: [], total: 0, page: 1, pageSize: 8, loading: false,
  9. drawer: null, // 选中的订单(详情)
  10. detail: null, // 详情数据(含 events)
  11. detailBusy: false
  12. };
  13. const previewCache = new Map(); // "orderNo:key" -> objectURL
  14. const STATUS_OPTS = {
  15. pending_payment: { label: '待支付', color: 'orange' },
  16. producing: { label: '生产中', color: 'processing' },
  17. shipped: { label: '已发货', color: 'geekblue' },
  18. completed: { label: '已完成', color: 'success' },
  19. cancelled: { label: '已取消', color: 'default' }
  20. };
  21. const PAY_OPTS = {
  22. unpaid: { label: '未支付', color: 'orange' },
  23. paid: { label: '已支付', color: 'success' },
  24. refunded: { label: '已退款', color: 'purple' }
  25. };
  26. const TYPE_LABEL = { bag: '包装袋', box: '纸盒' };
  27. function fmtTime(ts) { return ts ? dayjs(ts * 1000).format('YYYY-MM-DD HH:mm') : '-'; }
  28. function statusTag(s) {
  29. const o = STATUS_OPTS[s] || { label: s, color: 'default' };
  30. return h(A.Tag, { color: o.color }, o.label);
  31. }
  32. function payTag(p) {
  33. const o = PAY_OPTS[p] || { label: p, color: 'default' };
  34. return h(A.Tag, { color: o.color }, o.label);
  35. }
  36. function loadPreview(orderNo, key) {
  37. const k = orderNo + ':' + key;
  38. if (previewCache.has(k)) return Promise.resolve(previewCache.get(k));
  39. return fetch('/api/orders/mine/' + orderNo + '/preview/' + key, {
  40. headers: PL.api.token() ? { Authorization: 'Bearer ' + PL.api.token() } : {}
  41. }).then(function (res) {
  42. if (!res.ok) throw new Error('preview');
  43. return res.blob();
  44. }).then(function (blob) {
  45. const url = URL.createObjectURL(blob);
  46. previewCache.set(k, url);
  47. return url;
  48. }).catch(function () {
  49. previewCache.set(k, null);
  50. return null;
  51. });
  52. }
  53. function loadList() {
  54. state.loading = true;
  55. render();
  56. PL.api.get('/orders/mine?page=' + state.page + '&page_size=' + state.pageSize).then(function (r) {
  57. state.items = r.items || [];
  58. state.total = r.total || 0;
  59. state.loading = false;
  60. render();
  61. // 预取每行首图
  62. state.items.forEach(function (o) {
  63. if (o.previews && o.previews.length) loadPreview(o.orderNo, o.previews[0]).then(function () { render(); });
  64. });
  65. }, function (err) {
  66. state.loading = false;
  67. if (!err.network) UI.message.error(err.message || '订单加载失败');
  68. render();
  69. });
  70. }
  71. function openDetail(order) {
  72. state.drawer = order;
  73. state.detail = null;
  74. render();
  75. PL.api.get('/orders/mine/' + order.orderNo).then(function (d) {
  76. state.detail = d;
  77. render();
  78. (d.previews || []).forEach(function (key) {
  79. loadPreview(d.orderNo, key).then(function () { render(); });
  80. });
  81. }, function (err) {
  82. UI.message.error(err.message || '详情加载失败');
  83. state.drawer = null;
  84. render();
  85. });
  86. }
  87. function doPay(order) {
  88. state.detailBusy = true;
  89. render();
  90. PL.api.post('/orders/mine/' + order.orderNo + '/pay').then(function (d) {
  91. state.detailBusy = false;
  92. state.detail = d;
  93. UI.message.success('支付成功(模拟),订单已进入生产');
  94. loadList();
  95. }, function (err) {
  96. state.detailBusy = false;
  97. UI.message.error(err.message || '支付失败');
  98. render();
  99. });
  100. }
  101. function doCancel(order) {
  102. state.detailBusy = true;
  103. render();
  104. PL.api.post('/orders/mine/' + order.orderNo + '/cancel').then(function (d) {
  105. state.detailBusy = false;
  106. state.detail = d;
  107. UI.message.success('订单已取消');
  108. loadList();
  109. }, function (err) {
  110. state.detailBusy = false;
  111. UI.message.error(err.message || '取消失败');
  112. render();
  113. });
  114. }
  115. function copyTracking(no) {
  116. if (navigator.clipboard && navigator.clipboard.writeText) {
  117. navigator.clipboard.writeText(no).then(function () { UI.message.success('运单号已复制'); });
  118. }
  119. }
  120. /* ---------------- 视图 ---------------- */
  121. function Header(user) {
  122. return h('div', { className: 'pl-orders-header' },
  123. h('div', { className: 'pl-orders-wrap' },
  124. h('a', { className: 'logo', href: 'index.html' },
  125. h('span', { className: 'logo-mark' },
  126. h('svg', { viewBox: '0 0 24 24', fill: 'none' },
  127. 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' }),
  128. h('path', { d: 'M7.4 11.1h9.2', stroke: '#17140f', 'stroke-width': 1.4, 'stroke-linecap': 'round' }),
  129. h('circle', { cx: 14.2, cy: 11.1, r: 1.15, fill: '#ff5c2b' }),
  130. h('path', { d: 'M9.5 14.4h4.9', stroke: '#ff5c2b', 'stroke-width': 1.4, 'stroke-linecap': 'round' }),
  131. h('path', { d: 'M17.8 5.2v2.4M16.6 6.4h2.4', stroke: '#17140f', 'stroke-width': 1.2, 'stroke-linecap': 'round' }))),
  132. h('span', { className: 'logo-type' }, h('b', null, '袋研社'), h('span', null, 'POUCHLAB'))),
  133. h('span', { className: 'spacer' }),
  134. h('a', { className: 'pl-nav', href: 'studio.html' }, '包装袋设计'),
  135. h('a', { className: 'pl-nav', href: 'box.html' }, '纸盒设计'),
  136. user
  137. ? h(A.Button, { size: 'small', onClick: function () { window.PL.auth.logout(); } }, '退出登录')
  138. : h(A.Button, { size: 'small', onClick: function () { window.PL.auth.open(); } }, '登录')
  139. )
  140. );
  141. }
  142. function LoginGate() {
  143. return h('div', { style: { padding: '90px 0', background: '#fffdf8', borderRadius: 16, border: '1px solid #e2dacb' } },
  144. h(A.Empty, {
  145. description: '登录后可查看订单的支付状态、生产进度与物流信息',
  146. image: A.Empty.PRESENTED_IMAGE_SIMPLE
  147. }),
  148. h('div', { style: { textAlign: 'center', marginTop: 18 } },
  149. h(A.Button, { type: 'primary', size: 'large', onClick: function () { window.PL.auth.open(); } }, '手机号登录')
  150. )
  151. );
  152. }
  153. function OrderCard(order) {
  154. const key = order.previews && order.previews.length ? order.previews[0] : null;
  155. const thumb = key ? previewCache.get(order.orderNo + ':' + key) : null;
  156. return h('div', { className: 'pl-orders-card', key: order.orderNo, onClick: function () { openDetail(order); } },
  157. h('div', { className: 'pl-orders-thumb' }, thumb ? h('img', { src: thumb }) : null),
  158. h('div', { className: 'pl-orders-info' },
  159. h('div', { className: 'pl-orders-no' }, order.orderNo,
  160. h(A.Tag, { style: { marginLeft: 10 } }, TYPE_LABEL[order.type] || order.type)),
  161. h('div', { className: 'pl-orders-summary' }, order.summary || '-'),
  162. h('div', { className: 'pl-orders-time' }, '下单时间 ' + fmtTime(order.createdAt))
  163. ),
  164. h('div', { className: 'pl-orders-side' },
  165. h('div', { className: 'pl-orders-amount' }, '¥' + Number(order.total).toFixed(2),
  166. h('small', null, ' / ' + ((order.spec && order.spec.qty) || '-') + ' 件')),
  167. h('div', { className: 'pl-orders-tags' },
  168. payTag(order.payStatus),
  169. statusTag(order.status))
  170. )
  171. );
  172. }
  173. function DetailDrawer() {
  174. const order = state.detail || state.drawer || {};
  175. const d = state.detail;
  176. const spec = order.spec || {};
  177. const canPay = order.payStatus === 'unpaid' && (order.status === 'pending_payment');
  178. const canCancel = order.payStatus === 'unpaid' && ['pending_payment', 'producing'].indexOf(order.status) >= 0;
  179. const events = d ? d.events || [] : [];
  180. const shipped = d && d.carrier;
  181. return h(A.Drawer, {
  182. open: !!state.drawer, width: 560, title: '订单详情 ' + (order.orderNo || ''),
  183. onClose: function () { state.drawer = null; state.detail = null; render(); },
  184. footer: null
  185. },
  186. d ? h('div', null,
  187. (d.previews || []).length ? h('div', { className: 'pl-preview-grid' },
  188. d.previews.map(function (key) {
  189. const url = previewCache.get(d.orderNo + ':' + key);
  190. return url ? h('img', { key: key, src: url, title: key }) : null;
  191. })
  192. ) : null,
  193. h(A.Descriptions, { title: '规格与金额', column: 2, size: 'small', bordered: true, style: { marginTop: 14 } },
  194. h(A.Descriptions.Item, { label: '类型', span: 1 }, TYPE_LABEL[d.type] || d.type),
  195. h(A.Descriptions.Item, { label: '数量', span: 1 }, (spec.qty || '-') + ' 件'),
  196. h(A.Descriptions.Item, { label: '规格', span: 2 }, d.summary || '-'),
  197. h(A.Descriptions.Item, { label: '单价', span: 1 }, '¥' + Number(d.unitPrice || 0).toFixed(2)),
  198. h(A.Descriptions.Item, { label: '总额', span: 1 }, h('b', { style: { color: '#ff5c2b' } }, '¥' + Number(d.total || 0).toFixed(2))),
  199. h(A.Descriptions.Item, { label: '支付状态', span: 1 }, payTag(d.payStatus)),
  200. h(A.Descriptions.Item, { label: '订单状态', span: 1 }, statusTag(d.status))
  201. ),
  202. h(A.Descriptions, { title: '收货信息', column: 1, size: 'small', bordered: true, style: { marginTop: 18 } },
  203. h(A.Descriptions.Item, { label: '联系人' }, d.contactName + ' · ' + d.contactPhone),
  204. h(A.Descriptions.Item, { label: '地址' }, d.address || '-'),
  205. d.note ? h(A.Descriptions.Item, { label: '备注' }, d.note) : null
  206. ),
  207. shipped ? h(A.Descriptions, { title: '物流信息', column: 1, size: 'small', bordered: true, style: { marginTop: 18 } },
  208. h(A.Descriptions.Item, { label: '承运商' }, d.carrier),
  209. h(A.Descriptions.Item, { label: '运单号' },
  210. h('span', null, h('b', { style: { letterSpacing: '0.04em' } }, d.trackingNo), ' ',
  211. h(A.Button, { size: 'small', type: 'link', onClick: function () { copyTracking(d.trackingNo); } }, '复制')))
  212. ) : h(A.Alert, {
  213. type: d.status === 'completed' || d.status === 'cancelled' ? 'info' : 'warning',
  214. message: d.status === 'shipped' ? '物流信息录入中' : '尚未发货',
  215. description: d.status === 'cancelled' ? '订单已取消' : '工厂发货后将在此展示快递公司与运单号,可直接复制查询。',
  216. showIcon: true, style: { marginTop: 18 }
  217. }),
  218. h('div', { style: { margin: '20px 0 6px', fontWeight: 700 } }, '进度时间线'),
  219. h(A.Timeline, {
  220. items: events.slice().reverse().map(function (e) {
  221. return {
  222. color: e.kind === 'logistics' ? 'geekblue' : (e.kind === 'status' && e.title.indexOf('取消') >= 0 ? 'gray' : 'green'),
  223. children: h('div', null,
  224. h('div', { style: { fontWeight: 600 } }, e.title),
  225. e.detail ? h('div', { style: { fontSize: 12, color: '#7d7466' } }, e.detail) : null,
  226. h('div', { style: { fontSize: 11, color: '#a79d8d' } }, fmtTime(e.createdAt))
  227. )
  228. };
  229. })
  230. }),
  231. (canPay || canCancel) ? h('div', { style: { display: 'flex', gap: 10, marginTop: 16 } },
  232. canPay ? h(A.Popconfirm, {
  233. title: '模拟支付', description: '确认支付 ¥' + Number(order.total).toFixed(2) + '?(演示环境不会真实扣款)',
  234. onConfirm: function () { doPay(order); }
  235. }, h(A.Button, { type: 'primary', loading: state.detailBusy }, '立即支付')) : null,
  236. canCancel ? h(A.Popconfirm, {
  237. title: '取消订单', description: '确定取消该订单吗?',
  238. onConfirm: function () { doCancel(order); }
  239. }, h(A.Button, { danger: true }, '取消订单')) : null
  240. ) : null
  241. ) : h('div', { style: { padding: 60, textAlign: 'center' } }, h(A.Spin, null))
  242. );
  243. }
  244. function App() {
  245. const user = window.PL.auth ? window.PL.auth.user() : null;
  246. return h('div', null,
  247. Header(user),
  248. h('main', { className: 'pl-orders-main' },
  249. !user ? LoginGate() :
  250. h('div', null,
  251. h('div', { style: { display: 'flex', alignItems: 'center', marginBottom: 16 } },
  252. h('h2', { style: { margin: 0, fontSize: 22, letterSpacing: '-0.02em' } }, '我的订单'),
  253. h('span', { style: { color: '#a79d8d', fontSize: 12.5, marginLeft: 10 } }, '共 ' + state.total + ' 单')),
  254. state.loading && !state.items.length
  255. ? h('div', { style: { padding: 80, textAlign: 'center' } }, h(A.Spin, null))
  256. : (state.items.length
  257. ? h('div', null,
  258. state.items.map(OrderCard),
  259. h('div', { style: { textAlign: 'center', marginTop: 20 } },
  260. h(A.Pagination, {
  261. current: state.page, total: state.total, pageSize: state.pageSize,
  262. showSizeChanger: false,
  263. onChange: function (p) { state.page = p; loadList(); }
  264. })))
  265. : h(A.Empty, { description: '还没有订单,去设计一款包装吧', image: A.Empty.PRESENTED_IMAGE_SIMPLE },
  266. h(A.Button, { type: 'primary', href: 'studio.html' }, '开始设计'))),
  267. DetailDrawer()
  268. )
  269. )
  270. );
  271. }
  272. function render() {
  273. UI.render(document.getElementById('root'), App());
  274. }
  275. document.addEventListener('pl:auth', function () {
  276. const user = window.PL.auth ? window.PL.auth.user() : null;
  277. if (user) { state.page = 1; loadList(); } else { state.items = []; state.total = 0; }
  278. render();
  279. });
  280. document.addEventListener('pl:auth-expired', function () {
  281. state.items = []; state.total = 0; state.drawer = null; state.detail = null;
  282. UI.message.warning('登录已失效,请重新登录');
  283. render();
  284. });
  285. const user = window.PL.auth ? window.PL.auth.user() : null;
  286. if (user) loadList();
  287. render();
  288. })();