favorite.js 3.37 KB
'use strict';
const api = global.yoho.API;
const helpers = global.yoho.helpers;

/**
 * 处理用户收藏的商品数据
 *
 * @param int uid 用户ID
 * @param int page 第几页
 * @param int limit 限制读取的数目,默认10
 * @return array 处理之后的收藏的商品数据
 */
exports.getFavProductData = (uid, page, limit) => {
    return api.get('', {
        method: 'app.favorite.product',
        uid: uid,
        page: page,
        limit: limit
    }).then(result => {
        var isend = true,
            list = [],
            data = result.data;

        if (data && page <= data.page_total) {
            data.product_list.forEach(function(d) {
                if (!d.product_skn) {
                    return;
                }

                let link = '';
                let discountPrice = false;

                if (d.goodsId && d.cnAlphabet) {
                    link = helpers.urlFormat(`/product/pro_${d.product_id}_${d.goodsId}/${d.cnAlphabet}.html`);
                }

                if (Number(d.market_price) - Number(d.sales_price) > 0) {
                    discountPrice = '¥' + Number(Math.max(d.sales_price, 0)).toFixed(2);
                }

                list.push({
                    fav_id: d.product_id,
                    link: link,
                    imgUrl: d.image ? helpers.image(d.image) : '',
                    title: d.product_name,
                    price: '¥' + Number(Math.max(d.market_price, 0)).toFixed(2),
                    discountPrice: discountPrice,
                    sellOut: d.storage <= 0,
                    invalidGoods: d.status === 0
                });
            });

            if (page < data.page_total) {
                isend = false;
            }
        }
        return {
            isend: isend,
            list: list
        };
    });
};

/**
 * 处理用户收藏的品牌数据
 *
 * @param int uid 用户ID
 * @param string gender 性别 1,3表示男,2,3表示女,1,2,3表示全部
 * @param int page 第几页
 * @param int limit 限制读取的数目
 * @return array 处理之后的收藏的品牌数据
 */
exports.getFavBrandData = (uid, gender, page, limit) => {
    return api.get('', {
        method: 'app.favorite.brand',
        uid: uid,
        gender: gender,
        page: page,
        limit: limit
    }).then(result => {
        var isend = true,
            list = [],
            data = result.data;

        if (data && page <= data.page_total) {
            data.brand_list.forEach(function(d) {
                list.push({
                    fav_id: d.brand_id,
                    link: '', // todo
                    imgUrl: d.brand_ico ? helpers.image(d.brand_ico, 160, 125) : '',
                    brandName: d.brand_name,
                    down: d.status === 0
                });
            });

            if (page < data.page_total) {
                isend = false;
            }
        }

        return {
            isend: isend,
            list: list
        };
    });
};

/**
 * 取消收藏的商品/品牌数据
 *
 * @param int uid 用户ID
 * @param int favId 要取消的收藏id
 * @param string type 取消类型(brand:品牌,product:商品)
 * @return array 接口返回的数据
 */
exports.favoriteDelete = (uid, favId, type) => {
    return api.get('', {
        method: 'app.favorite.cancel',
        uid: uid,
        type: type,
        fav_id: favId
    });
};