info.js 3.08 KB
/**
 * 逛详情models
 * @author: chenfeng<feng.chen@yoho.cn>
 * @date: 2016/09/07
 */
'use strict';
const serviceAPI = global.yoho.ServiceAPI;

const URI_PACKAGE_ARTICLE = 'guang/service/v2/article/';
const URI_PACKAGE_AUTHOR = 'guang/service/v1/author/';

/**
 * [逛资讯详情页数据封装]
 * @param  {[int]}  id    [内容ID]
 * @param  {Boolean} isApp [标识是否是APP访问]
 * @return {[array]}
 */
const _package = (id, isApp) => {
    let result = {
        getAuthor: {},
        getArticle: {},
        getArticleContent: {},
        getBrand: {},
        getOtherArticle: {}
    };

    // 客户端类型
    let clientType = isApp ? 'iphone' : 'h5';

    // 获取资讯
    let param = {
        article_id: id,
        client_type: clientType,
        // private_key: sign.privateKey[clientType]
    };

    return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getArticle`, param, {
        cache: true
    }).then(data => {
        // 接口要判断一下返回状态是否成功,OK,I 服了 YOU
        if (data.code !== 200) {
            result.code = 400;
            return result;
        }
        let article = data.data;

        result.getArticle = article;

        let promises = [];

        // 获取作者信息
        param = {
            author_id: article.author_id
        };

        // param.client_secret = sign.apiSign(param);
        promises.push(serviceAPI.get(`${URI_PACKAGE_AUTHOR}getAuthor`, param, {
            cache: true
        }));

        // 获取资讯内容
        param = {
            article_id: id,
            client_type: clientType,
            // private_key: sign.privateKey[clientType]
        };

        // param.client_secret = sign.apiSign(param);
        promises.push(serviceAPI.get(`${URI_PACKAGE_ARTICLE}getArticleContent`, param, {
            cache: true
        }));

        // 获取资讯相关的品牌
        param = {
            article_id: id,
            client_type: clientType,
            // private_key: sign.privateKey[clientType]
        };

        // param.client_secret = sign.apiSign(param);
        promises.push(serviceAPI.get(`${URI_PACKAGE_ARTICLE}getBrand`, param, {
            cache: true
        }));

        // 获取资讯相关的其它资讯
        if (article.tag) {
            param = {
                article_id: id,
                client_type: clientType,
                tags: article.tag,
                offset: 0,
                limit: 3,
                // private_key: sign.privateKey[clientType]
            };

            // param.client_secret = sign.apiSign(param);
            promises.push(serviceAPI.get(`${URI_PACKAGE_ARTICLE}getOtherArticle`, param, {
                cache: true
            }));
        }
        return Promise.all(promises).then(datas => {
            result.getAuthor = datas[0].data;
            result.getArticleContent = datas[1].data;
            result.getBrand = datas[2].data;
            if (datas.length === 4) {
                result.getOtherArticle = datas[3].data;
            }
            return result;
        });
    });
};

module.exports = {
    package: _package
};