index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const App = getApp();
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. // 分类列表
  8. categoryList: [],
  9. // 文章列表
  10. articleList: [],
  11. // 当前的分类id (0则代表首页)
  12. category_id: 0,
  13. scrollHeight: null,
  14. no_more: false, // 没有更多数据
  15. isLoading: true, // 是否正在加载中
  16. page: 1, // 当前页码
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: function (options) {
  22. let _this = this;
  23. // 设置默认的分类
  24. if (options.category_id) {
  25. _this.setData({
  26. category_id: options.category_id
  27. })
  28. }
  29. // 设置文章列表高度
  30. _this.setListHeight();
  31. // Api:获取文章首页
  32. _this.getIndexData();
  33. },
  34. /**
  35. * Api:获取文章列表
  36. */
  37. getIndexData() {
  38. let _this = this;
  39. // 获取文章首页
  40. App._get('article/index', {}, function (result) {
  41. _this.setData({
  42. categoryList: result.data.categoryList
  43. });
  44. });
  45. // Api:获取文章列表
  46. _this.getArticleList();
  47. },
  48. /**
  49. * Api:切换导航栏
  50. */
  51. onSwitchTab: function (e) {
  52. let _this = this;
  53. // 第一步:切换当前的分类id
  54. _this.setData({
  55. category_id: e.currentTarget.dataset.id,
  56. articleList: {},
  57. page: 1,
  58. no_more: false,
  59. isLoading: true,
  60. });
  61. // 第二步:更新当前的文章列表
  62. _this.getArticleList();
  63. },
  64. /**
  65. * Api:获取文章列表
  66. */
  67. getArticleList(isPage, page) {
  68. let _this = this;
  69. App._get('article/lists', {
  70. page: page || 1,
  71. category_id: _this.data.category_id
  72. }, function (result) {
  73. let resList = result.data.list,
  74. dataList = _this.data.articleList;
  75. if (isPage == true) {
  76. _this.setData({
  77. 'articleList.data': dataList.data.concat(resList.data),
  78. isLoading: false,
  79. });
  80. } else {
  81. _this.setData({
  82. articleList: resList,
  83. isLoading: false,
  84. });
  85. }
  86. });
  87. },
  88. /**
  89. * 跳转文章详情页
  90. */
  91. onTargetDetail(e) {
  92. wx.navigateTo({
  93. url: './detail/index?article_id=' + e.currentTarget.dataset.id
  94. });
  95. },
  96. /**
  97. * 下拉到底加载数据
  98. */
  99. bindDownLoad() {
  100. // 已经是最后一页
  101. if (this.data.page >= this.data.articleList.last_page) {
  102. this.setData({
  103. no_more: true
  104. });
  105. return false;
  106. }
  107. // 加载下一页列表
  108. this.getArticleList(true, ++this.data.page);
  109. },
  110. /**
  111. * 设置文章列表高度
  112. */
  113. setListHeight() {
  114. let systemInfo = wx.getSystemInfoSync(),
  115. rpx = systemInfo.windowWidth / 750, // 计算rpx
  116. tapHeight = Math.floor(rpx * 98), // tap高度
  117. scrollHeight = systemInfo.windowHeight - tapHeight; // swiper高度
  118. this.setData({
  119. scrollHeight
  120. });
  121. },
  122. /**
  123. * 分享当前页面
  124. */
  125. onShareAppMessage() {
  126. return {
  127. title: '文章首页',
  128. path: "/pages/article/index?" + App.getShareUrlParams()
  129. };
  130. },
  131. /**
  132. * 分享到朋友圈
  133. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  134. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  135. */
  136. onShareTimeline() {
  137. return {
  138. title: '文章首页',
  139. path: "/pages/article/index?" + App.getShareUrlParams()
  140. };
  141. },
  142. })