// 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: [{ 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元" }] }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, handleMoneyWrapperTap: function (e) { const index = e.currentTarget.dataset.index; console.log(index) if (index !== undefined) { this.setData({ activeIndex: parseInt(index), isClickable: true }); } }, onPullDownRefresh() { console.log("onPullDownRefresh") }, //创建订单并调起支付 creatOrder() { if (!this.data.isClickable) { return; } const data = { sessionId: "2024061708384800006", vcId: "2024060610404500006", //虚拟卡ID rechargeAmount: 1 //(单位是分) }; console.log("postCloseValaue" + data); post({ url: "https://44978f7456.imdo.co/sell/payment/placeOrder", data: data }).then(response => { // 处理成功响应 console.log('请求成功:', response); const timeStamp = String(Math.floor(Date.now() / 1000)); const nonceStr = timeStamp; const prepayId = response.content.prepay_id; const signType = 'MD5'; // 生成支付签名 const paySign = this.generatePaySign({ appId: getApp().globalData.APPID, timeStamp: timeStamp, nonceStr: nonceStr, package: prepayId, signType: signType }); // 调起支付 wx.requestPayment({ timeStamp: timeStamp, nonceStr: nonceStr, package: prepayId, signType: signType, paySign: paySign, success(res) { console.log('支付成功', res); }, fail(err) { console.log('支付失败', err); } }); }).catch(error => { if (error.code === "1002") {} // 处理错误响应 console.error('请求失败:', error); }); }, getRechargList() { }, // 生成支付签名的函数 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(); } })