index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. page: 0,
  15. skip: 0,
  16. limit: 5,
  17. total: 0,
  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. tabsChange: function (e) {
  27. const that = this;
  28. that.setData({ 'tabs.active': e.detail.name });
  29. that.setData({ skip: 0 })
  30. that.setData({ page: 0 })
  31. that.setData({ list: [] })
  32. that.watchLogin()
  33. },
  34. // 详情
  35. toView: function (e) {
  36. const that = this;
  37. that.setData({ skip: 0 })
  38. that.setData({ page: 0 })
  39. that.setData({ list: [] })
  40. const { item } = e.currentTarget.dataset;
  41. wx.navigateTo({ url: `/pages/news/info?id=${item._id}` })
  42. },
  43. // 分页
  44. toPage: function () {
  45. const that = this;
  46. let list = that.data.list;
  47. let limit = that.data.limit;
  48. if (that.data.total > list.length) {
  49. wx.showLoading({ title: '加载中', mask: true })
  50. let page = that.data.page + 1;
  51. that.setData({ page: page })
  52. let skip = page * limit;
  53. that.setData({ skip: skip })
  54. that.watchLogin();
  55. wx.hideLoading()
  56. } else { wx.showToast({ title: '没有更多数据了', icon: 'none', duration: 2000 }) }
  57. },
  58. /**
  59. * 生命周期函数--监听页面加载
  60. */
  61. onLoad: function (options) { },
  62. // 监听用户是否登录
  63. watchLogin: async function () {
  64. const that = this;
  65. let type = that.data.tabs.active;
  66. // 监听用户是否登录,
  67. wx.getStorage({
  68. key: 'user',
  69. success: async res => {
  70. let info = { skip: that.data.skip, limit: that.data.limit, type: type, is_show: '0' };
  71. const arr = await app.$get(`/newCourt/api/news`, { ...info });
  72. if (arr.errcode == '0') {
  73. that.setData({ list: [...that.data.list, ...arr.data] });
  74. that.setData({ total: arr.total })
  75. }
  76. else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  77. },
  78. fail: async res => {
  79. wx.redirectTo({ url: '/pages/index/index' })
  80. }
  81. })
  82. },
  83. /**
  84. * 生命周期函数--监听页面初次渲染完成
  85. */
  86. onReady: function () {
  87. },
  88. /**
  89. * 生命周期函数--监听页面显示
  90. */
  91. onShow: function () {
  92. const that = this;
  93. // 监听用户是否登录
  94. that.watchLogin();
  95. },
  96. /**
  97. * 生命周期函数--监听页面隐藏
  98. */
  99. onHide: function () {
  100. },
  101. /**
  102. * 生命周期函数--监听页面卸载
  103. */
  104. onUnload: function () {
  105. },
  106. /**
  107. * 页面相关事件处理函数--监听用户下拉动作
  108. */
  109. onPullDownRefresh: function () {
  110. },
  111. /**
  112. * 页面上拉触底事件的处理函数
  113. */
  114. onReachBottom: function () {
  115. },
  116. /**
  117. * 用户点击右上角分享
  118. */
  119. onShareAppMessage: function () {
  120. }
  121. })