favorite.js 6.11 KB
/**
* 个人中心我的收藏
* @author: zxr<xiaoru.zhang@yoho.cn>
* @date: 2016/08/16
*/

'use strict';

const logger = global.yoho.logger;
const api = global.yoho.API;

// const serviceAPI = global.yoho.ServiceAPI;
const camelCase = global.yoho.camelCase;
const _ = require('lodash');
const config = global.yoho.config;
const helpers = global.yoho.helpers;

const favProduct = (uid, page, limit) => {

    return api.get('', {
        method: 'app.favorite.product',
        uid: uid,
        page: page,
        limit: limit
    }).then((result) => {

        let resu = {
            hasFavProduct: []
        };

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

            let list = camelCase(result);

            if (page > 1 && list === []) {
                resu.end = true;
            }

            if (list.data.total === 0) {
                resu.total = 0;
            }

            if (page <= list.data.pageTotal) {

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

                    // if (empty(val.productSkn)) {
                    //     continue;
                    // }

                    if (val.goodsId && val.cnAlphabet) {
                        obj = _.assign(obj, {
                            link: config.siteUrl + '/product/pro_' +
                            val.productId + '_' + val.goodsId + '/' +
                            val.cnAlphabet + '.html'
                        });
                    } else {
                        obj = _.assign(obj, {
                            link: ''
                        });
                    }

                    if (val.image) {
                        obj = _.assign(obj, {
                            imgUrl: val.image
                        });
                    } else {
                        obj = _.assign(obj, {
                            imgUrl: ''
                        });
                    }

                    if (val.marketPrice - val.salesPrice > 0) {
                        obj = _.assign(obj, {
                            discountPrice: '¥' + Number(val.salesPrice).toFixed(2),
                        });
                    }

                    if (val.priceDown > 0) {
                        obj = _.assign(obj, {
                            savePrice: '¥' + Number(val.priceDown).toFixed(2),
                        });
                    } else {
                        obj = _.assign(obj, {
                            savePrice: false
                        });
                    }

                    if (val.storage <= 0) {
                        obj = _.assign(obj, {
                            sellOut: true
                        });
                    }

                    obj = _.assign(obj, {
                        favId: val.productId,
                        title: val.productName,
                        price: '¥' + Number(val.marketPrice).toFixed(2),
                        invalidGoods: val.status === 0
                    });

                    resu.hasFavProduct.push(obj);
                });

            } else {
                resu.more = true;
            }

            return resu;
        } else {
            logger.error('收藏商品 cood 不是 200');
            return {
                total: 0,
            };
        }
    });
};

const favfavBrand = (uid, page, limit) => {

    return api.get('', {
        method: 'app.favorite.brand',
        uid: uid,
        page: page,
        limit: limit
    }).then((result) => {

        let resu = {
            hasFavBrand: []
        };

        if (result && result.code === 200) {
            let list = camelCase(result);

            if (page > 1 && list === []) {
                resu.end = true;
            }

            if (list.data.total === 0) {
                resu.total = 0;
            }

            if (page <= list.data.pageTotal) {

                _.forEach(list.data.brandList, function(val) {
                    let obj = {
                        productList: []
                    };

                    // if (empty(val.productSkn)) {
                    //     continue;
                    // }
                    if (val.brandOrShopType === 'brandOrShopType') {
                        obj = _.assign(obj, {
                            link: helpers.urlFormat('/product/index/brand', {
                                shop_id: val.shopId
                            })
                        });
                    } else {
                        obj = _.assign(obj, {
                            link: helpers.urlFormat('', {}, val.brandDomain)

                        });
                    }

                    obj = _.assign(obj, {
                        id: val.brandId,
                        brandName: val.brandName,
                        updata: val.newProductNum,
                        discount: val.productDiscountNum,
                        brandImg: val.brandIco,
                        update: val.newProductNum
                    });

                    _.forEach(val.newProduct, function(data) {
                        obj.productList.push({
                            link: '/product/pro_' + data.productId + '_' +
                                  data.goods[0].id + '/' + data.cnAlphabet + '.html',
                            imgUrl: data.defaultImages,
                            price: '¥' + Number(data.marketPrice).toFixed(2),
                            discount: data.marketPrice > data.salesPrice ? '¥' + Number(data.salesPrice).toFixed(2) : false,
                        });
                    });

                    resu.hasFavBrand.push(obj);
                });

            } else {
                resu.more = true;
            }

            return resu;
        } else {
            logger.error('收藏品牌 cood 不是 200');
            return {
                total: 0,
            };
        }
    });
};

const favoriteDelete = (uid, type, favId) => {

    return api.get('', {
        method: 'app.favorite.cancel',
        uid: uid,
        type: type,
        fav_id: favId
    });
};

module.exports = {
    favProduct,
    favfavBrand,
    favoriteDelete
};