chargemoney.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. log.info('[充值]', '[微信支付回调]', '[成功]');
  105. } else {
  106. wx.showModal({
  107. showCancel: false,
  108. content: '充值失败'
  109. });
  110. log.info('[充值]', '[微信支付回调]', '[失败data null||data.code<>200]', data);
  111. }
  112. }
  113. });
  114. },
  115. fail(res3) {
  116. wx.hideLoading();
  117. console.log(res3);
  118. log.info('[充值]', '[微信支付]', '[fail]', res3);
  119. }
  120. });
  121. }
  122. });
  123. } else {
  124. wx.hideLoading();
  125. console.log('登录失败!' + res.errMsg)
  126. log.info('[充值]', '[开始微信充值]', '[wx.login响应失败code null]', res);
  127. }
  128. }
  129. })
  130. }
  131. },
  132. /**
  133. * 生命周期函数--监听页面加载
  134. */
  135. onLoad: function(options) {
  136. log.info('[充值]', '[页面加载完成]');
  137. let userInfo = wx.getStorageSync('userInfo');
  138. let isLogin = wx.getStorageSync('isLogin');
  139. // 页面显示
  140. if (userInfo && isLogin) {
  141. //console.log(userInfo);
  142. //userInfo.flag = true;
  143. this.setData({
  144. userInfo: userInfo,
  145. isLogin: isLogin
  146. });
  147. }
  148. },
  149. /**
  150. * 生命周期函数--监听页面初次渲染完成
  151. */
  152. onReady: function() {
  153. },
  154. /**
  155. * 生命周期函数--监听页面显示
  156. */
  157. onShow: function() {
  158. },
  159. /**
  160. * 生命周期函数--监听页面隐藏
  161. */
  162. onHide: function() {
  163. },
  164. /**
  165. * 生命周期函数--监听页面卸载
  166. */
  167. onUnload: function() {
  168. },
  169. /**
  170. * 页面相关事件处理函数--监听用户下拉动作
  171. */
  172. onPullDownRefresh: function() {
  173. },
  174. /**
  175. * 页面上拉触底事件的处理函数
  176. */
  177. onReachBottom: function() {
  178. },
  179. /**
  180. * 用户点击右上角分享
  181. */
  182. onShareAppMessage: function() {
  183. }
  184. })