author.js 4.2 KB
// 逛编辑(作者)列表

import { API_HOST, SERVICE_HOST } from '../../../../libs/config';
import { GET, POST } from '../../../../libs/request';
import objectAssign from '../../../../vendors/object-assign';
import guangUtil from './util';
import Promise from '../../../../vendors/es6-promise';
import util from '../../../../utils/util';
import {
  logEvent,
  YB_PAGE_OPEN_L,
} from '../../../../libs/analytics.js'

//获取应用实例
let app = getApp();
let PV_ID = new Date().getTime() + '';

const screenHeight = app.globalData.systemInfo.screenHeight;
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;

Page({
    data: {
        options: {},

        windowHeight: windowHeight,
        screenHeight: screenHeight - 60,
        scrollTop: 0,
        isShowIndicator: false,
        isLoading: true,

        yasParam: '',
        from_page_name: '',
        from_page_param: '',
        current_page_name: 'guangAuthor',
        current_page_param: '',

        authorResult: [],
        authorInfo: {},
        page: 0,
        end: false
    },
    onLoad: function(options) {
        this.setData({
            options,
            yasParam: `&page_name=${this.data.current_page_name}&page_param=${options.author_id || ''}`,
            current_page_param: options.author_id || '',
            from_page_name: options.page_name || '',
            from_page_param: options.page_param || '',
        });
        this._init();

        var pages = getCurrentPages();
        var currentPage = pages[pages.length - 1];
        var url = currentPage.route;
        let params = {
            PAGE_NAME: this.data.current_page_name,
            PAGE_PARAM: this.data.current_page_param,
            FROM_PAGE_NAME: this.data.from_page_name,
            FROM_PAGE_PARAM: this.data.from_page_param,
            PV_ID: PV_ID,
            PAGE_PATH: url,
        };
        logEvent(YB_PAGE_OPEN_L, params);
    },
    _init: function() {
        return Promise.all([
            this._getAuthor(),
            this.loadMore()
        ]);
    },
    _getAuthor: function() {
        guangUtil._getAuthor({
            author_id: this.data.options.author_id
        }, GET, API_HOST).then(rdata => {
            if (rdata.avatar) {
                rdata.avatar = util.formatImageUrl(rdata.avatar, 80, 80);
            }

            this.setData({authorInfo: rdata});
        });
    },
    loadMore: function() {
        let authorResult = this.data.authorResult;
        let page = this.data.page + 1;
        let end = this.data.end;

        if (end) {
            this.setData({isLoading: false});
            return;
        }

        // 设置请求页+1,防止请求数据重复
        this.setData({page});

        let loadData;
        guangUtil._getList(objectAssign({
            page: page,
            author_id: this.data.options.author_id,
            gender: app.globalData.selectedChannel === 'boy' ? '1,3' : '2,3'
        }), GET, API_HOST).then(rdata => {
            loadData = rdata;
            return guangUtil._getSimpleArticleList((rdata.list.artList || []).map(item => {
                return item.id;
            }), GET, API_HOST);
        }).then(result => {
            let artList = loadData.list && loadData.list.artList || [];

            artList.map(item => {
                (result.artList || []).map(sub => {
                    if (item.id === sub.articleId) {
                        objectAssign(item, sub);
                    }
                });
            });

            if (!end) {
                end = loadData.page >= loadData.totalPage;
            }

            authorResult = authorResult.concat(guangUtil._formatArticle(artList));

            loadData = [];
            result = [];

            this.setData({authorResult: authorResult, isLoading: !end});
        });
    },
    onScroll: function(e) {
        var { scrollTop } = e.detail;
        var isShow = scrollTop > windowHeight * 2 ? true : false;

        if (isShow != this.data.isShowIndicator) {
            this.setData({
                isShowIndicator: isShow
            });
        }
    },
    backToTop: function () {
        this.setData({
            scrollTop: 0
        });
    }
});