index.js 3.4 KB

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