chargemoney.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // pages/chargemoney/chargemoney.js
  2. let log = require('../../utils/log.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. chargemoney_select: null,
  9. userInfo: {},
  10. isLogin: false
  11. },
  12. bindMoneyTap(e) {
  13. let chargemoney_select = e.currentTarget.id;
  14. this.setData({
  15. chargemoney_select
  16. });
  17. },
  18. saveMoney(e) {
  19. //console.log(e);
  20. let chargemoney_select = e.detail.value;
  21. this.setData({
  22. chargemoney_select
  23. });
  24. },
  25. beginChargemoney(e) {
  26. //console.log(this.data.chargemoney_select);
  27. log.info('[充值]', '[开始充值流程]');
  28. let that = this;
  29. if (this.data.chargemoney_select) {
  30. wx.showLoading({
  31. title: '充值中...',
  32. mask: true
  33. });
  34. log.info('[充值]', '[开始微信充值]', '[wx.login请求]');
  35. wx.login({
  36. success(res) {
  37. console.log(res);
  38. log.info('[充值]', '[开始微信充值]', '[wx.login响应]', res);
  39. if (res.code) {
  40. // 发起网络请求
  41. log.info('[充值]', '[服务端微信充值]', '[请求]', {
  42. //outTradeNo: new Date().getTime(), //订单号
  43. totalFee: that.data.chargemoney_select, //钱
  44. //totalFee:0.01,
  45. code: res.code,
  46. userId: that.data.userInfo.userId
  47. });
  48. wx.request({
  49. url: getApp().globalData.postHeadAgreement +'/restapi/wechatpay/dopaychargmoney',
  50. data: {
  51. //outTradeNo: new Date().getTime(), //订单号
  52. totalFee: that.data.chargemoney_select, //钱
  53. //totalFee:0.01,
  54. code: res.code,
  55. userId: that.data.userInfo.userId
  56. },
  57. header: {
  58. 'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
  59. },
  60. method: 'POST',
  61. success(res1) {
  62. log.info('[充值]', '[服务端微信充值]', '[响应]', res1.data);
  63. //console.log(res1);
  64. log.info('[充值]', '[微信支付]', '[请求]', {
  65. timeStamp: res1.data.timeStamp,
  66. nonceStr: res1.data.nonceStr,
  67. package: res1.data.package,
  68. signType: res1.data.signType,
  69. paySign: res1.data.paySign
  70. });
  71. wx.requestPayment({
  72. timeStamp: res1.data.timeStamp,
  73. nonceStr: res1.data.nonceStr,
  74. package: res1.data.package,
  75. signType: res1.data.signType,
  76. paySign: res1.data.paySign,
  77. success(res2) {
  78. console.log(res2);
  79. log.info('[充值]', '[微信支付]', '[响应]', res2);
  80. log.info('[充值]', '[微信支付回调]', '[请求]', {
  81. userId: that.data.userInfo.userId,
  82. rechargeMoney: that.data.chargemoney_select
  83. //rechargeMoney: 0.01,
  84. });
  85. wx.request({
  86. url: getApp().globalData.postHeadAgreement +'/restapi/pileLog/wechatrechargeback',
  87. data: {
  88. userId: that.data.userInfo.userId,
  89. rechargeMoney: that.data.chargemoney_select
  90. //rechargeMoney: 0.01,
  91. },
  92. method: 'POST',
  93. success(res3) {
  94. wx.hideLoading();
  95. let {
  96. data
  97. } = res3;
  98. log.info('[充值]', '[微信支付回调]', '[响应]', data);
  99. if (data && data.code == 200) {
  100. // wx.showModal({
  101. // showCancel: false,
  102. // content: '充值成功'
  103. // });
  104. let url = `/pages/chargemoneyresult/chargemoneyresult?timeStamp=${res1.data.timeStamp}&money=${that.data.chargemoney_select}`;
  105. wx.redirectTo({
  106. url
  107. })
  108. log.info('[充值]', '[微信支付回调]', '[成功]');
  109. } else {
  110. wx.showModal({
  111. showCancel: false,
  112. content: '充值失败'
  113. });
  114. log.info('[充值]', '[微信支付回调]', '[失败data null||data.code<>200]', data);
  115. }
  116. }
  117. });
  118. },
  119. fail(res3) {
  120. wx.hideLoading();
  121. console.log(res3);
  122. log.info('[充值]', '[微信支付]', '[fail]', res3);
  123. }
  124. });
  125. }
  126. });
  127. } else {
  128. wx.hideLoading();
  129. console.log('登录失败!' + res.errMsg)
  130. log.info('[充值]', '[开始微信充值]', '[wx.login响应失败code null]', res);
  131. }
  132. }
  133. })
  134. }
  135. },
  136. /**
  137. * 生命周期函数--监听页面加载
  138. */
  139. onLoad: function(options) {
  140. log.info('[充值]', '[页面加载完成]');
  141. let userInfo = wx.getStorageSync('userInfo');
  142. let isLogin = wx.getStorageSync('isLogin');
  143. // 页面显示
  144. if (userInfo && isLogin) {
  145. //console.log(userInfo);
  146. //userInfo.flag = true;
  147. this.setData({
  148. userInfo: userInfo,
  149. isLogin: isLogin
  150. });
  151. }
  152. },
  153. /**
  154. * 生命周期函数--监听页面初次渲染完成
  155. */
  156. onReady: function() {
  157. },
  158. /**
  159. * 生命周期函数--监听页面显示
  160. */
  161. onShow: function() {
  162. },
  163. /**
  164. * 生命周期函数--监听页面隐藏
  165. */
  166. onHide: function() {
  167. },
  168. /**
  169. * 生命周期函数--监听页面卸载
  170. */
  171. onUnload: function() {
  172. },
  173. /**
  174. * 页面相关事件处理函数--监听用户下拉动作
  175. */
  176. onPullDownRefresh: function() {
  177. },
  178. /**
  179. * 页面上拉触底事件的处理函数
  180. */
  181. onReachBottom: function() {
  182. },
  183. /**
  184. * 用户点击右上角分享
  185. */
  186. onShareAppMessage: function() {
  187. }
  188. })