order.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // pages/order/order.js
  2. let Util = require("../../utils/util");
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. orderid: null,
  9. userId: null,
  10. chargPileId: null,
  11. order: null,
  12. startTime: null,
  13. endTime: null,
  14. totalTime: null,
  15. orderFlag:false
  16. },
  17. payOrder(e) {
  18. console.log('去支付');
  19. console.log(this.order);
  20. let that = this;
  21. let {
  22. order,
  23. userId
  24. } = this.data;
  25. wx.login({
  26. success(res) {
  27. console.log(res);
  28. if (res.code) {
  29. // 发起网络请求
  30. wx.request({
  31. url: getApp().globalData.postHeadAgreement +'://cdgl.xinyhy.cn/restapi/wechatpay/chargpayfor',
  32. data: {
  33. outTradeNo: order.orderid, //订单号
  34. totalFee: order.chargallmoney, //钱
  35. code: res.code,
  36. user_id: userId
  37. },
  38. method: 'POST',
  39. header: {
  40. 'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
  41. },
  42. success(res1) {
  43. let {
  44. data
  45. } = res1;
  46. console.log(res1);
  47. if (data && data.code == 200) {
  48. if (data.result) {
  49. //wx.showModal({
  50. // showCancel: false,
  51. // content: '请微信支付'
  52. // });
  53. wx.requestPayment({
  54. timeStamp: data.result.timeStamp,
  55. nonceStr: data.result.nonceStr,
  56. package: data.result.package,
  57. signType: data.result.signType,
  58. paySign: data.result.paySign,
  59. success(res2) {
  60. wx.request({
  61. url: getApp().globalData.postHeadAgreement +'://cdgl.xinyhy.cn/restapi/pileLog/wechatpayback',
  62. data: {
  63. //chargPileId,
  64. //userId,
  65. orderid: order.orderid
  66. },
  67. method: 'POST',
  68. success(res3) {
  69. let {
  70. data
  71. } = res3;
  72. if (data && data.code == 200) {
  73. that.setData({
  74. orderFlag:true
  75. });
  76. wx.showModal({
  77. showCancel: false,
  78. content: '支付订单成功'
  79. });
  80. } else {
  81. wx.showModal({
  82. showCancel: false,
  83. content: '支付订单失败'
  84. });
  85. }
  86. }
  87. });
  88. },
  89. fail(res3) {
  90. console.log(res3);
  91. }
  92. });
  93. } else {
  94. that.setData({
  95. orderFlag: true
  96. });
  97. wx.showModal({
  98. showCancel: false,
  99. content: '支付订单成功'
  100. });
  101. }
  102. } else {
  103. wx.showModal({
  104. showCancel: false,
  105. content: '支付订单失败'
  106. });
  107. }
  108. }
  109. });
  110. } else {
  111. console.log('登录失败!' + res.errMsg)
  112. }
  113. }
  114. });
  115. },
  116. /**
  117. * 生命周期函数--监听页面加载
  118. */
  119. onLoad: function(options) {
  120. let i = 0;
  121. let {
  122. orderid,
  123. userId,
  124. chargPileId
  125. } = options;
  126. this.setData({
  127. orderid,
  128. userId,
  129. chargPileId
  130. });
  131. var that = this;
  132. wx.showLoading({
  133. title: '订单生成中...',
  134. mask:true
  135. });
  136. var timeoutflag = setInterval(function() {
  137. i++;
  138. wx.request({
  139. url: getApp().globalData.postHeadAgreement +'://cdgl.xinyhy.cn/restapi/pileLog/wechatsettlement',
  140. data: {
  141. chargPileId,
  142. userId,
  143. orderid
  144. },
  145. method: 'POST',
  146. success(res) {
  147. let {
  148. data
  149. } = res;
  150. let {
  151. result: order
  152. } = data;
  153. if (data && data.code == 200) {
  154. if (order.transstatus==3){
  155. wx.hideLoading();
  156. clearInterval(timeoutflag);
  157. }
  158. // if (order.chargallmoney && order.chargpower) {
  159. // wx.hideLoading();
  160. // clearInterval(timeoutflag);
  161. // }
  162. //wx.showModal({
  163. //showCancel: false,
  164. //content: '获取订单成功'
  165. //});
  166. var startDate = new Date(order.chargstarttime);
  167. var endDate = new Date(order.chargendtime);
  168. var totalTime = Math.floor((endDate.getTime() - startDate.getTime()) / 60000);
  169. var totalTimeHour = Math.floor(totalTime / 60);
  170. var totalTimeMinute = totalTime % 60;
  171. var startTime = Util.formatNumber(startDate.getMonth() + 1) + '月' + Util.formatNumber(startDate.getDate()) + '日 ' + Util.formatNumber(startDate.getHours()) + ':' + Util.formatNumber(startDate.getMinutes()) + ':' + Util.formatNumber(startDate.getSeconds());
  172. var endTime = Util.formatNumber(endDate.getMonth() + 1) + '月' + Util.formatNumber(endDate.getDate()) + '日 ' + Util.formatNumber(endDate.getHours()) + ':' + Util.formatNumber(endDate.getMinutes()) + ':' + Util.formatNumber(endDate.getSeconds());
  173. totalTime = Util.formatNumber(totalTimeHour) + "时" + Util.formatNumber(totalTimeMinute) + "分";
  174. that.setData({
  175. order,
  176. startTime,
  177. endTime,
  178. totalTime
  179. });
  180. //console.log(that.data.result);
  181. } else {
  182. //wx.hideLoading();
  183. if (i>=60){
  184. clearInterval(timeoutflag);
  185. wx.showModal({
  186. showCancel: false,
  187. content: '获取订单失败'
  188. });
  189. }
  190. }
  191. },
  192. fail(err) {
  193. wx.hideLoading();
  194. clearInterval(timeoutflag);
  195. wx.showModal({
  196. showCancel: false,
  197. content: '获取订单失败'
  198. });
  199. }
  200. });
  201. },
  202. 1000);
  203. },
  204. /**
  205. * 生命周期函数--监听页面初次渲染完成
  206. */
  207. onReady: function() {
  208. },
  209. /**
  210. * 生命周期函数--监听页面显示
  211. */
  212. onShow: function() {
  213. },
  214. /**
  215. * 生命周期函数--监听页面隐藏
  216. */
  217. onHide: function() {
  218. },
  219. /**
  220. * 生命周期函数--监听页面卸载
  221. */
  222. onUnload: function() {
  223. },
  224. /**
  225. * 页面相关事件处理函数--监听用户下拉动作
  226. */
  227. onPullDownRefresh: function() {
  228. },
  229. /**
  230. * 页面上拉触底事件的处理函数
  231. */
  232. onReachBottom: function() {
  233. },
  234. /**
  235. * 用户点击右上角分享
  236. */
  237. onShareAppMessage: function() {
  238. }
  239. })