logistics-detail.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import DictionariesBehavior from "../../../../core/behavior/dictionaries.behavior";
  3. import tickleBehavior from "../../../../core/behavior/tickle.behavior";
  4. import { getLogisticsMethod } from "../../request"
  5. interface TraceItem {
  6. status: string;
  7. time: string;
  8. desc: string;
  9. timeKey?: string;
  10. }
  11. interface LogisticsTrack {
  12. time?: string;
  13. context?: string;
  14. areaName?: string;
  15. status?: string;
  16. location?: string;
  17. areaCode?: string | null;
  18. areaCenter?: any;
  19. areaPinYin?: string | null;
  20. statusCode?: any;
  21. }
  22. interface MapPoint {
  23. latitude: number;
  24. longitude: number;
  25. }
  26. Page({
  27. behaviors: [PageContainerBehavior, DictionariesBehavior, tickleBehavior],
  28. async onLoad(options: any) {
  29. const id = options?.id;
  30. if (id) {
  31. try {
  32. const res = await getLogisticsMethod(id as any);
  33. const tracks: LogisticsTrack[] = res?.data?.tracks ?? [];
  34. const [latestTrack, ...historyTracks] = tracks;
  35. this.setData({
  36. showAllTrace: false,
  37. latestTrace: latestTrack ? this.trackToTraceItem(latestTrack) : ({ status: "", time: "", desc: "" } as TraceItem),
  38. traceHistory: historyTracks.map((t) => this.trackToTraceItem(t)),
  39. });
  40. console.log(tracks, "tracks")
  41. // 顶部地图背景:只使用 tracks.areaCenter 画轨迹
  42. this.applyMapFromLogistics(tracks);
  43. } catch (e: any) {
  44. this.setData({
  45. showAllTrace: false,
  46. latestTrace: { status: "", time: "", desc: "" } as TraceItem,
  47. traceHistory: [],
  48. });
  49. }
  50. }
  51. if (options.goods) {
  52. const goosInfo = JSON.parse(options.goods);
  53. const expressType = goosInfo.expressType
  54. this.setData({
  55. deliveryAddress: goosInfo.address,
  56. recipientName: goosInfo.liaison,
  57. recipientPhone: goosInfo.phone,
  58. expressType,
  59. carrierName: goosInfo.expressType,
  60. trackingNo: goosInfo.expressNo,
  61. goosInfo
  62. });
  63. }
  64. },
  65. data: {
  66. // 物流start
  67. hasMapPosition: false,
  68. latitude: 0,
  69. longitude: 0,
  70. mapScale: 14,
  71. markers: [] as any[],
  72. polyline: [] as any[],
  73. includePoints: [] as MapPoint[],
  74. // end
  75. carrierName: "",
  76. expressType: "",
  77. trackingNo: "",
  78. courierPhone: "",
  79. showAllTrace: false,
  80. latestTrace: {
  81. status: "",
  82. time: "",
  83. desc: "",
  84. } as TraceItem,
  85. traceHistory: [
  86. ] as TraceItem[],
  87. deliveryAddress: "",
  88. recipientName: "",
  89. recipientPhone: "",
  90. goosInfo: {}
  91. },
  92. // --start
  93. buildRoutePointsFromTracks(tracks: LogisticsTrack[]): MapPoint[] {
  94. if (!Array.isArray(tracks) || tracks.length === 0) return [];
  95. const pts: MapPoint[] = [];
  96. for (const t of [...tracks].reverse()) {
  97. if (!t?.areaCenter) continue;
  98. const [lng, lat] = t.areaCenter.split(",").map((s: string) => Number(s.trim()));
  99. if (!Number.isNaN(lng) && !Number.isNaN(lat)) {
  100. pts.push({ latitude: lat, longitude: lng });
  101. }
  102. }
  103. return pts;
  104. },
  105. applyMapFromLogistics(tracks: LogisticsTrack[]) {
  106. // 把每个点转为经纬度
  107. const routePoints = this.buildRoutePointsFromTracks(tracks || []);
  108. // 多点的情况
  109. if (routePoints.length >= 2) {
  110. // 第一个点的经纬度
  111. const first = routePoints[0];
  112. // 最后一个点的经纬度
  113. const last = routePoints[routePoints.length - 1];
  114. this.setData({
  115. // 设置地图的显示状态
  116. hasMapPosition: true,
  117. // 设置地图的经纬度
  118. latitude: (first.latitude + last.latitude) / 2,
  119. longitude: (first.longitude + last.longitude) / 2,
  120. // 设置地图的缩放级别
  121. mapScale: 11,
  122. // 设置轨迹点
  123. markers: [
  124. { id: 1, latitude: first.latitude, longitude: first.longitude, width: 26, height: 26 },
  125. { id: 2, latitude: last.latitude, longitude: last.longitude, width: 26, height: 26 },
  126. ],
  127. // 设置轨迹线
  128. polyline: [
  129. {
  130. points: routePoints,
  131. color: "#FF6B00AA",
  132. width: 4,
  133. },
  134. ],
  135. // 设置轨迹点
  136. includePoints: routePoints,
  137. });
  138. return;
  139. }
  140. // 单点的情况
  141. if (routePoints.length === 1) {
  142. const only = routePoints[0];
  143. this.setData({
  144. hasMapPosition: true,
  145. latitude: only.latitude,
  146. longitude: only.longitude,
  147. mapScale: 14,
  148. markers: [{ id: 1, latitude: only.latitude, longitude: only.longitude, width: 28, height: 28 }],
  149. polyline: [],
  150. includePoints: [only],
  151. });
  152. return;
  153. }
  154. // 无轨迹点的情况
  155. this.setData({
  156. hasMapPosition: false,
  157. markers: [],
  158. polyline: [],
  159. includePoints: [],
  160. });
  161. },
  162. // end
  163. trackToTraceItem(track: LogisticsTrack): TraceItem {
  164. const time = String(track?.time ?? "");
  165. return {
  166. status: String(track?.status ?? ""),
  167. time,
  168. desc: String(track?.context ?? ""),
  169. timeKey: time,
  170. };
  171. },
  172. toggleTraceExpand() {
  173. this.setData({ showAllTrace: !this.data.showAllTrace });
  174. },
  175. onCopyTracking() {
  176. const no = this.data.trackingNo;
  177. wx.setClipboardData({
  178. data: no,
  179. success: () => {
  180. wx.showToast({ title: "已复制运单号", icon: "none" });
  181. },
  182. });
  183. },
  184. });