plustar.js 12.3 KB
'use strict';

const productNameProcess = require(`${global.utils}/product-name-process`);
const _ = require('lodash');
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
const htmlProcess = require(`${global.utils}/html-process`);
const imageProcess = require(`${global.utils}/image`);

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

    formaData(data, gender) {
        let build = [];

        _.forEach(data, function(val) {
            // 多张图
            if (val.data[1]) {
                let imgs = [];

                _.forEach(val.data, function(list) {
                    let obj = {};

                    obj = _.assign(obj, {
                        url: '/guang/plustar/brandinfo?id=' + val.id + '&gender=' + gender,
                        img: list.src,
                        deps: val.brand_title
                    });

                    imgs.push(obj);
                });

                build.push({
                    imgs: imgs
                });
            }

            // 单张图
            if (val.data[0] && !val.data[1]) {
                _.forEach(val.data, function(list) {
                    build.push({
                        url: '/guang/plustar/brandinfo?id=' + val.id + '&gender=' + gender,
                        img: list.src,
                        deps: val.brand_title
                    });
                });
            }
        });

        return build;
    }

    getContentData(gender, type, channel, isRecommend) {
        let param = {
            gender: gender,
            brand_type: type,
            yh_channel: channel
        };

        if (isRecommend) {
            param.is_recommend = isRecommend;
        }

        return this.get({
            url: 'guang/api/v3/plustar/getlist',
            data: param,
            api: global.yoho.ServiceAPI,
            param: {cache: true}
        }).then((result) => {
            if (result && result.code === 200) {
                return this.formaData(result.data.data.list[0].data, gender);
            } else {
                logger.error('list data return code is not 200');
                return {};
            }
        });
    }

    getListData(gender, recom, all) {
        return Promise.all([this.getContentData(gender, recom), this.getContentData(gender, all)]).then((result) => {
            let ps = [];

            if (result[0]) {
                ps.push({
                    focus: true,
                    name: '设计师',
                    list: result[0]
                });
            }

            if (result[1]) {
                ps.push({
                    name: '经典潮牌',
                    list: result[1]
                });
            }
            return ps;
        });
    }

    getBrandsData(gender, starBrand, originalBrand, channel, isRecommend) {
        return Promise.all([
            this.getContentData(gender, starBrand, isRecommend, channel),
            this.getContentData(gender, originalBrand, isRecommend, channel)
        ]).then((result) => {
            let ps = [];

            if (result[1]) {
                ps.push({
                    focus: true,
                    name: '原创潮牌',
                    list: result[1]
                });
            }

            if (result[0]) {
                ps.push({
                    name: '明星潮品',
                    list: result[0]
                });
            }
            return ps;
        });
    }

    // 新品到着
    getNewProduct(brandId, gender, url, isApp) {

        return this.get({
            data: {
                method: 'app.search.li',
                brand: brandId,
                gender: gender,
                limit: 6,
                order: 's_t_desc',
                page: 1
            },
            api: global.yoho.API,
            param: {cache: true}
        }).then((result) => {

            if (result && result.code === 200) {

                let newArrival = {
                    naList: []
                };

                if (result.data.product_list) {
                    _.forEach(result.data.product_list, function(list, index) {
                        let obj = {};
                        let price;

                        if (index <= 5) {
                            let tag = [];

                            if (!list.product_skn) {
                                return false;
                            }

                            if (list.market_price === list.sales_price) {
                                list.market_price = false;
                            }
                            tag.push({
                                is_new: list.is_new === 'Y',
                                is_discount: list.is_discount === 'Y',
                                is_limited: list.is_limited === 'Y',
                                is_yohood: list.is_yohood === 'Y',
                                is_advance: list.is_advance === 'Y'
                            });

                            _.forEach(tag, function(data) {

                                if (data.is_discount === true && list.is_soon_soldOut === true) {
                                    data.is_new = true;
                                    data.is_discount = false;
                                } else if (data.is_discount === true &&
                                    (data.is_new === true || data.is_limited === true ||
                                        data.is_yohood === true || data.is_advance === true)) {
                                    data.is_discount = false;
                                } else if (data.is_yohood === true && data.is_new === true) {
                                    data.is_new = false;
                                }
                            });

                            if (list.market_price && list.market_price !== false) {
                                price = list.market_price + '.00';
                            }

                            newArrival.moreUrl = url;

                            if (list.cn_alphabet) {
                                list.cn_alphabet = productNameProcess(list.cn_alphabet);
                            }

                            let productUrl = '//m.yohobuy.com/product/' + list.product_skn + '.html'; // 商品url改版

                            if (isApp) {
                                productUrl += `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${list.product_skn}"}}`; // eslint-disable-line
                            }

                            obj = _.assign(obj, {
                                id: list.product_skn,
                                product_id: list.product_id,
                                thumb: helpers.image(list.default_images, 235, 314),
                                name: list.product_name,
                                salePrice: list.sales_price + '.00',
                                studentPrice: list.sales_price * 0.9,
                                is_soon_sold_out: list.is_soon_sold_out === 'Y',
                                isShowSaleTagDis: list.sales_price * 2 < list.market_price,
                                url: productUrl,
                                tags: tag
                            });

                            if (price) {
                                obj = _.assign(obj, {
                                    price: price
                                });
                            }

                            obj = _.assign(obj, {
                                similar: true
                            });

                            newArrival.naList.push(obj);

                        }
                    });


                }

                return newArrival;
            } else {
                logger.error('new data return code is not 200');
                return {};
            }
        });
    }

    // 相关资讯--不能加cache,否则点赞有缓存.
    getRelatedEditorial(brandId, uid, udid, isApp) {

        return this.get({
            url: 'guang/service/v1/article/getArticleByBrand',
            data: {
                brand_id: brandId,
                uid: uid,
                udid: udid,
                limit: 3
            },
            api: global.yoho.ServiceAPI
        }).then((result) => {
            if (result && result.code === 200) {

                let list = [];

                _.forEach(result.data, function(data) {
                    if (isApp) {
                        data.url = data.url + '';
                    }

                    list.push({
                        id: data.id,
                        url: isApp ? `${helpers.https(data.url)}&openby:yohobuy={"action":"go.h5","params":{"param":{"id":"${data.id}"},"shareparam":{"id":"${data.id}"},"share":"/guang/api/v1/share/guang","id":${data.id},"type":1,"url":"http:${helpers.urlFormat('/info/index', null, 'guang')}","islogin":"N"}}` : data.url, // eslint-disable-line
                        title: data.title,
                        text: data.intro,
                        img: helpers.image(data.src, 640, 640),
                        publishTime: data.publish_time,
                        like: data.like,
                        pageView: data.views_num
                    });
                });
                return list;
            } else {
                logger.error('editorial data return code is not 200');
                return {};
            }
        });
    }

    // 是否收藏
    isCollection(brandId, uid) {

        let param = {
            brandId: brandId
        };

        if (uid) {
            param.uid = uid;
        }

        return this.get({
            url: 'shops/service/v1/favorite/getUidBrandFav',
            data: param,
            api: global.yoho.ServiceAPI
        }).then((result) => {

            if (result && result.code === 200) {

                let isLike = true;

                return isLike;
            } else {
                let isLike = false;

                return isLike;
            }
        });
    }

    // 品牌详情
    getDetailData(id, uid, udid, gender, isApp) {

        return this.get({
            url: 'guang/api/v1/plustar/getbrandinfo',
            data: {
                id: id
            },
            api: global.yoho.ServiceAPI,
            param: {cache: true}
        }).then((result) => {
            if (result && result.code === 200) {
                let list = result.data || [];
                let jumpToApp;

                let url;

                let brandId = list.brand_id;
                let brandDomain = list.brand_domain;
                let isDifferent = list.is_different;

                if (isApp) {
                    url = '?openby:yohobuy={"action":"go.brand","params":{"brand_id":"' + brandId + '","is_different":"' + isDifferent + '"}}'; // eslint-disable-line
                    uid = parseInt(uid, 10);
                } else {
                    url = '//' + brandDomain + '.m.yohobuy.com';
                }

                if (isApp && !uid) {
                    jumpToApp = 1;
                }

                list.brand_ico = imageProcess.getSourceUrl(list.brand_ico, 'brandLogo');

                return this.getNewProduct(brandId, gender, url, isApp).then((data) => {

                    return _.assign(list, {
                        isLike: false,
                        newArrival: data,
                        jumpToApp: jumpToApp,
                        shareLink: '//guang.m.yohobuy.com/plustar/brandinfo?id=' + id,
                        shareTitle: list.brand_name,
                        shareImg: list.brand_ico,
                        shareDesc: htmlProcess.removeHtml(list.brand_intro)
                    });
                });

            } else {
                logger.error('detail data return code is not 200');
                return {};
            }
        });
    }

    /**
     * [品牌详情异步数据]
     */
    getDetailDataAsync(brandId, uid, udid, isApp) {
        return Promise.all([
            this.isCollection(brandId, uid),
            this.getRelatedEditorial(brandId, uid, udid, isApp)
        ]).then((result) => {
            return {
                isLike: result[0],
                infos: result[1]
            };
        });
    }
};