chargemoney.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. if(res1.data.errCodeDes==2 && res1.data.bindingPhone==0 ){
  83. wx.hideLoading();
  84. wx.showModal({
  85. title: '提示',
  86. content: '您未绑定手机号,请重新登录后自动刷新绑定信息',
  87. confirmText: '去登录',
  88. showCancel:false,
  89. confirmColor:'#00AADD',
  90. success: function (res1) {
  91. if (res1.confirm) {
  92. wx.removeStorageSync('userInfo');
  93. wx.removeStorageSync('isLogin');
  94. let url = '/pages/login/phone_login/phone_login';
  95. wx.redirectTo({
  96. url
  97. })
  98. }
  99. }
  100. });
  101. return;
  102. }
  103. log.info('[充值]', '[微信支付]', '[请求]', {
  104. timeStamp: res1.data.timeStamp,
  105. nonceStr: res1.data.nonceStr,
  106. package: res1.data.package,
  107. signType: res1.data.signType,
  108. paySign: res1.data.paySign
  109. });
  110. wx.requestPayment({
  111. timeStamp: res1.data.timeStamp,
  112. nonceStr: res1.data.nonceStr,
  113. package: res1.data.package,
  114. signType: res1.data.signType,
  115. paySign: res1.data.paySign,
  116. success(res2) {
  117. console.log(res2);
  118. log.info('[充值]', '[微信支付]', '[响应]', res2);
  119. log.info('[充值]', '[微信支付回调]', '[请求]', {
  120. userId: that.data.userInfo.userId,
  121. rechargeMoney: that.data.chargemoney_select
  122. //rechargeMoney: 0.01,
  123. });
  124. wx.request({
  125. url: getApp().globalData.postHeadAgreement +'/restapi/pileLog/wechatrechargeback',
  126. data: {
  127. userId: that.data.userInfo.userId,
  128. rechargeMoney: that.data.chargemoney_select
  129. //rechargeMoney: 0.01,
  130. },
  131. method: 'POST',
  132. success(res3) {
  133. wx.hideLoading();
  134. let {
  135. data
  136. } = res3;
  137. log.info('[充值]', '[微信支付回调]', '[响应]', data);
  138. if (data && data.code == 200) {
  139. wx.showModal({
  140. showCancel: false,
  141. content: '微信扣款成功,平台入账稍有延迟,请您确认余额更新后再启动充电。'
  142. });
  143. let url = `/pages/chargemoneyresult/chargemoneyresult?timeStamp=${res1.data.timeStamp}&money=${that.data.chargemoney_select}`;
  144. wx.redirectTo({
  145. url
  146. })
  147. log.info('[充值]', '[微信支付回调]', '[成功]');
  148. } else {
  149. wx.showModal({
  150. showCancel: false,
  151. content: '充值失败'
  152. });
  153. log.info('[充值]', '[微信支付回调]', '[失败data null||data.code<>200]', data);
  154. }
  155. }
  156. });
  157. },
  158. fail(res3) {
  159. wx.hideLoading();
  160. console.log(res3);
  161. log.info('[充值]', '[微信支付]', '[fail]', res3);
  162. }
  163. });
  164. }
  165. });
  166. } else {
  167. wx.hideLoading();
  168. console.log('登录失败!' + res.errMsg)
  169. log.info('[充值]', '[开始微信充值]', '[wx.login响应失败code null]', res);
  170. }
  171. }
  172. })
  173. }
  174. },
  175. /**
  176. * 生命周期函数--监听页面加载
  177. */
  178. onLoad: function(options) {
  179. log.info('[充值]', '[页面加载完成]');
  180. let userInfo = wx.getStorageSync('userInfo');
  181. let isLogin = wx.getStorageSync('isLogin');
  182. // 页面显示
  183. if (userInfo && isLogin) {
  184. if(userInfo.bindingPhone==0){
  185. wx.showModal({
  186. title: '提示',
  187. content: '当前账户未绑定手机号,请您进行手机号绑定操作',
  188. showCancel:false,
  189. confirmText: '去绑定',
  190. confirmColor:'#00AADD',
  191. success: function (res1) {
  192. let url = `/pages/bindPhone/bindPhone`;
  193. wx.navigateTo({
  194. url
  195. });
  196. }
  197. })
  198. return;
  199. }
  200. //console.log(userInfo);
  201. //userInfo.flag = true;
  202. this.setData({
  203. userInfo: userInfo,
  204. isLogin: isLogin
  205. });
  206. }
  207. },
  208. /**
  209. * 生命周期函数--监听页面初次渲染完成
  210. */
  211. onReady: function() {
  212. },
  213. /**
  214. * 生命周期函数--监听页面显示
  215. */
  216. onShow: function() {
  217. },
  218. /**
  219. * 生命周期函数--监听页面隐藏
  220. */
  221. onHide: function() {
  222. },
  223. /**
  224. * 生命周期函数--监听页面卸载
  225. */
  226. onUnload: function() {
  227. },
  228. /**
  229. * 页面相关事件处理函数--监听用户下拉动作
  230. */
  231. onPullDownRefresh: function() {
  232. },
  233. /**
  234. * 页面上拉触底事件的处理函数
  235. */
  236. onReachBottom: function() {
  237. },
  238. /**
  239. * 用户点击右上角分享
  240. */
  241. onShareAppMessage: function() {
  242. }
  243. })