chargemoney.js 8.2 KB

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