plDriverPlanCopy.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * 司机运输计划单「复制发群」纯文本(不含附件)
  3. */
  4. const DASH = '—'
  5. function stopTypeOf(stop) {
  6. return String(stop?.stopType || stop?.stop_type || '').trim()
  7. }
  8. function customerNameOf(stop) {
  9. const name = String(stop?.customerName || stop?.customer_name || '').trim()
  10. return name || DASH
  11. }
  12. function formatActualQtyLine(partyLabel, product) {
  13. const unit = String(product?.unit || '').trim()
  14. const raw = product?.actualQty ?? product?.actual_qty
  15. let qtyPart = DASH
  16. if (raw != null && raw !== '') {
  17. qtyPart = String(raw)
  18. if (unit) qtyPart = `${qtyPart} ${unit}`
  19. }
  20. return `${partyLabel}:${qtyPart}`
  21. }
  22. function appendQtySection(lines, title, qtyLines) {
  23. lines.push(title)
  24. if (!qtyLines.length) {
  25. lines.push(DASH)
  26. } else {
  27. lines.push(...qtyLines)
  28. }
  29. }
  30. /**
  31. * @param {object} page driverPlanApi.get 详情
  32. * @returns {string}
  33. */
  34. export function buildDriverPlanCopyText(page = {}) {
  35. const stops = Array.isArray(page?.stops) ? page.stops : []
  36. const loadStop = stops.find((s) => stopTypeOf(s) === 'load')
  37. const deliveryStops = stops.filter((s) => stopTypeOf(s) === 'delivery')
  38. const rawSeq = page?.copySeqNo ?? page?.copy_seq_no
  39. const seqText = rawSeq != null && rawSeq !== '' ? String(rawSeq) : DASH
  40. const lines = [
  41. `序号:${seqText}`,
  42. `计划编号:${String(page?.planNo || page?.plan_no || '').trim() || DASH}`,
  43. `装货方:${loadStop ? customerNameOf(loadStop) : DASH}`
  44. ]
  45. if (!deliveryStops.length) {
  46. lines.push(`送货方#1:${DASH}`)
  47. } else {
  48. deliveryStops.forEach((stop, index) => {
  49. lines.push(`送货方#${index + 1}:${customerNameOf(stop)}`)
  50. })
  51. }
  52. const loadQtyLines = []
  53. if (loadStop) {
  54. const products = Array.isArray(loadStop.products) ? loadStop.products : []
  55. products.forEach((product) => {
  56. loadQtyLines.push(formatActualQtyLine('装货方', product))
  57. })
  58. }
  59. appendQtySection(lines, '实际装货量:', loadQtyLines)
  60. const deliveryQtyLines = []
  61. deliveryStops.forEach((stop, index) => {
  62. const products = Array.isArray(stop?.products) ? stop.products : []
  63. products.forEach((product) => {
  64. deliveryQtyLines.push(formatActualQtyLine(`送货方#${index + 1}`, product))
  65. })
  66. })
  67. appendQtySection(lines, '实际送货量:', deliveryQtyLines)
  68. return lines.join('\n')
  69. }
  70. /** 复制到剪贴板;成功返回 true */
  71. export function copyDriverPlanToClipboard(page) {
  72. const text = buildDriverPlanCopyText(page)
  73. if (!text) {
  74. uni.showToast({ title: '暂无可复制内容', icon: 'none' })
  75. return Promise.resolve(false)
  76. }
  77. return new Promise((resolve) => {
  78. uni.setClipboardData({
  79. data: text,
  80. success: () => {
  81. uni.showToast({ title: '已复制', icon: 'success' })
  82. resolve(true)
  83. },
  84. fail: () => {
  85. uni.showToast({ title: '复制失败', icon: 'none' })
  86. resolve(false)
  87. }
  88. })
  89. })
  90. }