recom.js 2.71 KB
'use strict';

const api = global.yoho.API;
const _ = require('lodash');
const camelCase = global.yoho.camelCase;
const logger = global.yoho.logger;

const formatProduct = (list) => {

    list = list || [];
    list = camelCase(list);

    _.forEach(list, function(val) {
        let tag = [];

        if (val.isSoonSoldOut) {
            val.isSoonSoldOut = val.isSoonSoldOut === 'Y';
        }

        val.url = '/product/pro_' + val.productId + '_' + val.goodsList[0].goodsId +
                    '/' + val.cnAlphabet + '.html';

        tag.push({
            isNew: val.isNew === 'Y',
            isDiscount: val.isDiscount === 'Y',
            isLimited: val.isLimited === 'Y',
            isYohood: val.isYohood === 'Y',
            isAdvance: val.isAdvance === 'Y'
        });

        _.forEach(tag, function(data) {

            if (data.isDiscount === true && val.isSoonSoldOut === true) {
                data.isNew = true;
                data.isDiscount = false;
            } else if (data.isDiscount === true && (data.isNew === true || data.isLimited === true || data.isYohood === true || data.isAdvance === true)) {
                data.isDiscount = false;
            } else if (data.isYohood === true && data.isNew === true) {
                data.isNew = false;
            }
        });

        val.tags = tag;
    });


    return list;
};

const mayLike = (uid, page, limit, gender, udid, recPos, channel) => {
    return api.get('', {
        method: 'app.search.newLast7day',
        uid: uid,
        page: page,
        limit: limit,
        udid: udid,
        rec_pos: recPos,
        yh_channel: channel,
        gender: gender
    }).then((result) => {

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

            if (result.data.page_total && page <= result.data.page_total) {
                if (result.data.product_list) {

                    return formatProduct(result.data.product_list);

                } else {
                    return false;
                }
            } else {
                return false;
            }

        } else {
            logger.error('mayLike cood 不是 200');
        }
    });
};

const mayLikeKids = (page, limit, channel) => {
    return api.get('', {
        method: 'app.search.kids',
        page: page,
        limit: limit,
        yh_channel: channel
    }).then((result) => {

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

            if (page > result.data.page_total) {
                return;
            }

            if (result.data.product_list) {

                return formatProduct(result.data.product_list);

            }
        } else {
            logger.error('mayLikeKids cood 不是 200');
        }
    });
};

module.exports = {
    mayLike,
    mayLikeKids
};