scan_result.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad: function (options) {
  17. let that = this;
  18. wx.getStorage({
  19. key: 'scan_chargpile',
  20. success: function (res) {
  21. //console.log(res);
  22. //console.log(res.data);
  23. var chargPile = res.data;
  24. chargPile.sharpTotalPrice = (chargPile.sharpChargPrice + chargPile.sharpServicePrice).toFixed(4);
  25. chargPile.peakTotalPrice = (chargPile.peakChargPrice + chargPile.peakServicePrice).toFixed(4);
  26. chargPile.flatTotalPrice = (chargPile.flatChargPrice + chargPile.flatServicePrice).toFixed(4);
  27. chargPile.valleyTotalPrice = (chargPile.valleyChargPrice + chargPile.valleyServicePrice).toFixed(4);
  28. chargPile.totalprice = (chargPile.chargprice + chargPile.serviceprice).toFixed(4);
  29. that.setData({
  30. chargPile
  31. });
  32. //console.log(chargPile);
  33. wx.removeStorage({
  34. key: 'scan_chargpile',
  35. success(res) {
  36. console.log(res);
  37. }
  38. });
  39. log.info('[扫码]', '[加载完成]', '[参数]', res.data);
  40. let userInfo = wx.getStorageSync('userInfo');
  41. wx.request({
  42. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/syncUser',
  43. data: {
  44. loginName: userInfo.loginName
  45. },
  46. method: 'POST',
  47. success: function (res) {
  48. let {
  49. data
  50. } = res;
  51. log.info('[扫码]', '[同步用户数据]', '[响应]', data);
  52. console.info('[扫码]', '[同步用户数据]', '[响应]', data);
  53. if (data.code && data.code != 200) {
  54. log.info('[扫码]', '[同步用户数据]', '[失败data null||data.code<>200跳转登陆界面]', data);
  55. } else {
  56. that.setData({
  57. repaidbalance:data.repaidbalance
  58. });
  59. }
  60. },
  61. fail(e) {
  62. console.log("用户数据同步失败");
  63. log.info('[扫码]', '[同步用户数据]', '[fail]', e);
  64. }
  65. });
  66. }
  67. });
  68. },
  69. beginCharge(e) {
  70. let isBindPhone = wx.getStorageSync('isBindPhone');
  71. if(isBindPhone==0){
  72. let url = `/pages/bindPhone/bindPhone`;
  73. wx.navigateTo({
  74. url
  75. });
  76. return;
  77. }
  78. wx.showLoading({
  79. title: '开启充电中...',
  80. mask: true
  81. });
  82. log.info('[扫码]', '[开始充电流程]');
  83. //同步用户数据
  84. let userInfo = wx.getStorageSync('userInfo');
  85. let isLogin = wx.getStorageSync('isLogin');
  86. let that = this;
  87. log.info('[扫码]', '[同步用户数据]', '[请求]', {
  88. loginName: userInfo.loginName
  89. });
  90. wx.request({
  91. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/syncUser',
  92. data: {
  93. loginName: userInfo.loginName
  94. },
  95. method: 'POST',
  96. success: function (res) {
  97. wx.hideLoading();
  98. let {
  99. data
  100. } = res;
  101. log.info('[扫码]', '[同步用户数据]', '[响应]', data);
  102. console.info('[扫码]', '[同步用户数据]', '[响应]', data);
  103. if (data.code && data.code != 200) {
  104. wx.removeStorageSync('userInfo');
  105. wx.removeStorageSync('isLogin');
  106. let url = '/pages/login/phone_login/phone_login';
  107. wx.redirectTo({
  108. url,
  109. });
  110. log.info('[扫码]', '[同步用户数据]', '[失败data null||data.code<>200跳转登陆界面]', data);
  111. } else {
  112. wx.setStorageSync('userInfo', data);
  113. wx.setStorageSync('isLogin', true);
  114. let repaidbalance = data.repaidbalance;
  115. that.setData({
  116. repaidbalance
  117. });
  118. console.info("sssssss " + data.repaidbalance)
  119. //开始充电
  120. if (!data.repaidbalance || data.repaidbalance <= 0) {
  121. wx.showModal({
  122. showCancel: false,
  123. content: '余额不足,请充值!',
  124. success: function (res) {
  125. if (res.confirm) {
  126. let url = "/pages/chargemoney/chargemoney";
  127. wx.navigateTo({
  128. url
  129. });
  130. }
  131. }
  132. });
  133. log.info('[扫码]', '[同步用户数据]', '[data.repaidbalance null||data.repaidbalance<=0余额不足跳转充值界面]', data);
  134. } else if (data.repaidbalance > 0 && data.repaidbalance <= 15) {
  135. wx.showModal({
  136. title: '余额:' + data.repaidbalance + '元',
  137. showCancel: false,
  138. confirmText: "去充值",
  139. content: '余额过低,请尽快充值!',
  140. success: function (res) {
  141. if (res.confirm) {
  142. let url = "/pages/chargemoney/chargemoney";
  143. wx.navigateTo({
  144. url
  145. });
  146. }
  147. }
  148. });
  149. log.info('[扫码]', '[同步用户数据]', '[0<data.repaidbalance<=15余额过低]', data);
  150. // wx.showModal({
  151. // title:'请选择',
  152. // showCancel: true,
  153. // confirmText:"继续充电",
  154. // cancelText:"去充值",
  155. // content: '余额过低,请尽快充值!',
  156. // success: function (res) {
  157. // if (res.confirm) {
  158. // that.beginCharge_satrt();
  159. // } else if (res.cancel) {
  160. // let url = "/pages/chargemoney/chargemoney";
  161. // wx.navigateTo({
  162. // url
  163. // });
  164. // }
  165. // }
  166. // });
  167. } else if (data.repaidbalance > 15) {
  168. log.info('[扫码]', '[同步用户数据]', '[余额充足开始充电]', data);
  169. that.beginCharge_satrt();
  170. }
  171. }
  172. },
  173. fail(e) {
  174. console.log("用户数据同步失败");
  175. log.info('[扫码]', '[同步用户数据]', '[fail]', e);
  176. }
  177. });
  178. },
  179. beginCharge_satrt() {
  180. wx.showLoading({
  181. title: '开启充电中...',
  182. mask: true
  183. });
  184. let that = this;
  185. let terminalNum = this.data.chargPile.chargPileId;
  186. let userInfo = wx.getStorageSync('userInfo');
  187. let isLogin = wx.getStorageSync('isLogin');
  188. console.log(terminalNum);
  189. let userId = userInfo.userId;
  190. let chargstarttime = new Date().getTime();
  191. console.log(chargstarttime);
  192. log.info('[扫码]', '[开始充电]', '[请求]', {
  193. //chargPileId: '201811010000004202',
  194. chargPileId: terminalNum,
  195. userId: userId,
  196. chargstarttime: chargstarttime
  197. });
  198. wx.request({
  199. url: getApp().globalData.postHeadAgreement + '/restapi/pileLog/miniprogramadd',
  200. data: {
  201. //chargPileId: '201811010000004202',
  202. chargPileId: terminalNum,
  203. userId: userId,
  204. chargstarttime: chargstarttime
  205. },
  206. method: 'POST',
  207. fail(e) {
  208. wx.hideLoading();
  209. //开启充电失败,提示用户
  210. wx.showModal({
  211. showCancel: false,
  212. content: '开启充电失败'
  213. });
  214. log.info('[扫码]', '[开始充电]', '[fail]', e);
  215. },
  216. success(res) {
  217. wx.hideLoading();
  218. let {
  219. data
  220. } = res;
  221. log.info('[扫码]', '[开始充电]', '[响应]', data);
  222. if (data && data.code == 200) {
  223. //if (data && data.code == 200 && data.orderid) {
  224. //开启充电成功,跳转到正在充电界面
  225. let {
  226. orderid
  227. } = data;
  228. log.info('[扫码]', '[开始充电]', '[成功,跳转充电中页面]', data);
  229. //console.log(this.data.city);
  230. let url = `/pages/charging/charging?orderid=${orderid}&userId=${userId}&chargPileId=${terminalNum}`;
  231. wx.redirectTo({
  232. url
  233. });
  234. } else if (data && data.code == 501) {
  235. //开启充电失败,提示用户
  236. wx.showModal({
  237. showCancel: false,
  238. content: '余额不足,请充值!'
  239. });
  240. log.info('[扫码]', '[开始充电]', '[失败data.code=501余额不足]', data);
  241. } else if (data && data.code == 502) {
  242. //开启充电失败,提示用户
  243. wx.showModal({
  244. showCancel: false,
  245. content: data.message
  246. });
  247. log.info('[扫码]', '[开始充电]', '[失败data.code=502' + data.message + ']', data);
  248. } else {
  249. //开启充电失败,提示用户
  250. wx.showModal({
  251. showCancel: false,
  252. content: '开启充电失败'
  253. });
  254. log.info('[扫码]', '[开始充电]', '[失败]', data);
  255. }
  256. }
  257. });
  258. },
  259. /**
  260. * 生命周期函数--监听页面初次渲染完成
  261. */
  262. onReady: function () {
  263. },
  264. /**
  265. * 生命周期函数--监听页面显示
  266. */
  267. onShow: function () {
  268. },
  269. /**
  270. * 生命周期函数--监听页面隐藏
  271. */
  272. onHide: function () {
  273. },
  274. /**
  275. * 生命周期函数--监听页面卸载
  276. */
  277. onUnload: function () {
  278. },
  279. /**
  280. * 页面相关事件处理函数--监听用户下拉动作
  281. */
  282. onPullDownRefresh: function () {
  283. },
  284. /**
  285. * 页面上拉触底事件的处理函数
  286. */
  287. onReachBottom: function () {
  288. },
  289. /**
  290. * 用户点击右上角分享
  291. */
  292. onShareAppMessage: function () {
  293. }
  294. })