charging.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // pages/charging/charging.js
  2. let log = require('../../utils/log.js');
  3. var chargingPng;
  4. var chargingStatus;
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. orderid: null,
  11. userId: null,
  12. chargPileId: null,
  13. chargingRate: 0,
  14. chargingTime: '',
  15. yczds:'',
  16. totalMoney:'',
  17. chargingOrder: null,
  18. chargPile: null
  19. },
  20. drawCircle: function(step) {
  21. var context = wx.createCanvasContext('canvasProgress');
  22. context.setLineWidth(4); // 设置圆环的宽度
  23. // 设置渐变
  24. //var gradient = context.createLinearGradient(200, 100, 100, 200);
  25. //gradient.addColorStop("0", "#2661DD");
  26. //gradient.addColorStop("0.5", "#40ED94");
  27. //gradient.addColorStop("1.0", "#5956CC");
  28. context.setLineWidth(this.getSize(20));
  29. context.setStrokeStyle('#24e383');
  30. context.setLineCap('round')
  31. context.beginPath();
  32. // 参数step 为绘制的圆环周长,从0到2为一周 。 -Math.PI / 2 将起始角设在12点钟位置 ,结束角 通过改变 step 的值确定
  33. 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);
  34. context.stroke();
  35. context.draw()
  36. },
  37. getSize: function(v) {
  38. //console.log(wx.getSystemInfoSync().windowWidth);
  39. //console.log(v * (wx.getSystemInfoSync().windowWidth / 375));
  40. return v * (wx.getSystemInfoSync().windowWidth / 375)
  41. },
  42. stopCharging(e) {
  43. wx.showLoading({
  44. title: '结束充电中...',
  45. mask: true
  46. });
  47. clearInterval(chargingStatus);
  48. let {
  49. orderid,
  50. userId,
  51. chargPileId
  52. } = this.data;
  53. log.info('[充电中]', '[结束充电]', '[请求]', {
  54. chargPileId,
  55. userId,
  56. orderid
  57. });
  58. wx.request({
  59. url: getApp().globalData.postHeadAgreement + '/restapi/pileLog/chargstop',
  60. data: {
  61. chargPileId,
  62. userId,
  63. orderid
  64. },
  65. method: 'POST',
  66. success(res) {
  67. wx.hideLoading();
  68. let {
  69. data
  70. } = res;
  71. log.info('[充电中]', '[结束充电]', '[响应]', data);
  72. if (data && data.code == 200) {
  73. //wx.showModal({
  74. //showCancel: false,
  75. //content: '结束充电成功'
  76. //});
  77. log.info('[充电中]', '[结束充电]', '[成功跳转订单支付界面]', data);
  78. let url = `/pages/order/order?orderid=${orderid}&userId=${userId}&chargPileId=${chargPileId}`;
  79. wx.redirectTo({
  80. url
  81. });
  82. } else {
  83. log.info('[充电中]', '[结束充电]', '[失败data null||data.code<>200]', data);
  84. wx.showModal({
  85. showCancel: false,
  86. content: '结束充电失败'
  87. });
  88. }
  89. },
  90. fail(err){
  91. wx.hideLoading();
  92. wx.showModal({
  93. showCancel: false,
  94. content: '结束充电失败'
  95. });
  96. log.info('[充电中]', '[结束充电]', '[fail]', err );
  97. }
  98. });
  99. },
  100. showChargingPng() {
  101. var i = 0;
  102. let that = this;
  103. if (that.data.chargingOrder && that.data.chargingOrder.soc && that.data.chargingOrder.soc != 0) {
  104. chargingPng = setInterval(function() {
  105. if (i <= that.data.chargingOrder.soc) {
  106. that.drawCircle(i);
  107. that.setData({
  108. chargingRate: i
  109. });
  110. i++;
  111. } else {
  112. //console.log(chargingPng);
  113. clearInterval(chargingPng);
  114. }
  115. }, 20);
  116. }
  117. },
  118. /**
  119. * 生命周期函数--监听页面加载
  120. */
  121. onLoad: function(options) {
  122. //console.log(options);
  123. let {
  124. orderid,
  125. userId,
  126. chargPileId
  127. } = options;
  128. log.info('[充电中]', '[加载完成]', '[参数]', {
  129. orderid,
  130. userId,
  131. chargPileId
  132. });
  133. this.setData({
  134. orderid,
  135. userId,
  136. chargPileId
  137. });
  138. if (!orderid||orderid=="null"){
  139. orderid=undefined;
  140. }
  141. let that = this;
  142. log.info('[充电中]', '[获取订单状态]', '[请求]', {
  143. chargPileId,
  144. userId,
  145. orderid
  146. });
  147. wx.request({
  148. url: getApp().globalData.postHeadAgreement + '/restapi/pileLog/wechatchargstatus',
  149. data: {
  150. chargPileId,
  151. userId,
  152. orderid
  153. },
  154. method: 'POST',
  155. success(res) {
  156. let {
  157. data
  158. } = res;
  159. log.info('[充电中]', '[获取订单状态]', '[响应]', data);
  160. if (data && data.code == 200) {
  161. let {
  162. result: chargingOrder
  163. } = data;
  164. let {
  165. chargPileId,
  166. soc: chargingRate
  167. } = chargingOrder;
  168. let orderid_r = chargingOrder.orderid;
  169. console.log(chargingOrder);
  170. log.info('[充电中]', '[获取充电桩]', '[请求]', chargPileId);
  171. wx.request({
  172. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/chargPile',
  173. data: chargPileId,
  174. method: 'POST',
  175. success(res1) {
  176. console.log(res1.data);
  177. if (res1.data) {
  178. let {
  179. data: chargPile
  180. } = res1;
  181. log.info('[充电中]', '[获取充电桩]', '[响应]', chargPile);
  182. that.setData({
  183. chargPile
  184. });
  185. }
  186. }
  187. });
  188. console.log(chargingOrder.chargPileStatus);
  189. if (chargingOrder.chargPileStatus == 1) { //启动中,继续轮训
  190. log.info('[充电中]', '[初次订单状态判定]', '[chargPileStatus状态1启动中,继续轮训]', data)
  191. that.setData({
  192. chargingOrder,
  193. chargPileId,
  194. orderid: orderid_r
  195. });
  196. } else if (chargingOrder.chargPileStatus == 2) { //充电中,继续轮训
  197. console.log(chargingOrder.chargPileStatus == 2);
  198. if (chargingOrder.ljcdsj || chargingOrder.ljcdsj == 0) {
  199. var chargingTimeHour = Math.floor(chargingOrder.ljcdsj / 60);
  200. if (chargingTimeHour <= 9) {
  201. chargingTimeHour = '0' + chargingTimeHour;
  202. }
  203. var chargingTimeMinute = chargingOrder.ljcdsj % 60;
  204. if (chargingTimeMinute <= 9) {
  205. chargingTimeMinute = '0' + chargingTimeMinute
  206. }
  207. var chargingTime = chargingTimeHour + ":" + chargingTimeMinute + ":00";
  208. }
  209. var totalMoney = (parseFloat(chargingOrder.ycdfje) + parseFloat(chargingOrder.ycfwfje)).toFixed(2);
  210. var yczds = parseFloat(chargingOrder.yczds).toFixed(2);
  211. that.setData({
  212. chargingOrder,
  213. chargPileId,
  214. chargingTime,
  215. orderid: orderid_r,
  216. totalMoney,
  217. yczds
  218. });
  219. that.showChargingPng();
  220. } else if (chargingOrder.chargPileStatus == 3) { //充电完成,适用于主动拔枪
  221. clearInterval(chargingStatus);
  222. log.info('[充电中]', '[初次订单状态判定]', '[chargPileStatus状态3跳转到订单支付界面]', data);
  223. let {
  224. orderid,
  225. userId,
  226. chargPileId
  227. } = that.data;
  228. let url = `/pages/order/order?orderid=${orderid_r}&userId=${userId}&chargPileId=${chargPileId}`;
  229. wx.redirectTo({
  230. url
  231. });
  232. } else if (chargingOrder.chargPileStatus == 4) { //启动异常,回退界面
  233. clearInterval(chargingStatus);
  234. log.info('[充电中]', '[初次订单状态判定]', '[chargPileStatus状态4启动异常删除订单]', data);
  235. wx.showModal({
  236. showCancel: false,
  237. content: '开启充电失败'
  238. });
  239. that.delOrder(orderid_r);
  240. console.log(orderid_r);
  241. }
  242. } else {
  243. console.log(res);
  244. }
  245. },
  246. fail(err) {
  247. // var chargingOrder = {chargStatus:1};
  248. // that.setData({
  249. // chargingOrder
  250. // });
  251. }
  252. });
  253. },
  254. delOrder(orderId){
  255. if (orderId){
  256. log.info('[充电中]', '[删除订单]', '[请求]', { "orderid": orderId });
  257. wx.request({
  258. url: getApp().globalData.postHeadAgreement + '/restapi/pileLog/exceptOrder',
  259. data: {"orderid":orderId},
  260. method: 'POST',
  261. success(res1) {
  262. console.log(res1.data);
  263. log.info('[充电中]', '[删除订单]', '[响应]', res1.data);
  264. //if (res1.data ) {
  265. //}
  266. wx.navigateBack();
  267. }
  268. });
  269. }
  270. },
  271. /**
  272. * 生命周期函数--监听页面初次渲染完成
  273. */
  274. onReady: function() {},
  275. /**
  276. * 生命周期函数--监听页面显示
  277. */
  278. onShow: function(e) {
  279. clearInterval(chargingStatus);
  280. this.showChargingPng();
  281. let {
  282. orderid,
  283. userId,
  284. chargPileId
  285. } = this.data;
  286. if (!orderid || orderid == "null") {
  287. orderid = undefined;
  288. }
  289. let that = this;
  290. chargingStatus = setInterval(function() {
  291. wx.request({
  292. url: getApp().globalData.postHeadAgreement + '/restapi/pileLog/wechatchargstatus',
  293. data: {
  294. chargPileId,
  295. userId,
  296. orderid
  297. },
  298. method: 'POST',
  299. success(res) {
  300. let {
  301. data
  302. } = res;
  303. if (data && data.code == 200) {
  304. let {
  305. result: chargingOrder
  306. } = data;
  307. let {
  308. chargPileId,
  309. soc: chargingRate
  310. } = chargingOrder;
  311. let orderid_r = chargingOrder.orderid;
  312. if (chargingOrder.chargPileStatus == 1) { //启动中,继续轮训
  313. log.info('[充电中]', '[充电轮训]', '[chargPileStatus状态1启动中,继续轮训]', data);
  314. that.setData({
  315. chargingOrder,
  316. chargPileId,
  317. orderid: orderid_r
  318. });
  319. } else if (chargingOrder.chargPileStatus == 2) { //充电中,继续轮训
  320. if (chargingOrder.ljcdsj || chargingOrder.ljcdsj == 0) {
  321. var chargingTimeHour = Math.floor(chargingOrder.ljcdsj / 60);
  322. if (chargingTimeHour <= 9) {
  323. chargingTimeHour = '0' + chargingTimeHour;
  324. }
  325. var chargingTimeMinute = chargingOrder.ljcdsj % 60;
  326. if (chargingTimeMinute <= 9) {
  327. chargingTimeMinute = '0' + chargingTimeMinute
  328. }
  329. var chargingTime = chargingTimeHour + ":" + chargingTimeMinute + ":00";
  330. }
  331. var totalMoney = (parseFloat(chargingOrder.ycdfje) + parseFloat(chargingOrder.ycfwfje)).toFixed(2);
  332. var yczds = parseFloat(chargingOrder.yczds).toFixed(2);
  333. that.setData({
  334. chargingOrder,
  335. chargPileId,
  336. chargingTime,
  337. chargingRate,
  338. orderid: orderid_r,
  339. totalMoney,
  340. yczds
  341. });
  342. that.drawCircle(chargingRate);
  343. } else if (chargingOrder.chargPileStatus == 3) { //充电完成,适用于主动拔枪
  344. clearInterval(chargingStatus);
  345. log.info('[充电中]', '[充电轮训]', '[chargPileStatus状态3跳转到订单支付界面]', data);
  346. let {
  347. orderid,
  348. userId,
  349. chargPileId
  350. } = that.data;
  351. let url = `/pages/order/order?orderid=${orderid_r}&userId=${userId}&chargPileId=${chargPileId}`;
  352. wx.redirectTo({
  353. url
  354. });
  355. } else if (chargingOrder.chargPileStatus == 4) { //启动异常,回退界面
  356. clearInterval(chargingStatus);
  357. log.info('[充电中]', '[充电轮训]', '[chargPileStatus状态4启动异常删除订单]', data);
  358. wx.showModal({
  359. showCancel: false,
  360. content: '开启充电失败'
  361. });
  362. console.log(orderid_r);
  363. that.delOrder(orderid_r);
  364. }
  365. } else {
  366. console.log(res);
  367. }
  368. }
  369. });
  370. }, 20000);
  371. },
  372. /**
  373. * 生命周期函数--监听页面隐藏
  374. */
  375. onHide: function() {
  376. clearInterval(chargingStatus);
  377. },
  378. /**
  379. * 生命周期函数--监听页面卸载
  380. */
  381. onUnload: function() {
  382. clearInterval(chargingStatus);
  383. },
  384. /**
  385. * 页面相关事件处理函数--监听用户下拉动作
  386. */
  387. onPullDownRefresh: function() {
  388. },
  389. /**
  390. * 页面上拉触底事件的处理函数
  391. */
  392. onReachBottom: function() {
  393. },
  394. /**
  395. * 用户点击右上角分享
  396. */
  397. onShareAppMessage: function() {
  398. }
  399. })