info.js 4.18 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 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 packageData = (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 => {
        // 接口要判断一下返回状态是否成功
        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 (typeof article.tag !== 'undefined') {
            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
            }));
        }

        if (isApp) {
            promises.push(api.get('', {
                method: 'app.resources.getSingleTemplate',
                module: 'wechat',
                key: 'guang_detail_wechat'
            }));
        }

        return Promise.all(promises).then(datas => {
            let getArticleContent = datas[1].data;

            if (isApp && datas[4].data) {

                let preCount = 0;
                let i;

                for (i = 0; i < getArticleContent.length; i++) {
                    if (getArticleContent[i].singleImage || getArticleContent[i].text || getArticleContent[i].smallPic) {
                        preCount++;
                    }
                }

                getArticleContent.splice(preCount, 0, {weixinPublic: datas[4].data});
            }

            result.getAuthor = datas[0].data;
            result.getArticleContent = getArticleContent;
            result.getBrand = datas[2].data;
            if (datas.length === 5 && isApp || datas.length === 4 && !isApp) {
                result.getOtherArticle = datas[3].data;
            }
            return result;
        });
    });
};

/**
 * [获取详情信息]
 * @param  {[int]} id [资讯id]
 * @return {[object]}
 */
const intro = (id) => {
    let param = {
        article_id: id,
        client_type: 'h5'
    };

    return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getArticleContent`, param, {
        cache: true
    });
};

module.exports = {
    packageData,
    intro
};