plDriverPlanCopy.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 lines = [
  39. `计划编号:${String(page?.planNo || page?.plan_no || '').trim() || DASH}`,
  40. `装货方:${loadStop ? customerNameOf(loadStop) : DASH}`
  41. ]
  42. if (!deliveryStops.length) {
  43. lines.push(`送货方#1:${DASH}`)
  44. } else {
  45. deliveryStops.forEach((stop, index) => {
  46. lines.push(`送货方#${index + 1}:${customerNameOf(stop)}`)
  47. })
  48. }
  49. const loadQtyLines = []
  50. if (loadStop) {
  51. const products = Array.isArray(loadStop.products) ? loadStop.products : []
  52. products.forEach((product) => {
  53. loadQtyLines.push(formatActualQtyLine('装货方', product))
  54. })
  55. }
  56. appendQtySection(lines, '实际装货量:', loadQtyLines)
  57. const deliveryQtyLines = []
  58. deliveryStops.forEach((stop, index) => {
  59. const products = Array.isArray(stop?.products) ? stop.products : []
  60. products.forEach((product) => {
  61. deliveryQtyLines.push(formatActualQtyLine(`送货方#${index + 1}`, product))
  62. })
  63. })
  64. appendQtySection(lines, '实际送货量:', deliveryQtyLines)
  65. return lines.join('\n')
  66. }
  67. /** 复制到剪贴板;成功返回 true */
  68. export function copyDriverPlanToClipboard(page) {
  69. const text = buildDriverPlanCopyText(page)
  70. if (!text) {
  71. uni.showToast({ title: '暂无可复制内容', icon: 'none' })
  72. return Promise.resolve(false)
  73. }
  74. return new Promise((resolve) => {
  75. uni.setClipboardData({
  76. data: text,
  77. success: () => {
  78. uni.showToast({ title: '已复制', icon: 'success' })
  79. resolve(true)
  80. },
  81. fail: () => {
  82. uni.showToast({ title: '复制失败', icon: 'none' })
  83. resolve(false)
  84. }
  85. })
  86. })
  87. }