电速宝
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. const api = require('../../api/index.js');
  2. const app = getApp();
  3. Page({
  4. data: {
  5. // 地图初始中心坐标(北京市中心)
  6. longitude: 116.404,
  7. latitude: 39.915,
  8. scale:10,
  9. markers: [],
  10. polyline: [],
  11. startLocation: {},
  12. endLocation: {},
  13. showTripInfo: true,
  14. showCallButton: true,
  15. showDriverInfo: true,
  16. carsNearby: 0,
  17. // 司机信息
  18. driverAvatar: 'https://esos-iot.bjdexn.cn/myminio/project/2a1b32553c3d4ef992bc797d26cf1614.png',
  19. driverName: '王师傅',
  20. carNumber: '京A88888',
  21. estimatedArrival: 5,
  22. locationTimer: null,
  23. isTracking: false,
  24. userdata:[],
  25. workorderdata:[]
  26. },
  27. // 收益统计
  28. onLoad() {
  29. // 初始化地图
  30. this.getuser()
  31. this.initMap()
  32. wx.$on('wsMessage', this.handleWsMessage);
  33. },
  34. onHide(){
  35. // 页面卸载时,取消订阅,这是至关重要的一步!
  36. wx.$off('wsMessage', this.handleWsMessage);
  37. },
  38. /**
  39. * 处理接收到的 WebSocket 消息
  40. * @param {Object} message 从 app.js 广播过来的消息对象
  41. */
  42. handleWsMessage(message) {
  43. console.log('Other Page 收到 WebSocket 消息:', message);
  44. // 根据消息类型进行不同的处理
  45. switch (message.api) {
  46. case '/sysworkorder/selectworkorderlist':
  47. // 在这里可以更新地图、刷新UI等
  48. if (message.type==1) {
  49. this.getworkordertype()
  50. }
  51. break;
  52. case 'trajectoryInit':
  53. // 处理初始轨迹数据
  54. break;
  55. case 'heartbeatReply':
  56. // 心跳回复,通常不需要UI处理
  57. break;
  58. default:
  59. // 处理其他类型的消息
  60. break;
  61. }
  62. },
  63. // 获取待执行工单
  64. getworkordertype(){
  65. let data ={
  66. workorderId:'',
  67. workorderName:'',
  68. addressId:'',
  69. driverId:'',
  70. carId:'',
  71. customerId:'',
  72. workorderType:4,
  73. chargedischargeType:'',
  74. }
  75. api.request(`/sysworkorder/selectworkorderlist`, 'post',data ,{ isPublic: false })
  76. .then((data) => {
  77. console.log(data.data);
  78. if (data.code==200) {
  79. this.setData({
  80. workorderdata:data.data
  81. })
  82. }
  83. })
  84. .catch((err) => {
  85. console.error('请求失败:', err);
  86. });
  87. },
  88. getuser(){
  89. api.request(`/sysoperation/selectoperationid`, 'post',{ isPublic: false })
  90. .then((data) => {
  91. console.log(data.data);
  92. if (data.code==200) {
  93. this.setData({
  94. userdata:data.data
  95. })
  96. // 设置数据到本地存储
  97. wx.setStorage({
  98. key: 'user', // 存储的key值
  99. data:data.data
  100. });
  101. }
  102. })
  103. .catch((err) => {
  104. console.error('请求失败:', err);
  105. });
  106. },
  107. // 初始化地图,获取用户位置
  108. initMap() {
  109. const that = this;
  110. // 获取用户当前位置
  111. wx.getLocation({
  112. type: 'gcj02', // 国测局坐标系,微信地图使用该坐标系
  113. success(res) {
  114. const longitude = res.longitude;
  115. const latitude = res.latitude;
  116. // 更新地图中心位置
  117. that.setData({
  118. longitude,
  119. latitude,
  120. startLocation: {
  121. longitude,
  122. latitude,
  123. name: '我的位置'
  124. },
  125. // 添加起点标记
  126. markers: [{
  127. id: 0,
  128. longitude,
  129. latitude,
  130. iconPath: 'https://esos-iot.bjdexn.cn/myminio/project/a0d245bf00c6493d949c3be82a6d2b98.png',
  131. width: 40,
  132. height: 40,
  133. anchor: {x: 0.5, y: 1}
  134. }]
  135. });
  136. // 加载附近车辆数据
  137. that.getloadNearbyCars();
  138. },
  139. fail() {
  140. wx.showToast({
  141. title: '无法获取位置,请检查权限',
  142. icon: 'none'
  143. });
  144. }
  145. });
  146. },
  147. // 预约叫车
  148. onelectricity(){
  149. wx.navigateTo({
  150. url: `/package-order/pages/createorder/index?order=`,
  151. })
  152. },
  153. onHide() {
  154. // 页面卸载时停止追踪
  155. this.stopTracking();
  156. },
  157. // 停止追踪
  158. stopTracking() {
  159. if (this.data.locationTimer) {
  160. clearInterval(this.data.locationTimer);
  161. this.setData({
  162. locationTimer: null,
  163. isTracking: false
  164. });
  165. }
  166. },
  167. // 加载附近车辆
  168. getloadNearbyCars() {
  169. const that = this;
  170. let data ={
  171. carId:'',
  172. carType:'',
  173. carMondel:'',
  174. }
  175. api.request(`/syscar/selectcarall`, 'post',data,{ isPublic: false })
  176. .then((data) => {
  177. if (data.code==200) {
  178. const cars = [];
  179. for (let index = 0; index < data.data.length; index++) {
  180. let carPosition = data.data[index].carPosition.split(",");
  181. cars.push({
  182. id: 100+index,
  183. longitude: carPosition[1],
  184. latitude: carPosition[0],
  185. iconPath: 'https://esos-iot.bjdexn.cn/myminio/project/f6fba6c83b2d4864a73f5f1d83cda416.png',
  186. width: 30,
  187. height: 40,
  188. anchor: {x: 0.5, y: 0.5}
  189. });
  190. }
  191. // 将车辆标记添加到现有标记中
  192. const updatedMarkers = [...that.data.markers];
  193. // 先移除之前的车辆标记
  194. const filteredMarkers = updatedMarkers.filter(marker => marker.id);
  195. // 添加新的车辆标记
  196. filteredMarkers.push(...cars);
  197. that.setData({
  198. markers: filteredMarkers,
  199. carsNearby: data.data.length
  200. });
  201. // this.loadNearbyCars()
  202. }
  203. })
  204. .catch((err) => {
  205. console.error('请求失败:', err);
  206. });
  207. },
  208. // 加载附近车辆
  209. loadNearbyCars() {
  210. const { longitude, latitude } = this.data;
  211. if (this.data.isTracking) return;
  212. const that = this;
  213. this.setData({ isTracking: true });
  214. this.setData({
  215. locationTimer:setInterval(() => {
  216. let data ={
  217. carId:'',
  218. carType:'',
  219. carMondel:'',
  220. }
  221. api.request(`/syscar/selectcarall`, 'post',data,{ isPublic: false })
  222. .then((data) => {
  223. if (data.code==200) {
  224. const cars = [];
  225. for (let index = 0; index < data.data.length; index++) {
  226. let carPosition = data.data[index].carPosition.split(",");
  227. cars.push({
  228. id: 100+index,
  229. longitude: carPosition[1],
  230. latitude: carPosition[0],
  231. iconPath: 'https://esos-iot.bjdexn.cn/myminio/project/f6fba6c83b2d4864a73f5f1d83cda416.png',
  232. width: 30,
  233. height: 40,
  234. anchor: {x: 0.5, y: 0.5}
  235. });
  236. }
  237. // 将车辆标记添加到现有标记中
  238. const updatedMarkers = [...that.data.markers];
  239. // 先移除之前的车辆标记
  240. const filteredMarkers = updatedMarkers.filter(marker => marker.id);
  241. // 添加新的车辆标记
  242. filteredMarkers.push(...cars);
  243. that.setData({
  244. markers: filteredMarkers,
  245. carsNearby: data.data.length
  246. });
  247. }
  248. })
  249. .catch((err) => {
  250. console.error('请求失败:', err);
  251. });
  252. }, 10000) // 每10秒更新一次位置
  253. })
  254. },
  255. // 取消订单
  256. positioning(){
  257. this.initMap()
  258. },
  259. details(e){
  260. wx.navigateTo({
  261. url: `/package-map/pages/map/index?id=${e.currentTarget.dataset.id}`,
  262. })
  263. },
  264. onShow() {
  265. // this.loadNearbyCars()
  266. this.getworkordertype()
  267. },
  268. });
  269. //