seoModel.js 2.46 KB
'use strict';

const _ = require('lodash');
const seoHelper = require('../utils/seoHelper');
const model = require('../../../lib/model');
class seoModel extends model {
    constructor(ctx) {
        super(ctx);
    }

    /**
     * 获取所有品牌(redis || api)
     */
    getBrands() {
        let brandsRedisKey = 'global:yoho:brands';

        // 先取redis
        return this.ctx.redis.getAsync(brandsRedisKey).then(brands => {
            if (!brands) {

                // 调用接口
                return this.get('http://192.168.102.216:8080/yohosearch/brand/list.json').then(res => {
                    if (res) {
                        this.ctx.redis.setAsync(brandsRedisKey, JSON.stringify(res));
                        brands = res;
                    }
                    return brands;
                })
            } else {
                return JSON.parse(brands);
            }

        }).catch(()=>{
            return {};
        });
    }

    /**
     * 获取所有分类 (redis || api)
     */
    getSort() {
        let sortsRedisKey = 'global:yoho:sorts';

        // 先取redis
        return this.ctx.redis.getAsync(sortsRedisKey).then(sorts => {
            if (!sorts) {

                // 调用接口
                return this.get('http://192.168.102.216:8080/yohosearch/sortgroup.json',{needAllSort:1, needSmallSort: 1}).then(res => {
                    if (res) {
                        this.ctx.redis.setAsync(sortsRedisKey, JSON.stringify(res));
                        sorts = res;
                    }
                    return sorts;
                })
            } else {
                return JSON.parse(sorts);
            }

        }).catch(()=>{
            return {};
        });
    }

    /**
     * 添加词根数据
     */
    addRootWordsData() {
        return Promise.all([this.getBrands(), this.getSort()]).then(result => {
            return {brand: seoHelper.sortBrands(result[0]), sort: seoHelper.getSubSorts(result[1])};
        })
    }

    /**
     * 词根列表数据
     */
    rootWordsData() {
        return Promise.all([this.getBrands(), this.getSort()]).then(result => {
            return {brand: seoHelper.sortBrands(result[0]), sort: seoHelper.getSubSorts(result[1])};
        })
    }

    /**
     * 获取子分类
     */
    getSubSorts(sortId) {

        return this.getSort().then(sorts => {
            return seoHelper.getSubSorts(sorts, sortId);
        });
    }
}



module.exports = seoModel;