scan_result.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // pages/scan_result/scan_result.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. chargPile: null,
  8. orderid: null,
  9. userId: null
  10. },
  11. /**
  12. * 生命周期函数--监听页面加载
  13. */
  14. onLoad: function (options) {
  15. let that = this;
  16. wx.getStorage({
  17. key: 'scan_chargpile',
  18. success: function (res) {
  19. //console.log(res);
  20. //console.log(res.data);
  21. that.setData({
  22. chargPile: res.data
  23. });
  24. wx.removeStorage({
  25. key: 'scan_chargpile',
  26. success(res) {
  27. console.log(res);
  28. }
  29. })
  30. }
  31. });
  32. },
  33. beginCharge(e) {
  34. wx.showLoading({
  35. title: '开启充电中...',
  36. mask: true
  37. });
  38. //同步用户数据
  39. let userInfo = wx.getStorageSync('userInfo');
  40. let isLogin = wx.getStorageSync('isLogin');
  41. let that = this;
  42. wx.request({
  43. url: getApp().globalData.postHeadAgreement + '://cdgl.xinyhy.cn/restapi/wechat/syncUser',
  44. data: {
  45. loginName: userInfo.loginName
  46. },
  47. method: 'POST',
  48. success: function (res) {
  49. wx.hideLoading();
  50. let {
  51. data
  52. } = res;
  53. if (data.code && data.code != 200) {
  54. wx.removeStorageSync('userInfo');
  55. wx.removeStorageSync('isLogin');
  56. let url = '/pages/login/login';
  57. wx.redirectTo({
  58. url,
  59. });
  60. } else {
  61. wx.setStorageSync('userInfo', data);
  62. wx.setStorageSync('isLogin', true);
  63. //开始充电
  64. if (!data.repaidbalance||data.repaidbalance<=0){
  65. wx.showModal({
  66. showCancel: false,
  67. content: '余额不足,请充值!',
  68. success: function (res) {
  69. if (res.confirm){
  70. let url = "/pages/chargemoney/chargemoney";
  71. wx.navigateTo({
  72. url
  73. });
  74. }
  75. }
  76. });
  77. } else if (data.repaidbalance > 0 && data.repaidbalance<=10){
  78. wx.showModal({
  79. title: '余额:' + data.repaidbalance+'元',
  80. showCancel: false,
  81. confirmText: "去充值",
  82. content: '余额过低,请尽快充值!',
  83. success: function (res) {
  84. if (res.confirm) {
  85. let url = "/pages/chargemoney/chargemoney";
  86. wx.navigateTo({
  87. url
  88. });
  89. }
  90. }
  91. });
  92. // wx.showModal({
  93. // title:'请选择',
  94. // showCancel: true,
  95. // confirmText:"继续充电",
  96. // cancelText:"去充值",
  97. // content: '余额过低,请尽快充值!',
  98. // success: function (res) {
  99. // if (res.confirm) {
  100. // that.beginCharge_satrt();
  101. // } else if (res.cancel) {
  102. // let url = "/pages/chargemoney/chargemoney";
  103. // wx.navigateTo({
  104. // url
  105. // });
  106. // }
  107. // }
  108. // });
  109. } else if (data.repaidbalance > 10 ) {
  110. that.beginCharge_satrt();
  111. }
  112. }
  113. },
  114. fail(e) {
  115. console.log("用户数据同步失败");
  116. }
  117. });
  118. },
  119. beginCharge_satrt(){
  120. wx.showLoading({
  121. title: '开启充电中...',
  122. mask: true
  123. });
  124. let that = this;
  125. let terminalNum = this.data.chargPile.chargPileId;
  126. let userInfo = wx.getStorageSync('userInfo');
  127. let isLogin = wx.getStorageSync('isLogin');
  128. console.log(terminalNum);
  129. let userId = userInfo.userId;
  130. let chargstarttime = new Date().getTime();
  131. console.log(chargstarttime);
  132. wx.request({
  133. url: getApp().globalData.postHeadAgreement + '://cdgl.xinyhy.cn/restapi/pileLog/miniprogramadd',
  134. data: {
  135. //chargPileId: '201811010000004202',
  136. chargPileId: terminalNum,
  137. userId: userId,
  138. chargstarttime: chargstarttime
  139. },
  140. method: 'POST',
  141. fail() {
  142. wx.hideLoading();
  143. //开启充电失败,提示用户
  144. wx.showModal({
  145. showCancel: false,
  146. content: '开启充电失败'
  147. });
  148. },
  149. success(res) {
  150. wx.hideLoading();
  151. let {
  152. data
  153. } = res;
  154. if (data && data.code == 200 ) {
  155. //if (data && data.code == 200 && data.orderid) {
  156. //开启充电成功,跳转到正在充电界面
  157. let {
  158. orderid
  159. } = data;
  160. //console.log(this.data.city);
  161. let url = `/pages/charging/charging?orderid=${orderid}&userId=${userId}&chargPileId=${terminalNum}`;
  162. wx.redirectTo({
  163. url
  164. });
  165. } else if (data && data.code == 501) {
  166. //开启充电失败,提示用户
  167. wx.showModal({
  168. showCancel: false,
  169. content: '余额不足,请充值!'
  170. });
  171. message
  172. } else if (data && data.code == 502) {
  173. //开启充电失败,提示用户
  174. wx.showModal({
  175. showCancel: false,
  176. content: data.message
  177. });
  178. }else {
  179. //开启充电失败,提示用户
  180. wx.showModal({
  181. showCancel: false,
  182. content: '开启充电失败'
  183. });
  184. }
  185. }
  186. });
  187. },
  188. /**
  189. * 生命周期函数--监听页面初次渲染完成
  190. */
  191. onReady: function () {
  192. },
  193. /**
  194. * 生命周期函数--监听页面显示
  195. */
  196. onShow: function () {
  197. },
  198. /**
  199. * 生命周期函数--监听页面隐藏
  200. */
  201. onHide: function () {
  202. },
  203. /**
  204. * 生命周期函数--监听页面卸载
  205. */
  206. onUnload: function () {
  207. },
  208. /**
  209. * 页面相关事件处理函数--监听用户下拉动作
  210. */
  211. onPullDownRefresh: function () {
  212. },
  213. /**
  214. * 页面上拉触底事件的处理函数
  215. */
  216. onReachBottom: function () {
  217. },
  218. /**
  219. * 用户点击右上角分享
  220. */
  221. onShareAppMessage: function () {
  222. }
  223. })