index.js 4.86 KB
'use strict';

const ROOT_PATH = global.ROOT_PATH;
const _ = require('lodash');
const fs = require('fs');  
const moment = require('moment');
const helpers = global.yoho.helpers;
const goodsHbs = require(`${ROOT_PATH}/hbs/partials/seo/index.hbs`);
const STEP = 5;

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

    writerGoodsXml(params) {
        let page = ((params.page || 1) - 1) * STEP + 1;
        let fileName  = `goods-${page}.xml`;

        return this.searchGoodsHandle(params, []).then(result => {
            if (result.length <= 0) {
                return {code: 400, data: ''};
            }

            const fWrite = fs.createWriteStream(`${ROOT_PATH}/public/dist/${fileName}`);

            fWrite.write(goodsHbs({products: result}));

            return {code: 200, data: `http://127.0.0.1:6005/dist/${fileName}`};
        });
    }

    searchGoodsHandle(params, products) {

        return this.searchList(params, products).then(rdata => {
            let productLists = _.get(rdata, 'data.product_list', []);

            if (productLists.length <= 0) {
                return products;
            }

            _.each(productLists, item => {
                let images = [];

                _.each(item.goods_list, goods => {
                    images.push({
                        contentUrl: helpers.image(goods.images_url, 450, 600),
                        height: 600,
                        width: 450,
                        description: `${item.product_name}-${goods.color_name}`,
                        tag: '',
                        name: goods.color_name
                    });
                });

                products.push({
                    loc: 'https://www.yohobuy.com',
                    lastmod: moment.unix(item.edit_time).format('YYYY-MM-DD'),
                    changefreq: 'weekly',
                    priority: 1.0,
                    fromSrc: 'YOHO!BUY有货',
                    images: images,
                    name: item.product_name,
                    brand: item.brand_name,
                    offers: {
                        price: item.sales_price,
                        administrativeAreaLv2: '全国',
                        wapUrl: `https://m.yohobuy.com/product/${item.product_skn}.html`,
                        url: `https://www.yohobuy.com/product/${item.product_skn}.html`,
                        eligibleTransactionVolume: item.sales_num,
                        seller_name: item.shop_name,
                        ratingValue: '', // 总评分
                        favorableRating: '', // 好评率
                        onShelfTime: '', // 上架时间
                        offShelfTime: '', // 下架时间
                        type: '自营', // 商品属性,是否自营
                        isOverseas: item.is_global === 'N' ? 1 : 0,
                        discount: '', // 优惠信息
                        isFreightFree: 1,
                        commentCount: '', // 评论总数
                        deliveryPlace: {
                            administrativeAreaLv1: '江苏' // 发货地
                        },
                        stockSpecification: {// 分地域库存信息
                            AdministrativeAreaLv2: '南京', // 区域
                            volume: item.storage_num, // 库存
                            status: (item.storage_num <= 0 ? 'outstock' : 'instock')
                        },
                        shelfSpecification: {// 分地域上下架具体信息
                            AdministrativeAreaLv2: '南京',
                            status: 1
                        },
                        purchaseMethod: item.tags && item.tags['is_presell'] ? '付费预约' : '直接购买', // eslint-disable-line
                        coupon: [] // 优惠券
                    },
                    structuredCategory: {
                        categoryLv1: '',
                        categoryLv2: item.middle_sort_name,
                        categoryLv3: item.small_sort_name,
                        categoryLv4: ''
                    },
                    exifData: {
                        name: '',
                        value: '',
                        propertyID: '',
                        unitText: '',
                        unitCode: ''
                    },
                    id: item.product_skn
                });
            });

            return products;
        });
    }

    searchList(params) {
        params = _.assign({
            limit: 60,
            status: 1,
            sales: 'Y',
            stocknumber: 1,
            method: 'app.search.li',
            attribute_not: 2
        }, params);

        return this.get({
            data: params,
            param: {
                cache: 86400
            }
        });
    }
}

module.exports = SeoIndexModel;