tagDetail.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // pages/tagDetail/tagDetail.js
  2. const net = require("../../utils/net.js");
  3. const utils = require("../../utils/util.js");
  4. const {
  5. api_material_topic_info,
  6. api_news_list,
  7. api_topic_type_list,
  8. } = require("../../utils/api.js");
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. categoryList: [],
  15. tagId: null,
  16. selectTab: 0,
  17. topicInfo: {},
  18. scrollH: 500,
  19. listData: [],
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad: function (options) {
  25. if (options && options.tagId) {
  26. this._tagId = options.tagId;
  27. this.loadTopicInfo();
  28. this.loadCategoryListData();
  29. }
  30. },
  31. tapNavBarAction() {
  32. const { topicInfo } = this.data;
  33. const { isAuthor, isAttention } = topicInfo;
  34. if (!isAuthor && isAttention == 0) {
  35. let pages = getCurrentPages(); /*在内存中的所有页面栈对象*/
  36. let reloadPage = pages[pages.length - 2]; /*我的页面对象*/
  37. reloadPage && reloadPage.cancelAttention && reloadPage.cancelAttention();
  38. }
  39. },
  40. loadTopicInfo() {
  41. let params = {};
  42. params.url = api_material_topic_info;
  43. params.data = {
  44. id: this._tagId,
  45. };
  46. net.req(params, true).then(
  47. (data) => {
  48. this.setData({
  49. topicInfo: data,
  50. });
  51. },
  52. (e) => {
  53. utils.showToast({ title: e.msg || "" });
  54. }
  55. );
  56. },
  57. loadCategoryListData() {
  58. let params = {};
  59. params.url = api_topic_type_list;
  60. params.data = { tagId: this._tagId, type: 1 };
  61. net.req(params, true).then(
  62. (data) => {
  63. this.setData(
  64. {
  65. categoryList: data,
  66. },
  67. () => {
  68. this._selectTab = 0;
  69. this.loadNewsData({ isRefresh: true });
  70. this.getScrollH();
  71. }
  72. );
  73. },
  74. (e) => {
  75. utils.showToast({ title: e.msg || "" });
  76. }
  77. );
  78. },
  79. loadNewsData(obj) {
  80. const { limit, listData, categoryList } = this.data;
  81. let params = {};
  82. params.url = api_news_list;
  83. params.data = {
  84. tagId: this._tagId,
  85. limit: limit,
  86. type: categoryList[this._selectTab].type,
  87. };
  88. const len = listData.length;
  89. if (!obj.isRefresh && len) {
  90. params.data.lastId = listData[len - 1].id;
  91. }
  92. net.req(params, true).then(
  93. (data) => {
  94. if (obj.isRefresh) {
  95. this.setData({
  96. listData: data.list,
  97. isEnd: data.list.length < limit,
  98. });
  99. this._refresh = false;
  100. this._isload = false;
  101. } else {
  102. if (data.list.length) {
  103. this.setData({
  104. listData: listData.concat(data.list),
  105. isEnd: data.list.length < limit,
  106. });
  107. }
  108. this._isload = data.list.length < limit;
  109. }
  110. },
  111. (e) => {
  112. obj.isRefresh ? (this._refresh = false) : (this._isload = false);
  113. }
  114. );
  115. },
  116. tapAttentionAction(e) {
  117. const { attentionNum } = e.detail;
  118. this.setData({
  119. ["topicInfo.attentionNum"]: attentionNum,
  120. });
  121. },
  122. getScrollH() {
  123. const _this = this;
  124. wx.createSelectorQuery()
  125. .select("#fixedTop")
  126. .boundingClientRect(function (res) {
  127. const sys = wx.getSystemInfoSync();
  128. _this.setData({
  129. scrollH: sys.windowHeight - res.height,
  130. });
  131. })
  132. .exec();
  133. },
  134. handleTabAction(e) {
  135. const { index, item } = e.detail;
  136. const { categoryList } = this.data;
  137. const newCategoryId = item.type;
  138. const oldCategoryId = categoryList[this._selectTab].type;
  139. if (oldCategoryId != newCategoryId) {
  140. this.setData({
  141. scrollTop: 0,
  142. });
  143. this._selectTab = index;
  144. this.loadNewsData({ isRefresh: true });
  145. }
  146. },
  147. scrollToLower() {
  148. if (this._isload) return;
  149. this._isload = true;
  150. this.loadNewsData({ isRefresh: false });
  151. },
  152. refreshCallback() {
  153. this.loadNewsData({ isRefresh: true });
  154. },
  155. });