沙盘演示系统应用的微信小程序
zuoxiao
2024-11-04 232b383c629ac75dbf1bb780a63239b6f57d5071
pages/rechargeMoney/rechargMoney.js
@@ -1,4 +1,10 @@
// pages/rechargeMoney/rechargMoney.js
//充值界面
const {
  get,
  post
} = require('../../api/request.js');
const md5 = require('js-md5');
Page({
  /**
@@ -10,37 +16,31 @@
    userCode: "15584236",
    balance: "1025元",
    activeIndex: -1,
    allRechargeList: [{
      time: "2024-05-12:20:20",
      type: "远程",
      morny: "500元"
    }, {
      time: "2024-05-12:20:20",
      type: "远程",
      morny: "500元"
    }, {
      time: "2024-05-12:20:20",
      type: "远程",
      morny: "500元"
    }, {
      time: "2024-05-12:20:20",
      type: "远程",
      morny: "500元"
    }]
    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();
  },
  /**
@@ -68,7 +68,10 @@
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageCurr: 1,
    })
    this.getRechargList(true)
  },
  /**
@@ -90,10 +93,143 @@
    if (index !== undefined) {
      this.setData({
        activeIndex: parseInt(index),
        isClickable: true
      });
    }
  },
  onPullDownRefresh() {
    console.log("onPullDownRefresh")
  //创建订单并调起支付
  creatOrder() {
    if (!this.data.isClickable) {
      return;
    }
    const data = {
      sessionId: getApp().globalData.sessionId,
      vcId: this.data.vcId, //虚拟卡ID
      rechargeAmount: this.data.moneyList[this.data.activeIndex]//(单位是分)
    };
    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 / 100 + "元"
      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 ,
      })
      this.updateDisplayText();
    }).catch(err => {
      // 错误回调
      wx.showToast({
        title: err.msg,
        icon: 'error',
        duration: 3000
      })
    });
  }
})