沙盘演示系统应用的微信小程序
zuoxiao
2024-10-25 917252ef3ea2b63c74d162cc67a6fbe103cb9b4d
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,29 @@
    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,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      vcId: options.vcId
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getRechargList()
  },
  /**
@@ -68,7 +66,10 @@
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageCurr: 1
    })
    this.getRechargList()
  },
  /**
@@ -90,10 +91,123 @@
    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: 1 //(单位是分)
    };
    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) {
          console.log('支付成功', res);
        },
        fail(err) {
          console.log('支付失败', err);
        }
      });
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'error',
        duration: 3000
      })
    });
  },
  //获取充值记录
  getRechargList() {
    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:this.data.allRechargeList.concat( data.content.obj),
        isWXRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
        loading: false,
        hasMore: !data.content.obj.pageTotal === this.data.pageCurr
      })
      this.updateDisplayText();
    }).catch(err => {
      // 错误回调
      this.setData({
        isWXRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
        loading: false
      })
      wx.showToast({
        title: err.msg,
        icon: 'error',
        duration: 3000
      })
    });
  },
  // 生成支付签名的函数
  generatePaySign(params) {
    const sortedKeys = Object.keys(params).sort();
    const stringToSign = sortedKeys.map(key => `${key}=${params[key]}`).join('&') + `&key=your-mch-key`;
    return md5(stringToSign).toUpperCase();
  },
  //加载更多
  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
    });
  }
})