index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import {
  2. addOrder,
  3. cancelOrder,
  4. orderDelit,
  5. driverLocation,
  6. driverLocationLog,
  7. } from "@/api/trip.js";
  8. export const mixin = {
  9. data() {
  10. return {
  11. seconds: 0,
  12. minutes: 0,
  13. timer: null,
  14. loading: false,
  15. hangupObj: {
  16. orderId: "",
  17. orderNo: "",
  18. queryId: "",
  19. reason: "",
  20. },
  21. hangupShow: false,
  22. optionList: [
  23. {
  24. name: "地址选错了",
  25. },
  26. {
  27. name: "不需要用车了",
  28. },
  29. {
  30. name: "司机太远了",
  31. },
  32. {
  33. name: "计划有变",
  34. },
  35. {
  36. name: "其他原因",
  37. },
  38. ],
  39. driver: {
  40. driverId: "",
  41. driverName: "",
  42. driverPhone: "",
  43. driverAvatar: "",
  44. driverStatus: "",
  45. driverReceivedTime: "",
  46. payPrice: "", // 预估价格
  47. },
  48. location: {
  49. driverId: "", // 司机ID
  50. longitude: 0, // 司机当前位置 gcj-02坐标系
  51. latitude: 0, // 司机当前位置 gcj-02坐标系
  52. createTime: "", // 司机定位时间
  53. gpsAccuracyStatus: 1, // 司机定位信号强度, 1:GPS定位信号好;其他:定位信号不好
  54. accuracy: 0, // 定位进度,单位米
  55. },
  56. path: [],
  57. priceItems: [],
  58. };
  59. },
  60. methods: {
  61. // 呼叫司机
  62. call() {
  63. // this.markers = [];
  64. // console.log(this.markers)
  65. // console.log(this.map)
  66. // this.map.removeMarkers()
  67. this.loading = true;
  68. if (this.step !== 2) {
  69. uni.showToast({
  70. title: "请先选择目的地",
  71. icon: "none",
  72. });
  73. setTimeout(() => {
  74. this.chooseAddress("end");
  75. this.loading = false;
  76. }, 1000);
  77. return;
  78. }
  79. uni.showLoading({
  80. title: "加载中",
  81. });
  82. // adcode string(11) Y 下单地区的行政区划代码,六位数字,精确到省市区(县)
  83. // originName string(40) Y 起点名称,如 XXX大厦,XXX小区XX栋,XXX工业园
  84. // originLocation string(200) Y 地点详细全称,XXX省XX市XX区XX街道XXXX小区XXX栋X单元
  85. // originLongitude double Y 起点经度,gcj-02坐标系
  86. // originLatitude double Y 起点纬度,gcj-02坐标系
  87. // destinationName string(40) N 终点地名,选填,关于终点的四个参数,要么都不传入,要么都要传入。
  88. // destinationLocation string(200) N 终点全名
  89. // destinationLongitude double N 终点终点经度,gcj-02坐标系
  90. // destinationLatitude double N 终点纬度,gcj-02坐标系
  91. addOrder({
  92. channel: "ZhongShu",
  93. adcode: this.form.adcode,
  94. originName: this.form.start,
  95. originLocation: this.form.start,
  96. originLongitude: Math.floor(this.form.startLocation.lng * 100) / 100,
  97. originLatitude: Math.floor(this.form.startLocation.lat * 100) / 100,
  98. destinationName: this.form.end,
  99. destinationLocation: this.form.end,
  100. destinationLongitude: Math.floor(this.form.endLocation.lng * 100) / 100,
  101. destinationLatitude: Math.floor(this.form.endLocation.lat * 100) / 100,
  102. }).then((res) => {
  103. this.loading = false;
  104. if (res.state === "Success") {
  105. this.hangupObj.orderNo = res.content.orderNo;
  106. this.hangupObj.queryId = res.content.goodsList[0].id;
  107. uni.setStorageSync("tripId", this.hangupObj.queryId);
  108. this.step = 3;
  109. this.startTimer();
  110. } else {
  111. uni.showToast({
  112. title: res.message,
  113. icon: "none",
  114. });
  115. }
  116. uni.hideLoading({
  117. noConflict: true,
  118. });
  119. });
  120. },
  121. // 取消
  122. hangup() {
  123. this.hangupShow = true;
  124. },
  125. // 取消订单
  126. cancelOrder() {
  127. uni.showLoading({
  128. title: "取消中...",
  129. });
  130. cancelOrder({
  131. orderId: this.hangupObj.queryId,
  132. reason: this.hangupObj.reason,
  133. }).then((res) => {
  134. console.log(res);
  135. uni.hideLoading();
  136. if (res.state === "Success") {
  137. uni.showToast({
  138. title: "取消成功",
  139. icon: "success",
  140. });
  141. this.step = 2;
  142. this.loading = false;
  143. clearInterval(this.timer);
  144. this.timer = null;
  145. this.seconds = 0;
  146. this.minutes = 0;
  147. } else {
  148. uni.showToast({
  149. title: res.content.msg || "取消失败!",
  150. icon: "none",
  151. });
  152. }
  153. });
  154. },
  155. startTimer(rein) {
  156. // 保证只存在一个实例
  157. if (this.timer) {
  158. return;
  159. } else {
  160. this.timer = setInterval(() => {
  161. this.seconds++;
  162. // 每隔5秒请求一次
  163. if (this.seconds % 5 == 0) {
  164. // 获取订单状态
  165. orderDelit(this.hangupObj.queryId).then((res) => {
  166. if (res.state === "Success") {
  167. // 可能取消了可能接单了
  168. const id = res.content.orderStatus;
  169. const driverStatus = res.content.driverStatus;
  170. // 更新司机数据
  171. this.driver.driverId = res.content.driverId;
  172. this.driver.driverName = res.content.driverName;
  173. this.driver.driverPhone = res.content.driverPhone;
  174. this.driver.driverAvatar = res.content.driverAvatar;
  175. this.driver.driverStatus = res.content.driverStatus;
  176. this.driver.payPrice = res.content.payPrice;
  177. this.priceItems = res.content.priceItems;
  178. if (id == 1) {
  179. // 获取司机位置
  180. driverLocation({
  181. orderId: this.hangupObj.queryId,
  182. }).then((res) => {
  183. if (res.state == "Success") {
  184. const { content } = res.content;
  185. this.location.driverId = content.driverId;
  186. this.location.longitude = content.longitude;
  187. this.location.latitude = content.latitude;
  188. this.location.createTime = content.createTime;
  189. this.location.gpsAccuracyStatus =
  190. content.gpsAccuracyStatus;
  191. this.location.accuracy = content.accuracy;
  192. // 订单进行中
  193. if (driverStatus == 1) {
  194. // 有人接单了
  195. this.step = 4;
  196. // 绘制司机的行驶轨迹
  197. this.goToUser();
  198. } else if (driverStatus == 2) {
  199. // 到达了目的地开始了等待
  200. this.step = 5;
  201. } else if (driverStatus == 3) {
  202. // 乘客已经上车
  203. this.step = 6;
  204. this.goToEnd();
  205. } else if (driverStatus == 4) {
  206. // 订单完成
  207. this.step = 7;
  208. }
  209. }
  210. });
  211. } else if (id == 2) {
  212. // 订单已经完成
  213. this.step = 7;
  214. this.loading = false;
  215. clearInterval(this.timer);
  216. this.timer = null;
  217. uni.removeStorageSync("tripId");
  218. this.seconds = 0;
  219. this.minutes = 0;
  220. this.go(true);
  221. } else if (id == -1) {
  222. this.step = 2;
  223. this.loading = false;
  224. clearInterval(this.timer);
  225. this.timer = null;
  226. this.seconds = 0;
  227. this.minutes = 0;
  228. uni.removeStorageSync("tripId");
  229. // 重置地图
  230. uni.showToast({
  231. title: "订单已经取消!",
  232. icon: "none",
  233. duration: 2000,
  234. });
  235. this.go();
  236. } else if (id == 0) {
  237. if (rein) {
  238. // 重新进入后如果是在呼叫状态需要同步呼叫后的时间
  239. const createTime = new Date(
  240. res.content.createTime
  241. ).getTime();
  242. const now = new Date().getTime();
  243. const diff = now - createTime;
  244. const seconds = Math.floor(diff / 1000);
  245. this.minutes = Math.floor(seconds / 60);
  246. this.seconds = seconds % 60;
  247. }
  248. // 呼叫中
  249. this.step = 3;
  250. }
  251. }
  252. });
  253. }
  254. if (this.seconds == 60) {
  255. this.minutes++;
  256. this.seconds = 0;
  257. }
  258. }, 1000);
  259. }
  260. },
  261. selectClick(e) {
  262. console.log(e);
  263. this.hangupObj.reason = e.name;
  264. this.hangupShow = false;
  265. this.cancelOrder();
  266. },
  267. },
  268. };