scan_result.js 17 KB

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