scan_result.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // pages/scan_result/scan_result.js
  2. let log = require('../../utils/log.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. chargPile: null,
  9. orderid: null,
  10. userId: null,
  11. repaidbalance: 0,
  12. resultTable: [],
  13. // 是否减免停车费
  14. carParkingRate: false,
  15. // 停车时间
  16. parkTime: 0,
  17. url:'/images/scan_result.png',
  18. authStatus: 1,
  19. defPlateNumber:''
  20. },
  21. resetResultList(resultList){
  22. let that = this
  23. resultList.forEach(item=>{
  24. item["sumPrice"] = that.resetResultListSumPrice(item)
  25. })
  26. },
  27. resetResultListSumPrice(item){
  28. if(!item.elecPrice && item.elecPrice!=0){
  29. return '暂无费用'
  30. }
  31. if(!item.servicePrice && item.servicePrice!=0){
  32. return '暂无费用'
  33. }
  34. return parseFloat((item.elecPrice + item.servicePrice).toFixed(6))
  35. },
  36. /**
  37. * 生命周期函数--监听页面加载
  38. */
  39. onLoad: function (options) {
  40. let that = this;
  41. wx.getStorage({
  42. key: 'scan_chargpile',
  43. success: function (res) {
  44. console.log(res);
  45. //console.log(res.data);
  46. var chargPile = res.data;
  47. let authStatus = chargPile.authStatus
  48. let resultTable = chargPile.resultList
  49. that.resetResultList(resultTable)
  50. that.setData({
  51. resultTable,
  52. authStatus
  53. })
  54. chargPile.sharpTotalPrice = parseFloat((chargPile.sharpChargPrice + chargPile.sharpServicePrice).toFixed(6));
  55. chargPile.peakTotalPrice = parseFloat((chargPile.peakChargPrice + chargPile.peakServicePrice).toFixed(6));
  56. chargPile.flatTotalPrice = parseFloat((chargPile.flatChargPrice + chargPile.flatServicePrice).toFixed(6));
  57. chargPile.valleyTotalPrice = parseFloat((chargPile.valleyChargPrice + chargPile.valleyServicePrice).toFixed(6));
  58. chargPile.totalprice = parseFloat((chargPile.chargprice + chargPile.serviceprice).toFixed(6));
  59. that.setData({
  60. chargPile
  61. });
  62. //console.log(chargPile);
  63. // 更新是否减停车费
  64. that.execParking(that,chargPile)
  65. wx.removeStorage({
  66. key: 'scan_chargpile',
  67. success(res) {
  68. console.log(res);
  69. }
  70. });
  71. log.info('[扫码]', '[加载完成]', '[参数]', res.data);
  72. let userInfo = wx.getStorageSync('userInfo');
  73. wx.request({
  74. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/syncUser',
  75. data: {
  76. loginName: userInfo.loginName
  77. },
  78. method: 'POST',
  79. success: function (res) {
  80. let {
  81. data
  82. } = res;
  83. log.info('[扫码]', '[同步用户数据]', '[响应]', data);
  84. console.info('[扫码]', '[同步用户数据]', '[响应]', data);
  85. if (data.code && data.code != 200) {
  86. log.info('[扫码]', '[同步用户数据]', '[失败data null||data.code<>200跳转登陆界面]', data);
  87. } else {
  88. that.setData({
  89. repaidbalance:data.repaidbalance
  90. });
  91. }
  92. },
  93. fail(e) {
  94. console.log("用户数据同步失败");
  95. log.info('[扫码]', '[同步用户数据]', '[fail]', e);
  96. }
  97. });
  98. }
  99. });
  100. },
  101. execParking(that,chargPile){
  102. let stationId = chargPile.chargStationId;
  103. wx.request({
  104. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/carParkingRate?stationId=' + stationId,
  105. // data: {
  106. // loginName: userInfo.loginName
  107. // },
  108. method: 'POST',
  109. success: function (res) {
  110. // 是否减免停车费
  111. // carParkingRate: false,
  112. // parkTime: 0,
  113. console.info(res)
  114. let reduceTime = res.data?.reduceTime;
  115. if(!reduceTime){
  116. that.setData({
  117. carParkingRate:false
  118. })
  119. return;
  120. }
  121. that.setData({
  122. carParkingRate:true
  123. })
  124. try{
  125. that.setData({
  126. parkTime: parseFloat((reduceTime/ 3600).toFixed(2))
  127. })
  128. }catch(e){
  129. that.setData({
  130. carParkingRate:false
  131. })
  132. }
  133. },
  134. fail(e) {
  135. console.log("用户数据同步失败");
  136. log.info('[扫码]', '[同步用户数据]', '[fail]', e);
  137. }
  138. });
  139. },
  140. beginChargeInner(that,plateNumber){
  141. let userInfo = wx.getStorageSync('userInfo');
  142. let isLogin = wx.getStorageSync('isLogin');
  143. // 扫码启动
  144. wx.showLoading({
  145. title: '开启充电中...',
  146. mask: true
  147. });
  148. log.info('[扫码]', '[开始充电流程]');
  149. //同步用户数据
  150. log.info('[扫码]', '[同步用户数据]', '[请求]', {
  151. loginName: userInfo.loginName
  152. });
  153. wx.request({
  154. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/syncUser',
  155. data: {
  156. loginName: userInfo.loginName,
  157. },
  158. method: 'POST',
  159. success: function (res) {
  160. wx.hideLoading();
  161. let {
  162. data
  163. } = res;
  164. log.info('[扫码]', '[同步用户数据]', '[响应]', data);
  165. console.info('[扫码]', '[同步用户数据]', '[响应]', data);
  166. if (data.code && data.code != 200) {
  167. wx.removeStorageSync('userInfo');
  168. wx.removeStorageSync('isLogin');
  169. let url = '/pages/login/phone_login/phone_login';
  170. wx.redirectTo({
  171. url,
  172. });
  173. log.info('[扫码]', '[同步用户数据]', '[失败data null||data.code<>200跳转登陆界面]', data);
  174. } else {
  175. wx.setStorageSync('userInfo', data);
  176. wx.setStorageSync('isLogin', true);
  177. let repaidbalance = data.repaidbalance;
  178. that.setData({
  179. repaidbalance
  180. });
  181. //开始充电
  182. if(data.bindingPhone==0){
  183. wx.showModal({
  184. title: '提示',
  185. content: '您未绑定手机号,请重新登录后自动刷新绑定信息',
  186. confirmText: '去登录',
  187. showCancel:false,
  188. confirmColor:'#00AADD',
  189. success: function (res1) {
  190. if (res1.confirm) {
  191. wx.removeStorageSync('userInfo');
  192. wx.removeStorageSync('isLogin');
  193. let url = '/pages/login/phone_login/phone_login';
  194. wx.redirectTo({
  195. url
  196. })
  197. }
  198. }
  199. });
  200. log.info('[扫码]', '[同步用户数据]', '[未绑定手机号]', data);
  201. }else if (!data.repaidbalance || data.repaidbalance <= 0) {
  202. wx.showModal({
  203. showCancel: false,
  204. content: '余额不足,请充值!',
  205. confirmColor:'#00AADD',
  206. success: function (res) {
  207. if (res.confirm) {
  208. let url = "/pages/chargemoney/chargemoney";
  209. wx.navigateTo({
  210. url
  211. });
  212. }
  213. }
  214. });
  215. log.info('[扫码]', '[同步用户数据]', '[data.repaidbalance null||data.repaidbalance<=0余额不足跳转充值界面]', data);
  216. } else if (data.repaidbalance > 0 && data.repaidbalance <= 15) {
  217. wx.showModal({
  218. title: '余额:' + data.repaidbalance + '元',
  219. showCancel: false,
  220. confirmText: "去充值",
  221. confirmColor:'#00AADD',
  222. content: '余额过低,请尽快充值!',
  223. success: function (res) {
  224. if (res.confirm) {
  225. let url = "/pages/chargemoney/chargemoney";
  226. wx.navigateTo({
  227. url
  228. });
  229. }
  230. }
  231. });
  232. log.info('[扫码]', '[同步用户数据]', '[0<data.repaidbalance<=15余额过低]', data);
  233. // wx.showModal({
  234. // title:'请选择',
  235. // showCancel: true,
  236. // confirmText:"继续充电",
  237. // cancelText:"去充值",
  238. // content: '余额过低,请尽快充值!',
  239. // success: function (res) {
  240. // if (res.confirm) {
  241. // that.beginCharge_satrt();
  242. // } else if (res.cancel) {
  243. // let url = "/pages/chargemoney/chargemoney";
  244. // wx.navigateTo({
  245. // url
  246. // });
  247. // }
  248. // }
  249. // });
  250. } else if (data.repaidbalance > 15) {
  251. log.info('[扫码]', '[同步用户数据]', '[余额充足开始充电]', data);
  252. that.beginCharge_satrt(plateNumber,1);
  253. }
  254. }
  255. }
  256. });
  257. // 扫描结束
  258. },
  259. beginChargeMsg(e){
  260. let that = this
  261. wx.requestSubscribeMessage({
  262. tmplIds: ['3FR5XRyycT81ZWMdVi_ZQYKJcVatZr2Tm7DgcLwCD1I','KWHBIONCrdumgTkE1dpIpAZRgrN5TuhpHuZ0uU-Eud4','XWj70eHMLAjLlufPuhE_NQmdSQzhrOwEXIblWveH0bk'],
  263. success (res33333) {
  264. that.beginCharge(e)
  265. },fail(reee){
  266. console.info("ffffffffffffff")
  267. console.info(reee)
  268. }
  269. })
  270. },
  271. beginCharge(e) {
  272. let that = this
  273. let userInfo = wx.getStorageSync('userInfo');
  274. let isLogin = wx.getStorageSync('isLogin');
  275. if(!that.data.carParkingRate){
  276. that.beginChargeInner(that,'');
  277. return;
  278. }
  279. // 读取默认车牌
  280. wx.request({
  281. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/carList?loginName=' + userInfo.loginName,
  282. // data: {
  283. // loginName: userInfo.loginName
  284. // },
  285. method: 'POST',
  286. success: function (res) {
  287. let userCarList = res.data;
  288. userCarList = userCarList.filter(it=>it.defaultType==1);
  289. console.info(userCarList)
  290. if(userCarList.length==0 ){
  291. wx.showModal({
  292. title: '提示',
  293. content: '当前充电站可减免停车费,是否绑定车牌信息?',
  294. confirmText: '绑定车牌',
  295. cancelText: '直接启动',
  296. showCancel:true,
  297. confirmColor:'#00AADD',
  298. cancelColor:'#00AADD',
  299. success: function (ress1) {
  300. if(ress1.confirm){
  301. let url = '/pages/ucenter/car/chooseCar'
  302. wx.navigateTo({
  303. url
  304. });
  305. }else{
  306. that.beginChargeInner(that,'')
  307. }
  308. }
  309. });
  310. }else{
  311. let userCar = userCarList[0]
  312. wx.showModal({
  313. title: '提示',
  314. content: '确定使用' + userCar.plateNumber + '车牌信息减免停车费?',
  315. confirmText: '确定',
  316. cancelText: '其他车牌',
  317. showCancel:true,
  318. confirmColor:'#00AADD',
  319. cancelColor:'#00AADD',
  320. success: function (ress1) {
  321. if(ress1.confirm){
  322. that.beginChargeInner(that,userCar.plateNumber)
  323. }else{
  324. let url = '/pages/ucenter/car/chooseCar'
  325. wx.navigateTo({
  326. url
  327. });
  328. }
  329. return;
  330. },
  331. fail: function(res1){
  332. console.info(res1)
  333. }
  334. });
  335. }
  336. },
  337. fail(e) {
  338. console.log("查询车辆失败");
  339. log.info('[查询车辆]','[fail]', e);
  340. }
  341. });
  342. },
  343. beginCharge_satrt(plateNumber,runTimes) {
  344. wx.showLoading({
  345. title: '开启充电中...',
  346. mask: true
  347. });
  348. let that = this;
  349. let terminalNum = this.data.chargPile.chargPileId;
  350. let userInfo = wx.getStorageSync('userInfo');
  351. let isLogin = wx.getStorageSync('isLogin');
  352. console.log(terminalNum);
  353. let userId = userInfo.userId;
  354. let chargstarttime = new Date().getTime();
  355. console.log(chargstarttime);
  356. log.info('[扫码]', '[开始充电]', '[请求]', {
  357. //chargPileId: '201811010000004202',
  358. chargPileId: terminalNum,
  359. userId: userId,
  360. chargstarttime: chargstarttime
  361. });
  362. wx.request({
  363. url: getApp().globalData.postHeadAgreement + '/restapi/pileLog/miniprogramadd',
  364. data: {
  365. //chargPileId: '201811010000004202',
  366. chargPileId: terminalNum,
  367. userId: userId,
  368. chargstarttime: chargstarttime,
  369. userPlateNumber: plateNumber,
  370. runTimes:runTimes
  371. },
  372. method: 'POST',
  373. fail(e) {
  374. wx.hideLoading();
  375. //开启充电失败,提示用户
  376. wx.showModal({
  377. showCancel: false,
  378. content: '开启充电失败',
  379. confirmColor:'#00AADD',
  380. });
  381. log.info('[扫码]', '[开始充电]', '[fail]', e);
  382. },
  383. success(res) {
  384. wx.hideLoading();
  385. let {
  386. data
  387. } = res;
  388. log.info('[扫码]', '[开始充电]', '[响应]', data);
  389. if (data && data.code == 200) {
  390. //if (data && data.code == 200 && data.orderid) {
  391. //开启充电成功,跳转到正在充电界面
  392. let {
  393. orderid
  394. } = data;
  395. log.info('[扫码]', '[开始充电]', '[成功,跳转充电中页面]', data);
  396. //console.log(this.data.city);
  397. let url = `/pages/charging/charging?orderid=${orderid}&userId=${userId}&chargPileId=${terminalNum}`;
  398. wx.redirectTo({
  399. url
  400. });
  401. } else if (data && data.code == 501) {
  402. //开启充电失败,提示用户
  403. wx.showModal({
  404. showCancel: false,
  405. content: '余额不足,请充值!',
  406. confirmColor:'#00AADD',
  407. });
  408. log.info('[扫码]', '[开始充电]', '[失败data.code=501余额不足]', data);
  409. } else if (data && data.code == 502) {
  410. //开启充电失败,提示用户
  411. wx.showModal({
  412. showCancel: false,
  413. content: data.message,
  414. confirmColor:'#00AADD',
  415. });
  416. log.info('[扫码]', '[开始充电]', '[失败data.code=502' + data.message + ']', data);
  417. } else if (data && data.code == 503) {
  418. //开启充电失败,提示用户
  419. let msg = res?.data?.message
  420. wx.showModal({
  421. title: '提示',
  422. showCancel: true,
  423. content: msg,
  424. cancelText:'其他车辆',
  425. confirmText:'继续启动',
  426. confirmColor:'#00AADD',
  427. cancelColor:'#00AADD',
  428. success: function (res) {
  429. if (res.cancel) {
  430. //点击取消,其他车辆
  431. } else {
  432. //点击确定,继续启动
  433. that.beginCharge_satrt(plateNumber,runTimes + 1)
  434. }
  435. },
  436. fail: function (res) { },//接口调用失败的回调函数
  437. complete: function (res) { },//接口调用结束的回调函数(调用成功、失败都会执行)
  438. });
  439. log.info('[扫码]', '[开始充电]', '[失败data.code=502' + data.message + ']', data);
  440. } else {
  441. //开启充电失败,提示用户
  442. let msg = res?.data?.message
  443. msg = msg?msg:'开启充电失败'
  444. wx.showModal({
  445. showCancel: false,
  446. content: msg,
  447. confirmColor:'#00AADD',
  448. });
  449. log.info('[扫码]', '[开始充电]', '[失败]', data);
  450. }
  451. }
  452. });
  453. },
  454. previewImage(e){
  455. let cur = e.target.dataset.src;//获取本地一张图片链接
  456. console.info(cur)
  457. wx.previewImage({
  458. current: cur, //字符串,默认显示urls的第一张
  459. urls: [cur] // 数组,需要预览的图片链接列表
  460. })
  461. },
  462. /**
  463. * 生命周期函数--监听页面初次渲染完成
  464. */
  465. onReady: function () {
  466. },
  467. /**
  468. * 生命周期函数--监听页面显示
  469. */
  470. onShow: function () {
  471. let car = wx.getStorageSync('chooseCar_plateNumber')
  472. let plateNumber = ''
  473. if(car){
  474. plateNumber = car.plateNumber;
  475. wx.removeStorageSync('chooseCar_plateNumber')
  476. }
  477. this.setData({
  478. plateNumber
  479. })
  480. console.info('将会自动跳过弹窗,默认车牌号:' + plateNumber)
  481. if(car){
  482. this.beginChargeInner(this,plateNumber)
  483. }
  484. },
  485. /**
  486. * 生命周期函数--监听页面隐藏
  487. */
  488. onHide: function () {
  489. },
  490. /**
  491. * 生命周期函数--监听页面卸载
  492. */
  493. onUnload: function () {
  494. },
  495. /**
  496. * 页面相关事件处理函数--监听用户下拉动作
  497. */
  498. onPullDownRefresh: function () {
  499. },
  500. /**
  501. * 页面上拉触底事件的处理函数
  502. */
  503. onReachBottom: function () {
  504. },
  505. /**
  506. * 用户点击右上角分享
  507. */
  508. onShareAppMessage: function () {
  509. }
  510. })