brand-fav.js 3.59 KB
var $ = require('yoho-jquery');

exports.getFavNum = function(sid, bid) {
    return $.getJSON('/product/index/favnum', {sid: sid, bid: bid})
        .then(function(result) {
            if (result.code === 200) {
                return result.data.count;
            } else {
                return $.Deferred().reject().promise();// eslint-disable-line
            }
        });
};

exports.addFav = function(sid, bid) {
    if (!getUid()) {// eslint-disable-line
        return $.Deferred().reject(window.jumpUrl(window.signinUrl())).promise(); // eslint-disable-line
    }

    if (sid) {
        return $.post('/product/shop/togglecollect', {
            isFavorite: 0,
            shopId: sid
        }).then(function(result) {
            if (result.code === 200) {
                return $.Deferred().resolve().promise();  //eslint-disable-line
            } if (result.code === 401) {
                return window.jumpUrl(window.signinUrl());
            } else {
                return $.Deferred().reject().promise();//eslint-disable-line
            }
        });
    }

    if (bid) {
        return $.post('/product/index/favoriteBrand', {
            type: 'add',
            brandId: bid
        }).then(function(result) {
            if (result.code === 200) {
                return $.Deferred().resolve().promise();//eslint-disable-line
            } else if (result.code === 403) {
                return window.jumpUrl(window.signinUrl());
            } else {
                return $.Deferred().reject().promise();//eslint-disable-line
            }
        });
    }
};

exports.cancelFav = function(sid, bid) {
    if (!getUid()) {// eslint-disable-line
        return $.Deferred().reject(window.jumpUrl(window.signinUrl())).promise();// eslint-disable-line
    }

    if (sid) {
        return $.post('/product/shop/togglecollect', {
            isFavorite: 1,
            shopId: sid
        }).then(function(result) {
            if (result.code === 200) {
                return $.Deferred().resolve().promise();//eslint-disable-line
            }
            if (result.code === 401) {
                return window.jumpUrl(window.signinUrl());
            } else {
                return $.Deferred().reject().promise();//eslint-disable-line
            }
        });
    }

    if (bid) {
        return $.post('/product/index/favoriteBrand', {
            type: 'cancel',
            brandId: bid
        }).then(function(result) {
            if (result.code === 200) {
                return $.Deferred().resolve().promise();//eslint-disable-line
            } else if (result.code === 403) {
                return window.jumpUrl(window.signinUrl());
            } else {
                return $.Deferred().reject().promise();//eslint-disable-line
            }
        });
    }
};

exports.statusFav = function(sid, bid) {
    if (sid) {
        return $.post('/product/index/isFavoriteShop', {
            shopId: sid
        }).then(function(result) {
            if (result.code === 200) {
                return $.Deferred().resolve().promise();//eslint-disable-line
            } else {
                return $.Deferred().reject().promise();//eslint-disable-line
            }
        });
    }

    if (bid) {
        return $.getJSON('/product/index/isfav', {
            brandId: bid
        }).then(function(result) {
            if (result.code === 200 && result.data) {
                return $.Deferred().resolve().promise();//eslint-disable-line
            } else {
                return $.Deferred().reject().promise();//eslint-disable-line
            }
        });
    }

    return $.Deferred().reject().promise();
};