chargemoney.js 6.9 KB

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