personal.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // pages/personal/personal.js
  2. const net = require("../../utils/net.js");
  3. const utils = require("../../utils/util.js");
  4. const {
  5. api_news_list,
  6. api_topic_type_list,
  7. api_user_info,
  8. } = require("../../utils/api.js");
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. userId: null,
  15. categoryList: [],
  16. listData: [],
  17. limit: 20,
  18. scrollH: 400,
  19. selectTab: 0,
  20. userData: {},
  21. isEnd: false,
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. if (options && options.userId) {
  28. this._userId = options.userId;
  29. this.loadCategoryListData();
  30. this.getUserInfoData();
  31. this.getScrollH();
  32. } else {
  33. utils.showToast({ title: "用户信息错误,请您稍后再试!" });
  34. }
  35. },
  36. tapNavBarAction() {
  37. const { userData } = this.data;
  38. const { isAuthor, isAttention } = userData.userInfo;
  39. if (!isAuthor && isAttention == 0) {
  40. let pages = getCurrentPages(); /*在内存中的所有页面栈对象*/
  41. let reloadPage = pages[pages.length - 2]; /*我的页面对象*/
  42. reloadPage && reloadPage.cancelAttention && reloadPage.cancelAttention();
  43. }
  44. },
  45. getUserInfoData() {
  46. let params = {};
  47. params.url = api_user_info;
  48. params.data = {
  49. userId: this._userId,
  50. };
  51. net.req(params).then((data) => {
  52. this.setData({
  53. userData: data,
  54. });
  55. });
  56. },
  57. getScrollH() {
  58. const _this = this;
  59. wx.createSelectorQuery()
  60. .select("#fixedTop")
  61. .boundingClientRect(function (res) {
  62. const sys = wx.getSystemInfoSync();
  63. _this.setData({
  64. scrollH: sys.screenHeight - res.height - 30,
  65. });
  66. })
  67. .exec();
  68. },
  69. loadCategoryListData() {
  70. let params = {};
  71. params.url = api_topic_type_list;
  72. params.data = { type: 2 };
  73. net.req(params, true).then(
  74. (data) => {
  75. this.setData(
  76. {
  77. categoryList: data,
  78. },
  79. () => {
  80. this.loadListData({ isRefresh: true });
  81. }
  82. );
  83. },
  84. (e) => {
  85. utils.showToast({ title: e.msg || "" });
  86. }
  87. );
  88. },
  89. loadListData(obj) {
  90. const { limit, listData, categoryList, selectTab } = this.data;
  91. let params = {};
  92. params.url = api_news_list;
  93. params.data = {
  94. limit: limit,
  95. userId: this._userId,
  96. type: categoryList[selectTab].type,
  97. };
  98. const list = listData[selectTab] || [];
  99. const len = list.length;
  100. if (!obj.isRefresh && len) {
  101. params.data.lastId = list[len - 1].id;
  102. }
  103. net.req(params, true).then(
  104. (data) => {
  105. if (obj.isRefresh) {
  106. this.setData({
  107. [`listData[${selectTab}]`]: data.list,
  108. isEnd: data.list.length < limit,
  109. });
  110. this._refresh = false;
  111. this._isload = false;
  112. } else {
  113. if (data.list.length) {
  114. this.setData({
  115. [`listData[${selectTab}]`]: listData[selectTab].concat(data.list),
  116. isEnd: data.list.length < limit,
  117. });
  118. }
  119. this._isload = data.list.length < limit;
  120. }
  121. },
  122. (e) => {
  123. obj.isRefresh ? (this._refresh = false) : (this._isload = false);
  124. }
  125. );
  126. },
  127. handleTabAction(e) {
  128. const { index, item } = e.detail;
  129. const { categoryList, selectTab, listData } = this.data;
  130. const newType = item.type;
  131. const oldType = categoryList[selectTab].type;
  132. if (newType != oldType) {
  133. this.setData({
  134. selectTab: index,
  135. });
  136. const list = listData[index];
  137. if (!list || !list.length) {
  138. this.loadListData({ isRefresh: true });
  139. }
  140. }
  141. },
  142. tapAttentionListAction() {
  143. const { userData } = this.data;
  144. wx.navigateTo({
  145. url: `/pages/myAttention/myAttention?userId=${userData.userInfo.userId}`,
  146. });
  147. },
  148. tapFansAction() {
  149. const { userData } = this.data;
  150. wx.navigateTo({
  151. url: `/pages/myfans/myfans?userId=${userData.userInfo.userId}`,
  152. });
  153. },
  154. /**
  155. * scroll-view上拉触底事件的处理函数
  156. */
  157. scrollToLower() {
  158. if (this._isload) return;
  159. this._isload = true;
  160. this.loadListData({
  161. isRefresh: false,
  162. });
  163. },
  164. refreshCallback() {
  165. this.getUserInfoData();
  166. },
  167. });