chargemoney.js 8.3 KB

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