index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. paid_price: '',
  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. paid_price: this.data.picList[0].price,
  51. numberPic: this.data.picList[0].quota,
  52. activePic: 0
  53. })
  54. },
  55. /**
  56. * 选择金额
  57. */
  58. picCharge:function(e){
  59. if (e.currentTarget.dataset.id == this.data.picList.length) {
  60. this.setData({
  61. activePic: e.currentTarget.dataset.id,
  62. paid_price: '',
  63. numberPic: ''
  64. })
  65. }else {
  66. this.setData({
  67. number: '',
  68. activePic: e.currentTarget.dataset.id,
  69. paid_price: e.currentTarget.dataset.pic,
  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. paid_price: res.data.recharge_quota[0].price,
  89. numberPic: res.data.recharge_quota[0].quota,
  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;
  108. if (that.data.active){
  109. if (parseFloat(value) < 0 || !value) return app.Tips({ title: '请输入金额' });
  110. wx.showModal({
  111. title: '转入余额',
  112. content: '转入余额后无法再次转出,确认是否转入余额',
  113. success(res){
  114. if (res.confirm){
  115. rechargeRoutine({ price: value, type: 1 }).then(res => {
  116. that.setData({ 'userinfo.now_money': app.help().Add(value, that.data.userinfo.now_money) });
  117. return app.Tips({ title: '转入成功', icon: 'success' }, {tab:5,url:'/pages/user_money/index'});
  118. }).catch(err => {
  119. return app.Tips({ title: err })
  120. });
  121. } else if (res.cancel){
  122. return app.Tips({title:'已取消'});
  123. }
  124. },
  125. })
  126. }else{
  127. if (this.data.picList.length == this.data.activePic && !value) return app.Tips({ title: '请输入金额' });
  128. let price = '', paid_price = '';
  129. if (value){
  130. price = value;
  131. paid_price =value;
  132. }else{
  133. price = that.data.numberPic;
  134. paid_price = that.data.paid_price;
  135. }
  136. wx.showLoading({
  137. title: '正在支付',
  138. })
  139. rechargeRoutine({ price: price, type: 0, paid_price: paid_price}).then(res=>{
  140. wx.hideLoading();
  141. let jsConfig = res.data;
  142. wx.requestPayment({
  143. timeStamp: jsConfig.timestamp,
  144. nonceStr: jsConfig.nonceStr,
  145. package: jsConfig.package,
  146. signType: jsConfig.signType,
  147. paySign: jsConfig.paySign,
  148. success: function (res) {
  149. that.setData({ 'userinfo.now_money': app.help().Add(value, that.data.userinfo.now_money) });
  150. return app.Tips({ title: '支付成功', icon: 'success' }, {tab:5,url:'/pages/user_money/index'});
  151. },
  152. fail: function () {
  153. return app.Tips({ title: '支付失败' });
  154. },
  155. complete: function (res) {
  156. if (res.errMsg == 'requestPayment:cancel') return app.Tips({ title: '取消支付' });
  157. }
  158. })
  159. }).catch(err=>{
  160. wx.hideLoading();
  161. return app.Tips({title:err})
  162. });
  163. }
  164. }
  165. })