index.js 10.1 KB
/**
 * 逛models
 * @author: chenfeng<feng.chen@yoho.cn>
 * @date: 2016/09/07
 */
'use strict';
const serviceAPI = global.yoho.ServiceAPI;
const api = global.yoho.API;
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
const guangProcess = require(`${global.utils}/guang-process`);
const _ = require('lodash');

class IndexModel extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    /**
     * [获取作者信息]
     * @param  {[int]} id [作者id]
     * @return {[object]}
     */
    getAuthor(id) {
        return this.get({
            api: serviceAPI,
            url: 'guang/service/v1/author/getAuthor',
            data: {
                author_id: id
            },
            param: {
                cache: true
            }
        }).then((result) => {
            if (result && result.code === 200) {
                return result;
            } else {
                logger.error('getAuthor code no 200');
                return {};
            }
        });
    }

    /**
     * [逛内容列表]
     * @param  {[string]} gender  ["1,3"表示男, "2,3"表示女, "1,2,3"表示所有]
     * @param  {[int]} sortId [分类ID]
     * @param  {Number} uid [用户ID]
     * @param  {String} udid [客户端唯一标识]
     * @param  {Number} page [分页第几页, 默认第1页]
     * @param  {[string]} tag [标签]
     * @param  {[int]} authorId [作者ID]
     * @param  {[int]} limit [返回的限制数]
     * @param  {Boolean} useCache [是否使用缓存]
     * @return {[array]}
     */
    getArticleList(gender, sortId, uid, udid, page, tag, authorId, limit, useCache) {
        return this.get({
            api: serviceAPI,
            url: 'guang/api/v2/article/getList',
            data: {
                page: page || 1,
                uid: uid || 0,
                udid: udid || '',
                gender: gender || '',
                sort_id: sortId,
                tag: tag,
                author_id: authorId,
                limit: limit || 4
            },
            param: {
                cache: useCache
            }
        }).then((result) => {
            if (result && result.code === 200) {
                return result;
            } else {
                logger.error('getAuthor code no 200');
                return [];
            }
        });

    }

    /**
     * [获取切换逛类别或者分页时的文章数据]
     * @param  {[string]}  gender     ["1,3"表示男, "2,3"表示女]
     * @param  {[int]}  sortId     [分类ID]
     * @param  {[int]}  uid        [用户ID]
     * @param  {[string]}  udid       [客户端唯一标识]
     * @param  {[int]}  page       [分页第几页, 默认第1页]
     * @param  {[string]}  tag        [标签]
     * @param  {[string]}  authorId   [作者ID]
     * @param  {Boolean} isApp      [是否是APP]
     * @param  {[Boolean]}  showAuthor [是否显示作者]
     * @param  {Boolean} isTab      [是否为tab切换操作]
     * @return {[array]}
     */
    getPageData(gender, sortId, uid, udid, page, tag, authorId, isApp, showAuthor, isTab) {
        return this.getArticleList(gender, sortId, uid, udid, page, tag, authorId).then(article => {
            let result = {};

            if (!_.get(article, 'data.list.artList', false)) {
                return result;
            }

            let adList = article.data.list.adlist;

            // 广告列表
            if (isTab && adList) {
                result.swiper = adList.map(function(ad) {
                    return {
                        url: guangProcess.getFilterUrl(ad.url),
                        img: helpers.image(ad.src, 830, 327)
                    };
                });
            }

            /* 构建资讯文章内容 */
            let artList = article.data.list.artList;

            result.infos = artList.map(function(art) {
                return guangProcess.formatArticle(art, true, isApp, showAuthor, uid);
            });

            return result;
        });
    }

    /**
     * 逛
     * @param params
     */
    getArticle(param) {

        let page = param.page ? param.page : 1;

        Object.assign(param, {
            page: page
        });

        return api.all([
            this._category(),
            this._article(param),
            this._getTopArticles(param)
        ]).then(result => {

            let type = param.type;

            let resu = {
                guang: {
                    gender: param.gender,
                }
            };

            // 顶部的分类列表
            let curIndex = 0; // 当前tab顺序

            let indexTmp = 0;

            if (result[0] && result[0].data) {

                indexTmp = 0;

                let cateList = result[0].data;

                let build = [];

                let inf = [];

                _.forEach(cateList, val => {
                    build.push({
                        typeId: val.id,
                        type: val.name,
                        focus: (val.id === type)
                    });

                    inf.push({
                        show: (val.id === type),
                        typeId: type,
                        info: []
                    });

                    if ((val.id === type)) {
                        curIndex = indexTmp;
                    }

                    indexTmp++;

                    resu.guang.navs = build;

                    resu.guang.infos = inf;

                });

            }

            if (result && result[1] && result[1].data && result[1].data.list) {

                let swp = [];

                let swiperList = _.get(result[1], 'data.list.adlist', []);


                swiperList.forEach(val => {
                    swp.push({
                        url: guangProcess.getFilterUrl(val.url),
                        img: helpers.image(val.src, 640, 275)
                    });
                });

                resu.guang.swiper = swp;

            }

            let inf = []; // 资讯列表
            let topList = _.get(result && result[2], 'data.list.artList', []); // 置顶文章

            _.forEach(topList, value => {
                inf.push(guangProcess.formatArticle(value, true, false, true, false, true));
            });

            let infoList = _.get(result && result[1], 'data.list.artList', []); // 普通文章

            infoList.forEach(val => {
                inf.push(guangProcess.formatArticle(val, true, false, true));
            });

            _.set(resu, `guang.infos[${curIndex}].info`, inf);

            return resu;

        });

    }

    getTagEditor(param) {

        let page = param.page ? param.page : 1;

        Object.assign(param, {
            page: page
        });

        return api.all([
            this._article(param)
        ]).then(result => {

            let resu = {
                guang: {
                    tag: param.tag,
                    gender: param.gender,
                    isApp: param.isApp ? 1 : 0,
                    guangList: true
                }
            };

            if (result && result[0] && result[0].data && result[0].data.list && result[0].data.list.artList) {

                let inf = [];
                let infoList = result[0].data.list.artList;

                infoList.forEach(val => {
                    val.colparam = {
                        urlpath: param.path,
                        param: ''
                    };
                    if (param.tag) {
                        val.colparam.param = `?query=${param.tag}`;
                    }
                    inf.push(guangProcess.formatArticle(val, true, param.isApp, true));
                });

                resu.guang.infos = inf;

            }

            return resu;

        });

    }

    /**
     * 获取制指定文章的动态信息
     * @param ids
     * @param udid
     * @param other [Obejct] 包含uid,query,type等非必传参数
     * @returns {Promise.<T>|*}
     */

    getDynamicDataByIds(ids, udid, other) {
        let params = {
            articleIds: ids,
            udid: udid
        };

        if (other.uid) {
            _.assign(params, {
                uid: other.uid
            });
        }

        if (other.query) {
            _.assign(params, {
                query: other.query
            });
        }

        if (other.type) {
            _.assign(params, {
                type: other.type
            });
        }

        return this.get({
            api: serviceAPI,
            url: 'guang/api/v6/article/getSimpleArticleList',
            data: params
        });
    }

    /**
     * 获取制指定文章的动态信息
     * @param ids
     * @returns {Promise.<T>|*}
     */

    getDynamicDataById(id, uid, udid) {
        return this.get({
            api: serviceAPI,
            url: 'guang/api/v6/article/getArticlePraiseAndFavor',
            data: {
                id: id,
                uid: uid,
                udid: udid
            }
        });
    }

    /**
     * 获取置顶的文章
     * @param {*} params
     */
    _getTopArticles(params) {
        return this.get({
            api: serviceAPI,
            url: 'guang/api/v6/article/getTopList',
            data: {
                gender: params.gender
            }
        });
    }

    /**
     * 逛分类
     */
    _category() {
        return this.get({
            api: serviceAPI,
            url: '/guang/api/v1/category/get',
            param: {
                cache: true,
                code: 200
            }
        });
    }

    /**
     * 逛内容列表
     */
    _article(param) {
        return this.get({
            api: serviceAPI,
            url: '/guang/api/v2/article/getList',
            data: {
                gender: param.gender,
                page: param.page || 1,
                uid: param.uid,
                udid: param.udid,
                sort_id: param.type || 0,
                tag: param.tag ? param.tag : null,
                limit: 4
            }, param: {
                cache: true,
                code: 200
            }
        }).then(result => {
            return result;
        });
    }
}

module.exports = IndexModel;