list.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const app = getApp()
  2. const moment = require("../../../utils/moment.min")
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. frameStyle: { useTop: true, name: '课程列表', leftArrow: true, useBar: false },
  9. user: {},
  10. list: [],
  11. total: 0,
  12. page: 0,
  13. skip: 0,
  14. limit: 5,
  15. // 课程类型
  16. typeList: [],
  17. // 状态
  18. statusList: []
  19. },
  20. // 返回
  21. back: function () {
  22. wx.navigateBack({ delta: 1 })
  23. },
  24. //添加信息,信息维护
  25. toCommon: function (e) {
  26. const that = this;
  27. const { item, route } = e.currentTarget.dataset;
  28. that.setData({ skip: 0, page: 0, list: [] });
  29. wx.navigateTo({ url: `/pages/${route}?id=${item && item._id ? item._id : ''}` })
  30. },
  31. // 签到
  32. toSign: async function (e) {
  33. const that = this;
  34. const user = that.data.user;
  35. const { item } = e.currentTarget.dataset;
  36. let arr;
  37. arr = await app.$get(`/lessonCoach`, { school_id: item.school_id, lesson_id: item._id, coach_id: user.info._id });
  38. if (arr.errcode == '0' && arr.total > 0) {
  39. arr = await app.$post(`/lessonCoach/${arr.data[0]._id}`, { is_sign: '1' });
  40. if (arr.errcode == '0') {
  41. wx.showToast({ title: `签到成功`, icon: 'error', duration: 2000 })
  42. that.setData({ skip: 0, page: 0, list: [] });
  43. that.watchLogin();
  44. } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  45. }
  46. },
  47. // 分页
  48. toPage: function () {
  49. const that = this;
  50. let list = that.data.list;
  51. let limit = that.data.limit;
  52. if (that.data.total > list.length) {
  53. wx.showLoading({ title: '加载中', mask: true })
  54. let page = that.data.page + 1;
  55. that.setData({ page: page })
  56. let skip = page * limit;
  57. that.setData({ skip: skip })
  58. that.watchLogin();
  59. wx.hideLoading()
  60. } else { wx.showToast({ title: '没有更多数据了', icon: 'none', duration: 2000 }) }
  61. },
  62. /**
  63. * 生命周期函数--监听页面加载
  64. */
  65. onLoad: function (options) {
  66. },
  67. /**
  68. * 生命周期函数--监听页面初次渲染完成
  69. */
  70. onReady: function () {
  71. },
  72. /**
  73. * 生命周期函数--监听页面显示
  74. */
  75. onShow: async function () {
  76. const that = this;
  77. // 查询其他信息
  78. await that.searchOther();
  79. // 监听用户是否登录
  80. await that.watchLogin();
  81. },
  82. // 查询其他信息
  83. searchOther: async function () {
  84. const that = this;
  85. let arr;
  86. // 课程类型
  87. arr = await app.$get(`/dict`, { code: 'lesson_type' });
  88. if (arr.errcode == '0' && arr.total > 0) that.setData({ typeList: arr.data[0].list });
  89. // 审核状态
  90. arr = await app.$get(`/dict`, { code: 'lesson_status' })
  91. if (arr.errcode == '0' && arr.total > 0) that.setData({ statusList: arr.data[0].list });
  92. },
  93. // 监听用户是否登录
  94. watchLogin: async function () {
  95. const that = this;
  96. let typeList = that.data.typeList;
  97. let statusList = that.data.statusList;
  98. wx.getStorage({
  99. key: 'user',
  100. success: async res => {
  101. that.setData({ user: res.data })
  102. let info = { skip: that.data.skip, limit: that.data.limit };
  103. let arr = await app.$get(`/lesson`, { ...info });
  104. if (arr.errcode == '0') {
  105. let list = [...that.data.list, ...arr.data];
  106. for (const val of list) {
  107. // 课程类型
  108. let type = typeList.find(i => i.value == val.type);
  109. if (type) val.zhType = type.label;
  110. // 课程状态
  111. let status = statusList.find(i => i.value == val.status);
  112. if (status) val.zhStatus = status.label;
  113. }
  114. that.setData({ list: list })
  115. that.setData({ total: arr.total })
  116. } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  117. },
  118. fail: async res => {
  119. wx.redirectTo({ url: '/pages/index/index' })
  120. }
  121. })
  122. },
  123. /**
  124. * 生命周期函数--监听页面隐藏
  125. */
  126. onHide: function () {
  127. },
  128. /**
  129. * 生命周期函数--监听页面卸载
  130. */
  131. onUnload: function () {
  132. },
  133. /**
  134. * 页面相关事件处理函数--监听用户下拉动作
  135. */
  136. onPullDownRefresh: function () {
  137. },
  138. /**
  139. * 页面上拉触底事件的处理函数
  140. */
  141. onReachBottom: function () {
  142. },
  143. /**
  144. * 用户点击右上角分享
  145. */
  146. onShareAppMessage: function () {
  147. }
  148. })