charge.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. var app = getApp();
  2. var util = require('../../utils/util.js');
  3. var WxParse = require('../../wxParse/wxParse.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. canPay: false,
  10. money: 0,
  11. onFocus: false,
  12. accountMoney: 0,
  13. chargetype_list: [],
  14. },
  15. rech_id: 0,
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: function (options) {
  20. this.getAccountMoney();
  21. },
  22. /**
  23. * 生命周期函数--监听页面显示
  24. */
  25. onShow: function () {
  26. if (!util.check_login()) {
  27. wx.redirectTo({
  28. url: '/lionfish_comshop/pages/user/me',
  29. })
  30. }
  31. },
  32. getAccountMoney(){
  33. let token = wx.getStorageSync('token');
  34. let that = this;
  35. app.util.request({
  36. 'url': 'entry/wxapp/user',
  37. 'data': {
  38. controller: 'user.get_account_money',
  39. token: token
  40. },
  41. dataType: 'json',
  42. success: function (res) {
  43. if (res.data.code == 0){
  44. let rdata= res.data;
  45. let member_charge_publish = rdata.member_charge_publish;
  46. WxParse.wxParse('article', 'html', member_charge_publish, that, 15, app.globalData.systemInfo);
  47. let chargetype_list = rdata.chargetype_list;
  48. that.setData({ accountMoney: rdata.data, chargetype_list })
  49. } else if(res.data.code == 1) {
  50. wx.redirectTo({
  51. url: '/lionfish_comshop/pages/user/me',
  52. })
  53. }
  54. }
  55. })
  56. },
  57. getMoney: function(e){
  58. var val = e.detail.value;
  59. val ? this.setData({ canPay: true }) : this.setData({ canPay: false });
  60. let money = val
  61. this.setData({ money });
  62. // return money;
  63. },
  64. /**
  65. * 余额充值
  66. */
  67. wxcharge: function (sendMoney=0) {
  68. let oriMoney = 0;
  69. if (sendMoney>0) {
  70. oriMoney = sendMoney;
  71. } else {
  72. oriMoney = this.data.money;
  73. console.log(oriMoney)
  74. var reg = /^\d+(\.\d+)?$/;
  75. if(!reg.test(oriMoney)) {
  76. wx.showToast({
  77. title: '请输入正确的金额',
  78. icon: 'none'
  79. })
  80. return false;
  81. }
  82. }
  83. let money = parseFloat(oriMoney).toFixed(2) || 0;
  84. let token = wx.getStorageSync('token');
  85. let that = this;
  86. that.data.canPay&&app.util.request({
  87. 'url': 'entry/wxapp/user',
  88. 'data': {
  89. controller: 'car.wxcharge',
  90. token: token,
  91. money: money,
  92. rech_id: that.rech_id
  93. },
  94. dataType: 'json',
  95. success: function (res) {
  96. wx.requestPayment({
  97. "appId": res.data.appId,
  98. "timeStamp": res.data.timeStamp,
  99. "nonceStr": res.data.nonceStr,
  100. "package": res.data.package,
  101. "signType": res.data.signType,
  102. "paySign": res.data.paySign,
  103. 'success': function (wxres) {
  104. that.setData({ canPay: false })
  105. // that.data.canPay = true;
  106. that.getAccountMoney();
  107. that.rech_id = 0;
  108. wx.showToast({
  109. icon: 'none',
  110. title: '充值成功',
  111. })
  112. setTimeout(() => {
  113. wx.reLaunch({
  114. url: '/lionfish_comshop/pages/user/me',
  115. })
  116. }, 2000)
  117. },
  118. 'fail': function (error) {
  119. if (that.rech_id > 0) that.setData({ canPay: false }), that.rech_id = 0;
  120. wx.showToast({
  121. icon: 'none',
  122. title: '充值失败,请重试!',
  123. })
  124. }
  125. })
  126. }
  127. })
  128. },
  129. /**
  130. * 获得焦点
  131. */
  132. bindIptFocus: function(){
  133. this.setData({ onFocus: true })
  134. },
  135. /**
  136. * 失去焦点
  137. */
  138. bindIptBlur: function () {
  139. this.setData({ onFocus: false })
  140. },
  141. goCharge: function(e){
  142. let that = this;
  143. let chargetype_list = this.data.chargetype_list;
  144. let idx = e.currentTarget.dataset.idx;
  145. let rech_id = chargetype_list[idx].id;
  146. let money = chargetype_list[idx].money;
  147. this.rech_id = rech_id;
  148. this.setData({
  149. canPay: true
  150. },()=>{
  151. that.wxcharge(money);
  152. })
  153. }
  154. })