shopCollect.js 3.55 KB
/**
 * 店铺收藏
 * @author: zxr<xiaoru.zhang@yoho.cn>
 * @date: 2016/10/17
 */
'use strict';
const _ = require('lodash');
const logger = global.yoho.logger;
const serviceAPI = global.yoho.ServiceAPI;

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    shopList(uid, tabName, channelId) {
        let params = {
            method: 'app.shops.promote'
        };

        if (uid) {
            params.uid = uid;
        }

        if (tabName) {
            params.tab_name = tabName;
        }

        if (channelId) {
            params.channel_id = channelId;
        }

        return this.get({
            data: params,
            param: {
                code: 200
            }
        }).then((result) => {
            if (result && result.code === 200) {
                _.forEach(result.data, function(data) {
                    let href = '//m.yohobuy.com/product/shop?shop_id=' +
                        data.shopsId + '&openby:yohobuy={"action":"go.shop","params":{"shop_id":"' +
                    data.shopsId + '","shop_template_type":"' +
                        data.shopTemplateType + '","shop_name":"' + data.shopName + '"}}';

                    data.isFavorite = data.isFavorite === 'Y';

                    if (parseInt(data.collectionNum, 10) > 10000) {
                        data.collectionNum = (data.collectionNum / 10000).toFixed(1) + ' w';
                    }

                    data.href = href;
                });
                return result.data;
            } else {
                logger.error('shop list data return code is not 200');
                return {};
            }
        });
    }

    shopNav(channelId) {
        let params = {
            method: 'app.shops.promoteTabNameList'
        };

        if (channelId) {
            params.channel_id = channelId;
        }

        return this.get({
            data: params,
            param: {
                cache: true,
                code: 200
            }
        }).then((result) => {
            if (result.data) {
                if (result.data.length === 0 || (result.data.length === 1 && result.data[0] === 'NULL')) {
                    return false;
                } else {
                    return result.data;
                }
            }
        });
    }

    banner(contentCode) {
        return this.get({
            api: serviceAPI,
            url: 'operations/api/v5/resource/get',
            data: {
                content_code: contentCode,
                platform: 'iphone'
            },
            param: {
                cache: true,
                code: 200
            }
        }).then((result) => {
            if (result && result.data) {
                return result.data[0];
            }
        });
    }

    shopFav(uid, shopIds) {
        return this.get({
            data: {
                method: 'app.shops.promoteFavorite',
                shop_ids: shopIds,
                uid: uid
            },
            param: {
                code: 200
            }
        }).then((result) => {
            if (result.data) {
                return result.data;
            }
        });
    }

    /**
     * 根据店铺ID调获取店铺数据
     * 图片字段:pic_popular
        order 传 pools_id_asc 或者 pools_id_desc
     * @param {*} params
     */
    batchGetShops(params) {
        return this.get({
            data: {
                method: 'app.shops.batchGetShops',
                shop_ids: params.shop_ids,
                order: params.order
            }
        });
    }
};