// pages/charging/charging.js let log = require('../../utils/log.js'); var chargingPng; var chargingStatus; Page({ /** * 页面的初始数据 */ data: { orderid: null, userId: null, chargPileId: null, chargingRate: 0, chargingTime: '', yczds:'', totalMoney:'', chargingOrder: null, chargPile: null }, drawCircle: function(step) { var context = wx.createCanvasContext('canvasProgress'); context.setLineWidth(4); // 设置圆环的宽度 // 设置渐变 //var gradient = context.createLinearGradient(200, 100, 100, 200); //gradient.addColorStop("0", "#2661DD"); //gradient.addColorStop("0.5", "#40ED94"); //gradient.addColorStop("1.0", "#5956CC"); context.setLineWidth(this.getSize(20)); context.setStrokeStyle('#24e383'); context.setLineCap('round') context.beginPath(); // 参数step 为绘制的圆环周长,从0到2为一周 。 -Math.PI / 2 将起始角设在12点钟位置 ,结束角 通过改变 step 的值确定 context.arc(this.getSize(100) - 1, this.getSize(100) - 1, this.getSize(90) - 1, -Math.PI / 2, step / (100 / 2) * Math.PI - Math.PI / 2, false); context.stroke(); context.draw() }, getSize: function(v) { //console.log(wx.getSystemInfoSync().windowWidth); //console.log(v * (wx.getSystemInfoSync().windowWidth / 375)); return v * (wx.getSystemInfoSync().windowWidth / 375) }, stopCharging(e) { wx.showLoading({ title: '结束充电中...', mask: true }); clearInterval(chargingStatus); let { orderid, userId, chargPileId } = this.data; log.info('[充电中]', '[结束充电]', '[请求]', { chargPileId, userId, orderid }); wx.request({ url: getApp().globalData.postHeadAgreement + '/restapi/pileLog/chargstop', data: { chargPileId, userId, orderid }, method: 'POST', success(res) { wx.hideLoading(); let { data } = res; log.info('[充电中]', '[结束充电]', '[响应]', data); if (data && data.code == 200) { //wx.showModal({ //showCancel: false, //content: '结束充电成功' //}); log.info('[充电中]', '[结束充电]', '[成功跳转订单支付界面]', data); let url = `/pages/order/order?orderid=${orderid}&userId=${userId}&chargPileId=${chargPileId}`; wx.redirectTo({ url }); } else { log.info('[充电中]', '[结束充电]', '[失败data null||data.code<>200]', data); wx.showModal({ showCancel: false, content: '结束充电失败' }); } }, fail(err){ wx.hideLoading(); wx.showModal({ showCancel: false, content: '结束充电失败' }); log.info('[充电中]', '[结束充电]', '[fail]', err ); } }); }, showChargingPng() { var i = 0; let that = this; if (that.data.chargingOrder && that.data.chargingOrder.soc && that.data.chargingOrder.soc != 0) { chargingPng = setInterval(function() { if (i <= that.data.chargingOrder.soc) { that.drawCircle(i); that.setData({ chargingRate: i }); i++; } else { //console.log(chargingPng); clearInterval(chargingPng); } }, 20); } }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { //console.log(options); let { orderid, userId, chargPileId } = options; log.info('[充电中]', '[加载完成]', '[参数]', { orderid, userId, chargPileId }); this.setData({ orderid, userId, chargPileId }); if (!orderid||orderid=="null"){ orderid=undefined; } let that = this; log.info('[充电中]', '[获取订单状态]', '[请求]', { chargPileId, userId, orderid }); wx.request({ url: getApp().globalData.postHeadAgreement + '/restapi/pileLog/wechatchargstatus', data: { chargPileId, userId, orderid }, method: 'POST', success(res) { let { data } = res; log.info('[充电中]', '[获取订单状态]', '[响应]', data); if (data && data.code == 200) { let { result: chargingOrder } = data; let { chargPileId, soc: chargingRate } = chargingOrder; let orderid_r = chargingOrder.orderid; console.log(chargingOrder); log.info('[充电中]', '[获取充电桩]', '[请求]', chargPileId); wx.request({ url: getApp().globalData.postHeadAgreement + '/restapi/wechat/chargPile', data: chargPileId, method: 'POST', success(res1) { console.log(res1.data); if (res1.data) { let { data: chargPile } = res1; log.info('[充电中]', '[获取充电桩]', '[响应]', chargPile); that.setData({ chargPile }); } } }); console.log(chargingOrder.chargPileStatus); if (chargingOrder.chargPileStatus == 1) { //启动中,继续轮训 log.info('[充电中]', '[初次订单状态判定]', '[chargPileStatus状态1启动中,继续轮训]', data) that.setData({ chargingOrder, chargPileId, orderid: orderid_r }); } else if (chargingOrder.chargPileStatus == 2) { //充电中,继续轮训 console.log(chargingOrder.chargPileStatus == 2); if (chargingOrder.ljcdsj || chargingOrder.ljcdsj == 0) { var chargingTimeHour = Math.floor(chargingOrder.ljcdsj / 60); if (chargingTimeHour <= 9) { chargingTimeHour = '0' + chargingTimeHour; } var chargingTimeMinute = chargingOrder.ljcdsj % 60; if (chargingTimeMinute <= 9) { chargingTimeMinute = '0' + chargingTimeMinute } var chargingTime = chargingTimeHour + ":" + chargingTimeMinute + ":00"; } var totalMoney = (parseFloat(chargingOrder.ycdfje) + parseFloat(chargingOrder.ycfwfje)).toFixed(2); var yczds = parseFloat(chargingOrder.yczds).toFixed(2); that.setData({ chargingOrder, chargPileId, chargingTime, orderid: orderid_r, totalMoney, yczds }); that.showChargingPng(); } else if (chargingOrder.chargPileStatus == 3) { //充电完成,适用于主动拔枪 clearInterval(chargingStatus); log.info('[充电中]', '[初次订单状态判定]', '[chargPileStatus状态3跳转到订单支付界面]', data); let { orderid, userId, chargPileId } = that.data; let url = `/pages/order/order?orderid=${orderid_r}&userId=${userId}&chargPileId=${chargPileId}`; wx.redirectTo({ url }); } else if (chargingOrder.chargPileStatus == 4) { //启动异常,回退界面 clearInterval(chargingStatus); log.info('[充电中]', '[初次订单状态判定]', '[chargPileStatus状态4启动异常删除订单]', data); wx.showModal({ showCancel: false, content: '开启充电失败' }); that.delOrder(orderid_r); console.log(orderid_r); } } else { console.log(res); } }, fail(err) { // var chargingOrder = {chargStatus:1}; // that.setData({ // chargingOrder // }); } }); }, delOrder(orderId){ if (orderId){ log.info('[充电中]', '[删除订单]', '[请求]', { "orderid": orderId }); wx.request({ url: getApp().globalData.postHeadAgreement + '/restapi/pileLog/exceptOrder', data: {"orderid":orderId}, method: 'POST', success(res1) { console.log(res1.data); log.info('[充电中]', '[删除订单]', '[响应]', res1.data); //if (res1.data ) { //} wx.navigateBack(); } }); } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() {}, /** * 生命周期函数--监听页面显示 */ onShow: function(e) { clearInterval(chargingStatus); this.showChargingPng(); // var { // orderid, // userId, // chargPileId // } = this.data; // if (!orderid || orderid == "null") { // orderid = ''; // } let that = this; chargingStatus = setInterval(function() { var { orderid, userId, chargPileId } = that.data; if (!orderid || orderid == "null") { orderid = ''; } wx.request({ url: getApp().globalData.postHeadAgreement + '/restapi/pileLog/wechatchargstatus', data: { chargPileId, userId, orderid }, method: 'POST', success(res) { let { data } = res; if (data && data.code == 200) { let { result: chargingOrder } = data; let { chargPileId, soc: chargingRate } = chargingOrder; let orderid_r = chargingOrder.orderid; if (chargingOrder.chargPileStatus == 1) { //启动中,继续轮训 log.info('[充电中]', '[充电轮训]', '[chargPileStatus状态1启动中,继续轮训]', data); that.setData({ chargingOrder, chargPileId, orderid: orderid_r }); } else if (chargingOrder.chargPileStatus == 2) { //充电中,继续轮训 if (chargingOrder.ljcdsj || chargingOrder.ljcdsj == 0) { var chargingTimeHour = Math.floor(chargingOrder.ljcdsj / 60); if (chargingTimeHour <= 9) { chargingTimeHour = '0' + chargingTimeHour; } var chargingTimeMinute = chargingOrder.ljcdsj % 60; if (chargingTimeMinute <= 9) { chargingTimeMinute = '0' + chargingTimeMinute } var chargingTime = chargingTimeHour + ":" + chargingTimeMinute + ":00"; } var totalMoney = (parseFloat(chargingOrder.ycdfje) + parseFloat(chargingOrder.ycfwfje)).toFixed(2); var yczds = parseFloat(chargingOrder.yczds).toFixed(2); that.setData({ chargingOrder, chargPileId, chargingTime, chargingRate, orderid: orderid_r, totalMoney, yczds }); that.drawCircle(chargingRate); } else if (chargingOrder.chargPileStatus == 3) { //充电完成,适用于主动拔枪 clearInterval(chargingStatus); log.info('[充电中]', '[充电轮训]', '[chargPileStatus状态3跳转到订单支付界面]', data); let { orderid, userId, chargPileId } = that.data; let url = `/pages/order/order?orderid=${orderid_r}&userId=${userId}&chargPileId=${chargPileId}`; wx.redirectTo({ url }); } else if (chargingOrder.chargPileStatus == 4) { //启动异常,回退界面 clearInterval(chargingStatus); log.info('[充电中]', '[充电轮训]', '[chargPileStatus状态4启动异常删除订单]', data); wx.showModal({ showCancel: false, content: '开启充电失败' }); console.log(orderid_r); that.delOrder(orderid_r); } } else if (data && data.code == 500) { clearInterval(chargingStatus); log.info('[充电中]', '[充电轮训]', '[chargPileStatus状态3跳转到订单支付界面]', data); let url = `/pages/order/order?orderid=${orderid}&userId=${userId}&chargPileId=${chargPileId}`; wx.redirectTo({ url }); }else{ console.log(res); } } }); }, 20000); }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { clearInterval(chargingStatus); }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { clearInterval(chargingStatus); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { } })