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

const URI_PACKAGE_ARTICLE = 'guang/service/v2/article/';
const URI_PACKAGE_AUTHOR = 'guang/service/v1/author/';
const URI_PACKAGE_COMMENTS = 'guang/api/v1/comments/';
const URI_PACKAGE_PRAISE = 'guang/api/v1/article/';

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

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

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

        return color;
    }

    /**
     * 微信侧边栏导航数据
     * @param {*} list
     * @param {*} choosed
     */
    _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: this._getSidebarColor(choosed)
                });
            }

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

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

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

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

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

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

                return result.data;
            }
        });
    }

    /**
     * 获取文章接口调用
     * @param {*} articleId
     */
    _getArticle(articleId) {
        return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getArticle`, {
            article_id: articleId
        }, {
            cache: true
        });
    }

    /**
     * 获取作者接口调用
     * @param {*} authorId
     */
    _getAuthor(authorId) {
        return serviceAPI.get(`${URI_PACKAGE_AUTHOR}getAuthor`, {
            author_id: authorId
        }, {
            cache: true
        });
    }

    /**
     * 获取文章详情接口调用
     * @param {*} articleId
     */
    _getArticleContent(articleId) {
        return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getArticleContent`, {
            article_id: articleId
        }, {
            cache: true
        });
    }

    /**
     * 获取文章相关品牌接口调用
     * @param {*} articleId
     */
    _getBrand(articleId) {
        return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getBrand`, {
            article_id: articleId
        }, {
            cache: true
        });
    }

    /**
     * 获取资讯相关的其它资讯接口调用
     * @param {*} articleId
     * @param {*} tag
     */
    _getOtherArticle(articleId, tag) {
        return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getOtherArticle`, {
            article_id: articleId,
            tags: tag,
            offset: 0,
            limit: 3
        }, {
            cache: true
        });
    }

    /**
     * APP 获取微信模块
     */
    _getSingleTemplateWechat() {
        return api.get('', {
            method: 'app.resources.getSingleTemplate',
            module: 'wechat',
            key: 'guang_detail_wechat'
        });
    }

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

        // 获取资讯
        return this._getArticle(id).then(data => {
            // 调用接口失败
            if (!data || data.code !== 200) {
                result.code = 400;
                return result;
            }
            let article = result.getArticle = data && data.data || {};
            let promises = [
                this._getAuthor(article.author_id),
                this._getArticleContent(id),
                this._getBrand(id)
            ];

            // 获取资讯相关的其它资讯
            if (typeof article.tag !== 'undefined') {
                promises.push(this._getOtherArticle(id, article.tag));
            }

            // APP 获取微信模块
            if (isApp) {
                promises.push(this._getSingleTemplateWechat());
            }

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

                promises.push(
                    this._getLeftNav(navGender),
                    this._getShareData(id)
                );
            }

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

                if (!datas) {
                    return result;
                }

                if (datas[1]) {
                    result.getArticleContent = getArticleContent = datas[1].data;
                }

                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]}
     */
    intro(id) {
        let param = {
            article_id: id,
            client_type: 'h5'
        };

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

    /**
     * [根据商品SKN获取商品的简要信息]
     * @param  {[array]} sknString [skns]
     * @return {[type]}
     */
    productInfoBySkns(sknString) {
        // 调用搜索接口
        let param = {
            method: 'h5.product.batch',
            productSkn: sknString,
            order: 's_t_desc'
        };

        return api.get('', param, {
            cache: true
        }).then(result => {
            return _.get(result, 'data.product_list', []);
        });
    }

    /**
     * 获取文章评论列表
     */
    comments(params) {
        return serviceAPI.get(`${URI_PACKAGE_COMMENTS}getList`, {
            article_id: params.article_id,
            page: params.page,
            limit: params.limit || 20,
            udid: params.udid
        }).then(result => {
            return _.get(result, 'data.list', []);
        });
    }

    /**
     * 获取评论数量
     * @param {*} params
     */
    commentsTotal(params) {
        return serviceAPI.get(`${URI_PACKAGE_COMMENTS}getList`, {
            article_id: params.article_id,
            page: 1,
            limit: 1,
            udid: params.udid
        }).then(result => {
            return _.get(result, 'data.total', 0);
        });
    }

    /**
     * 获取点赞相关数据
     * @param {*} params
     */
    getArticlePraiseAndFavor(params) {
        return serviceAPI.get(`${URI_PACKAGE_PRAISE}getArticlePraiseAndFavor`, {
            uid: params.uid,
            id: params.id,
            udid: params.udid
        }).then(result => {
            return _.get(result, 'data', {});
        });
    }
}

module.exports = DetailModel;