...
|
...
|
@@ -8,65 +8,55 @@ |
|
|
|
|
|
const Router = require('koa-router');
|
|
|
const moment = require('moment');
|
|
|
const redisStore = require('koa-redis');
|
|
|
const redis = require("redis");
|
|
|
const client = redis.createClient();
|
|
|
const multi = client.multi();
|
|
|
|
|
|
client.on("error", function (err) {
|
|
|
console.log("Error " + err);
|
|
|
});
|
|
|
const pager = require('../utils/pager');
|
|
|
|
|
|
const r = new Router();
|
|
|
|
|
|
const multiAsync=()=>{
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
multi.exec(async function(errors, results) {
|
|
|
if(errors)reject(errors);
|
|
|
let res = results;
|
|
|
resolve(res);
|
|
|
});
|
|
|
const multiAsync=(multi)=>{
|
|
|
return multi.execAsync().then(function(res) {
|
|
|
return res;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
//查看
|
|
|
const getDataList = async(key, page)=>{
|
|
|
let start = (page-1)*10;
|
|
|
let end = (page)*10-1;
|
|
|
multi.lrange(key, start, end);
|
|
|
multi.LLEN(key);
|
|
|
return multiAsync();
|
|
|
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(arr)=>{
|
|
|
const getDataValues = async(ctx, arr)=>{
|
|
|
let multi = ctx.redis.multi();
|
|
|
arr.forEach(item=>{
|
|
|
multi.get(item);
|
|
|
});
|
|
|
return multiAsync();
|
|
|
return multiAsync(multi);
|
|
|
}
|
|
|
//删除
|
|
|
const delDataValues = async(arr)=>{
|
|
|
const delDataValues = async(ctx, arr)=>{
|
|
|
let multi = ctx.redis.multi();
|
|
|
arr.forEach(item=>{
|
|
|
let key = `keywords_mana:${item}`;
|
|
|
multi.del(key);
|
|
|
multi.lrem('keywords_mana_list', 1, key);
|
|
|
multi.del(key).lrem('keywords_mana_list', 1, key);
|
|
|
});
|
|
|
return multiAsync();
|
|
|
return multiAsync(multi);
|
|
|
}
|
|
|
//添加
|
|
|
const addDataValues = async(value)=>{
|
|
|
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);
|
|
|
multi.lpush('keywords_mana_list', key);
|
|
|
multi.lrem('keywords_mana_list', 1, key).lpush('keywords_mana_list', key);
|
|
|
|
|
|
return multiAsync();
|
|
|
return multiAsync(multi);
|
|
|
}
|
|
|
//编辑
|
|
|
const editDataValues = async(value, oldValue)=>{
|
|
|
const editDataValues = async(ctx, value, oldValue)=>{
|
|
|
try{
|
|
|
await delDataValues([oldValue]);
|
|
|
await addDataValues(value);
|
|
|
await delDataValues(ctx, [oldValue]);
|
|
|
await addDataValues(ctx, value);
|
|
|
return true;
|
|
|
}catch(e){
|
|
|
return false;
|
...
|
...
|
@@ -74,34 +64,32 @@ const editDataValues = async(value, oldValue)=>{ |
|
|
|
|
|
}
|
|
|
//搜索
|
|
|
const searchDataValues = async(value)=>{
|
|
|
const searchDataValues = async(ctx, value)=>{
|
|
|
let multi = ctx.redis.multi();
|
|
|
let key=`keywords_mana:*${value}*`;
|
|
|
multi.KEYS(key);
|
|
|
return multiAsync();
|
|
|
return multiAsync(multi);
|
|
|
}
|
|
|
|
|
|
r.get('/', async(ctx) => {
|
|
|
await ctx.render('action/keywords');
|
|
|
});
|
|
|
|
|
|
r.get('/getKeywords', async(ctx) => {
|
|
|
let q = ctx.request.query;
|
|
|
let r = await getDataList("keywords_mana_list", q.currentPage, q.pageCount);
|
|
|
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(r[0]);
|
|
|
ctx.body = {
|
|
|
code: 200,
|
|
|
message: 'success',
|
|
|
data: result,
|
|
|
total:r[1]
|
|
|
};
|
|
|
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(keys);
|
|
|
await delDataValues(ctx, keys);
|
|
|
ctx.body = {
|
|
|
code: 200,
|
|
|
message: 'success',
|
...
|
...
|
@@ -120,7 +108,7 @@ r.get('/addKeywords', async(ctx) => { |
|
|
let q = ctx.request.query;
|
|
|
let key = q['keywords'];
|
|
|
try{
|
|
|
await addDataValues(key);
|
|
|
await addDataValues(ctx, key);
|
|
|
ctx.body = {
|
|
|
code: 200,
|
|
|
message: 'success',
|
...
|
...
|
@@ -139,7 +127,7 @@ r.get('/editKeywords', async(ctx) => { |
|
|
let q = ctx.request.query;
|
|
|
let key = JSON.parse(q['keywords']);
|
|
|
try{
|
|
|
await editDataValues(key[0], key[1]);
|
|
|
await editDataValues(ctx, key[0], key[1]);
|
|
|
ctx.body = {
|
|
|
code: 200,
|
|
|
message: 'success',
|
...
|
...
|
@@ -157,9 +145,9 @@ r.get('/editKeywords', async(ctx) => { |
|
|
r.get('/searchKeywords', async(ctx) => {
|
|
|
let q = ctx.request.query;
|
|
|
let key = q['association'];
|
|
|
let r = await searchDataValues(key);
|
|
|
let r = await searchDataValues(ctx, key);
|
|
|
let result = [];
|
|
|
if(r[0].length)result = await getDataValues(r[0]);
|
|
|
if(r[0].length)result = await getDataValues(ctx, r[0]);
|
|
|
ctx.body = {
|
|
|
code: 200,
|
|
|
message: 'success',
|
...
|
...
|
|