index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { getArticleCategoryList, getArticleList, getArticleHotList, getArticleBannerList} from '../../api/api.js';
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. parameter: {
  9. 'navbar': '1',
  10. 'return': '1',
  11. 'title': '资讯',
  12. 'color': false
  13. },
  14. imgUrls: [],
  15. articleList:[],
  16. indicatorDots: false,
  17. circular: true,
  18. autoplay: true,
  19. interval: 3000,
  20. duration: 500,
  21. navList:[],
  22. active: 0,
  23. page:1,
  24. limit:8,
  25. status:false,
  26. scrollLeft: 0
  27. },
  28. /**
  29. * 生命周期函数--监听页面加载
  30. */
  31. onLoad: function (options) {
  32. },
  33. getArticleHot: function () {
  34. var that = this;
  35. getArticleHotList().then(res=>{
  36. that.setData({ articleList: res.data });
  37. });
  38. },
  39. getArticleBanner: function () {
  40. var that = this;
  41. getArticleBannerList().then(res=>{
  42. that.setData({ imgUrls: res.data });
  43. });
  44. },
  45. getCidArticle: function () {
  46. var that = this;
  47. if (that.data.active == 0) return ;
  48. var limit = that.data.limit;
  49. var page = that.data.page;
  50. var articleList = that.data.articleList;
  51. if (that.data.status) return ;
  52. getArticleList(that.data.active, { page: page, limit: limit}).then(res=>{
  53. var articleListNew = [];
  54. var len = res.data.length;
  55. articleListNew = articleList.concat(res.data);
  56. that.data.page++
  57. that.setData({
  58. articleList: articleListNew,
  59. status: limit > len,
  60. page: that.data.page,
  61. });
  62. });
  63. },
  64. getArticleCate:function(){
  65. var that = this;
  66. getArticleCategoryList().then(res=>{
  67. that.setData({ navList: res.data });
  68. });
  69. },
  70. tabSelect(e) {
  71. this.setData({
  72. active: e.currentTarget.dataset.id,
  73. scrollLeft: (e.currentTarget.dataset.id - 1) * 50
  74. })
  75. if (this.data.active == 0) this.getArticleHot();
  76. else{
  77. this.setData({ articleList: [], page: 1, status: false});
  78. this.getCidArticle();
  79. }
  80. },
  81. /**
  82. * 生命周期函数--监听页面初次渲染完成
  83. */
  84. onReady: function () {
  85. },
  86. /**
  87. * 生命周期函数--监听页面显示
  88. */
  89. onShow: function () {
  90. this.getArticleHot();
  91. this.getArticleBanner();
  92. this.getArticleCate();
  93. this.setData({ status: false, page: 1, articleList:[]});
  94. this.getCidArticle();
  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. this.getCidArticle();
  116. },
  117. /**
  118. * 用户点击右上角分享
  119. */
  120. onShareAppMessage: function () {
  121. }
  122. })