const app = getApp() const moment = require("../../../utils/moment.min") Page({ /** * 页面的初始数据 */ data: { frameStyle: { useTop: true, name: '课程列表', leftArrow: true, useBar: false }, user: {}, list: [], total: 0, page: 0, skip: 0, limit: 5, // 课程类型 typeList: [], // 状态 statusList: [] }, // 返回 back: function () { wx.navigateBack({ delta: 1 }) }, //添加信息,信息维护 toCommon: function (e) { const that = this; const { item, route } = e.currentTarget.dataset; that.setData({ skip: 0, page: 0, list: [] }); wx.navigateTo({ url: `/pages/${route}?id=${item && item._id ? item._id : ''}` }) }, // 签到 toSign: async function (e) { const that = this; const user = that.data.user; const { item } = e.currentTarget.dataset; let arr; arr = await app.$get(`/lessonCoach`, { school_id: item.school_id, lesson_id: item._id, coach_id: user.info._id }); if (arr.errcode == '0' && arr.total > 0) { arr = await app.$post(`/lessonCoach/${arr.data[0]._id}`, { is_sign: '1' }); if (arr.errcode == '0') { wx.showToast({ title: `签到成功`, icon: 'error', duration: 2000 }) that.setData({ skip: 0, page: 0, list: [] }); that.watchLogin(); } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) } } }, // 分页 toPage: function () { const that = this; let list = that.data.list; 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.watchLogin(); wx.hideLoading() } else { wx.showToast({ title: '没有更多数据了', icon: 'none', duration: 2000 }) } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: async function () { const that = this; // 查询其他信息 await that.searchOther(); // 监听用户是否登录 await that.watchLogin(); }, // 查询其他信息 searchOther: async function () { const that = this; let arr; // 课程类型 arr = await app.$get(`/dict`, { code: 'lesson_type' }); if (arr.errcode == '0' && arr.total > 0) that.setData({ typeList: arr.data[0].list }); // 审核状态 arr = await app.$get(`/dict`, { code: 'lesson_status' }) if (arr.errcode == '0' && arr.total > 0) that.setData({ statusList: arr.data[0].list }); }, // 监听用户是否登录 watchLogin: async function () { const that = this; let typeList = that.data.typeList; let statusList = that.data.statusList; wx.getStorage({ key: 'user', success: async res => { that.setData({ user: res.data }) let info = { skip: that.data.skip, limit: that.data.limit }; let arr = await app.$get(`/lesson`, { ...info }); if (arr.errcode == '0') { let list = [...that.data.list, ...arr.data]; for (const val of list) { // 课程类型 let type = typeList.find(i => i.value == val.type); if (type) val.zhType = type.label; // 课程状态 let status = statusList.find(i => i.value == val.status); if (status) val.zhStatus = status.label; } that.setData({ list: list }) that.setData({ total: arr.total }) } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) } }, fail: async res => { wx.redirectTo({ url: '/pages/index/index' }) } }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })