scan_result.js 14 KB

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