//index.js //获取应用实例 let app = getApp(); let wechat = require("../../utils/wechat"); let amap = require("../../utils/amap"); let Util = require("../../utils/util"); let mapregionchange_makertap = false; Page({ data: { markers: [], latitude: 39.9088120620532, longitude: 116.39747668717192, user_lat: 39.9088120620532, user_lon: 116.39747668717192, textData: null, city: null, markerId: null, keywords: "搜索充电桩", userInfo: {}, isLogin: false, myChargeStationsIds: '', polygon: [{ points: [{ latitude: 31, longitude: 131 }, { latitude: 31, longitude: 131.1 }, { latitude: 31.1, longitude: 131.1 }, , { latitude: 31.1, longitude: 131 }] }] }, onLoad(e) { amap.getRegeo(function() { var fail = function(obj) { wx.showModal({ title: '请求失败', content: obj.errMsg, }); } }) .then(d => { //console.log(d); let { latitude, longitude, latitude: user_lat, longitude: user_lon } = d[0]; //console.log(d[0].regeocodeData.addressComponent); var { city } = d[0].regeocodeData.addressComponent; if (d[0].regeocodeData.addressComponent.city.length == 0) { var { province: city } = d[0].regeocodeData.addressComponent; } let that = this; let userInfo = wx.getStorageSync('userInfo'); let isLogin = wx.getStorageSync('isLogin'); this.setData({ city, latitude, longitude, user_lat, user_lon, userInfo, isLogin }); //开始请求充电站信息 var chargStationType; if (!userInfo.flag) { //用户是普通用户 chargStationType = '2'; } wx.request({ url: getApp().globalData.postHeadAgreement+'://cdgl.xinyhy.cn/restapi/wechat/chargStations', data: { lon: longitude, lat: latitude, distance: 10000, chargStationType }, method: 'POST', success(res) { //userInfo.flag = true; if (isLogin && userInfo.flag) { wx.request({ url: getApp().globalData.postHeadAgreement+'://cdgl.xinyhy.cn/restapi/wechat/userChargStations', data: { userId: userInfo.userId, lat: latitude, lon: longitude }, method: 'POST', success(res1) { //console.log(res1); var myChargeStationsIds = ''; res1.data.forEach((item, index) => { myChargeStationsIds += item.id + ","; }); console.log(myChargeStationsIds); let { data } = res; let markers = []; data.forEach((item, index) => { //item.callout = { // content: item.name, //文本 String 1.2.0 // display: 'BYCLICK', //'BYCLICK': 点击显示; 'ALWAYS': 常显 String 1.2.0 // textAlign: 'center' //文本对齐方式。有效值: left, right, center String 1.6.0 // }; var iconPath = "/images/marker.png"; if (myChargeStationsIds.indexOf(item.id + ',') != -1) { iconPath = "/images/marker1.png"; } var marker = { name: item.chargStationName, address: item.address, width: "88rpx", height: "112rpx", iconPath: iconPath, id: item.id, callout: {}, latitude: item.lat, longitude: item.lon, //distance: item.distance / 1000, distance: Util.distance(that.data.user_lat, that.data.user_lon, item.lat, item.lon), chargPileNum: item.fastCharg + item.slowCharg, fastCharg: item.fastCharg, slowCharg: item.slowCharg, freenum: item.freenum, fastfreenum: item.fastfreenum, slowfreenum: item.slowfreenum, breaknum: item.breaknum, /** 电费 */ chargprice: item.chargprice, /** 服务费 */ serviceprice: item.serviceprice, /** 停车费 */ stopprice: item.stopprice }; markers[index] = marker; }); that.setData({ markers, myChargeStationsIds }); } }); } else { let { data } = res; let markers = []; data.forEach((item, index) => { //item.callout = { // content: item.name, //文本 String 1.2.0 // display: 'BYCLICK', //'BYCLICK': 点击显示; 'ALWAYS': 常显 String 1.2.0 // textAlign: 'center' //文本对齐方式。有效值: left, right, center String 1.6.0 // }; var iconPath = "/images/marker.png"; var marker = { name: item.chargStationName, address: item.address, width: "88rpx", height: "112rpx", iconPath: iconPath, id: item.id, callout: {}, latitude: item.lat, longitude: item.lon, //distance: item.distance / 1000, distance: Util.distance(that.data.user_lat, that.data.user_lon, item.lat, item.lon), chargPileNum: item.fastCharg + item.slowCharg, fastCharg: item.fastCharg, slowCharg: item.slowCharg, freenum: item.freenum, fastfreenum: item.fastfreenum, slowfreenum: item.slowfreenum, breaknum: item.breaknum, /** 电费 */ chargprice: item.chargprice, /** 服务费 */ serviceprice: item.serviceprice, /** 停车费 */ stopprice: item.stopprice }; markers[index] = marker; }); that.setData({ markers }); } } }); }) .catch(e => { console.log(e); }); }, //跳转个人中心 bindUserAvatarUrl() { let url = `/pages/ucenter/index/index`; wx.navigateTo({ url }); }, //搜索 bindInput() { var { user_lat: latitude, user_lon: longitude, city } = this.data; //console.log(this.data.city); let url = `/pages/inputtip/inputtip?city=${city}&lonlat=${longitude},${latitude}`; wx.navigateTo({ url }); }, //单击地图mark makertap(e) { mapregionchange_makertap = true; //console.log(e); let { markerId } = e; //console.log({ // markerId //}); let { markers } = this.data; markers.forEach((item, index) => { item.iconPath = "/images/marker.png"; if (this.data.myChargeStationsIds.indexOf(item.id + ',') != -1) { item.iconPath = "/images/marker1.png"; } if (item.id == markerId) { item.iconPath = "/images/marker_checked.png"; this.showMarkerInfo(item); } }); this.setData({ markers, markerId }); }, //将mark呈现在地图中心点,并弹出详细信息框 showMarkerInfo(data) { let { latitude, longitude } = data; this.setData({ textData: data, latitude: latitude, longitude: longitude }) }, //改变选中mark的颜色 changeMarkerColor(markerId) { let { markers } = this.data; markers.forEach((item, index) => { item.iconPath = "/images/marker.png"; if (this.data.myChargeStationsIds.indexOf(item.id + ',') != -1) { item.iconPath = "/images/marker1.png"; } if (item.id == markerId) { item.iconPath = "/images/marker_checked.png"; } }) this.setData({ markers, markerId }); }, //导航 getRoute(e) { //console.log(e); // 起点 let { user_lat: latitude, user_lon: longitude, markers, markerId, city, textData } = this.data; let { name, address } = textData; if (!markers.length) return; // 终点 markers.forEach((item, index) => { if (markerId && markerId == item.id) { let { latitude: latitude2, longitude: longitude2 } = item; let url = `/pages/routes/routes?longitude=${longitude}&latitude=${latitude}&longitude2=${longitude2}&latitude2=${latitude2}&city=${city}&name=${name}&desc=${address}`; //console.log(url); wx.navigateTo({ url }); } }); }, //回到当前位置 click_location_control(e) { //console.log("回到用户当前定位点"); let { controlId } = e; let mpCtx = wx.createMapContext("map"); mpCtx.moveToLocation(); }, //打开扫一扫 click_scan_control(e) { console.log("打开扫一扫"); if (!this.data.isLogin) { let url = `/pages/login/login`; wx.navigateTo({ url }); return; } else { wx.request({ url: getApp().globalData.postHeadAgreement+'://cdgl.xinyhy.cn/restapi/pileLog/wechatsettlement', data: { userId: this.data.userInfo.userId }, method: 'POST', success(res) { let { data } = res; let { result: order } = data; if (data && order && data.code == 200) { wx.showModal({ showCancel: false, content: '你有未支付的订单,请先支付', success: function(res) { console.log(res); if (!res.cancel) { //点击确定 let url = `/pages/order/order?orderid=${order.orderid}&userId=${order.userId}&chargPileId=${order.chargPileId}`; wx.navigateTo({ url }); } } }); //console.log(that.data.result); } else { console.log('无订单'); //console.log("打开扫一扫"); wx.scanCode({ success: function(d) { //console.log("完成扫一扫"); //console.log(d); wx.request({ url: getApp().globalData.postHeadAgreement+'://cdgl.xinyhy.cn/restapi/wechat/chargPile', data: d.result, method: 'POST', success(res) { console.log(res.data); if (!res.data) { //没有该充电桩信息 wx.showModal({ showCancel: false, content: '无效的终端编号' }); } else { //充电状态0:离线、1:故障、2:空闲中、3:充电中、4:欠压故障、5:过压故障、6:过电流故障、8:预约、9:在线升级、10:操作中等 if (!res.data.chargstatus || res.data.chargstatus == 0 || res.data.chargstatus == 1 || res.data.chargstatus == 4 || res.data.chargstatus == 5 || res.data.chargstatus == 6 || res.data.chargstatus == 9) { //充电桩故障 wx.showModal({ showCancel: false, content: '终端故障,维修中' }); } else if (res.data.chargstatus == 3 || res.data.chargstatus == 8 || res.data.chargstatus == 10) { wx.showModal({ showCancel: false, content: '正在充电中' }); } else if (res.data.chargstatus == 2) { wx.setStorage({ key: "scan_chargpile", data: res.data }); let url = `/pages/scan_result/scan_result`; wx.navigateTo({ url }); } } } }); } }); } } }); } }, //地图位移 mapchange(e) { if (e.type == 'end') { if (!mapregionchange_makertap) { //console.log("移动地图"); let that = this; let wMap = wx.createMapContext('map'); wMap.getCenterLocation({ type: 'gcj02', success: function(res) { let { latitude, longitude } = res; let userInfo = wx.getStorageSync('userInfo'); let isLogin = wx.getStorageSync('isLogin'); var chargStationType; if (!userInfo.flag) { //用户是普通用户 chargStationType = '2'; } wx.request({ url: getApp().globalData.postHeadAgreement+'://cdgl.xinyhy.cn/restapi/wechat/chargStations', data: { lon: longitude, lat: latitude, distance: 10000, chargStationType }, method: 'POST', success(res) { let { data } = res; let markers = []; var flag = false; data.forEach((item, index) => { var iconPath; if (that.data.markerId && that.data.markerId == item.id) { iconPath = "/images/marker_checked.png"; flag = true; } else { iconPath = "/images/marker.png"; if (that.data.myChargeStationsIds.indexOf(item.id + ',') != -1) { iconPath = "/images/marker1.png"; } } //console.log(Util.distance(that.data.user_lat, that.data.user_lon, item.lat, item.lon)); var marker = { name: item.chargStationName, address: item.address, width: "88rpx", height: "112rpx", iconPath: iconPath, id: item.id, callout: {}, latitude: item.lat, longitude: item.lon, //distance: item.distance / 1000, distance: Util.distance(that.data.user_lat, that.data.user_lon, item.lat, item.lon), chargPileNum: item.fastCharg + item.slowCharg, fastCharg: item.fastCharg, slowCharg: item.slowCharg, freenum: item.freenum, fastfreenum: item.fastfreenum, slowfreenum: item.slowfreenum, breaknum: item.breaknum, /** 电费 */ chargprice: item.chargprice, /** 服务费 */ serviceprice: item.serviceprice, /** 停车费 */ stopprice: item.stopprice }; markers[index] = marker; }); if (!flag) { that.setData({ textData: null, markerId: null, }); } that.setData({ markers: markers }); } }); } }); } else { //点击marker的情况下 //console.log("点击marker"); mapregionchange_makertap = false; } } }, /** * 生命周期函数--监听页面显示 */ onShow: function() { let userInfo = wx.getStorageSync('userInfo'); let isLogin = wx.getStorageSync('isLogin'); // 页面显示 if (userInfo && isLogin) { //console.log(userInfo); //userInfo.flag = true; this.setData({ userInfo: userInfo, isLogin: isLogin }); wx.request({ url: getApp().globalData.postHeadAgreement +'://cdgl.xinyhy.cn/restapi/pileLog/wechatchargstatus', data: { userId: userInfo.userId }, method: 'POST', success(res) { let { data } = res; if (data && data.code == 200) { //获取用户正在充电的订单,并跳转到该页面 let url = `/pages/charging/charging?orderid=${data.result.orderid}&userId=${userInfo.userId}&chargPileId=${data.result.chargPileId}`; //let url = `/pages/charging/charging?userId=${userInfo.userId}`; wx.navigateTo({ url }); } else { console.log(res); } } }); } else { //未登录信息 this.setData({ userInfo: {} }); } } })