scan_result.js 18 KB

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