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

const Promise = require('bluebird');
const co = Promise.coroutine;
const api = global.yoho.API;
const headerModel = require('../../../doraemon/models/header');
const brandsModel = require('./brands-model');
const _ = require('lodash');
const helpers = global.yoho.helpers;

/**
 * 获取品牌一览list
 * @param string $channel 频道名称
 * @param int start  开始位置 1 开始
 * @param int length 取数长度 0 取到最后
 */
exports.getBrandViewList = (channel) => {
    let apiMethod = [
        headerModel.requestHeaderData(channel),
        brandsModel.getBrandViewTop(channel),
        brandsModel.getBrandViewList(channel)
    ];

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

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

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

/**
 * brandList-Ajax调用
 */
exports.getBrandList = (channel, start) => {
    let apiMethod = [
        brandsModel.getBrandViewList(channel, start)
    ];

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

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

/**
 * 品牌接口数据
 *
 * @param string brandId 获取品牌ID
 * @return json
 */
exports.brandInfo = (brandId, uid) => {
    let apiMethod = [
        brandsModel.getBrandInfo(brandId, uid)
    ];

    return api.all(apiMethod).then(result => {

        return {
            code: _.isEmpty(result[0]) ? 400 : 200,
            brand: _.isEmpty(result[0]) ? '' : result[0]
        };
    });
};

/**
 * 多个品牌ID获取品牌信息
 *
 * @param array $brandIds
 * @return array
 */
exports.plusstarList = (channel, id) => {
    return co(function*() {
        let headerData = yield headerModel.requestHeaderData(channel);

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

        let items = yield brandsModel.getPlusstarBrandListItem(channel);

        let brandType = 1;

        if (items[id]) {
            brandType = items[id].brandType;
        }
        let plustarList = yield brandsModel.getPlustarList(brandType, '');

        let list = plustarList.data;

        let brandIds = [],
            brands = [];

        brandIds = plustarList.brandsIds.slice(0, 20);
        if (brandIds.length > 0) {
            //获取品牌信息
            let brandsInfo = yield brandsModel.getBrandInfoByIds(brandIds);

            _.forEach(brandIds, brandId => {
                if (brandsInfo[brandId]) {
                    list[brandId].desc = brandsInfo[brandId].desc;
                    list[brandId].url = helpers.urlFormat(brandsInfo[brandId].url + '?gender=' + '');
                }
                brands.push(list[brandId]);
            })
        }

        let data = {
            brandsHomePage: true,
            brands: {
                items: brands,
                tabs: items
            }
        };

        // 头部数据
        Object.assign(responseData, headerData);
        // 产品信息
        Object.assign(responseData, data);

        return responseData;
    })();

};