scan_result.js 15 KB

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