// pages/rechargeMoney/rechargMoney.js //充值界面 const { get, post } = require('../../api/request.js'); const md5 = require('js-md5'); Page({ /** * 页面的初始数据 */ data: { userName: "张三", userPhone: "15802220723", userCode: "15584236", balance: "1025元", activeIndex: -1, isClickable: false, allRechargeList: [], vcId: "", pageCurr: 1, //充值记录当前页码 pageSize: 20, //充值记录每页记录数 loading: false, //是否正在加载 hasMore: true, moneyList:[]//充值金额 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.setData({ vcId: options.vcId }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { this.getRechargList(); this. getMorneyList(); }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { this.setData({ pageCurr: 1, }) this.getRechargList(true) }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, handleMoneyWrapperTap: function (e) { const index = e.currentTarget.dataset.index; console.log(index) if (index !== undefined) { this.setData({ activeIndex: parseInt(index), isClickable: true }); } }, //创建订单并调起支付 creatOrder() { if (!this.data.isClickable) { return; } const data = { sessionId: getApp().globalData.sessionId, vcId: this.data.vcId, //虚拟卡ID rechargeAmount: this.data.moneyList[this.data.activeIndex].rechargeAmount//(单位是元) }; console.log("postCloseValaue" + data); post({ url: "wx/payment/placeOrder", data: data }).then(response => { // 处理成功响应 console.log('请求成功:', response); this.getOrderSign(response.content.prepay_id) }).catch(error => { if (error.code === "1002") {} // 处理错误响应 console.error('请求失败:', error); }); }, //获取订单返回的prepay_id的签名 getOrderSign(id) { const params = { url: 'wx/payment/signAgain', data: { prepayId: id } }; get(params).then(data => { // 调起支付 wx.requestPayment({ timeStamp: data.content.timeStamp, nonceStr: data.content.nonceStr, package: data.content.package, signType: data.content.signType, paySign: data.content.paySign, success(res) { wx.showToast({ title: "充值成功", icon: 'success', duration: 3000 }) console.log('支付成功', res); this. getRechargList(true); }, fail(err) { console.log('支付失败', err); } }); }).catch(err => { wx.showToast({ title: err.msg, icon: 'error', duration: 3000 }) }); }, //获取充值记录 getRechargList(isRefresh) { const params = { url: 'wx/virtual_card/getVcRechargeRecords', data: { vcId: this.data.vcId, pageCurr: this.data.pageCurr, pageSize: this.data.pageSize, } }; get(params).then(data => { this.setData({ allRechargeList: isRefresh ? data.content.obj : this.data.allRechargeList.concat(data.content.obj), isWXRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成 loading: false, hasMore: this.data.pageCurr < data.content.pageTotal }) this.updateDisplayText(); }).catch(err => { // 错误回调 this.setData({ isWXRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成 loading: false }) wx.showToast({ title: err.msg, icon: 'error', duration: 3000 }) }); }, //加载更多 loadMore() { if (this.data.hasMore && !this.data.loading) { this.setData({ loading: true, pageCurr: this.data.pageCurr + 1 }) this.getRechargList(); } }, //处理充值记录的数据 updateDisplayText() { const updatedList = this.data.allRechargeList.map(item => { let morny = item.rechargeAmount + "元" return { ...item, morny }; // 保留所有其他字段,并添加 displayText 字段 }); // 更新列表数据 this.setData({ allRechargeList: updatedList }); }, //获取金额 getMorneyList(){ const params = { url: 'wx/virtual_card/gerRechargeProfiles' }; get(params).then(data => { this.setData({ moneyList: data.content , }) }).catch(err => { // 错误回调 wx.showToast({ title: err.msg, icon: 'error', duration: 3000 }) }); } })