shop.js 6.16 KB
/**
 * 店铺
 */
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const api = global.yoho.API;
const singleAPI = global.yoho.SingleAPI;
const stringProcess = require(`${global.utils}/string-process`);

/**
 * 频道
 * @type {{}}
 */
const yhChannel = {
    boys: 1,
    girls: 2,
    kids: 3,
    lifestyle: 4
};

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

    /**
     * 店铺品牌列表
     */
    getShopBrands(shopId) {
        return api.get('', {
            method: 'app.shops.getShopsBrands',
            shop_id: shopId
        }, {code: 200}).then(result => {
            if (result && result.data) {
                _.forEach(result.data, value => {
                    value.url = helpers.urlFormat('', {
                        shop_id: shopId,
                        brand: value.brand_id,
                        title: value.brand_name
                    }, 'list');
                });

                return result.data;
            } else {
                return [];
            }
        });
    }

    /**
     * 获取店铺信息
     * @param {int} shopId 店铺id
     * @param {int} uid 用户id 判断用户是否收藏店铺
     * @return array
     */
    getShopInfo(shopId, uid) {
        let finalParams = {
            method: 'app.shops.getIntro',
            shop_id: shopId,
        };

        if (!shopId || !stringProcess.isNumeric(shopId)) {
            return Promise.resolve({});
        }

        if (uid && uid !== 'undefined') {
            Object.assign(finalParams, {
                uid: uid
            });
        }

        return api.get('', finalParams);
    }

    /**
     * 根据品牌域名获取品牌LOGO
     * @param {string} domain 品牌域名
     * @return array | false
     */
    getBrandLogoByDomain(domain) {
        return api.get('', {
            method: 'web.brand.byDomain',
            domain: domain
        }, {
            cache: true
        }).then(result => {
            if (result && result.data) {
                let formatData = result.data;

                return {
                    id: formatData.id,
                    url: helpers.urlFormat('', null, formatData.brand_domain),
                    thumb: helpers.image(formatData.brand_ico, 75, 40),
                    name: formatData.brand_name,
                    shopId: formatData.shop_id ? formatData.shop_id : 0, // 店铺id
                    type: formatData.type ? formatData.type : 0,
                    brandDomain: formatData.brand_domain
                };
            } else {
                return false;
            }
        });
    }

    /**
     * 获取品牌信息数据
     * @param  {int} brandId 品牌ID
     * @return array         banner数据
     */
    getBrandIntro(brandId, uid) {
        let param = {
            uid: uid
        };

        if (!brandId || brandId === '' || brandId === 'undefined') {
            return Promise.resolve({});
        }

        return api.get('', _.assign({
            method: 'app.brand.getBrandIntro',
            brand_id: brandId
        }, param), {
            code: 200
        }).then(result => {
            if (result && result.data) {
                let list = result.data;

                return {
                    id: list.brand_id,
                    intro: list.brand_intro,
                    collected: list.is_favorite && list.is_favorite === 'Y',
                    title: list.brand_name ? list.brand_name : ''
                };
            } else {
                return false;
            }
        });
    }

    /**
     * 获取品牌banner数据
     * @param  {int} brandId 品牌ID
     * @return array         banner数据
     */
    getBrandBanner(brandId) {
        return api.get('', {
            method: 'app.brand.banner',
            brand_id: brandId
        }, {
            cache: true
        }).then((result) => {
            if (result && result.code === 200 && result.data) {
                if (result.data.banner) {
                    return helpers.image(result.data.banner, 640, 150);
                } else {
                    return '';
                }
            } else {
                return {};
            }
        });
    }

    /**
     *  获取红人店铺 banner
     *  doc: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/商品列表/店铺装修.md
     *  @param int shopId 店铺id
     */
    getBanner(shopId) {
        let params = {
            method: 'app.popular.shop.banner',
            shop_id: shopId
        };

        return api.get('', params, {cache: true, code: 200});
    }


    /**
     *  查询红人店铺对应的装修元素
     *
     *  @param int shopId 店铺id
     */
    getShopsdecorator(shopId) {
        let params = {
            method: 'app.popular.shopsdecorator',
            shop_id: shopId
        };

        return api.get('', params, {cache: true, code: 200});
    }

    /**
     * 获取店铺下面的所有分类
     * @param {int} shopId 店铺id
     * @param {string} channel 频道
     * @return array
     */
    getShopCategory(shopId, channel) {
        return api.get('', {
            method: 'app.shop.getSortInfo',
            yh_channel: yhChannel[channel],
            shop_id: shopId
        });
    }

    /**
     * 店铺收藏数量
     */
    favCount(shopId, uid, channel, udid) {
        let finalParams = {
            method: 'app.favorite.queryFavoriteCountByShopIds',
            favIds: shopId,
            type: 'shop',
            udid: udid,
            physical_channel: yhChannel[channel],
        };

        if (uid) {
            Object.assign(finalParams, {
                uid: uid
            });
        }

        return singleAPI.get('favorite', finalParams);
    }

    /**
     * 获取非红人店铺的店铺装修数据
     * @param {int} shopId 店铺id
     * @return array
     */
    getShopDecorator(shopId) {
        return api.get('', {
            method: 'app.shopsdecorator.getList',
            shop_id: shopId
        }, {
            cache: true,
            code: 200
        }).then((result) => {
            return (result && result.data) || {};
        });
    }
};