favorite.js 7.52 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;

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

    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) {
                    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 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,
                            link: helpers.urlFormat('/product/shop', {
                                shop_id: val.shopId
                            })
                        });

                        _.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 api.get('', {
            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;