const app = getApp() import WxValidate from '../../../utils/wxValidate'; const moment = require("../../../utils/moment.min") Page({ data: { frameStyle: { useTop: true, name: '充值', leftArrow: true, useBar: false }, id: '', form: {}, user: {}, }, initValidate() { const rules = { paymoney: { required: true } } // 验证字段的提示信息,若不传则调用默认的信息 const messages = { paymoney: { required: '请输入金额' } }; this.WxValidate = new WxValidate(rules, messages) }, // 返回 back: function () { wx.navigateBack({ delta: 1 }) }, //提交 onSubmit: async function (e) { const that = this; const user = that.data.user; const form = that.data.form; const params = e.detail.value; var money = params.paymoney; if (form.money) money = Number(form.money) + Number(params.paymoney); if (!this.WxValidate.checkForm(params)) { const error = this.WxValidate.errorList[0]; wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) return false } else { wx.showModal({ title: '提示', content: '您确定是否充值?', async success(res) { if (res.confirm) { wx.getStorage({ key: 'user', success: async (res) => { let obj = { openid: res.data.openid, money: params.paymoney, school_id: params.school_id, payer_role: 'Student', pay_for: '充值', payer_id: user.info.id, time: moment().format('YYYY-MM-DD HH:mm:ss') } const arr = await app.$post(`/payOrder`, obj) if (arr.errcode == '0') { wx.requestPayment({ "timeStamp": arr.data.wxSign.timestamp, "nonceStr": arr.data.wxSign.nonceStr, "package": `prepay_id=${arr.data.wxSign.prepay_id}`, "signType": arr.data.wxSign.signType, "paySign": arr.data.wxSign.paySign, "success": async function (res) { const rss = await app.$post(`/rss/${that.data.id}`, { money: money }); if (rss.errcode == '0') { wx.showToast({ title: `充值成功`, icon: 'success', duration: 2000 }); that.back(); } }, "fail": async function (res) { wx.showToast({ title: `充值未成功`, icon: 'error', duration: 2000 }) that.watchLogin() }, }) } }, fail: async (res) => { wx.redirectTo({ url: '/pages/index/index' }); }, }); } else if (res.cancel) { } } }); } }, /** * 生命周期函数--监听页面加载 */ onLoad: async function (options) { const that = this; that.setData({ id: options.id || null }) //验证规则函数 that.initValidate(); // 监听用户是否登录 await that.watchLogin(); }, // 监听用户是否登录 watchLogin: async function () { const that = this; wx.getStorage({ key: 'user', success: async res => { that.setData({ user: res.data }); let arr; if (that.data.id) { arr = await app.$get(`/rss/${that.data.id}`); if (arr.errcode == '0') { that.setData({ form: arr.data }); } }; }, fail: async res => { wx.redirectTo({ url: '/pages/index/index' }) } }) }, // 查询其他信息 searchOther: async function () { const that = this; let arr; arr = await app.$get(`/dict`, { code: 'lesson_status' }); if (arr.errcode == '0' && arr.total > 0) that.setData({ statusList: arr.data[0].list }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })