scan_result.js 17 KB

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