order.js 11 KB

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