/** * 司机运输计划单「复制发群」纯文本(不含附件) */ const DASH = '—' function stopTypeOf(stop) { return String(stop?.stopType || stop?.stop_type || '').trim() } function customerNameOf(stop) { const name = String(stop?.customerName || stop?.customer_name || '').trim() return name || DASH } function formatActualQtyLine(partyLabel, product) { const unit = String(product?.unit || '').trim() const raw = product?.actualQty ?? product?.actual_qty let qtyPart = DASH if (raw != null && raw !== '') { qtyPart = String(raw) if (unit) qtyPart = `${qtyPart} ${unit}` } return `${partyLabel}:${qtyPart}` } function appendQtySection(lines, title, qtyLines) { lines.push(title) if (!qtyLines.length) { lines.push(DASH) } else { lines.push(...qtyLines) } } /** * @param {object} page driverPlanApi.get 详情 * @returns {string} */ export function buildDriverPlanCopyText(page = {}) { const stops = Array.isArray(page?.stops) ? page.stops : [] const loadStop = stops.find((s) => stopTypeOf(s) === 'load') const deliveryStops = stops.filter((s) => stopTypeOf(s) === 'delivery') const lines = [ `计划编号:${String(page?.planNo || page?.plan_no || '').trim() || DASH}`, `装货方:${loadStop ? customerNameOf(loadStop) : DASH}` ] if (!deliveryStops.length) { lines.push(`送货方#1:${DASH}`) } else { deliveryStops.forEach((stop, index) => { lines.push(`送货方#${index + 1}:${customerNameOf(stop)}`) }) } const loadQtyLines = [] if (loadStop) { const products = Array.isArray(loadStop.products) ? loadStop.products : [] products.forEach((product) => { loadQtyLines.push(formatActualQtyLine('装货方', product)) }) } appendQtySection(lines, '实际装货量:', loadQtyLines) const deliveryQtyLines = [] deliveryStops.forEach((stop, index) => { const products = Array.isArray(stop?.products) ? stop.products : [] products.forEach((product) => { deliveryQtyLines.push(formatActualQtyLine(`送货方#${index + 1}`, product)) }) }) appendQtySection(lines, '实际送货量:', deliveryQtyLines) return lines.join('\n') } /** 复制到剪贴板;成功返回 true */ export function copyDriverPlanToClipboard(page) { const text = buildDriverPlanCopyText(page) if (!text) { uni.showToast({ title: '暂无可复制内容', icon: 'none' }) return Promise.resolve(false) } return new Promise((resolve) => { uni.setClipboardData({ data: text, success: () => { uni.showToast({ title: '已复制', icon: 'success' }) resolve(true) }, fail: () => { uni.showToast({ title: '复制失败', icon: 'none' }) resolve(false) } }) }) }