const app = getApp() import WxValidate from '../../utils/wxValidate'; Page({ /** * 页面的初始数据 */ data: { frameStyle: { useTop: true, name: '赛事信息', leftArrow: true, useBar: false }, id: '', info: {}, form: {}, projectList: [], dialog: { title: '支付报名费', show: false, type: '1' }, enrollInfo: {} }, initValidate() { const rules = { grouping_id: { required: true }, project_id: { required: true } } // 验证字段的提示信息,若不传则调用默认的信息 const messages = { grouping_id: { required: '请选择赛事分组' }, project_id: { required: '请选择组内项目' } }; this.WxValidate = new WxValidate(rules, messages) }, // 跳转菜单 back(e) { wx.navigateBack({ delta: 1 }) }, // 赛事分组 groupingChange: async function (e) { const that = this; let data = that.data.info.grouping[e.detail.value]; that.setData({ 'form.grouping_id': data._id }); that.setData({ 'form.grouping_name': data.name }); let project = []; for (const val of data.project) { const arr = await app.$get(`/newCourt/api/matchProject/${val}`); if (arr.errcode == '0') { project.push(arr.data) } } that.setData({ projectList: project }); }, // 选择组内项目 projectChange: function (e) { const that = this; let data = that.data.projectList[e.detail.value]; that.setData({ 'form.project_id': data._id }); that.setData({ 'form.project_name': data.name }); }, onSubmit: async function (e) { const that = this; const params = e.detail.value; if (!this.WxValidate.checkForm(params)) { const error = this.WxValidate.errorList[0]; wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) return false } else { const arr = await app.$post(`/newCourt/api/enroll`, params); if (arr.errcode == '0') { wx.showToast({ title: `报名成功`, icon: 'success', duration: 2000 }); that.setData({ enrollInfo: arr.data }) that.setData({ dialog: { title: '支付报名费', show: true, type: '1' } }) wx.showShareMenu({ menus: ['shareTimeline', 'shareAppMessage'], }) } else { wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 }); } } }, async shareFinish() { const enroll = this.data.enrollInfo; if (!enroll) { wx.showToast({ title: '未找到报名信息', icon: 'error' }) return; } const { _id } = enroll; const res = await app.$post(`/newCourt/api/enroll/${_id}`, { has_turn: '1' }) if (app.$checkRes(res)) { console.log('已转发') console.log(res) } }, toForward: function () { }, // 支付 toPay: async function () { const that = this; const enroll = that.data.enrollInfo; wx.getStorage({ key: 'user', success: async (res) => { let obj = { openid: res.data.openid, money: 0.1, enroll_id: enroll._id, type: '报名' } const arr = await app.$post(`/newCourt/api/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) { wx.showToast({ title: `支付成功`, icon: 'success', duration: 2000 }); const aee = await app.$post(`/newCourt/api/payOrder/${arr.data.data._id}`, { status: '1' }); if (aee.errcode == '0') { that.watchLogin(); that.toclose(); that.back() } }, "fail": function (res) { wx.showToast({ title: `支付未成功`, icon: 'error', duration: 2000 }) that.watchLogin() }, }) } }, fail: async (res) => { wx.redirectTo({ url: '/pages/index/index' }); }, }); }, // 关闭弹框 toClose: function () { console.log('in') const that = this; that.setData({ dialog: { title: '支付报名费', show: false, type: '1' } }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { const that = this; that.setData({ id: options.id || '62e23cd3bb22e03d0b49d73b' }); //验证规则函数 that.initValidate(); // 监听用户是否登录 that.watchLogin(); // 先禁用分享 wx.hideShareMenu() }, watchLogin: function () { const that = this; wx.getStorage({ key: 'user', success: async (res) => { if (that.data.id) { const arr = await app.$get(`/newCourt/api/match/${that.data.id}`); if (arr.errcode == '0') { that.setData({ info: arr.data }) let data = { openid: res.data.openid, match_id: arr.data._id, match_name: arr.data.name } that.setData({ form: data }) } else { wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 }); } } }, fail: async (res) => { wx.redirectTo({ url: '/pages/index/index' }); }, }); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, onShareTimeline() { this.shareFinish(); return { title: '羽毛球线上比赛', } } })