feature.js 7.54 KB
'use strict';
const _ = require('lodash');
const api = global.yoho.API;
const helpers = global.yoho.helpers;
const yasProcess = require('../../../utils/yas-process');

class featureModel extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    _getProductBySkns(productObj, ctx) {
        return this.get({
            data: {
                productSkn: productObj.defaultSkns,
                method: 'h5.product.batch'
            },
            param: {
                cache: true
            }
        }).then((result) => {
            productObj.defaultPros = [];
            if (result && result.data && result.data.product_list && result.code === 200) {
                result.data.product_list.forEach(function(val) {
                    var obj = {
                        producturl: `//m.yohobuy.com/product/${val.product_skn}.html?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${val.product_skn}","from_page_name":"${yasProcess.getPname(ctx.req)}","from_page_param":"${_.get(ctx,'req.url', '')}"}}`, // eslint-disable-line
                        productimg: helpers.image(val.default_images, 213, 284, 2, 60).replace('quality/80', 'quality/60'), // eslint-disable-line
                        productname: val.product_name,
                        vipprice: val.vip_price,
                        saleprice: val.sales_price,
                        marketprice: val.sales_price === val.market_price ? '' : val.market_price,
                        brandname: val.brand_name,
                        product_skn: val.product_skn
                    };

                    if (val.shop_id) {
                        obj.brandurl = `//m.yohobuy.com/product/shop?domain=${val.brand_domain}&openby:yohobuy={"action":"go.shop","params":{"shop_id":${val.shop_id},"shop_template_type":${val.shop_template_type || "1"},"is_red_shop":${val.is_red_shop || 1}}}`; // eslint-disable-line
                    } else {
                        obj.brandurl = `//m.yohobuy.com/product/shop?domain=${val.brand_domain}&openby:yohobuy={"action":"go.brand","params":{"brand_id":${val.brand_id}}}`; // eslint-disable-line
                    }

                    productObj.defaultPros.push(obj);
                });
            }
        });
    }

    /**
     * 获取店铺组店铺数据
     */
    _getShopGroup(shopRawData) {
        // 个性化店铺组,前端去查询
        if (shopRawData.searchCondition) {
            shopRawData.individuation = true;
            return Promise.resolve();
        }

        // 固定店铺组
        return this.get({
            data: {
                method: 'app.shops.batchGetShops',
                shop_ids: shopRawData.defaultShopIds
            }
        }).then(result => {
            let renderData = _.get(result, 'data', []);

            _.forEach(renderData, shop => {
                let displayStyle = _.get(shopRawData, 'displayStyle', '0');

                shop.picture = displayStyle === '0' ? shop.pic_popular : shop.shop_logo;
                shop.href = `//m.yohobuy.com/product/shop?domain=${shop.shop_domain}&openby:yohobuy={"action":"go.shop","params":{"shop_id":${shop.shops_id},"shop_template_type":${shop.shop_template_type || "1"},"is_red_shop":${shop.is_red_shop || 1}}}`; // eslint-disable-line
            });

            shopRawData.renderData = renderData;
        });
    }

    index(params) {
        const self = this;

        return Promise.coroutine(function*() {
            if (!params.code) {
                return Promise.resolve({});
            }

            let data,
                sknsArr = [];
            let shopGroups = [];

            if (params.type === 'preview') { // 开发/预览模式
                data = yield api.get('', {
                    method: 'app.activity.template.ignoreCache',
                    activity_id: params.code
                });
            } else {
                data = yield api.get('', { // 生产模式
                    method: 'app.activity.template',
                    activity_id: params.code
                }, {
                    cache: true
                });
            }
            if (!data) {
                return;
            }
            data = data.data;
            if (data && data.floors) {
                data.floors.forEach(f => {
                    if (f.component && f.component[0] &&
                        f.component[0].type === 'productGroup' && f.component[0].defaultSkns) {
                        sknsArr.push(self._getProductBySkns(f.component[0], self.ctx));

                        f.component[0].isStyle2 = _.get(f, 'component[0].newStyle') === '2' && _.get(f, 'component[0].numOfOneRow') === '2';// eslint-disable-line
                        f.component[0].newStyle = _.get(f, 'component[0].newStyle') === '1';
                    }

                    if (f.component && f.component[0] &&
                        f.component[0].type === 'productGroup' && f.component[0].favourite_prds_enable === '1') {
                        f.component[0].searchCondition = Object.assign({
                            maybeLike: '1',
                            limit: '60'
                        }, f.component[0].searchCondition || {});

                        f.component[0].isStyle2 = _.get(f, 'component[0].newStyle') === '2' && _.get(f, 'component[0].numOfOneRow') === '2';// eslint-disable-line
                        f.component[0].newStyle = _.get(f, 'component[0].newStyle') === '1';
                    }

                    // 新增店铺组
                    if (_.get(f, 'component[0].type') === 'shopGroup') {
                        shopGroups.push(self._getShopGroup(f.component[0]));
                    }

                    if (_.get(f, 'type') === 'bottombar') {
                        f.height = _.get(f, 'param.height') / 20;
                    }

                    _.forEach(f.component, component => {
                        if (component.url && component.url.indexOf('go.productDetail') !== -1) {

                            component.url = yasProcess.addParamsToGoodsHref({
                                href: component.url,
                                fromPageName: yasProcess.getPname(_.get(self, 'ctx.req', {})),
                                fromPageParam: _.get(self, 'ctx.req.path', '')
                            });
                        }

                        if (component.persenal_enable === '1') {
                            f.hide = true; // 个性化券楼层先不展示,前端异步查询到的时候,再展示
                        }
                    });
                });
            }

            if (sknsArr.length) {
                yield Promise.all(sknsArr);
            }

            if (shopGroups.length) {
                yield Promise.all(shopGroups);
            }

            return data;
        })();
    }

    /**
     * 领取优惠券
     */
    couponSend(uid, token) {
        let data = {
            method: 'app.coupons.couponsSend',
            uid: uid,
            coupon_send_token: token
        };

        if (/:/.test(token)) {
            let [couponId, personCouponId] = token.split(':');

            data = {
                method: 'app.promotion.getACoupon',
                uid: uid,
                couponId: couponId,
                personCouponId: personCouponId
            };
        }

        return this.get({data}).then(result => {

            if (!result) {
                result.code = 404;
                result.message = '出错啦~';
            }

            return result;
        });
    }

}

module.exports = featureModel;