123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- //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: {}
- });
- }
- }
- })
|