order.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.request({
  88. url: getApp().globalData.postHeadAgreement +'/restapi/pileLog/wechatpayback',
  89. data: {
  90. //chargPileId,
  91. //userId,
  92. orderid: order.orderid
  93. },
  94. method: 'POST',
  95. success(res3) {
  96. wx.hideLoading();
  97. let {
  98. data
  99. } = res3;
  100. log.info('[订单支付]', '[微信支付成功回调服务端]', '[响应]', data);
  101. if (data && data.code == 200) {
  102. that.setData({
  103. orderFlag:true
  104. });
  105. wx.showModal({
  106. showCancel: false,
  107. content: '支付订单成功'
  108. });
  109. log.info('[订单支付]', '[微信支付成功回调服务端]', '[成功]');
  110. } else {
  111. wx.showModal({
  112. showCancel: false,
  113. content: '支付订单失败'
  114. });
  115. log.info('[订单支付]', '[微信支付成功回调服务端]', '[失败data null||data.code<>200]', data);
  116. }
  117. }
  118. });
  119. },
  120. fail(res3) {
  121. wx.hideLoading();
  122. log.info('[订单支付]', '[余额不足微信支付]', '[fail]', res3);
  123. console.log(res3);
  124. }
  125. });
  126. } else {
  127. wx.hideLoading();
  128. that.setData({
  129. orderFlag: true
  130. });
  131. wx.showModal({
  132. showCancel: false,
  133. content: data.msg
  134. });
  135. }
  136. log.info('[订单支付]', '[服务端订单支付]', '[余额支付成功]', data);
  137. } else {
  138. wx.hideLoading();
  139. wx.showModal({
  140. showCancel: false,
  141. content: data.msg
  142. });
  143. log.info('[订单支付]', '[服务端订单支付]', '[失败data null||data.code<>200]', data);
  144. }
  145. }
  146. });
  147. } else {
  148. wx.hideLoading();
  149. console.log('登录失败!' + res.errMsg);
  150. log.info('[订单支付]', '[用户触发支付订单]', '[wx.login响应失败code null]', res);
  151. }
  152. }
  153. });
  154. },
  155. /**
  156. * 生命周期函数--监听页面加载
  157. */
  158. onLoad: function(options) {
  159. let i = 0;
  160. let {
  161. orderid,
  162. userId,
  163. chargPileId
  164. } = options;
  165. log.info('[订单支付]', '[加载完成]', '[参数]', {
  166. orderid,
  167. userId,
  168. chargPileId
  169. });
  170. console.info("ooooooooooooooooooooooooooooo")
  171. console.info(orderid)
  172. console.info(userId)
  173. console.info(chargPileId)
  174. console.info("ooooooooooooooooooooooooooooo")
  175. this.setData({
  176. orderid,
  177. userId,
  178. chargPileId
  179. });
  180. var that = this;
  181. wx.showLoading({
  182. title: '订单生成中...',
  183. mask:true
  184. });
  185. var timeoutflag = setInterval(function() {
  186. i++;
  187. log.info('[订单支付]', '[轮训获取订单]', '[请求]', {
  188. chargPileId,
  189. userId,
  190. orderid
  191. });
  192. wx.request({
  193. url: getApp().globalData.postHeadAgreement +'/restapi/pileLog/wechatsettlement_new',
  194. data: {
  195. chargPileId,
  196. userId,
  197. orderid
  198. },
  199. method: 'POST',
  200. success(res) {
  201. let {
  202. data
  203. } = res;
  204. log.info('[订单支付]', '[轮训获取订单]', '[响应]', data);
  205. let {
  206. result: order
  207. } = data;
  208. if (data && data.code == 200) {
  209. if (order.transstatus==3){
  210. //order.paystatus=1;
  211. //order.chargstarttime='2020-10-19 10:00:00';
  212. //order.chargendtime = '2020-10-19 10:20:20';
  213. wx.hideLoading();
  214. clearInterval(timeoutflag);
  215. log.info('[订单支付]', '[轮训获取订单]', '[订单transstatus状态3]可手动支付');
  216. }
  217. // if (order.chargallmoney && order.chargpower) {
  218. // wx.hideLoading();
  219. // clearInterval(timeoutflag);
  220. // }
  221. //wx.showModal({
  222. //showCancel: false,
  223. //content: '获取订单成功'
  224. //});
  225. var startDate = Util.parseDate(order.chargstarttime);
  226. var endDate = Util.parseDate(order.chargendtime);
  227. var totalTime = Math.floor((endDate.getTime() - startDate.getTime()) / 60000);
  228. var totalTimeHour = Math.floor(totalTime / 60);
  229. var totalTimeMinute = totalTime % 60;
  230. var startTime = Util.formatNumber(startDate.getMonth() + 1) + '月' + Util.formatNumber(startDate.getDate()) + '日 ' + Util.formatNumber(startDate.getHours()) + ':' + Util.formatNumber(startDate.getMinutes()) + ':' + Util.formatNumber(startDate.getSeconds());
  231. var endTime = Util.formatNumber(endDate.getMonth() + 1) + '月' + Util.formatNumber(endDate.getDate()) + '日 ' + Util.formatNumber(endDate.getHours()) + ':' + Util.formatNumber(endDate.getMinutes()) + ':' + Util.formatNumber(endDate.getSeconds());
  232. totalTime = Util.formatNumber(totalTimeHour) + "时" + Util.formatNumber(totalTimeMinute) + "分";
  233. that.setData({
  234. order,
  235. startTime,
  236. endTime,
  237. totalTime
  238. });
  239. //console.log(that.data.result);
  240. } else {
  241. //wx.hideLoading();
  242. if (i>=60){
  243. clearInterval(timeoutflag);
  244. log.info('[订单支付]', '[轮训获取订单]', '[60S超时]', data);
  245. wx.showModal({
  246. showCancel: false,
  247. content: '获取订单失败'
  248. });
  249. }
  250. }
  251. },
  252. fail(err) {
  253. wx.hideLoading();
  254. clearInterval(timeoutflag);
  255. log.info('[订单支付]', '[轮训获取订单]', '[fail]', err);
  256. wx.showModal({
  257. showCancel: false,
  258. content: '获取订单失败'
  259. });
  260. }
  261. });
  262. },
  263. 1000);
  264. },
  265. closeView:function(){
  266. let url = `/pages/index/index`;
  267. wx.reLaunch({
  268. url
  269. });
  270. },
  271. /**
  272. * 生命周期函数--监听页面初次渲染完成
  273. */
  274. onReady: function() {
  275. },
  276. /**
  277. * 生命周期函数--监听页面显示
  278. */
  279. onShow: function() {
  280. },
  281. /**
  282. * 生命周期函数--监听页面隐藏
  283. */
  284. onHide: function() {
  285. },
  286. /**
  287. * 生命周期函数--监听页面卸载
  288. */
  289. onUnload: function() {
  290. },
  291. /**
  292. * 页面相关事件处理函数--监听用户下拉动作
  293. */
  294. onPullDownRefresh: function() {
  295. },
  296. /**
  297. * 页面上拉触底事件的处理函数
  298. */
  299. onReachBottom: function() {
  300. },
  301. /**
  302. * 用户点击右上角分享
  303. */
  304. onShareAppMessage: function() {
  305. }
  306. })