order-detail.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // order-detail.js
  2. Page({
  3. data: {
  4. order: null,
  5. mapCenter: {
  6. latitude: 39.9042,
  7. longitude: 116.4074
  8. },
  9. mapScale: 14
  10. },
  11. onLoad(options) {
  12. const orderId = parseInt(options.id)
  13. const app = getApp()
  14. const orders = app.getOrders()
  15. const order = orders.find(o => o.id === orderId)
  16. if (order) {
  17. this.setData({
  18. order: order
  19. })
  20. // 如果订单有地图数据,设置地图中心点和缩放级别
  21. if (order.mapTracking && order.mapTracking.markers) {
  22. this.setupMap(order.mapTracking.markers)
  23. }
  24. } else {
  25. wx.showToast({
  26. title: '订单不存在',
  27. icon: 'none'
  28. })
  29. setTimeout(() => {
  30. wx.navigateBack()
  31. }, 1500)
  32. }
  33. },
  34. // 设置地图中心点和缩放级别
  35. setupMap(markers) {
  36. if (markers.length === 0) return
  37. // 计算所有标记点的中心点
  38. let totalLat = 0
  39. let totalLng = 0
  40. markers.forEach(marker => {
  41. totalLat += marker.latitude
  42. totalLng += marker.longitude
  43. })
  44. const centerLat = totalLat / markers.length
  45. const centerLng = totalLng / markers.length
  46. // 计算合适的缩放级别(基于标记点之间的距离)
  47. let minLat = Infinity
  48. let maxLat = -Infinity
  49. let minLng = Infinity
  50. let maxLng = -Infinity
  51. markers.forEach(marker => {
  52. minLat = Math.min(minLat, marker.latitude)
  53. maxLat = Math.max(maxLat, marker.latitude)
  54. minLng = Math.min(minLng, marker.longitude)
  55. maxLng = Math.max(maxLng, marker.longitude)
  56. })
  57. const latDiff = maxLat - minLat
  58. const lngDiff = maxLng - minLng
  59. const maxDiff = Math.max(latDiff, lngDiff)
  60. // 根据距离设置缩放级别
  61. let scale = 14
  62. if (maxDiff > 0.01) {
  63. scale = 12
  64. } else if (maxDiff > 0.005) {
  65. scale = 13
  66. } else if (maxDiff > 0.002) {
  67. scale = 14
  68. } else {
  69. scale = 15
  70. }
  71. this.setData({
  72. mapCenter: {
  73. latitude: centerLat,
  74. longitude: centerLng
  75. },
  76. mapScale: scale
  77. })
  78. },
  79. // 返回上一页
  80. goBack() {
  81. wx.navigateBack({
  82. delta: 1
  83. })
  84. },
  85. // 联系客服
  86. contactSupport() {
  87. wx.showModal({
  88. title: '联系客服',
  89. content: '客服电话:400-123-4567\n工作时间:9:00-18:00',
  90. showCancel: false
  91. })
  92. },
  93. // 再次购买
  94. reorder() {
  95. const order = this.data.order
  96. wx.showModal({
  97. title: '再次购买',
  98. content: `确认再次购买${order.hospital}的药品吗?`,
  99. success: (res) => {
  100. if (res.confirm) {
  101. // 跳转到下单页面
  102. wx.switchTab({
  103. url: '/pages/order/order'
  104. })
  105. // 延迟设置搜索关键词
  106. setTimeout(() => {
  107. const pages = getCurrentPages()
  108. const orderPage = pages.find(page => page.route === 'pages/order/order')
  109. if (orderPage) {
  110. orderPage.setData({
  111. currentTab: 0,
  112. hospitalKeyword: order.hospital
  113. })
  114. }
  115. }, 500)
  116. }
  117. }
  118. })
  119. },
  120. // 删除订单
  121. deleteOrder() {
  122. const order = this.data.order
  123. wx.showModal({
  124. title: '确认删除',
  125. content: '确定要删除这个订单吗?删除后无法恢复。',
  126. success: (res) => {
  127. if (res.confirm) {
  128. const app = getApp()
  129. app.deleteOrder(order.id)
  130. wx.showToast({
  131. title: '订单已删除',
  132. icon: 'success',
  133. success: () => {
  134. setTimeout(() => {
  135. wx.navigateBack()
  136. }, 1500)
  137. }
  138. })
  139. }
  140. }
  141. }
  142. )
  143. }
  144. })