index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. activePic: 0
  51. })
  52. if (this.data.picList.length){
  53. this.setData({
  54. rechar_id: this.data.picList[0].id,
  55. numberPic: this.data.picList[0].price
  56. })
  57. }
  58. },
  59. /**
  60. * 选择金额
  61. */
  62. picCharge:function(e){
  63. if (e.currentTarget.dataset.id == 0) {
  64. this.setData({
  65. activePic: e.currentTarget.dataset.index,
  66. rechar_id: 0,
  67. numberPic: ''
  68. })
  69. }else {
  70. this.setData({
  71. number: '',
  72. activePic: e.currentTarget.dataset.index,
  73. rechar_id: e.currentTarget.dataset.id,
  74. numberPic: e.currentTarget.dataset.quota,
  75. placeholderOther: '其他'
  76. })
  77. }
  78. },
  79. /**
  80. * 生命周期函数--监听页面加载
  81. */
  82. onLoad: function (options) {
  83. },
  84. /**
  85. * 充值额度选择
  86. */
  87. getRecharge: function () {
  88. var that = this;
  89. getRechargeApi().then(res => {
  90. that.setData({
  91. picList: res.data.recharge_quota,
  92. rechar_id: res.data.recharge_quota[0].id,
  93. numberPic: res.data.recharge_quota[0].price,
  94. recharge_attention: res.data.recharge_attention || []
  95. });
  96. })
  97. },
  98. /**
  99. * 获取用户信息
  100. */
  101. getUserInfo:function(){
  102. var that = this;
  103. getUserInfo().then(res=>{
  104. that.setData({ userinfo: res.data });
  105. })
  106. },
  107. /*
  108. * 用户充值
  109. */
  110. submitSub:function(e){
  111. let that = this, value = e.detail.value.number, commissionCount = that.data.userinfo.commissionCount;
  112. if (that.data.active){
  113. if (parseFloat(value) < 0 || !value) return app.Tips({ title: '请输入金额' });
  114. if (Number(value) > Number(commissionCount)) return app.Tips({ title: '转佣金额不能大于' + commissionCount });
  115. wx.showModal({
  116. title: '转入余额',
  117. content: '转入余额后无法再次转出,确认是否转入余额',
  118. success(res){
  119. if (res.confirm){
  120. rechargeRoutine({ price: value, type: 1 }).then(res => {
  121. that.setData({ 'userinfo.now_money': app.help().Add(value, that.data.userinfo.now_money) });
  122. return app.Tips({ title: '转入成功', icon: 'success' }, {tab:5,url:'/pages/user_money/index'});
  123. }).catch(err => {
  124. return app.Tips({ title: err })
  125. });
  126. } else if (res.cancel){
  127. return app.Tips({title:'已取消'});
  128. }
  129. },
  130. })
  131. }else{
  132. if (this.data.picList.length == this.data.activePic && !value) return app.Tips({ title: '请输入金额' });
  133. wx.showLoading({
  134. title: '正在支付',
  135. })
  136. rechargeRoutine({
  137. price: that.data.rechar_id == 0 ? value : that.data.numberPic,
  138. type: 0,
  139. rechar_id: that.data.rechar_id
  140. }).then(res=>{
  141. wx.hideLoading();
  142. let jsConfig = res.data;
  143. wx.requestPayment({
  144. timeStamp: jsConfig.timestamp,
  145. nonceStr: jsConfig.nonceStr,
  146. package: jsConfig.package,
  147. signType: jsConfig.signType,
  148. paySign: jsConfig.paySign,
  149. success: function (res) {
  150. that.setData({ 'userinfo.now_money': app.help().Add(value, that.data.userinfo.now_money) });
  151. return app.Tips({ title: '支付成功', icon: 'success' }, {tab:5,url:'/pages/user_money/index'});
  152. },
  153. fail: function () {
  154. return app.Tips({ title: '支付失败' });
  155. },
  156. complete: function (res) {
  157. if (res.errMsg == 'requestPayment:cancel') return app.Tips({ title: '取消支付' });
  158. }
  159. })
  160. }).catch(err=>{
  161. wx.hideLoading();
  162. return app.Tips({title:err})
  163. });
  164. }
  165. }
  166. })