From 41d66aca6fd98d431191a42c6ffa9a5f1f94a125 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 28 Oct 2026 21:34:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=AE=A2=E6=88=B7=E5=95=86=E5=9F=8E):=20?= =?UTF-8?q?=E4=B8=8B=E5=8D=95=E5=AE=A2=E6=88=B7=E5=95=86=E5=93=81=E6=BA=AF?= =?UTF-8?q?=E6=BA=90=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit order_customer 只能看自己 customer_portal_users 绑定订单;首页内嵌 customer-trace-panel;不返回内部字段。 --- .../components/customer-trace-panel/index.js | 310 +++++++++++++++++ .../customer-trace-panel/index.json | 3 + .../customer-trace-panel/index.wxml | 138 ++++++++ .../customer-trace-panel/index.wxss | 262 +++++++++++++++ miniprogram/modules/registry.js | 8 + miniprogram/pages/home/home.js | 2 + miniprogram/pages/home/home.json | 1 + miniprogram/pages/home/home.wxml | 6 + miniprogram/utils/request.js | 18 + server/src/routes/mobile.ts | 2 + .../mobile/mobileCustomerTraceRoutes.ts | 65 ++++ .../services/customerTraceAccessService.ts | 39 +++ server/src/services/customerTraceService.ts | 315 ++++++++++++++++++ server/tests/customerTraceAndMall.test.ts | 36 ++ 14 files changed, 1205 insertions(+) create mode 100644 miniprogram/components/customer-trace-panel/index.js create mode 100644 miniprogram/components/customer-trace-panel/index.json create mode 100644 miniprogram/components/customer-trace-panel/index.wxml create mode 100644 miniprogram/components/customer-trace-panel/index.wxss create mode 100644 server/src/routes/mobile/mobileCustomerTraceRoutes.ts create mode 100644 server/src/services/customerTraceAccessService.ts create mode 100644 server/src/services/customerTraceService.ts create mode 100644 server/tests/customerTraceAndMall.test.ts diff --git a/miniprogram/components/customer-trace-panel/index.js b/miniprogram/components/customer-trace-panel/index.js new file mode 100644 index 0000000..46e9773 --- /dev/null +++ b/miniprogram/components/customer-trace-panel/index.js @@ -0,0 +1,310 @@ +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() {}, + }, +}); diff --git a/miniprogram/components/customer-trace-panel/index.json b/miniprogram/components/customer-trace-panel/index.json new file mode 100644 index 0000000..467ce29 --- /dev/null +++ b/miniprogram/components/customer-trace-panel/index.json @@ -0,0 +1,3 @@ +{ + "component": true +} diff --git a/miniprogram/components/customer-trace-panel/index.wxml b/miniprogram/components/customer-trace-panel/index.wxml new file mode 100644 index 0000000..956037d --- /dev/null +++ b/miniprogram/components/customer-trace-panel/index.wxml @@ -0,0 +1,138 @@ + + + + + {{dateFrom}} + + 至 + + {{dateTo}} + + + + + + + + + 共 {{total}} 条 + + 加载中… + {{emptyText}} + + + + {{item.customer_name}} + + {{item.delivery_date}} + + + + + {{item.order_code}} + 商品 {{item.item_count}} 项 + + + + 加载中… + {{detailError}} + + 暂无溯源商品 + + + 商品 + 实收 + 报告 + 入库 + 分拣 + 配送拍照 + 送达 + + + + {{sku.item_name}} + {{sku.actual_qty}} {{sku.unit}} + + + + + {{report.label}} + + + + {{file.ext}} + + + + + - + + + + + + + - + + + + + + + + + diff --git a/miniprogram/components/customer-trace-panel/index.wxss b/miniprogram/components/customer-trace-panel/index.wxss new file mode 100644 index 0000000..91b7299 --- /dev/null +++ b/miniprogram/components/customer-trace-panel/index.wxss @@ -0,0 +1,262 @@ +/* 客户溯源面板:订单列表 + 详情就地展开(2026-10-24,替换原两个二级页) */ +.customer-trace-panel { + min-height: 100vh; + background: #f5f7f6; + padding: 20rpx; + box-sizing: border-box; +} + +.customer-trace-panel.is-embedded { + min-height: 0; + background: transparent; + padding: 8rpx var(--xd-module-grid-side, 16rpx) calc(170rpx + env(safe-area-inset-bottom)); +} + +.filter-card { + background: #fff; + border-radius: 16rpx; + padding: 20rpx; + margin-bottom: 16rpx; +} + +.date-row { + display: flex; + align-items: center; + gap: 12rpx; +} + +.date-pill { + background: #f3f6f4; + color: #1f2937; + border-radius: 10rpx; + padding: 10rpx 16rpx; + font-size: 26rpx; +} + +.date-sep { + color: #9ca3af; + font-size: 24rpx; +} + +.search-row { + display: flex; + align-items: center; + gap: 12rpx; + margin-top: 16rpx; +} + +.search-input { + flex: 1; + background: #f3f6f4; + border-radius: 10rpx; + padding: 12rpx 16rpx; + font-size: 26rpx; +} + +.search-btn { + margin: 0 !important; + background: #059669 !important; + color: #fff !important; + border-radius: 10rpx !important; +} + +.list-summary { + color: #6b7280; + font-size: 24rpx; + padding: 4rpx 8rpx 14rpx; +} + +.tip { + text-align: center; + color: #9ca3af; + font-size: 26rpx; + padding: 80rpx 0; +} + +.order-card { + background: #fff; + border-radius: 16rpx; + padding: 22rpx; + margin-bottom: 14rpx; +} + +.order-card.is-open { + border: 1rpx solid #d1fae5; +} + +.order-head { + display: flex; + align-items: center; + justify-content: space-between; +} + +.order-head-right { + display: flex; + align-items: center; + gap: 12rpx; +} + +.order-customer { + font-size: 30rpx; + font-weight: 600; + color: #111827; +} + +.order-date { + font-size: 24rpx; + color: #6b7280; +} + +/* 展开态指示:纯 CSS 箭头(不新增可见文字),向下=可展开、向上=可收起 */ +.order-caret { + width: 14rpx; + height: 14rpx; + border-right: 3rpx solid #9ca3af; + border-bottom: 3rpx solid #9ca3af; + transform: rotate(45deg); + transition: transform 0.2s ease; +} + +.order-caret.is-open { + transform: rotate(-135deg); +} + +.order-meta { + display: flex; + align-items: center; + justify-content: space-between; + margin-top: 12rpx; +} + +.order-code { + font-size: 22rpx; + color: #6b7280; + font-family: monospace; +} + +.order-count { + font-size: 24rpx; + color: #059669; +} + +.order-detail { + margin-top: 18rpx; + border-top: 1rpx solid #f0f2f1; + padding-top: 8rpx; +} + +/* ── 商品表:整屏铺满、超出隐藏,一行一个商品 ── */ +.trace-table { + margin-top: 14rpx; + width: 100%; + border: 1rpx solid #eef4f1; + border-radius: 12rpx; + overflow: hidden; +} + +.trace-tr { + display: flex; + align-items: stretch; + border-top: 1rpx solid #f0f2f1; +} + +.trace-thead { + border-top: none; + background: #f6faf8; +} + +.trace-th, +.trace-td { + box-sizing: border-box; + padding: 10rpx 6rpx; + font-size: 21rpx; + color: #1f2937; + display: flex; + flex-direction: column; + align-items: flex-start; + justify-content: center; + overflow: hidden; + white-space: normal; + word-break: break-all; +} + +.trace-th { + font-size: 18rpx; + font-weight: 600; + color: #6b7280; + line-height: 1.25; +} + +.trace-td.col-name { + font-weight: 600; + font-size: 23rpx; + line-height: 1.35; + color: #111827; +} + +.col-name { + width: 22%; +} + +.col-qty { + width: 12%; + font-size: 19rpx; + color: #059669; +} + +.col-reports { + width: 14%; +} + +.col-stage { + width: 13%; +} + +.stage-time { + font-size: 17rpx; + color: #9ca3af; + line-height: 1.35; +} + +.thumb-row { + display: flex; + flex-wrap: wrap; + gap: 4rpx; +} + +.thumb { + width: 52rpx; + height: 52rpx; + border-radius: 6rpx; + background: #f3f4f6; +} + +.report-item { + width: 100%; + margin-bottom: 6rpx; +} + +.report-label { + display: block; + font-size: 16rpx; + color: #6b7280; + line-height: 1.35; +} + +.report-files { + display: flex; + flex-wrap: wrap; + gap: 4rpx; + margin-top: 3rpx; +} + +/* 文档类报告:用扩展名标签区分(PDF / DOC / XLS…),点击调用 openDocument */ +.report-doc { + padding: 1rpx 5rpx; + border-radius: 4rpx; + border: 1rpx solid #a7f3d0; + background: #ecfdf5; + color: #047857; + font-size: 15rpx; + line-height: 1.4; +} diff --git a/miniprogram/modules/registry.js b/miniprogram/modules/registry.js index c930467..29470d3 100644 --- a/miniprogram/modules/registry.js +++ b/miniprogram/modules/registry.js @@ -217,6 +217,14 @@ const MOBILE_MODULES = [ handler: 'goProductTrace', homeEmbed: 'product-trace-panel', }, + { + id: 'customer-trace', + title: '商品溯源', + subtitle: '查看您订单的商品、实收、照片与报告', + page: '/pages/home/home', + roles: ['order_customer'], + homeEmbed: 'customer-trace-panel', + }, { id: 'mall', title: '下单', diff --git a/miniprogram/pages/home/home.js b/miniprogram/pages/home/home.js index 2f32d14..261bd85 100644 --- a/miniprogram/pages/home/home.js +++ b/miniprogram/pages/home/home.js @@ -40,6 +40,7 @@ const HOME_PANEL_SELECTORS = { 'prep-amend-review': '#prepAmendHomePanel', 'order-entry': '#orderEntryHomePanel', mall: '#mallHomePanel', + 'customer-trace': '#customerTraceHomePanel', }; function pxToRpx(px, windowWidth) { @@ -138,6 +139,7 @@ function pickExclusiveHomeProgress(flags) { function pickDefaultHomeModule(workspaceIds, workspaceMods) { const order = ['mall', + 'customer-trace', 'purchase', 'admin-purchase', 'delivery', diff --git a/miniprogram/pages/home/home.json b/miniprogram/pages/home/home.json index 03ff256..1bb062f 100644 --- a/miniprogram/pages/home/home.json +++ b/miniprogram/pages/home/home.json @@ -24,6 +24,7 @@ "admin-preparations-panel": "/components/admin-preparations-panel/index", "order-entry-panel": "/components/order-entry-panel/index", "mall-panel": "/components/mall-panel/index", + "customer-trace-panel": "/components/customer-trace-panel/index", "van-icon": "@vant/weapp/icon/index", "van-tag": "@vant/weapp/tag/index", "van-button": "@vant/weapp/button/index", diff --git a/miniprogram/pages/home/home.wxml b/miniprogram/pages/home/home.wxml index e395634..e6752c9 100644 --- a/miniprogram/pages/home/home.wxml +++ b/miniprogram/pages/home/home.wxml @@ -438,6 +438,12 @@ embedded="{{true}}" style="--xd-module-grid-side: {{moduleGridSide}};" /> +