info.js 8.62 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 _ = require('lodash');
const helpers = global.yoho.helpers;

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

/**
 * 获取二级菜单顶部颜色
 * @param  {[string]} choosed
 * @return {[string]}
 */
const _getSidebarColor = (choosed) => {
    let color = false;

    if (choosed === 'girls') {
        color = '#FF88AE';
    } else if (choosed === 'kids') {
        color = '#7ad9f9';
    } else if (choosed === 'lifestyle') {
        color = '#4f4138';
    }

    return color;
};

// 微信侧边栏导航数据
const _processSideBar = (list, choosed) => {
    const formatData = [];
    let offset = 0; // 分割数组用到的游标

    list = list || [];

    _.forEach(list, (item, i) => {
        if (item.sub) {
            item.sub.unshift({
                sort_name: item.sort_name,
                sort_name_en: item.sort_name_en,
                back: true,
                isSelect: false,
                bgColor: _getSidebarColor(choosed)
            });
        }

        // 如果有分隔符,分割数组
        if (item.separative_sign === 'Y') {
            formatData.push(list.slice(offset, i));
            offset = i;
        }
    });

    // 数组被分割剩余的部分
    formatData.push(list.slice(offset));
    return formatData;
};

const _getLeftNav = (choosed) => {
    choosed = choosed || 'all';

    return serviceAPI.get('operations/api/v6/category/getCategory', {}, {
        cache: true
    }).then(result => {
        if (result && result.code === 200) {

            return _processSideBar(result.data, choosed);
        }
    });
};

const _getShareData = (id) => {

    return serviceAPI.get('guang/api/v6/share/guang', {
        id: id
    }).then(result => {
        if (result && result.code === 200) {

            return result.data;
        }
    });
};


/**
 * [逛资讯详情页数据封装]
 * @param  {[int]}  id    [内容ID]
 * @param  {Boolean} isApp [标识是否是APP访问]
 * @return {[array]}
 */
const packageData = (id, isApp, isWeixin, channel, isShare) => {
    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 || 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'
            }));
        }

        if (isShare) {
            let navGender = _.cloneDeep(channel);

            promises.push(_getLeftNav(navGender));
        }

        if (isShare) {
            promises.push(_getShareData(id));
        }

        return Promise.all(promises).then(datas => {

            let getArticleContent = {};

            if (datas) {
                if (datas[1]) {

                    getArticleContent = datas[1].data;

                    result.getArticleContent = getArticleContent;

                }

                if (isApp && datas[4] && 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++;
                        }
                    }

                    _.forEach(datas[4].data, item => {
                        item.src = helpers.image(item.src, 280, 210, 1);
                    });

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

                if (isShare && datas[5]) {

                    if (datas[5].wechatShareImgUrl) {
                        datas[5].wechatShareImgUrl = datas[5].wechatShareImgUrl.substring(datas[5].wechatShareImgUrl.indexOf('//'));

                        if (datas[5].wechatShareImgUrl.indexOf('?') === -1) {
                            datas[5].wechatShareImgUrl = datas[5].wechatShareImgUrl + '?imageView2/2/interlace/1/q/75';
                        }
                    } else if (datas[5].qqShareImgUrl) {
                        datas[5].qqShareImgUrl = datas[5].qqShareImgUrl.substring(datas[5].qqShareImgUrl.indexOf('//'));

                        if (datas[5].qqShareImgUrl.indexOf('?') === -1) {
                            datas[5].qqShareImgUrl = datas[5].wechatShareImgUrl + '?imageView2/2/interlace/1/q/75';
                        }
                    } else if (datas[5].showShareImgUrl) {
                        datas[5].showShareImgUrl = datas[5].showShareImgUrl.substring(datas[5].showShareImgUrl.indexOf('//'));

                        if (datas[5].showShareImgUrl.indexOf('?') === -1) {
                            datas[5].showShareImgUrl = datas[5].showShareImgUrl + '?imageView2/2/interlace/1/q/75';
                        }
                    }

                    let preCount = 0;
                    let i;

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

                    getArticleContent.splice(preCount, 0, {
                        shareCode: datas[5]
                    });

                }

                if (datas[0]) {
                    result.getAuthor = datas[0].data;
                }


                if (datas[2]) {
                    result.getBrand = datas[2].data;
                }

                if (isShare && datas[4]) {
                    result.sideNav = datas[4];
                }

                if (datas.length === 5 && isApp || datas.length === 4 && !isApp || datas.length === 6 && isShare) {
                    if (datas[3]) {
                        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
};