index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const app = getApp()
  2. import { news_type } from '../../utils/dict';
  3. Page({
  4. data: {
  5. frameStyle: { useTop: true, name: '资讯', leftArrow: false, useBar: true },
  6. tabs: {
  7. active: '0',
  8. list: [
  9. { title: '推荐', name: '0' },
  10. { title: '羽毛球', name: '1' },
  11. ],
  12. },
  13. list: [],
  14. total: 0,
  15. page: 0,
  16. skip: 0,
  17. limit: 5,
  18. typeList: news_type,
  19. },
  20. // 跳转菜单
  21. tabPath(e) {
  22. let { route } = e.detail.detail;
  23. if (route) wx.redirectTo({ url: `/${route}` })
  24. },
  25. //查询
  26. onSearch: function (e) {
  27. const that = this;
  28. that.setData({ skip: 0, page: 0, list: [] })
  29. that.setData({ 'searchInfo.title': e.detail.value });
  30. that.watchLogin()
  31. },
  32. // 选项卡
  33. tabsChange: function (e) {
  34. const that = this;
  35. that.setData({ 'tabs.active': e.detail.name });
  36. that.setData({ skip: 0, page: 0, list: [] })
  37. that.watchLogin()
  38. },
  39. // 详情
  40. toView: function (e) {
  41. const that = this;
  42. that.setData({ skip: 0, page: 0, list: [] })
  43. const { item } = e.currentTarget.dataset;
  44. wx.navigateTo({ url: `/pages/news/info?id=${item._id}` })
  45. },
  46. // 分页
  47. toPage: function () {
  48. const that = this;
  49. let list = that.data.list;
  50. let limit = that.data.limit;
  51. if (that.data.total > list.length) {
  52. wx.showLoading({ title: '加载中', mask: true })
  53. let page = that.data.page + 1;
  54. that.setData({ page: page })
  55. let skip = page * limit;
  56. that.setData({ skip: skip })
  57. that.watchLogin();
  58. wx.hideLoading()
  59. } else { wx.showToast({ title: '没有更多数据了', icon: 'none', duration: 2000 }) }
  60. },
  61. /**
  62. * 生命周期函数--监听页面加载
  63. */
  64. onLoad: function (options) { },
  65. // 监听用户是否登录
  66. watchLogin: async function () {
  67. const that = this;
  68. let type = that.data.tabs.active;
  69. let searchInfo = that.data.searchInfo;
  70. // 监听用户是否登录,
  71. wx.getStorage({
  72. key: 'user',
  73. success: async res => {
  74. let info = { skip: that.data.skip, limit: that.data.limit, type: type, is_show: '0' };
  75. if (searchInfo && searchInfo.title) info.title = searchInfo.title;
  76. const arr = await app.$get(`/newCourt/api/news`, { ...info });
  77. if (arr.errcode == '0') {
  78. that.setData({ list: [...that.data.list, ...arr.data] });
  79. that.setData({ total: arr.total })
  80. }
  81. else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  82. },
  83. fail: async res => {
  84. wx.redirectTo({ url: '/pages/index/index' })
  85. }
  86. })
  87. },
  88. /**
  89. * 生命周期函数--监听页面初次渲染完成
  90. */
  91. onReady: function () {
  92. },
  93. /**
  94. * 生命周期函数--监听页面显示
  95. */
  96. onShow: function () {
  97. const that = this;
  98. // 监听用户是否登录
  99. that.watchLogin();
  100. },
  101. /**
  102. * 生命周期函数--监听页面隐藏
  103. */
  104. onHide: function () {
  105. },
  106. /**
  107. * 生命周期函数--监听页面卸载
  108. */
  109. onUnload: function () {
  110. },
  111. /**
  112. * 页面相关事件处理函数--监听用户下拉动作
  113. */
  114. onPullDownRefresh: function () {
  115. },
  116. /**
  117. * 页面上拉触底事件的处理函数
  118. */
  119. onReachBottom: function () {
  120. },
  121. /**
  122. * 用户点击右上角分享
  123. */
  124. onShareAppMessage: function () {
  125. }
  126. })