沙盘演示系统应用的微信小程序
zuoxiao
2024-10-25 917252ef3ea2b63c74d162cc67a6fbe103cb9b4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// 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,
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      vcId: options.vcId
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getRechargList()
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
 
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
 
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
 
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageCurr: 1
    })
    this.getRechargList()
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  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: 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
    });
  }
})