scan_result.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. beginCharge(e) {
  135. let userInfo = wx.getStorageSync('userInfo');
  136. let isLogin = wx.getStorageSync('isLogin');
  137. wx.showLoading({
  138. title: '开启充电中...',
  139. mask: true
  140. });
  141. log.info('[扫码]', '[开始充电流程]');
  142. //同步用户数据
  143. let that = this;
  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. fail(e) {
  251. console.log("用户数据同步失败");
  252. log.info('[扫码]', '[同步用户数据]', '[fail]', e);
  253. }
  254. });
  255. },
  256. beginCharge_satrt() {
  257. wx.showLoading({
  258. title: '开启充电中...',
  259. mask: true
  260. });
  261. let that = this;
  262. let terminalNum = this.data.chargPile.chargPileId;
  263. let userInfo = wx.getStorageSync('userInfo');
  264. let isLogin = wx.getStorageSync('isLogin');
  265. console.log(terminalNum);
  266. let userId = userInfo.userId;
  267. let chargstarttime = new Date().getTime();
  268. console.log(chargstarttime);
  269. log.info('[扫码]', '[开始充电]', '[请求]', {
  270. //chargPileId: '201811010000004202',
  271. chargPileId: terminalNum,
  272. userId: userId,
  273. chargstarttime: chargstarttime
  274. });
  275. wx.request({
  276. url: getApp().globalData.postHeadAgreement + '/restapi/pileLog/miniprogramadd',
  277. data: {
  278. //chargPileId: '201811010000004202',
  279. chargPileId: terminalNum,
  280. userId: userId,
  281. chargstarttime: chargstarttime
  282. },
  283. method: 'POST',
  284. fail(e) {
  285. wx.hideLoading();
  286. //开启充电失败,提示用户
  287. wx.showModal({
  288. showCancel: false,
  289. content: '开启充电失败',
  290. confirmColor:'#00AADD',
  291. });
  292. log.info('[扫码]', '[开始充电]', '[fail]', e);
  293. },
  294. success(res) {
  295. wx.hideLoading();
  296. let {
  297. data
  298. } = res;
  299. log.info('[扫码]', '[开始充电]', '[响应]', data);
  300. if (data && data.code == 200) {
  301. //if (data && data.code == 200 && data.orderid) {
  302. //开启充电成功,跳转到正在充电界面
  303. let {
  304. orderid
  305. } = data;
  306. log.info('[扫码]', '[开始充电]', '[成功,跳转充电中页面]', data);
  307. //console.log(this.data.city);
  308. let url = `/pages/charging/charging?orderid=${orderid}&userId=${userId}&chargPileId=${terminalNum}`;
  309. wx.redirectTo({
  310. url
  311. });
  312. } else if (data && data.code == 501) {
  313. //开启充电失败,提示用户
  314. wx.showModal({
  315. showCancel: false,
  316. content: '余额不足,请充值!',
  317. confirmColor:'#00AADD',
  318. });
  319. log.info('[扫码]', '[开始充电]', '[失败data.code=501余额不足]', data);
  320. } else if (data && data.code == 502) {
  321. //开启充电失败,提示用户
  322. wx.showModal({
  323. showCancel: false,
  324. content: data.message,
  325. confirmColor:'#00AADD',
  326. });
  327. log.info('[扫码]', '[开始充电]', '[失败data.code=502' + data.message + ']', data);
  328. } else {
  329. //开启充电失败,提示用户
  330. wx.showModal({
  331. showCancel: false,
  332. content: '开启充电失败',
  333. confirmColor:'#00AADD',
  334. });
  335. log.info('[扫码]', '[开始充电]', '[失败]', data);
  336. }
  337. }
  338. });
  339. },
  340. /**
  341. * 生命周期函数--监听页面初次渲染完成
  342. */
  343. onReady: function () {
  344. },
  345. /**
  346. * 生命周期函数--监听页面显示
  347. */
  348. onShow: function () {
  349. },
  350. /**
  351. * 生命周期函数--监听页面隐藏
  352. */
  353. onHide: function () {
  354. },
  355. /**
  356. * 生命周期函数--监听页面卸载
  357. */
  358. onUnload: function () {
  359. },
  360. /**
  361. * 页面相关事件处理函数--监听用户下拉动作
  362. */
  363. onPullDownRefresh: function () {
  364. },
  365. /**
  366. * 页面上拉触底事件的处理函数
  367. */
  368. onReachBottom: function () {
  369. },
  370. /**
  371. * 用户点击右上角分享
  372. */
  373. onShareAppMessage: function () {
  374. }
  375. })