index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // pages/mall/payment/payment.js
  2. import { getUserInfo, rechargeRoutine, getRechargeApi} from '../../api/user.js';
  3. var app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. picList:[],
  10. parameter: {
  11. 'navbar': '1',
  12. 'return': '1',
  13. 'title': '余额充值',
  14. 'color': false,
  15. },
  16. navRecharge: ['账户充值','佣金转入'],
  17. active:0,
  18. number:'',
  19. focus:true,
  20. userinfo:{},
  21. placeholder:"0.00",
  22. placeholderOther:"其他",
  23. activePic:0,
  24. numberPic: '',
  25. rechar_id: 0,
  26. recharge_attention:[]
  27. },
  28. /**
  29. * 登录授权回调
  30. */
  31. onLoadFun:function(){
  32. this.getUserInfo();
  33. this.getRecharge();
  34. },
  35. setPlaceholderStatus:function(event){
  36. if (event.detail.value.length == 0) this.setData({ placeholder: '0.00' });
  37. },
  38. setPlaceholder:function(){
  39. this.setData({ placeholder : '' })
  40. },
  41. setOtherPlaceholder:function(){
  42. this.setData({ placeholderOther : '' })
  43. },
  44. setOtherPlaceholderStatus:function(event){
  45. if (event.detail.value.length == 0) this.setData({ placeholderOther: '其他' });
  46. },
  47. navRecharge:function(e){
  48. this.setData({
  49. active: e.currentTarget.dataset.id,
  50. rechar_id: this.data.picList[0].id,
  51. numberPic: this.data.picList[0].price,
  52. activePic: 0
  53. })
  54. },
  55. /**
  56. * 选择金额
  57. */
  58. picCharge:function(e){
  59. if (e.currentTarget.dataset.id == 0) {
  60. this.setData({
  61. activePic: e.currentTarget.dataset.index,
  62. rechar_id: 0,
  63. numberPic: ''
  64. })
  65. }else {
  66. this.setData({
  67. number: '',
  68. activePic: e.currentTarget.dataset.index,
  69. rechar_id: e.currentTarget.dataset.id,
  70. numberPic: e.currentTarget.dataset.quota,
  71. placeholderOther: '其他'
  72. })
  73. }
  74. },
  75. /**
  76. * 生命周期函数--监听页面加载
  77. */
  78. onLoad: function (options) {
  79. },
  80. /**
  81. * 充值额度选择
  82. */
  83. getRecharge: function () {
  84. var that = this;
  85. getRechargeApi().then(res => {
  86. that.setData({
  87. picList: res.data.recharge_quota,
  88. rechar_id: res.data.recharge_quota[0].id,
  89. numberPic: res.data.recharge_quota[0].price,
  90. recharge_attention: res.data.recharge_attention || []
  91. });
  92. })
  93. },
  94. /**
  95. * 获取用户信息
  96. */
  97. getUserInfo:function(){
  98. var that = this;
  99. getUserInfo().then(res=>{
  100. that.setData({ userinfo: res.data });
  101. })
  102. },
  103. /*
  104. * 用户充值
  105. */
  106. submitSub:function(e){
  107. let that = this, value = e.detail.value.number, commissionCount = that.data.userinfo.commissionCount;
  108. if (that.data.active){
  109. if (parseFloat(value) < 0 || !value) return app.Tips({ title: '请输入金额' });
  110. if (Number(value) > Number(commissionCount)) return app.Tips({ title: '转佣金额不能大于' + commissionCount });
  111. wx.showModal({
  112. title: '转入余额',
  113. content: '转入余额后无法再次转出,确认是否转入余额',
  114. success(res){
  115. if (res.confirm){
  116. rechargeRoutine({ price: value, type: 1 }).then(res => {
  117. that.setData({ 'userinfo.now_money': app.help().Add(value, that.data.userinfo.now_money) });
  118. return app.Tips({ title: '转入成功', icon: 'success' }, {tab:5,url:'/pages/user_money/index'});
  119. }).catch(err => {
  120. return app.Tips({ title: err })
  121. });
  122. } else if (res.cancel){
  123. return app.Tips({title:'已取消'});
  124. }
  125. },
  126. })
  127. }else{
  128. if (this.data.picList.length == this.data.activePic && !value) return app.Tips({ title: '请输入金额' });
  129. wx.showLoading({
  130. title: '正在支付',
  131. })
  132. rechargeRoutine({
  133. price: that.data.rechar_id == 0 ? value : that.data.numberPic,
  134. type: 0,
  135. rechar_id: that.data.rechar_id
  136. }).then(res=>{
  137. wx.hideLoading();
  138. let jsConfig = res.data;
  139. wx.requestPayment({
  140. timeStamp: jsConfig.timestamp,
  141. nonceStr: jsConfig.nonceStr,
  142. package: jsConfig.package,
  143. signType: jsConfig.signType,
  144. paySign: jsConfig.paySign,
  145. success: function (res) {
  146. that.setData({ 'userinfo.now_money': app.help().Add(value, that.data.userinfo.now_money) });
  147. return app.Tips({ title: '支付成功', icon: 'success' }, {tab:5,url:'/pages/user_money/index'});
  148. },
  149. fail: function () {
  150. return app.Tips({ title: '支付失败' });
  151. },
  152. complete: function (res) {
  153. if (res.errMsg == 'requestPayment:cancel') return app.Tips({ title: '取消支付' });
  154. }
  155. })
  156. }).catch(err=>{
  157. wx.hideLoading();
  158. return app.Tips({title:err})
  159. });
  160. }
  161. }
  162. })