brands-service.js 13 KB
/**
 * 品牌一览 controller
 * @author: ghw<hongwei.gao@yoho.cn>
 * @date: 2016/9/29
 */
'use strict';

const Promise = require('bluebird');
const co = Promise.coroutine;
const HeaderModel = require('../../../doraemon/models/header');
const BrandsModel = require('./brands-model');
const _ = require('lodash');
const helpers = global.yoho.helpers;
const pager = require(`${global.utils}/pager`).setPager;

const BOYS = 'boys';
const GIRLS = 'girls';
const KIDS = 'kids';
const LIFESTYLE = 'lifestyle';
const queryString = require('querystring');

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    /**
     * 获取品牌一览资源位&channelType
     *
     * @param string $channelStr
     * @return array
     */
    getGenderByChannel(channel) {
        let gender = '';

        switch (channel) {

            case BOYS:
                gender = '1,3';
                break;
            case GIRLS:
                gender = '2,3';
                break;
            default:
                gender = '1,2,3';
                break;
        }
        return gender;
    }

    getHomeurlByChannel(channel) {
        let home;

        switch (channel) {
            case GIRLS:
                home = helpers.urlFormat('/woman', {}, 'new');
                break;
            case LIFESTYLE:
                home = helpers.urlFormat('/lifestyle', {}, 'new');
                break;
            case KIDS:
                home = helpers.urlFormat('/kids', {}, 'new');
                break;
            default:
                home = helpers.urlFormat('');
                break;
        }
        return home;
    }

    // 添加网站的SEO
    seoMap() {
        return {
            boys: {
                title: '【男生潮流品牌一览】潮流品牌大全,热销潮流品牌-YOHO!BUY 有货',
                keywords: '潮流品牌一览,潮流品牌大全,热销潮牌',
                description: '有货网品牌大全汇聚服饰、鞋履、包袋,配饰等大牌,潮流品牌排行榜包括vans、Lee、puma、' +
                    'reebok、AKOP、Levi\'s等国内外知名潮流品牌,够潮,就上有货网!'
            },
            girls: {
                title: '【男生潮流品牌一览】潮流品牌大全,热销潮流品牌-YOHO!BUY 有货',
                keywords: '潮流品牌一览,潮流品牌大全,热销潮牌',
                description: '有货网品牌大全汇聚服饰、鞋履、包袋,配饰等大牌,潮流品牌排行榜包括vans、Lee、puma、' +
                    'reebok、AKOP、Levi\'s等国内外知名潮流品牌,够潮,就上有货网!'
            },
            kids: {
                title: '【男生潮流品牌一览】潮流品牌大全,热销潮流品牌-YOHO!BUY 有货',
                keywords: '潮流品牌一览,潮流品牌大全,热销潮牌',
                description: '有货网品牌大全汇聚服饰、鞋履、包袋,配饰等大牌,潮流品牌排行榜包括vans、Lee、puma、' +
                    'reebok、AKOP、Levi\'s等国内外知名潮流品牌,够潮,就上有货网!'
            },
            lifestyle: {
                title: '【男生潮流品牌一览】潮流品牌大全,热销潮流品牌-YOHO!BUY 有货',
                keywords: '潮流品牌一览,潮流品牌大全,热销潮牌',
                description: '有货网品牌大全汇聚服饰、鞋履、包袋,配饰等大牌,潮流品牌排行榜包括vans、Lee、puma、' +
                    'reebok、AKOP、Levi\'s等国内外知名潮流品牌,够潮,就上有货网!'
            },
            'boys-plusstar': {
                title: '品牌plusstar,男装潮流品牌简介|YOHO!BUY有货',
                keywords: '品牌plusstar,男装潮流品牌简介',
                description: 'YOHO!BUY 有货男装品牌一览汇集国内国际各大男装品牌,为广大爱美男生提供品牌男装、' +
                '休闲男装、商务男装.买品牌男装就上YOHO!BUY 有货,全场品牌正品保证'
            },
            'girls-plusstar': {
                title: '品牌plusstar,女装潮流品牌简介|YOHO!BUY有货',
                keywords: '品牌plusstar,女装潮流品牌简介',
                description: 'YOHO!BUY 有货女装品牌一览汇集国内国际各大女装品牌,为广大爱美女生提供品牌女装、' +
                '休闲女装、商务女装.买品牌女装就上YOHO!BUY 有货,全场品牌正品保证'
            },
            'plusstar-id1': {
                title: '【设计新潮】热销新潮品牌,新潮品牌一览-YOHO!BUY 有货',
                keywords: '设计新潮、新潮品牌、新潮店铺',
                description: '有货网设计新潮品牌,提供一系列国内外知名潮流品牌,潮流品牌店铺,,想了解更多新潮品牌就来有货网!'
            },
            'plusstar-id2': {
                title: '【潮流经典】潮流经典品牌大全,热销经典品牌-YOHO!BUY 有货',
                keywords: '潮流经典,经典品牌,潮流店铺',
                description: '有货网潮流经典品牌,提供一系列国内外经典潮流品牌,经典潮流品牌店铺,想了解更多经典潮流品牌就来有货网!'
            },
            'plusstar-id3': {
                title: '【明星潮品】明星品牌大全,知名明星潮流品牌-YOHO!BUY 有货',
                keywords: '明星潮品,明星品牌,明星潮流店铺',
                description: '有货网明星潮品,提供一系列国内外明星潮流品牌,明星潮流品牌店铺,想了解更多明星潮流品牌就来有货网!'
            },
            'plusstar-id4': {
                title: '【原创潮牌】原创潮牌品牌大全,优质潮流品牌-YOHO!BUY 有货',
                keywords: '原创潮牌,原创品牌,原创潮流品牌店铺',
                description: '有货网原创潮牌,提供国内外原创潮流品牌,原创潮流品牌店铺,想了解更多原创潮流品牌就来有货网!'
            }
        };
    }

    /**
     * plusstar 分页链接伪静态
     * @param string uri
     * @param string originUrl  动态地址
     */
    staticUrl(uri, originUrl) {
        originUrl = queryString.parse(_.split(originUrl, '?')[1]);
        return `${uri}id${originUrl.id || 0}-p${originUrl.page || 1}/`;
    }

    /**
     * 获取品牌一览list
     * @param string $channel 频道名称
     * @param int start  开始位置 1 开始
     * @param int length 取数长度 0 取到最后
     */
    getBrandViewList(channel) {
        let that = this;
        let brandsModelCtx = new BrandsModel(that.ctx);
        let headerModelCtx = new HeaderModel(that.ctx);

        let apiMethod = [
            headerModelCtx.requestHeaderData(channel),
            brandsModelCtx.getBrandViewTop(channel),
            brandsModelCtx.getBrandViewList(channel) // 分屏加载
        ];

        return Promise.all(apiMethod).then(result => {
            let responseData = {
                module: 'brands',
                page: 'brands',
                noCashe: false
            };

            // 头部数据
            Object.assign(responseData, result[0]);

            // 品牌一览列表
            responseData.brands = result[1];
            responseData.brands.navigation = result[2].navigation;
            responseData.brands.category = result[2];

            // 导航pathNav
            responseData.brands.pathNav = [
                {
                    href: that.getHomeurlByChannel(channel),
                    name: `${_.toUpper(channel)}首页`,
                    pathTitle: 'YOHO!BUY 有货'
                },
                {
                    href: helpers.urlFormat('/brands'),
                    name: '品牌一览',
                    pathTitle: '品牌一览'
                }
            ];

            // SEO
            Object.assign(responseData, that.seoMap()[channel]);

            // 数据出错不cashe
            if (_.isEmpty(result[0].headerData) || _.isEmpty(result[1]) || _.isEmpty(result[2])) {
                responseData.noCashe = true;
            }
            return responseData;
        });
    }

    /**
     * brandList-Ajax调用
     */
    getBrandList(channel, start) {
        let that = this;
        let brandsModelCtx = new BrandsModel(that.ctx);

        let apiMethod = [
            brandsModelCtx.getBrandViewList(channel, start)
        ];

        return Promise.all(apiMethod).then(result => {
            let responseData = {};

            // 品牌一览列表
            responseData.category = result[0];
            return responseData;
        });
    }

    /**
     * 品牌接口数据
     *
     * @param string brandId 获取品牌ID
     * @return json
     */
    brandInfo(brandId, uid) {
        let that = this;
        let brandsModelCtx = new BrandsModel(that.ctx);

        let apiMethod = [
            brandsModelCtx.getBrandInfo(brandId, uid)
        ];

        return Promise.all(apiMethod).then(result => {
            let responseData = {
                code: _.isEmpty(result[0]) ? 400 : 200,
                brand: _.isEmpty(result[0]) ? '' : result[0],
                noCashe: false
            };

            // 数据出错不cashe
            if (_.isEmpty(result[0])) {
                responseData.noCashe = true;
            }
            return responseData;
        });
    }

    /**
     * 多个品牌ID获取品牌信息
     *
     * @param array $brandIds
     * @return array
     */
    plusstarList(channel, req) {
        let that = this;
        let brandsModelCtx = new BrandsModel(that.ctx);

        return co(function*() {
            let headerModelCtx = new HeaderModel(that.ctx);
            let headerData = yield headerModelCtx.requestHeaderData(channel, true);

            let responseData = {
                module: 'brands',
                page: 'brands',
                brandsHomePage: true,
                noCashe: false
            };

            let id = req.query.id || '',
                gender = req.query.gender || that.getGenderByChannel(channel),
                limit = 20,
                page = parseInt(req.query.page, 10) || 1;

            let items = yield brandsModelCtx.getPlusstarBrandListItem(channel);

            let brandType = 1;

            if (+id !== 0 && items[id]) {
                brandType = items[id].brandType;
                responseData.plusstarTitle = items[id].name;
            }
            let plustarList = yield brandsModelCtx.getPlustarList(brandType, gender);

            let list = plustarList.data;

            // 数据出错不cashe
            if (_.isEmpty(headerData.headerData) || _.isEmpty(items) || _.isEmpty(list)) {
                responseData.noCashe = true;
            }
            let brandIds = [],
                brands = [],
                pageList = {};

            brandIds = plustarList.brandsIds.slice((page - 1) * limit, page * limit);
            if (plustarList.brandsIds.length > limit) {
                pageList = pager(Math.ceil(plustarList.brandsIds.length / limit), {
                    page: page,
                    id: id
                });

                // url 伪静态
                _.forEach(pageList.pages, (val) => {
                    val.url = that.staticUrl(`/${channel}-brands/plusstar/`, val.url);
                });
                if (pageList.nextPage) {
                    pageList.nextPage.url = that.staticUrl(`/${channel}-brands/plusstar/`, pageList.nextPage.url);
                }
                if (pageList.prePage) {
                    pageList.prePage.url = that.staticUrl(`/${channel}-brands/plusstar/`, pageList.prePage.url);
                }
            }

            if (brandIds.length > 0) {
                // 获取品牌信息
                let brandsInfo = yield brandsModelCtx.getBrandInfoByIds(brandIds);

                _.forEach(brandIds, brandId => {
                    if (brandsInfo[brandId]) {
                        list[brandId].desc = brandsInfo[brandId].desc;

                        list[brandId].url = helpers.urlFormat(`/shop/${brandsInfo[brandId].brand_domain}-.html`);
                    }
                    brands.push(list[brandId]);
                });

                // 数据出错不cashe
                if (_.isEmpty(brandsInfo)) {
                    responseData.noCashe = true;
                }
            }
            let data = {
                brandsHomePage: true,
                brands: {
                    items: brands,
                    tabs: items

                }
            };

            // 头部数据
            Object.assign(responseData, headerData);

            // 产品信息
            Object.assign(responseData, data);

            // 页码
            Object.assign(responseData, pageList);

            let seomap = that.seoMap();

            // SEO
            Object.assign(responseData, seomap[`plusstar-id${id}`] || seomap[`${channel}-plusstar`] || {});
            return responseData;
        })();

    }
};