index.js 1.4 KB
'use strict';
const SeoIndexModel = require('../../models/seo/index');

const index = (req, res) => {
    if (global.IS_GOODS_XML_RUN) {
        return res.json({code: 201, message: '上一个任务正在运行中...'});
    }

    global.IS_GOODS_XML_RUN = true;

    req.ctx(SeoIndexModel).writerGoodsXml(req.params).then(() => {
        global.IS_GOODS_XML_RUN = false;
    });

    return res.json({
        code: 200,
        data: `http://spiderwebhook.yoho.cn/dist/goods-xml/goods-${req.params.start || 1}.xml`,
        message: '任务创建成功,开始运行...'
    });
};

const autoGoodsXml = (req, res) => {
    if (global.IS_GOODS_XML_RUN) {
        return res.json({code: 201, message: '上一个任务正在运行中...'});
    }

    global.IS_GOODS_XML_RUN = true;

    req.ctx(SeoIndexModel).autoGoodsXml({start: 1});

    res.json({code: 200, message: '任务创建成功,开始运行...'});
};

const setTask = (req, res) => {
    return req.ctx(SeoIndexModel).setTask(req.query).then(result => {
        return res.json(result);
    });
};

const delTask = (req, res) => {
    return req.ctx(SeoIndexModel).delTask(req.query.url).then(result => {
        return res.json(result);
    });
};

const demoXml = (req, res) => {
    res.setHeader('Content-Type', 'text/xml; charset=utf-8');
    res.render('seo/demo-xml');
};

module.exports = {
    index,
    autoGoodsXml,
    setTask,
    delTask,
    demoXml
};