chargemoney.js 8.6 KB

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