const app = getApp() const moment = require("../../utils/moment.min") const util = require('../../utils/util.js') Page({ data: { frameStyle: { useTop: true, name: '课程详细信息', leftArrow: true, useBar: false }, iconUrl: app.globalData.icon, id: '', tabs: { active: 'a', menu: [ { title: '课程信息', active: 'a' }, { title: '教练信息', active: 'b' }, { title: '学员信息', active: 'c' }, ], }, // 课程信息 form: {}, // 教练信息 coachList: [], // 学员信息 studentList: [], total: 0, skip: 0, limit: 10, page: 0, // 字典表 //状态 statusList: [], }, // 跳转菜单 back(e) { wx.navigateBack({ delta: 1 }) }, tabsChange: function (e) { const that = this; let { active } = e.detail; that.setData({ 'tabs.active': active }); }, // 报名 async toSign(e) { const that = this; const { item } = e.currentTarget.dataset; let id = that.data.id; wx.getStorage({ key: 'user', success: async res => { let user = res.data; if (user && user.type == '3') { let obj = { openid: user.openid, money: item.money, school_id: item.school_id, student_id: user.info._id, lesson_id: id }; // 创建报名 await that.createLesson(obj); } else { wx.showToast({ title: '账号角色不对,不可报名', icon: 'none' }) } }, fail: res => { wx.redirectTo({ url: '/pages/index/index', }) } }) }, // 创建报名信息 async createLesson(e) { const that = this; let res = await app.$post(`/lessonStudent`, e); if (res.errcode == '0') { // wxSign有值:余额不够,需手动支付 // 无值:余额够,无需支付 if (res?.data?.wxSign) { // 调取支付窗口 let wxSign = res.data.wxSign; wx.requestPayment({ timeStamp: wxSign.timestamp, nonceStr: wxSign.nonceStr, package: `prepay_id=${wxSign.prepay_id}`, signType: wxSign.signType, paySign: wxSign.paySign, async success(payRes) { wx.showToast({ title: '报名成功', icon: 'none' }) that.watchLogin(); }, async fail(payErr) { wx.showToast({ title: '支付不成功', icon: 'none' }) } }) } else { wx.showToast({ title: '报名成功', icon: 'none' }); that.watchLogin() } } else { wx.showToast({ title: res.errmsg, icon: 'none' }) } }, /** * 生命周期函数--监听页面加载 */ onLoad: async function (options) { const that = this; await that.setData({ id: options.id || '63563bbd8018a39220eca774' }) // 查询其他信息 await that.searchOther(); // 监听用户是否登录 await that.watchLogin(); }, // 监听用户是否登录 watchLogin: async function () { const that = this; if (that.data.id) { await that.searchInfo(); // 查询教练 await that.searchCoach(); // 查询学员信息 await that.searchStudent() } }, async searchInfo() { const that = this; const res = await app.$get(`/lesson/${that.data.id}`); if (res.errcode == '0') { // 课程状态 res.data.zhStatus = that.searchStatus(res.data.status); // 课程信息 that.setData({ form: res.data }) } else { wx.showToast({ title: res.errmsg, icon: 'none' }) } }, searchStatus(e) { const that = this; let data = that.data.statusList.find(i => i.value == e); if (data) return data.label }, async searchCoach() { const that = this; let id = that.data.id; let res = await app.$get(`/lessonCoach`, { lesson_id: id }); if (res.errcode == '0') { for (const val of res.data) { if (val.coach_id_phone) val.coach_id_phone = util.hidePhone(val.coach_id_phone); } that.setData({ coachList: res.data }) } else { wx.showToast({ title: res.errmsg, icon: 'none' }) } }, async searchStudent() { const that = this; let id = that.data.id; let info = { skip: that.data.skip, limit: that.data.limit, lesson_id: id } let res = await app.$get(`/lessonStudent`, { ...info }); if (res.errcode == '0') { let list = [...that.data.studentList, ...res.data]; for (const val of list) { if (val.student_id_phone) val.student_id_phone = util.hidePhone(val.student_id_phone); } that.setData({ studentList: list }) that.setData({ total: res.total }) } else { wx.showToast({ title: res.errmsg, icon: 'none' }) } }, // 清空列表 clearPage() { const that = this; that.setData({ list: [], skip: 0, limit: 10, page: 0 }) }, // 分页 toPage: function () { const that = this; let list = that.data.studentList; let limit = that.data.limit; if (that.data.total > list.length) { wx.showLoading({ title: '加载中', mask: true }) let page = that.data.page + 1; that.setData({ page: page }) let skip = page * limit; that.setData({ skip: skip }) that.searchStudent(); wx.hideLoading() } else { wx.showToast({ title: '没有更多数据了', icon: 'none', duration: 2000 }) } }, 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 () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })