// pages/mall/payment/payment.js import { getUserInfo, rechargeRoutine, getRechargeApi} from '../../api/user.js'; var app = getApp(); Page({ /** * 页面的初始数据 */ data: { picList:[], parameter: { 'navbar': '1', 'return': '1', 'title': '余额充值', 'color': false, }, navRecharge: ['账户充值','佣金转入'], active:0, number:'', focus:true, userinfo:{}, placeholder:"0.00", placeholderOther:"其他", activePic:0, numberPic: '', paid_price: '', recharge_attention:[] }, /** * 登录授权回调 */ onLoadFun:function(){ this.getUserInfo(); this.getRecharge(); }, setPlaceholderStatus:function(event){ if (event.detail.value.length == 0) this.setData({ placeholder: '0.00' }); }, setPlaceholder:function(){ this.setData({ placeholder : '' }) }, setOtherPlaceholder:function(){ this.setData({ placeholderOther : '' }) }, setOtherPlaceholderStatus:function(event){ if (event.detail.value.length == 0) this.setData({ placeholderOther: '其他' }); }, navRecharge:function(e){ this.setData({ active: e.currentTarget.dataset.id, paid_price: this.data.picList[0].price, numberPic: this.data.picList[0].quota, activePic: 0 }) }, /** * 选择金额 */ picCharge:function(e){ if (e.currentTarget.dataset.id == this.data.picList.length) { this.setData({ activePic: e.currentTarget.dataset.id, paid_price: '', numberPic: '' }) }else { this.setData({ number: '', activePic: e.currentTarget.dataset.id, paid_price: e.currentTarget.dataset.pic, numberPic: e.currentTarget.dataset.quota, placeholderOther: '其他' }) } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 充值额度选择 */ getRecharge: function () { var that = this; getRechargeApi().then(res => { that.setData({ picList: res.data.recharge_quota, paid_price: res.data.recharge_quota[0].price, numberPic: res.data.recharge_quota[0].quota, recharge_attention: res.data.recharge_attention || [] }); }) }, /** * 获取用户信息 */ getUserInfo:function(){ var that = this; getUserInfo().then(res=>{ that.setData({ userinfo: res.data }); }) }, /* * 用户充值 */ submitSub:function(e){ let that = this, value = e.detail.value.number; if (that.data.active){ if (parseFloat(value) < 0 || !value) return app.Tips({ title: '请输入金额' }); wx.showModal({ title: '转入余额', content: '转入余额后无法再次转出,确认是否转入余额', success(res){ if (res.confirm){ rechargeRoutine({ price: value, type: 1 }).then(res => { that.setData({ 'userinfo.now_money': app.help().Add(value, that.data.userinfo.now_money) }); return app.Tips({ title: '转入成功', icon: 'success' }, {tab:5,url:'/pages/user_money/index'}); }).catch(err => { return app.Tips({ title: err }) }); } else if (res.cancel){ return app.Tips({title:'已取消'}); } }, }) }else{ if (this.data.picList.length == this.data.activePic && !value) return app.Tips({ title: '请输入金额' }); let price = '', paid_price = ''; if (value){ price = value; paid_price =value; }else{ price = that.data.numberPic; paid_price = that.data.paid_price; } wx.showLoading({ title: '正在支付', }) rechargeRoutine({ price: price, type: 0, paid_price: paid_price}).then(res=>{ wx.hideLoading(); let jsConfig = res.data; wx.requestPayment({ timeStamp: jsConfig.timestamp, nonceStr: jsConfig.nonceStr, package: jsConfig.package, signType: jsConfig.signType, paySign: jsConfig.paySign, success: function (res) { that.setData({ 'userinfo.now_money': app.help().Add(value, that.data.userinfo.now_money) }); return app.Tips({ title: '支付成功', icon: 'success' }, {tab:5,url:'/pages/user_money/index'}); }, fail: function () { return app.Tips({ title: '支付失败' }); }, complete: function (res) { if (res.errMsg == 'requestPayment:cancel') return app.Tips({ title: '取消支付' }); } }) }).catch(err=>{ wx.hideLoading(); return app.Tips({title:err}) }); } } })