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

'use strict';

const logger = global.yoho.logger;
const camelCase = global.yoho.camelCase;
const _ = require('lodash');
const config = global.yoho.config;
const helpers = global.yoho.helpers;

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

    favProduct(uid, page, limit) {

        return this.get({data: {
            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) {
                    list.data.productList = _.filter(list.data.productList, function(n) {
                        return n.productSkn > 0;
                    });
                    _.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/' + val.productSkn + '.html' // 商品url改版
                            });
                        } 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('fav goods code no 200');
                return {
                    total: 0
                };
            }
        });
    }

    favfavBrand(uid, page, limit) {
        return this.get({data: {
            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: []
                        };
                        let shopOrBrandLink;

                        if (val.brandOrShopType === 'brand') {
                            shopOrBrandLink = helpers.urlFormat('/product/shop', {
                                brand_id: val.brandId
                            });
                        } else {
                            shopOrBrandLink = helpers.urlFormat('/product/shop', {
                                shop_id: val.shopId
                            });
                        }

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

                        _.forEach(val.newProduct, function(data, key) {
                            obj.productList.push({
                                link: '/product/' + data.productSkn + '.html', // 商品url改版
                                imgUrl: data.defaultImages,
                                price: '¥' + Number(data.marketPrice).toFixed(2),
                                discount: data.marketPrice > data.salesPrice ? '¥' +
                                Number(data.salesPrice).toFixed(2) : false,
                                top3: key < 3 ? 1 : 0
                            });
                        });

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

                } else {
                    resu.more = true;
                }

                return resu;
            } else {
                logger.error('fav brand code no 200');
                return {
                    total: 0
                };
            }
        });
    }

    favoriteDelete(uid, type, favId) {

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

    index(uid, page, limit, isbrand) {
        if (isbrand) {
            return this.favfavBrand(uid, page, limit).then(result=> {
                if (result.total === 0) {
                    result = {nobrandData: 1};
                }
                return result;
            });
        } else {
            return this.favProduct(uid, page, limit).then(result=> {
                if (result.total === 0) {
                    result = {noproductData: 1};
                }
                return result;
            });
        }
    }
}

module.exports = favoriteIndexModel;