favorite-api.js 2.58 KB
'use strict';

const api = global.yoho.API;
const service = global.yoho.ServiceAPI;
const _ = require('lodash');

const URL_PRODUCT_FAVORITE = 'shops/service/v1/favorite/';
const URL_ARTICLE_FAVORITE = '/guang/api/v1/favorite/';
const URL_ARTICLE_FAVORITE_BRAND = '/guang/service/v2/favorite/toggleBrand';

/**
 * 根据uid和商品的id查询是否被用户收藏
 * @param int $uid
 * @param int $productId
 * @param boolean $isOnlyUrl 是否指返回链接
 * @return boolean 收藏 true 未收藏 false
 */
const getUidProductFav = (uid, productId, isOnlyUrl)=> {
    isOnlyUrl = (isOnlyUrl === undefined) ? false : isOnlyUrl;
    let options = {
        method: 'web.favorite.isFavorite',
        id: productId,
        uid: uid,
        type: 'product'
    };

    return api.get('', options);
};

const favoriteArticleData = (uid, udid, page, limit)=> {
    let options = {
        uid: uid,
        udid: udid,
        page: page,
        limit: limit
    };

    return service.get(URL_ARTICLE_FAVORITE + 'getUserFavArticleList', options);
};

const cancelArticle = (uid, id) => {
    return service.get(URL_ARTICLE_FAVORITE + 'cancelFavorite', {
        uid: uid,
        article_id: id
    });
};

const getFavoriteProductList = (uid, page, limit)=> {
    let options = {
        method: 'web.favorite.product',
        uid: uid,
        page: 0,
        limit: limit || 10
    };

    return api.get('', options);
};

const redutionCount = (uid)=> {
    return api.get('', {
        method: 'web.redution.count',
        uid: uid
    });
};

const favoriteBrandData = (uid, page, limit) => {
    return api.get('', {
        method: 'app.favorite.brand',
        uid: uid,
        page: page || 1,
        limit: limit || 10
    });
};

const redutionAdd = (uid, mobile, pid) => {
    return api.get('', {
        method: 'web.redution.add',
        uid: uid,
        mobile: mobile,
        productId: pid
    });
};

const redutionCancel = (uid, pid) => {
    return api.get('', {
        method: 'web.redution.cancel',
        uid: uid,
        productIds: pid
    });
};

const _cancel = (type, uid, ids) => {
    return api.get('', {
        method: 'web.favorite.cancel',
        favIds: ids,
        uid: uid,
        type: type
    });
};

module.exports = {
    getUidProductFav,
    getFavoriteProductList,
    favoriteArticleData,
    favoriteBrandData,
    redutionCount,
    redutionAdd,
    redutionCancel,
    cancel: {
        product: _.partial(_cancel, 'product', _, _),
        shop: _.partial(_cancel, 'shop', _, _),
        brand: _.partial(_cancel, 'brand', _, _),
        article: cancelArticle
    }
};