const api = require('../../api/index.js'); const app = getApp(); Page({ data: { // 地图初始中心坐标(北京市中心) longitude: 116.404, latitude: 39.915, scale:10, markers: [], polyline: [], startLocation: {}, endLocation: {}, showTripInfo: true, showCallButton: true, showDriverInfo: true, carsNearby: 0, // 司机信息 driverAvatar: 'https://esos-iot.com/myminio/project/2a1b32553c3d4ef992bc797d26cf1614.png', driverName: '王师傅', carNumber: '京·A88888', estimatedArrival: 5, locationTimer: null, isTracking: false, userdata:[], workorderdata:[], background: ['https://esos-iot.com/myminio/project/8e68e25a4e6042b09e233d496c0aad3b.png', 'https://esos-iot.com/myminio/project/8e68e25a4e6042b09e233d496c0aad3b.png', 'https://esos-iot.com/myminio/project/8e68e25a4e6042b09e233d496c0aad3b.png'], indicatorDots: true, vertical: false, autoplay: true, interval: 3000, duration: 500 }, // 收益统计 onLoad() { // 初始化地图 this.getuser() this.initMap() wx.$on('wsMessage', this.handleWsMessage); }, onHide(){ // 页面卸载时,取消订阅,这是至关重要的一步! wx.$off('wsMessage', this.handleWsMessage); }, /** * 处理接收到的 WebSocket 消息 * @param {Object} message 从 app.js 广播过来的消息对象 */ handleWsMessage(message) { console.log('Other Page 收到 WebSocket 消息:', message); // 根据消息类型进行不同的处理 switch (message.api) { case '/sysworkorder/selectworkorderlist': // 在这里可以更新地图、刷新UI等 if (message.type==1) { this.getworkordertype() } break; case 'trajectoryInit': // 处理初始轨迹数据 break; case 'heartbeatReply': // 心跳回复,通常不需要UI处理 break; default: // 处理其他类型的消息 break; } }, // 获取待执行工单 getworkordertype(){ let data ={ workorderId:'', workorderName:'', addressId:'', driverId:'', carId:'', customerId:'', workorderType:4, chargedischargeType:'', pageNum:1, pageSize:99 } api.request(`/sysworkorder/selectworkorderlist`, 'post',data ,{ isPublic: false }) .then((data) => { console.log(data.data); if (data.code==200) { this.setData({ workorderdata:data.data.list }) } }) .catch((err) => { console.error('请求失败:', err); }); }, getuser(){ api.request(`/sysoperation/selectoperationid`, 'post',{ isPublic: false }) .then((data) => { console.log(data.data); if (data.code==200) { this.setData({ userdata:data.data }) // 设置数据到本地存储 wx.setStorage({ key: 'user', // 存储的key值 data:data.data }); } }) .catch((err) => { console.error('请求失败:', err); }); }, // 初始化地图,获取用户位置 initMap() { const that = this; // 获取用户当前位置 wx.getLocation({ type: 'gcj02', // 国测局坐标系,微信地图使用该坐标系 success(res) { const longitude = res.longitude; const latitude = res.latitude; // 更新地图中心位置 that.setData({ longitude, latitude, startLocation: { longitude, latitude, name: '我的位置' }, // 添加起点标记 markers: [{ id: 0, longitude, latitude, iconPath: 'https://esos-iot.com/myminio/project/a0d245bf00c6493d949c3be82a6d2b98.png', width: 40, height: 40, anchor: {x: 0.5, y: 1} }], carsNearby: 0, // 附近车辆数 stationsNearby: 0, // 附近电站数 powerUseNearby: 0, // 附近用电位置数 }); // 加载附近车辆数据 // // that.getloadNearbyCars(); that.refreshAllNearbyData() }, fail() { wx.showToast({ title: '无法获取位置,请检查权限', icon: 'none' }); } }); }, onelectricity(){ wx.navigateTo({ url: `/package-order/pages/createorder/index?order=`, }) }, onHide() { // 页面卸载时停止追踪 this.stopTracking(); }, // 停止追踪 stopTracking() { if (this.data.locationTimer) { clearInterval(this.data.locationTimer); this.setData({ locationTimer: null, isTracking: false }); } }, // 加载附近车辆 getloadNearbyCars() { const that = this; let data = {}; api.request(`/syscar/selectoperationcar`, 'post', data, { isPublic: false }) .then((data) => { if (data.code == 200) { const cars = []; for (let index = 0; index < data.data.syscarList.length; index++) { let carPosition = data.data.syscarList[index].carPosition.split(","); cars.push({ id: 100 + index, // 车辆标记 ID 段:100+ longitude: carPosition[1], latitude: carPosition[0], iconPath: 'https://esos-iot.com/myminio/project/e9c0fba9b61448e59458054d335b162b.png', width: 26, height: 32, anchor: { x: 0.5, y: 0.5 }, type: 'car' // 标记类型,用于过滤 }); } // 更新车辆标记:先移除旧车辆标记,再添加新的 const updatedMarkers = that.updateMarkers('car', cars); that.setData({ markers: updatedMarkers, carsNearby: data.data.carcount }); } }) .catch((err) => { console.error('加载附近车辆失败:', err); }); }, // ========== 新增:加载附近电站 ========== getloadNearbyStations() { const that = this; let data = {}; // 根据接口要求传参,如当前定位、范围等 // 替换为你的电站列表接口 api.request(`/sysaddress/selectpower`, 'post', data, { isPublic: false }) .then((data) => { if (data.code == 200) { console.log(data); const stations = []; // 假设接口返回的电站列表字段为 syscarList,坐标字段为 carPosition(格式:纬度,经度) // data.data.syscarList = [{carPosition: "40.155721,116.652896"},{carPosition: "40.105315,116.763173"},{carPosition: "40.108386,116.595002"}] for (let index = 0; index < data.data.length; index++) { // let carPosition = data.data[index].carPosition.split(","); if (data.data[index].addressType==2) { // console.log(''); // 网点 绿电 stations.push({ id: 200 + index, // 电站标记 ID 段:200+(避免和车辆冲突) longitude: data.data[index].longitude, latitude: data.data[index].latitude, // 替换为你的电站图标地址 iconPath: 'https://esos-iot.com/myminio/project/62a7a9c509f948558436bc56ee9bfe2e.png', width: 28, // 电站图标尺寸,和车辆区分 height: 32, anchor: { x: 0.5, y: 0.5 }, type: 'station' // 标记类型 }); } } // 更新电站标记 const updatedMarkers = that.updateMarkers('station', stations); that.setData({ markers: updatedMarkers, stationsNearby: data.data.length // 电站数量 }); } }) .catch((err) => { console.error('加载附近电站失败:', err); }); }, // ========== 新增:加载附近用电位置 ========== getloadNearbyPowerUse() { const that = this; let data = {}; // 根据接口要求传参 // 替换为你的用电位置列表接口 api.request(`/sysaddress/selectpower`, 'post', data, { isPublic: false }) .then((data) => { if (data.code == 200) { const powerUses = []; // 假设接口返回的用电位置列表字段为 syscarList,坐标字段为 carPosition(格式:纬度,经度) // data.data.syscarList = [{carPosition: "40.086206,116.850604"},{carPosition: "40.017232,116.61686"},{carPosition: "40.230298,116.739424"}] for (let index = 0; index < data.data.length; index++) { // let carPosition = data.data.syscarList[index].carPosition.split(","); if (data.data[index].addressType==0||data.data[index].addressType==1) { powerUses.push({ id: 300 + index, // 用电位置 ID 段:300+ longitude: data.data[index].longitude, latitude: data.data[index].latitude, // 替换为你的用电位置图标地址 iconPath: 'https://esos-iot.com/myminio/project/42dd004817dd4f268d97dcd1145ca113.png', width: 26, // 用电位置图标尺寸 height: 30, anchor: { x: 0.5, y: 0.5 }, type: 'powerUse' // 标记类型 }); } } // 更新用电位置标记 const updatedMarkers = that.updateMarkers('powerUse', powerUses); that.setData({ markers: updatedMarkers, powerUseNearby: data.data.length // 用电位置数量 }); } }) .catch((err) => { console.error('加载附近用电位置失败:', err); }); }, // ========== 通用方法:更新地图标记(核心复用逻辑) ========== /** * 更新地图标记 * @param {String} type 标记类型(car/station/powerUse) * @param {Array} newMarkers 新的标记列表 * @returns {Array} 更新后的所有标记 */ updateMarkers(type, newMarkers) { // 1. 过滤掉原有同类型的标记 const filteredMarkers = this.data.markers.filter(marker => marker.type !== type); // 2. 添加新的同类型标记 filteredMarkers.push(...newMarkers); // 3. 返回更新后的标记列表 return filteredMarkers; }, // 可选:刷新所有附近数据 refreshAllNearbyData() { this.getloadNearbyCars(); if (this.data.userdata.operationRole==6) { this.getloadNearbyPowerUse(); // 用电 }else if (this.data.userdata.operationRole==5) { this.getloadNearbyStations(); // 绿电 }else if (this.data.userdata.operationRole==4){ this.getloadNearbyStations(); this.getloadNearbyPowerUse(); } }, // 取消工单 positioning(){ this.initMap() }, details(e){ wx.navigateTo({ url: `/package-map/pages/map/index?id=${e.currentTarget.dataset.id}`, }) }, onShow() { // this.loadNearbyCars() this.getworkordertype() }, }); //