index.js 4.69 KB
'use strict';
const _ = require('lodash');
const moment = require('moment');
const helpers = global.yoho.helpers;
const STEP = 5;

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

    searchMipHandle(params) {
        let products = [];
        let page = ((params.page || 1) - 1) * STEP + 1;

        params.limit = params.limit > 100 ? 100 : params.limit;

        return Promise.all([
            this.searchList(Object.assign({}, params, {page: page})),
            this.searchList(Object.assign({}, params, {page: page + 1})),
            this.searchList(Object.assign({}, params, {page: page + 2})),
            this.searchList(Object.assign({}, params, {page: page + 3})),
            this.searchList(Object.assign({}, params, {page: page + 4}))
        ]).then(rdata => {
            let productLists = [];

            _.each(rdata, item => {
                productLists = productLists.concat(_.get(item, 'data.product_list', []));
            });

            _.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;