about.js 5.65 KB
/**
 * about model
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2017/11/20
 */

const _ = require('lodash');
const cache = global.yoho.cache;
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
const redis = global.yoho.redis;

const handleStaticUrl = require(`${global.utils}/parameter`).fullParamToMinPath;
const hotBrandsModel = require('../../../doraemon/models/hot-brands');

const CACHE_TIME_S = 60 * 60;

const indexUrl = {
    boys: helpers.urlFormat('/'),
    girls: helpers.urlFormat('/woman'),
    kids: helpers.urlFormat('/kids'),
    lifestyle: helpers.urlFormat('/lifestyle')
};

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }
    getCategoryFromApi(channel) {
        return this.get({data: {
            method: 'web.regular.groupsort.sale',
            sales: 'Y', // 在销售商品分类
            status: 1, // 上架商品分类
            stocknumber: 1, // 过滤掉已售罄
            yh_channel: channel
        }, param: {cache: true}});
    }
    getCategoryData(channel) {
        return Promise.all([
            this.getCategoryFromApi(1),
            this.getCategoryFromApi(2),
            this.getCategoryFromApi(3),
            this.getCategoryFromApi(4),
            hotBrandsModel.hotBrands()
        ]).then(result => {
            const nameMap = ['男生', '女生', '潮童', '创意生活'];
            const enMap = ['boys', 'girls', 'kids', 'lifestyle'];
            const genderMap = ['1,3', '2,3'];
            const baseUrl = '/list';
            const hotBrands = result.pop();

            let categoryList = [];

            _.forEach(result, (res, index) => {
                if (res.code !== 200) {
                    return;
                }

                let list = [],
                    gender = genderMap[index],
                    ename = enMap[index];

                _.forEach(res.data || [], cate => {
                    let sub = [];
                    let seatUrl = helpers.urlFormat(handleStaticUrl(baseUrl, {
                        category_id: '{categoryId}',
                        gender: gender,
                        channel: ename
                    }));

                    _.forEach(cate.sub, scate => {
                        sub.push({
                            name: scate.category_name,
                            url: seatUrl.replace('{categoryId}', scate.category_id)
                        });
                    });

                    list.push({
                        title: cate.category_name,
                        sub: sub
                    });
                });

                categoryList.push({
                    channelName: nameMap[index],
                    list: list
                });
            });

            let upChannel = _.toUpper(channel);

            return {
                pathNav: [
                    {pathTitle: `${upChannel}首页`, name: `${upChannel}首页`, href: indexUrl[channel]},
                    {pathTitle: '全部分类', name: '全部分类'}
                ],
                hotBrands,
                categoryList
            };
        });
    }
    getCategoryDataWithCache(channel) {
        let cacheKey = 'seo_category_group_map';

        return cache.get(cacheKey).then(cdata => {
            let cdataObj;

            if (cdata) {
                try {
                    cdataObj = JSON.parse(cdata);
                } catch (e) {
                    logger.debug('category map cache data parse fail.');
                }
            }

            if (cdataObj) {
                return cdataObj;
            }

            return this.getCategoryData(channel).then(list => {
                if (list && list.length) {
                    cache.set(cacheKey, list, CACHE_TIME_S);
                }
                return list;
            });
        }).catch(err => {
            logger.debug(`get category map cache data fail:${err.toString()}`);

            return this.getCategoryData(channel);
        });
    }
    getChanpinData(channel) {
        return Promise.all([
            redis.all([['get', 'global:yoho:seo:goodsAll']]),
            hotBrandsModel.hotBrands()
        ]).then(result => {
            let upChannel = _.toUpper(channel);

            let chanpinList = [];

            try {
                chanpinList = JSON.parse(_.get(result, '[0][0]'));
            } catch (e) {
                logger.debug('chanpin all goods data parse fail.');
            }

            return {
                pathNav: [
                    {pathTitle: `${upChannel}首页`, name: `${upChannel}首页`, href: indexUrl[channel]},
                    {pathTitle: '产品大全', name: '产品大全'}
                ],
                hotBrands: result[1],
                chanpinList
            };
        });
    }
    link() {
        return redis.all([
            ['get', 'friend:text:links'],
            ['get', 'friend:img:links']
        ]).then(redisData => {
            let textLinks = [],
                imgLinks = [];

            try {
                textLinks = JSON.parse(redisData[0]);
            } catch (e) {
                logger.error(`friend text links parse error : ${JSON.stringify(e)}`);
            }

            try {
                imgLinks = JSON.parse(redisData[1]);
            } catch (e) {
                logger.error(`friend img links parse error : ${JSON.stringify(e)}`);
            }

            return {
                textLinks: _.sortBy(textLinks, o => {
                    return -o.sort;
                }),
                imgLinks: _.sortBy(imgLinks, o => {
                    return -o.sort;
                })
            };
        });
    }
};