keywords.js 3.97 KB
/**
 *
 * @author: shijian<jian.shi@yoho.cn>
 * @date: 17/5/31
 */

'use strict';

const Router = require('koa-router');
const moment = require('moment');
const pager = require('../utils/pager');
const Mysql = require('../../../lib/mysql-promise');

const r = new Router();

const multiAsync=(multi)=>{
    return multi.execAsync().then(function(res) {
        return res;
    });
}

//查看
const getDataList = async(ctx, key, page, limit)=>{
    let multi = ctx.redis.multi();
    let start = (page-1)*limit;
    let end = (page)*limit-1;
    multi.lrange(key, start, end).LLEN(key);
    return multiAsync(multi);
}

const getDataValues = async(ctx, arr)=>{
    let multi = ctx.redis.multi();
    arr.forEach(item=>{
        multi.get(item);
    });
    return multiAsync(multi);
}
//删除
const delDataValues = async(ctx, arr)=>{
    let multi = ctx.redis.multi();
    arr.forEach(item=>{
        let key = `keywords_mana:${item}`;
        multi.del(key).lrem('keywords_mana_list', 1, key);
    });
    return multiAsync(multi);
}
//添加
const addDataValues = async(ctx, value)=>{
    let multi = ctx.redis.multi();
    let key=`keywords_mana:${value}`;
    multi.set(key, value);
    multi.lrem('keywords_mana_list', 1, key).lpush('keywords_mana_list', key);
    
    return multiAsync(multi);
}
//编辑
const editDataValues = async(ctx, value, oldValue)=>{
    try{
        await delDataValues(ctx, [oldValue]);
        await addDataValues(ctx, value);
        return true;
    }catch(e){
        return false;
    }
    
}
//搜索
const searchDataValues = async(ctx, value)=>{
    let multi = ctx.redis.multi();
    let key=`keywords_mana:*${value}*`;
    multi.KEYS(key);
    return multiAsync(multi);
}

r.get('/', async(ctx) => {
    let resData = {};
    let q = ctx.request.query,
    page = parseInt(`0${ctx.query.page}`, 10) || 1,
    limit = parseInt(`0${ctx.query.limit}`, 10) || 10;
    let r = await getDataList(ctx, "keywords_mana_list", page, limit);
    let result = [];
    if(r[0].length)result = await getDataValues(ctx, r[0]);
    let total = r[1];
    resData.pager = pager(Math.floor((total - 1) / limit) + 1, ctx.query);
    resData.tabs = result;
    await ctx.render('action/keywords', resData);
});

r.get('/delKeywords', async(ctx) => {
    let q = ctx.request.query;
    let keys = JSON.parse(q['keywords']);
    try{
        await delDataValues(ctx, keys);
        ctx.body = {
            code: 200,
            message: 'success',
            data: ''
        };
    }catch(e){
        ctx.body = {
            code: 500,
            message: 'fail',
            data: ''
        };
    }    
});

r.get('/addKeywords', async(ctx) => {
    let q = ctx.request.query;
    let key = q['keywords'];
    try{
        await addDataValues(ctx, key);
        ctx.body = {
            code: 200,
            message: 'success',
            data: ''
        };
    }catch(e){
        ctx.body = {
            code: 500,
            message: 'fail',
            data: ''
        };
    }    
});

r.get('/editKeywords', async(ctx) => {
    let q = ctx.request.query;
    let key = JSON.parse(q['keywords']);
    try{
        await editDataValues(ctx, key[0], key[1]);
        ctx.body = {
            code: 200,
            message: 'success',
            data: ''
        };
    }catch(e){
        ctx.body = {
            code: 500,
            message: 'fail',
            data: ''
        };
    }    
});

r.get('/searchKeywords', async(ctx) => {
    let q = ctx.request.query;
    let key = q['association'];
    let r = await searchDataValues(ctx, key);
    let result = [];
    if(r[0].length)result = await getDataValues(ctx, r[0]);
    ctx.body = {
        code: 200,
        message: 'success',
        data: result,
        total:1
    }; 
});

r.get('/syncWord', async(ctx) => {
    let mysql = new Mysql();

    let r = await mysql.query(`select * from seo_keywords`);
    console.log(r[0].keyword, ' ===');
    ctx.body = {
        code: 200,
        message: 'success',
        data: r
    };
});

module.exports = r;