about.js 4.27 KB

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

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 => {
            let nameMap = ['男生', '女生', '潮童', '创意生活'];
            let genderMap = ['1,3', '2,3'];
            let categoryList = [];

            let hotBrands = result.pop();

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

                res = res.data;

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

                _.forEach(res, cate => {
                    let sub = [];

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

                    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
            };

        });
    }
};