favorite.js 13.6 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) => {
        // console.log(result)
        let resu = {
            hasFavProduct: []
        };

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

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

            if (page <= list.data.pageTotal) {
                let hasFavProduct = [];

                _.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/list/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: val.salesPrice
                        });
                    }

                    if (val.priceDown > 0) {
                        obj = _.assign(obj, {
                            savePrice: '¥' + val.priceDown
                        });
                    } 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: '¥' + val.marketPrice,
                        invalidGoods: val.status === 0
                    });

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

            } else {
                resu.push({
                   end: true 
                });
            }
            
            return resu.hasFavProduct;
        } else {
            logger.error('收藏商品 cood 不是 200');
        }
    });
};

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

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

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

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

            if (page <= list.data.pageTotal) {
                let brand = [];

                _.forEach(list.data.brandList, function(val) {
                    let obj = {
                       productList: [] 
                    };
                    let brandPro = [];
                    // 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
                    });

                    _.forEach(val.newProduct, function(data) {
                        obj.productList.push({
                            link: '/product/pro_' + data.productId + '_' + data.goods[0].id + '/' + data.cnAlphabet + '.html',
                            imgUrl: data.defaultImages,
                            price: '¥' + data.marketPrice
                        })

                        if (data.marketPrice > data.salesPrice) {
                            obj.productList.push({
                                discount: '¥' + data.salesPrice
                            })
                        }
                    })
                    
                    hasFavBrand.push(obj);
                });

                console.log(hasFavBrand)
                return hasFavBrand;
            } else {
                hasFavBrand.push({
                   end: true 
                });
            }
            
        } else {
            logger.error('收藏品牌 cood 不是 200');
        }
    });
}

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

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

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