scan_result.js 17 KB

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