charge.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. var app = getApp();
  2. var util = require('../../utils/util.js');
  3. Page({
  4. mixins: [require('../../mixin/globalMixin.js')],
  5. data: {
  6. canPay: false,
  7. money: '',
  8. onFocus: false,
  9. accountMoney: 0,
  10. chargetype_list: [],
  11. activeTypeId: '',
  12. recharge_get_money: '',
  13. rewardIdx: -1,
  14. chargeArr: []
  15. },
  16. rech_id: 0,
  17. revenue: [],
  18. reward: [],
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function (options) {
  23. this.getAccountMoney();
  24. },
  25. /**
  26. * 生命周期函数--监听页面显示
  27. */
  28. onShow: function () {
  29. if (!util.check_login()) {
  30. wx.redirectTo({
  31. url: '/lionfish_comshop/pages/user/me',
  32. })
  33. }
  34. },
  35. getAccountMoney() {
  36. let token = wx.getStorageSync('token');
  37. let that = this;
  38. app.util.request({
  39. 'url': 'entry/wxapp/user',
  40. 'data': {
  41. controller: 'user.get_account_money',
  42. token: token
  43. },
  44. dataType: 'json',
  45. success: function (res) {
  46. if (res.data.code == 0) {
  47. let rdata = res.data;
  48. let member_charge_publish = rdata.member_charge_publish;
  49. let chargetype_list = rdata.chargetype_list;
  50. let recharge_get_money = rdata.recharge_get_money || '';
  51. let chargeArr = chargetype_list.sort(function (a, b) {
  52. return a.money - b.money;
  53. });
  54. let revenue = [];
  55. let reward = [];
  56. chargeArr.forEach(item => {
  57. revenue.push(item.money * 1);
  58. reward.push(item.send_money * 1);
  59. })
  60. that.revenue = revenue;
  61. that.reward = reward;
  62. let excharge_nav_name = res.data.excharge_nav_name || '详情';
  63. wx.setNavigationBarTitle({
  64. title: excharge_nav_name,
  65. })
  66. that.setData({
  67. accountMoney: rdata.data,
  68. chargetype_list,
  69. member_charge_publish,
  70. recharge_get_money,
  71. chargeArr
  72. })
  73. } else if (res.data.code == 1) {
  74. wx.redirectTo({
  75. url: '/lionfish_comshop/pages/user/me',
  76. })
  77. }
  78. }
  79. })
  80. },
  81. getMoney: function (e) {
  82. var val = e.detail.value;
  83. val ? this.setData({
  84. canPay: true
  85. }) : this.setData({
  86. canPay: false
  87. });
  88. let money = val;
  89. let rewardIdx = -1;
  90. if(this.data.recharge_get_money==1) {
  91. rewardIdx = this.canAwardMoney(money);
  92. console.log('rewardIdx', rewardIdx);
  93. }
  94. this.setData({
  95. money,
  96. rewardIdx
  97. });
  98. // return money;
  99. },
  100. /**
  101. * 获得送金额提示
  102. */
  103. canAwardMoney: function (money) {
  104. let revenue = this.revenue;
  105. let reward = this.reward;
  106. //小于最小值
  107. if (money < Math.min.apply(null, revenue)) {
  108. return -1;
  109. }
  110. if (money >= Math.max.apply(null, revenue)) {
  111. return revenue.length - 1;
  112. }
  113. let idx = 0, i = 0, j = revenue.length;
  114. for (i; i < j; i++) {
  115. if (revenue[i] > money) {
  116. idx = i;
  117. break;
  118. }
  119. }
  120. return idx-1;
  121. },
  122. /**
  123. * 余额充值
  124. */
  125. wxcharge: function (sendMoney = 0) {
  126. let oriMoney = 0;
  127. if (sendMoney > 0) {
  128. oriMoney = sendMoney;
  129. } else {
  130. oriMoney = this.data.money;
  131. var reg = /^\d+(\.\d+)?$/;
  132. if (!reg.test(oriMoney)) {
  133. wx.showToast({
  134. title: '请输入正确的金额',
  135. icon: 'none'
  136. })
  137. return false;
  138. }
  139. }
  140. let money = parseFloat(oriMoney).toFixed(2) || 0;
  141. let token = wx.getStorageSync('token');
  142. let that = this;
  143. that.data.canPay && app.util.request({
  144. url: 'entry/wxapp/user',
  145. data: {
  146. controller: 'car.wxcharge',
  147. token: token,
  148. money: money,
  149. rech_id: that.rech_id
  150. },
  151. dataType: 'json',
  152. success: function (res) {
  153. wx.requestPayment({
  154. "appId": res.data.appId,
  155. "timeStamp": res.data.timeStamp,
  156. "nonceStr": res.data.nonceStr,
  157. "package": res.data.package,
  158. "signType": res.data.signType,
  159. "paySign": res.data.paySign,
  160. 'success': function (wxres) {
  161. that.setData({
  162. canPay: false,
  163. activeTypeId: 0
  164. })
  165. that.getAccountMoney();
  166. that.rech_id = 0;
  167. wx.showToast({
  168. icon: 'none',
  169. title: '充值成功',
  170. })
  171. setTimeout(() => {
  172. wx.reLaunch({
  173. url: '/lionfish_comshop/pages/user/me',
  174. })
  175. }, 2000)
  176. },
  177. 'fail': function (error) {
  178. if (that.rech_id > 0) that.setData({
  179. canPay: false,
  180. activeTypeId: 0
  181. }), that.rech_id = 0;
  182. wx.showToast({
  183. icon: 'none',
  184. title: '充值失败,请重试!',
  185. })
  186. }
  187. })
  188. }
  189. })
  190. },
  191. /**
  192. * 获得焦点
  193. */
  194. bindIptFocus: function () {
  195. this.rech_id = 0;
  196. this.setData({
  197. onFocus: true,
  198. activeTypeId: 0,
  199. money: '',
  200. canPay: false
  201. })
  202. },
  203. /**
  204. * 失去焦点
  205. */
  206. bindIptBlur: function () {
  207. this.setData({
  208. onFocus: false
  209. })
  210. },
  211. goCharge: function (e) {
  212. let that = this;
  213. let chargetype_list = this.data.chargetype_list;
  214. let idx = e.currentTarget.dataset.idx;
  215. let rech_id = chargetype_list[idx].id;
  216. let money = chargetype_list[idx].money;
  217. this.rech_id = rech_id;
  218. this.setData({
  219. canPay: true
  220. }, () => {
  221. that.wxcharge(money);
  222. })
  223. },
  224. selChargeType: function (e) {
  225. let that = this;
  226. let chargetype_list = this.data.chargetype_list;
  227. let idx = e.currentTarget.dataset.idx;
  228. let activeTypeId = chargetype_list[idx].id || 0;
  229. let money = chargetype_list[idx].money;
  230. this.rech_id = activeTypeId;
  231. that.setData({
  232. activeTypeId,
  233. money,
  234. canPay: true
  235. })
  236. }
  237. })