chargemoney.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. if (!this.data.chargemoney_select) {
  27. return;
  28. }
  29. let that = this
  30. wx.showModal({
  31. title: '',
  32. content: '每日退款最大额度为500元,您是否确认本次充值为' + this.data.chargemoney_select + '元?',
  33. success(res) {
  34. if (res.confirm) {
  35. that.beginChargemoneySuccess(e);
  36. } else if (res.cancel) {
  37. wx.showToast({
  38. title: '充值已取消',
  39. icon: 'error',
  40. duration: 2000
  41. })
  42. }
  43. }
  44. })
  45. },
  46. beginChargemoneySuccess(e) {
  47. //console.log(this.data.chargemoney_select);
  48. log.info('[充值]', '[开始充值流程]');
  49. let that = this;
  50. if (this.data.chargemoney_select) {
  51. wx.showLoading({
  52. title: '充值中...',
  53. mask: true
  54. });
  55. log.info('[充值]', '[开始微信充值]', '[wx.login请求]');
  56. wx.login({
  57. success(res) {
  58. console.log(res);
  59. log.info('[充值]', '[开始微信充值]', '[wx.login响应]', res);
  60. if (res.code) {
  61. // 发起网络请求
  62. log.info('[充值]', '[服务端微信充值]', '[请求]', {
  63. //outTradeNo: new Date().getTime(), //订单号
  64. totalFee: that.data.chargemoney_select, //钱
  65. //totalFee:0.01,
  66. code: res.code,
  67. userId: that.data.userInfo.userId
  68. });
  69. wx.request({
  70. url: getApp().globalData.postHeadAgreement +'/restapi/wechatpay/dopaychargmoney',
  71. data: {
  72. //outTradeNo: new Date().getTime(), //订单号
  73. totalFee: that.data.chargemoney_select, //钱
  74. //totalFee:0.01,
  75. code: res.code,
  76. userId: that.data.userInfo.userId
  77. },
  78. header: {
  79. 'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
  80. },
  81. method: 'POST',
  82. success(res1) {
  83. log.info('[充值]', '[服务端微信充值]', '[响应]', res1.data);
  84. console.log(res1);
  85. log.info('[充值]', '[微信支付]', '[请求]', {
  86. timeStamp: res1.data.timeStamp,
  87. nonceStr: res1.data.nonceStr,
  88. package: res1.data.package,
  89. signType: res1.data.signType,
  90. paySign: res1.data.paySign
  91. });
  92. wx.requestPayment({
  93. timeStamp: res1.data.timeStamp,
  94. nonceStr: res1.data.nonceStr,
  95. package: res1.data.package,
  96. signType: res1.data.signType,
  97. paySign: res1.data.paySign,
  98. success(res2) {
  99. console.log(res2);
  100. log.info('[充值]', '[微信支付]', '[响应]', res2);
  101. log.info('[充值]', '[微信支付回调]', '[请求]', {
  102. userId: that.data.userInfo.userId,
  103. rechargeMoney: that.data.chargemoney_select
  104. //rechargeMoney: 0.01,
  105. });
  106. wx.request({
  107. url: getApp().globalData.postHeadAgreement +'/restapi/pileLog/wechatrechargeback',
  108. data: {
  109. userId: that.data.userInfo.userId,
  110. rechargeMoney: that.data.chargemoney_select
  111. //rechargeMoney: 0.01,
  112. },
  113. method: 'POST',
  114. success(res3) {
  115. wx.hideLoading();
  116. let {
  117. data
  118. } = res3;
  119. log.info('[充值]', '[微信支付回调]', '[响应]', data);
  120. if (data && data.code == 200) {
  121. // wx.showModal({
  122. // showCancel: false,
  123. // content: '充值成功'
  124. // });
  125. let url = `/pages/chargemoneyresult/chargemoneyresult?timeStamp=${res1.data.timeStamp}&money=${that.data.chargemoney_select}`;
  126. wx.redirectTo({
  127. url
  128. })
  129. log.info('[充值]', '[微信支付回调]', '[成功]');
  130. } else {
  131. wx.showModal({
  132. showCancel: false,
  133. content: '充值失败'
  134. });
  135. log.info('[充值]', '[微信支付回调]', '[失败data null||data.code<>200]', data);
  136. }
  137. }
  138. });
  139. },
  140. fail(res3) {
  141. wx.hideLoading();
  142. console.log(res3);
  143. log.info('[充值]', '[微信支付]', '[fail]', res3);
  144. }
  145. });
  146. }
  147. });
  148. } else {
  149. wx.hideLoading();
  150. console.log('登录失败!' + res.errMsg)
  151. log.info('[充值]', '[开始微信充值]', '[wx.login响应失败code null]', res);
  152. }
  153. }
  154. })
  155. }
  156. },
  157. /**
  158. * 生命周期函数--监听页面加载
  159. */
  160. onLoad: function(options) {
  161. log.info('[充值]', '[页面加载完成]');
  162. let userInfo = wx.getStorageSync('userInfo');
  163. let isLogin = wx.getStorageSync('isLogin');
  164. // 页面显示
  165. if (userInfo && isLogin) {
  166. //console.log(userInfo);
  167. //userInfo.flag = true;
  168. this.setData({
  169. userInfo: userInfo,
  170. isLogin: isLogin
  171. });
  172. }
  173. },
  174. /**
  175. * 生命周期函数--监听页面初次渲染完成
  176. */
  177. onReady: function() {
  178. },
  179. /**
  180. * 生命周期函数--监听页面显示
  181. */
  182. onShow: function() {
  183. },
  184. /**
  185. * 生命周期函数--监听页面隐藏
  186. */
  187. onHide: function() {
  188. },
  189. /**
  190. * 生命周期函数--监听页面卸载
  191. */
  192. onUnload: function() {
  193. },
  194. /**
  195. * 页面相关事件处理函数--监听用户下拉动作
  196. */
  197. onPullDownRefresh: function() {
  198. },
  199. /**
  200. * 页面上拉触底事件的处理函数
  201. */
  202. onReachBottom: function() {
  203. },
  204. /**
  205. * 用户点击右上角分享
  206. */
  207. onShareAppMessage: function() {
  208. }
  209. })