123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- // pages/personal/personal.js
- const net = require("../../utils/net.js");
- const utils = require("../../utils/util.js");
- const {
- api_news_list,
- api_topic_type_list,
- api_user_info,
- } = require("../../utils/api.js");
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- userId: null,
- categoryList: [],
- listData: [],
- limit: 20,
- scrollH: 400,
- selectTab: 0,
- userData: {},
- isEnd: false,
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- if (options && options.userId) {
- this._userId = options.userId;
- this.loadCategoryListData();
- this.getUserInfoData();
- this.getScrollH();
- } else {
- utils.showToast({ title: "用户信息错误,请您稍后再试!" });
- }
- },
- tapNavBarAction() {
- const { userData } = this.data;
- const { isAuthor, isAttention } = userData.userInfo;
- if (!isAuthor && isAttention == 0) {
- let pages = getCurrentPages(); /*在内存中的所有页面栈对象*/
- let reloadPage = pages[pages.length - 2]; /*我的页面对象*/
- reloadPage && reloadPage.cancelAttention && reloadPage.cancelAttention();
- }
- },
- getUserInfoData() {
- let params = {};
- params.url = api_user_info;
- params.data = {
- userId: this._userId,
- };
- net.req(params).then((data) => {
- this.setData({
- userData: data,
- });
- });
- },
- getScrollH() {
- const _this = this;
- wx.createSelectorQuery()
- .select("#fixedTop")
- .boundingClientRect(function (res) {
- const sys = wx.getSystemInfoSync();
- _this.setData({
- scrollH: sys.screenHeight - res.height - 30,
- });
- })
- .exec();
- },
- loadCategoryListData() {
- let params = {};
- params.url = api_topic_type_list;
- params.data = { type: 2 };
- net.req(params, true).then(
- (data) => {
- this.setData(
- {
- categoryList: data,
- },
- () => {
- this.loadListData({ isRefresh: true });
- }
- );
- },
- (e) => {
- utils.showToast({ title: e.msg || "" });
- }
- );
- },
- loadListData(obj) {
- const { limit, listData, categoryList, selectTab } = this.data;
- let params = {};
- params.url = api_news_list;
- params.data = {
- limit: limit,
- userId: this._userId,
- type: categoryList[selectTab].type,
- };
- const list = listData[selectTab] || [];
- const len = list.length;
- if (!obj.isRefresh && len) {
- params.data.lastId = list[len - 1].id;
- }
- net.req(params, true).then(
- (data) => {
- if (obj.isRefresh) {
- this.setData({
- [`listData[${selectTab}]`]: data.list,
- isEnd: data.list.length < limit,
- });
- this._refresh = false;
- this._isload = false;
- } else {
- if (data.list.length) {
- this.setData({
- [`listData[${selectTab}]`]: listData[selectTab].concat(data.list),
- isEnd: data.list.length < limit,
- });
- }
- this._isload = data.list.length < limit;
- }
- },
- (e) => {
- obj.isRefresh ? (this._refresh = false) : (this._isload = false);
- }
- );
- },
- handleTabAction(e) {
- const { index, item } = e.detail;
- const { categoryList, selectTab, listData } = this.data;
- const newType = item.type;
- const oldType = categoryList[selectTab].type;
- if (newType != oldType) {
- this.setData({
- selectTab: index,
- });
- const list = listData[index];
- if (!list || !list.length) {
- this.loadListData({ isRefresh: true });
- }
- }
- },
- tapAttentionListAction() {
- const { userData } = this.data;
- wx.navigateTo({
- url: `/pages/myAttention/myAttention?userId=${userData.userInfo.userId}`,
- });
- },
- tapFansAction() {
- const { userData } = this.data;
- wx.navigateTo({
- url: `/pages/myfans/myfans?userId=${userData.userInfo.userId}`,
- });
- },
- /**
- * scroll-view上拉触底事件的处理函数
- */
- scrollToLower() {
- if (this._isload) return;
- this._isload = true;
- this.loadListData({
- isRefresh: false,
- });
- },
- refreshCallback() {
- this.getUserInfoData();
- },
- });
|