scan_result.js 18 KB

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