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

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


/**
 * 逛分类
 */
const _category = () => {
    return serviceAPI.get('/guang/api/v1/category/get', {

    }, {

        code: 200
    });
};

/**
 * [逛内容列表]
 * @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]}
 */
const _getArticleList = (gender, sortId, uid, udid, page, tag, authorId, limit, useCache) => {
    let param = {
        page: page || 1,
        uid: uid || 0,
        udid: udid || '',
        gender: gender || '',
        sort_id: sortId,
        tag: tag,
        author_id: authorId,
        limit: limit
    };

    return serviceAPI.get('guang/api/v2/article/getList', 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]}
 */
const _getPageData = (gender, sortId, uid, udid, page, tag, authorId, isApp, showAuthor, isTab) => {
    return _getArticleList(gender, sortId, uid, udid, page, tag, authorId).then(article => {
        let result = {};

        if (!article.data.list.artList) {
            return result;
        }

        let adList = article.data.list.adlist;

        // 广告列表
        if (isTab && adList) {
            let build = [];

            adList.forEach(ad => {
                build.push({
                    url: guangProcess.getFilterUrl(ad.url),
                    img: helpers.image(ad.src, 830, 327)
                });
            });
            result.swiper = build;
        }

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

        artList.forEach(art => {
            build.push(guangProcess.formatArticle(art, true, isApp, showAuthor, uid));
        });
        result.infos = build;
        return result;
    });
};

/**
 * 逛内容列表
 */
const _article = (param) => {
    return serviceAPI.get('/guang/api/v2/article/getList', {
        gender: param.gender,
        page: param.page || 1,
        uid: param.uid,
        udid: param.udid,
        sort_id: param.type ? param.type : 1,
        tag: param.tag ? param.tag : null,
        author_id: param.authorId ? param.authorId : null,
        limit: param.limit ? param.limit : null,
    }, {
        code: 200
    }).then(result => {

        return result;

    });
};


/**
 * 逛
 * @param params
 */
const getArticle = (param) => {

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

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

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

        let resu = {
            guang: {
                infos: [{
                    show: true,
                    info: [{
                        'article_type': '1',
                        author: {
                            author_id: '8168296',
                            avatar: 'http://img10.static.yhbimg.com/author/2016/05/07/15/01bb3ae789c573502830726c3297d0c80a.png',
                            name: '雾萌萌',
                            url: 'http://guang.m.yohobuy.com/author/index?id=8168296'
                        },
                        'author_id': '8168296',
                        'category_id': '1',
                        'category_name': '话题',
                        'conver_image_type': '1',
                        'id': 34380,
                        text: '验证逛文章图片压缩测试验证逛文章图片压缩测试验证逛文章图片压缩测试',
                        'isFavor': 'N',
                        'isPraise': 'N',
                        'is_recommended': '1',
                        'praiseStatus': 'true',
                        'praise_num': '0',
                        publishTime: '09月02日 19:21',
                        'share': {
                            'url': 'http://guang.m.yohobuy.com/info/index?id=34380'
                        },
                        img: 'https://img13.static.yhbimg.com/article/2016/09/05/14/02d732942cdd9647465736cb79796b0ddd.jpg?imageView/2/w/640/h/640',
                        'title': '验证逛文章图片压缩测试',
                        'url': 'http://guang.m.yohobuy.com/info/index?id=34380',
                        pageView: '50'
                    }]
                }],
                navs: [{
                    typeId: '0',
                    type: '推荐',
                    focus: true
                }, {
                    typeId: '1',
                    type: '话题'
                }, {
                    typeId: '2',
                    type: '搭配'
                }, {
                    typeId: '3',
                    type: '潮人'
                }, {
                    typeId: '4',
                    type: '潮品'
                }, {
                    typeId: '19',
                    type: '专题'
                }],
                swiper: [{
                    img: 'https://img11.static.yhbimg.com/yhb-img01/2016/09/01/10/017b5ce31285f9062469303ba9ca197660.jpg?imageView2/2/w/830/h/327',
                    url: 'http://feature.yoho.cn/0418RUNNINGMAN/index.html?title=兄弟在奔跑,潮流在有货&share_id=944'
                }, {
                    img: 'https://img11.static.yhbimg.com/yhb-img01/2016/09/01/10/017b5ce31285f9062469303ba9ca197660.jpg?imageView2/2/w/830/h/327',
                    url: 'http://feature.yoho.cn/0418RUNNINGMAN/index.html?title=兄弟在奔跑,潮流在有货&share_id=944'
                }]
            }
        };
        return resu;
    });

};

module.exports = {
    getAuthor: _getAuthor,
    getArticleList: _getArticleList,
    getPageData: _getPageData,
    getArticle
};