order.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // pages/order/order.js
  2. let Util = require("../../utils/util");
  3. let log = require('../../utils/log.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. orderid: null,
  10. userId: null,
  11. chargPileId: null,
  12. order: null,
  13. startTime: null,
  14. endTime: null,
  15. totalTime: null,
  16. orderFlag:false
  17. },
  18. payOrder(e) {
  19. log.info('[订单支付]', '[用户触发支付订单]');
  20. wx.showLoading({
  21. title: '支付中...',
  22. mask: true
  23. });
  24. console.log('去支付');
  25. console.log(this.order);
  26. let that = this;
  27. let {
  28. order,
  29. userId
  30. } = this.data;
  31. log.info('[订单支付]', '[用户触发支付订单]', '[wx.login请求]');
  32. wx.login({
  33. success(res) {
  34. console.log(res);
  35. log.info('[订单支付]', '[用户触发支付订单]', '[wx.login响应]',res);
  36. if (res.code) {
  37. // 发起网络请求
  38. log.info('[订单支付]', '[服务端订单支付]', '[请求]', {
  39. outTradeNo: order.orderid, //订单号
  40. totalFee: order.chargallmoney, //钱
  41. code: res.code,
  42. user_id: userId
  43. });
  44. wx.request({
  45. url: getApp().globalData.postHeadAgreement +'/restapi/wechatpay/chargpayfor',
  46. data: {
  47. outTradeNo: order.orderid, //订单号
  48. totalFee: order.chargallmoney, //钱
  49. code: res.code,
  50. user_id: userId
  51. },
  52. method: 'POST',
  53. header: {
  54. 'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
  55. },
  56. success(res1) {
  57. let {
  58. data
  59. } = res1;
  60. log.info('[订单支付]', '[服务端订单支付]', '[响应]', data);
  61. console.log(res1);
  62. if (data && data.code == 200) {
  63. if (data.result) {
  64. //wx.showModal({
  65. // showCancel: false,
  66. // content: '请微信支付'
  67. // });
  68. log.info('[订单支付]', '[余额不足微信支付]','[请求]',{
  69. timeStamp: data.result.timeStamp,
  70. nonceStr: data.result.nonceStr,
  71. package: data.result.package,
  72. signType: data.result.signType,
  73. paySign: data.result.paySign});
  74. wx.requestPayment({
  75. timeStamp: data.result.timeStamp,
  76. nonceStr: data.result.nonceStr,
  77. package: data.result.package,
  78. signType: data.result.signType,
  79. paySign: data.result.paySign,
  80. success(res2) {
  81. log.info('[订单支付]', '[余额不足微信支付成功]', '[响应]', res2);
  82. log.info('[订单支付]', '[微信支付成功回调服务端]', '[请求]', {
  83. //chargPileId,
  84. //userId,
  85. orderid: order.orderid
  86. });
  87. wx.hideLoading();
  88. wx.showModal({
  89. showCancel: false,
  90. content: '支付订单成功',
  91. confirmColor:'#00AADD',
  92. });
  93. that.setData({
  94. orderFlag:true
  95. });
  96. // wx.request({
  97. // url: getApp().globalData.postHeadAgreement +'/restapi/pileLog/wechatpayback',
  98. // data: {
  99. // //chargPileId,
  100. // //userId,
  101. // orderid: order.orderid
  102. // },
  103. // method: 'POST',
  104. // success(res3) {
  105. // wx.hideLoading();
  106. // let {
  107. // data
  108. // } = res3;
  109. // log.info('[订单支付]', '[微信支付成功回调服务端]', '[响应]', data);
  110. // if (data && data.code == 200) {
  111. // that.setData({
  112. // orderFlag:true
  113. // });
  114. // wx.showModal({
  115. // showCancel: false,
  116. // content: '支付订单成功',
  117. // confirmColor:'#00AADD',
  118. // });
  119. // log.info('[订单支付]', '[微信支付成功回调服务端]', '[成功]');
  120. // } else {
  121. // wx.showModal({
  122. // showCancel: false,
  123. // content: '支付订单失败',
  124. // confirmColor:'#00AADD',
  125. // });
  126. // log.info('[订单支付]', '[微信支付成功回调服务端]', '[失败data null||data.code<>200]', data);
  127. // }
  128. // }
  129. // });
  130. },
  131. fail(res3) {
  132. wx.hideLoading();
  133. log.info('[订单支付]', '[余额不足微信支付]', '[fail]', res3);
  134. console.log(res3);
  135. }
  136. });
  137. } else {
  138. wx.hideLoading();
  139. that.setData({
  140. orderFlag: true
  141. });
  142. wx.showModal({
  143. showCancel: false,
  144. content: data.msg,
  145. confirmColor:'#00AADD',
  146. });
  147. }
  148. log.info('[订单支付]', '[服务端订单支付]', '[余额支付成功]', data);
  149. } else {
  150. wx.hideLoading();
  151. wx.showModal({
  152. showCancel: false,
  153. content: data.msg,
  154. confirmColor:'#00AADD',
  155. });
  156. log.info('[订单支付]', '[服务端订单支付]', '[失败data null||data.code<>200]', data);
  157. }
  158. }
  159. });
  160. } else {
  161. wx.hideLoading();
  162. console.log('登录失败!' + res.errMsg);
  163. log.info('[订单支付]', '[用户触发支付订单]', '[wx.login响应失败code null]', res);
  164. }
  165. }
  166. });
  167. },
  168. /**
  169. * 生命周期函数--监听页面加载
  170. */
  171. onLoad: function(options) {
  172. let i = 0;
  173. let {
  174. orderid,
  175. userId,
  176. chargPileId
  177. } = options;
  178. log.info('[订单支付]', '[加载完成]', '[参数]', {
  179. orderid,
  180. userId,
  181. chargPileId
  182. });
  183. console.info("ooooooooooooooooooooooooooooo")
  184. console.info(orderid)
  185. console.info(userId)
  186. console.info(chargPileId)
  187. console.info("ooooooooooooooooooooooooooooo")
  188. this.setData({
  189. orderid,
  190. userId,
  191. chargPileId
  192. });
  193. var that = this;
  194. wx.showLoading({
  195. title: '订单生成中...',
  196. mask:true
  197. });
  198. var timeoutflag = setInterval(function() {
  199. i++;
  200. log.info('[订单支付]', '[轮训获取订单]', '[请求]', {
  201. chargPileId,
  202. userId,
  203. orderid
  204. });
  205. wx.request({
  206. url: getApp().globalData.postHeadAgreement +'/restapi/pileLog/wechatsettlement_new',
  207. data: {
  208. chargPileId,
  209. userId,
  210. orderid
  211. },
  212. method: 'POST',
  213. success(res) {
  214. let {
  215. data
  216. } = res;
  217. log.info('[订单支付]', '[轮训获取订单]', '[响应]', data);
  218. let {
  219. result: order
  220. } = data;
  221. if (data && data.code == 200) {
  222. if (order.transstatus==3){
  223. //order.paystatus=1;
  224. //order.chargstarttime='2020-10-19 10:00:00';
  225. //order.chargendtime = '2020-10-19 10:20:20';
  226. wx.hideLoading();
  227. clearInterval(timeoutflag);
  228. log.info('[订单支付]', '[轮训获取订单]', '[订单transstatus状态3]可手动支付');
  229. }
  230. // if (order.chargallmoney && order.chargpower) {
  231. // wx.hideLoading();
  232. // clearInterval(timeoutflag);
  233. // }
  234. //wx.showModal({
  235. //showCancel: false,
  236. //content: '获取订单成功'
  237. //});
  238. var startDate = Util.parseDate(order.chargstarttime);
  239. var endDate = Util.parseDate(order.chargendtime);
  240. var totalTime = Math.floor((endDate.getTime() - startDate.getTime()) / 60000);
  241. var totalTimeHour = Math.floor(totalTime / 60);
  242. var totalTimeMinute = totalTime % 60;
  243. var startTime = Util.formatNumber(startDate.getMonth() + 1) + '月' + Util.formatNumber(startDate.getDate()) + '日 ' + Util.formatNumber(startDate.getHours()) + ':' + Util.formatNumber(startDate.getMinutes()) + ':' + Util.formatNumber(startDate.getSeconds());
  244. var endTime = Util.formatNumber(endDate.getMonth() + 1) + '月' + Util.formatNumber(endDate.getDate()) + '日 ' + Util.formatNumber(endDate.getHours()) + ':' + Util.formatNumber(endDate.getMinutes()) + ':' + Util.formatNumber(endDate.getSeconds());
  245. totalTime = Util.formatNumber(totalTimeHour) + "时" + Util.formatNumber(totalTimeMinute) + "分";
  246. that.setData({
  247. order,
  248. startTime,
  249. endTime,
  250. totalTime
  251. });
  252. //console.log(that.data.result);
  253. } else {
  254. //wx.hideLoading();
  255. if (i>=60){
  256. clearInterval(timeoutflag);
  257. log.info('[订单支付]', '[轮训获取订单]', '[60S超时]', data);
  258. wx.showModal({
  259. showCancel: false,
  260. content: '获取订单失败',
  261. confirmColor:'#00AADD',
  262. });
  263. }
  264. }
  265. },
  266. fail(err) {
  267. wx.hideLoading();
  268. clearInterval(timeoutflag);
  269. log.info('[订单支付]', '[轮训获取订单]', '[fail]', err);
  270. wx.showModal({
  271. showCancel: false,
  272. content: '获取订单失败',
  273. confirmColor:'#00AADD',
  274. });
  275. }
  276. });
  277. },
  278. 1000);
  279. },
  280. closeView:function(){
  281. let url = `/pages/index/index`;
  282. wx.reLaunch({
  283. url
  284. });
  285. },
  286. /**
  287. * 生命周期函数--监听页面初次渲染完成
  288. */
  289. onReady: function() {
  290. },
  291. /**
  292. * 生命周期函数--监听页面显示
  293. */
  294. onShow: function() {
  295. },
  296. /**
  297. * 生命周期函数--监听页面隐藏
  298. */
  299. onHide: function() {
  300. },
  301. /**
  302. * 生命周期函数--监听页面卸载
  303. */
  304. onUnload: function() {
  305. },
  306. /**
  307. * 页面相关事件处理函数--监听用户下拉动作
  308. */
  309. onPullDownRefresh: function() {
  310. },
  311. /**
  312. * 页面上拉触底事件的处理函数
  313. */
  314. onReachBottom: function() {
  315. },
  316. /**
  317. * 用户点击右上角分享
  318. */
  319. onShareAppMessage: function() {
  320. }
  321. })