charge.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. wx.showLoading({
  127. title: '支付中',
  128. })
  129. let oriMoney = 0;
  130. if (sendMoney > 0) {
  131. oriMoney = sendMoney;
  132. } else {
  133. oriMoney = this.data.money;
  134. var reg = /^\d+(\.\d+)?$/;
  135. if (!reg.test(oriMoney)) {
  136. wx.hideLoading();
  137. wx.showToast({
  138. title: '请输入正确的金额',
  139. icon: 'none'
  140. })
  141. return false;
  142. }
  143. }
  144. let money = parseFloat(oriMoney).toFixed(2) || 0;
  145. let token = wx.getStorageSync('token');
  146. let that = this;
  147. that.data.canPay && app.util.request({
  148. url: 'entry/wxapp/user',
  149. data: {
  150. controller: 'car.wxcharge',
  151. token: token,
  152. money: money,
  153. pay_method:'fypay',
  154. rech_id: that.rech_id
  155. },
  156. dataType: 'json',
  157. success: function (res) {
  158. wx.hideLoading();
  159. wx.requestPayment({
  160. "appId": res.data.appId,
  161. "timeStamp": res.data.timeStamp,
  162. "nonceStr": res.data.nonceStr,
  163. "package": res.data.package,
  164. "signType": res.data.signType,
  165. "paySign": res.data.paySign,
  166. 'success': function (wxres) {
  167. that.setData({
  168. canPay: false,
  169. activeTypeId: 0
  170. })
  171. that.getAccountMoney();
  172. that.rech_id = 0;
  173. wx.showToast({
  174. icon: 'none',
  175. title: '充值成功',
  176. })
  177. setTimeout(() => {
  178. wx.reLaunch({
  179. url: '/lionfish_comshop/pages/user/me',
  180. })
  181. }, 2000)
  182. },
  183. 'fail': function (error) {
  184. wx.hideLoading();
  185. if (that.rech_id > 0) that.setData({
  186. canPay: false,
  187. activeTypeId: 0
  188. }), that.rech_id = 0;
  189. wx.showToast({
  190. icon: 'none',
  191. title: error.msg,
  192. })
  193. }
  194. })
  195. }
  196. })
  197. },
  198. /**
  199. * 获得焦点
  200. */
  201. bindIptFocus: function () {
  202. this.rech_id = 0;
  203. this.setData({
  204. onFocus: true,
  205. activeTypeId: 0,
  206. money: '',
  207. canPay: false
  208. })
  209. },
  210. /**
  211. * 失去焦点
  212. */
  213. bindIptBlur: function () {
  214. this.setData({
  215. onFocus: false
  216. })
  217. },
  218. goCharge: function (e) {
  219. let that = this;
  220. let chargetype_list = this.data.chargetype_list;
  221. let idx = e.currentTarget.dataset.idx;
  222. let rech_id = chargetype_list[idx].id;
  223. let money = chargetype_list[idx].money;
  224. this.rech_id = rech_id;
  225. this.setData({
  226. canPay: true
  227. }, () => {
  228. that.wxcharge(money);
  229. })
  230. },
  231. selChargeType: function (e) {
  232. let that = this;
  233. let chargetype_list = this.data.chargetype_list;
  234. let idx = e.currentTarget.dataset.idx;
  235. let activeTypeId = chargetype_list[idx].id || 0;
  236. let money = chargetype_list[idx].money;
  237. this.rech_id = activeTypeId;
  238. that.setData({
  239. activeTypeId,
  240. money,
  241. canPay: true
  242. })
  243. }
  244. })