shop-service.js 1.44 KB
const _ = require('lodash');
const ShopModel = require('./shop-api');

const {CHANNELS} = require('./vars');
const fs = require('fs');

const BRAND_TYPE = {
    shop: 2
};

function getShopUrl(domain, shopId, mip) {
    if (mip) {
        return `/mip/shop/${domain}-${shopId}.html`;
    }

    return `/shop/${domain}-${shopId}.html`;
}

function indexUrl(type, page) {
    return `/sitemap_${type}/shop_${page}.xml`;
}

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

    async _getShopUrl(isMip) {
        let shopUrls = [];

        await Promise.map(Object.values(CHANNELS), async (channel) => {
            let {data: {all_list}} = await this.shopModel.getBrandListData(channel);

            _.forEach(all_list, shops => {
                _.forEach(shops, shop => {

                    if (_.has(shop, 'shop_id')) {
                        let shopId = shop.shop_id;
                        let domain = shop.brand_domain || 'default_domain';

                        shopUrls.push(getShopUrl(domain.toLowerCase(), shopId, isMip));
                    }
                });
            });
        }, {concurrency: 1});

        return shopUrls;
    }

    async getUrls() {
        return this._getShopUrl();
    }

    async getMipUrls() {
        return this._getShopUrl(true);
    }

    async getIndex(type) {
        return [indexUrl(type, 1)];
    }
};