...
|
...
|
@@ -2,6 +2,7 @@ |
|
|
|
|
|
const Router = require('koa-router');
|
|
|
const _ = require('lodash');
|
|
|
const md5 = require('md5');
|
|
|
const pager = require('../utils/pager');
|
|
|
|
|
|
let r = new Router();
|
...
|
...
|
@@ -14,50 +15,81 @@ const TYPE_LIST = [ |
|
|
];
|
|
|
|
|
|
const tdk = {
|
|
|
_getDeaultList: async(ctx, next) => {
|
|
|
|
|
|
},
|
|
|
|
|
|
// tdk 列表
|
|
|
index: async(ctx, next) => {
|
|
|
let resData = {};
|
|
|
let type = ctx.query.type || 'skn',
|
|
|
page = parseInt(`0${ctx.query.page}`, 10) || 1,
|
|
|
limit = parseInt(`0${ctx.query.limit}`, 10) || 20;
|
|
|
limit = parseInt(`0${ctx.query.limit}`, 10) || 20,
|
|
|
query = ctx.query.query;
|
|
|
let listKey = `tdk:${type}:links`,
|
|
|
startId = (page - 1) * limit,
|
|
|
typeObj = _.find(TYPE_LIST, {'type': type}) || {};
|
|
|
let hmget = [];
|
|
|
|
|
|
await ctx.redis.multi([
|
|
|
['llen', listKey],
|
|
|
['lrange', listKey, startId, page * limit]
|
|
|
]).execAsync().then(function(res) {
|
|
|
let total = res[0] || 1;
|
|
|
if (query) {
|
|
|
query = _.trim(query);
|
|
|
resData.query = query;
|
|
|
|
|
|
resData.pager = pager(Math.floor((total - 1) / limit) + 1, ctx.query);
|
|
|
|
|
|
_.forEach(res[1], value => {
|
|
|
hmget.push(['hmget', `tdk:${type}:${value}`, 'key', 'title', 'keywords', 'description']);
|
|
|
});
|
|
|
});
|
|
|
query = type === 'url' ? md5(query) : query;
|
|
|
|
|
|
await ctx.redis.multi(hmget).execAsync().then(function(res) {
|
|
|
let tdkList = [];
|
|
|
await ctx.redis.multi([
|
|
|
['exists', `tdk:${type}:${query}`],
|
|
|
['hmget', `tdk:${type}:${query}`, 'key', 'title', 'keywords', 'description']
|
|
|
]).execAsync().then(function(res) {
|
|
|
console.log(res);
|
|
|
if (!res[0]) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
_.forEach(res, value => {
|
|
|
tdkList.push({
|
|
|
id: ++startId,
|
|
|
resData.tdkList = [{
|
|
|
id: 1,
|
|
|
typeName: typeObj.name,
|
|
|
typeLt: typeObj.lt,
|
|
|
key: value[0],
|
|
|
title: value[1],
|
|
|
keywords: value[2],
|
|
|
description: value[3]
|
|
|
key: res[1][0],
|
|
|
title: res[1][1],
|
|
|
keywords: res[1][2],
|
|
|
description: res[1][3]
|
|
|
}];
|
|
|
});
|
|
|
} else {
|
|
|
await ctx.redis.multi([
|
|
|
['llen', listKey],
|
|
|
['lrange', listKey, startId, page * limit]
|
|
|
]).execAsync().then(function(res) {
|
|
|
let total = res[0] || 1;
|
|
|
|
|
|
resData.pager = pager(Math.floor((total - 1) / limit) + 1, ctx.query);
|
|
|
|
|
|
_.forEach(res[1], value => {
|
|
|
hmget.push(['hmget', `tdk:${type}:${value}`, 'key', 'title', 'keywords', 'description']);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
if (tdkList.length) {
|
|
|
resData.tdkList = tdkList;
|
|
|
}
|
|
|
});
|
|
|
await ctx.redis.multi(hmget).execAsync().then(function(res) {
|
|
|
let tdkList = [];
|
|
|
|
|
|
_.forEach(res, value => {
|
|
|
tdkList.push({
|
|
|
id: ++startId,
|
|
|
typeName: typeObj.name,
|
|
|
typeLt: typeObj.lt,
|
|
|
key: value[0],
|
|
|
title: value[1],
|
|
|
keywords: value[2],
|
|
|
description: value[3]
|
|
|
});
|
|
|
});
|
|
|
|
|
|
if (tdkList.length) {
|
|
|
resData.tdkList = tdkList;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
await ctx.render('action/seo_tdk', Object.assign(resData, {
|
|
|
title: 'TDK管理',
|
...
|
...
|
@@ -69,37 +101,111 @@ const tdk = { |
|
|
|
|
|
// 添加tdk
|
|
|
add: async(ctx, next) => {
|
|
|
let result = {code: 500, message: '非法参数'},
|
|
|
typeList = ['skn', 'article', 'shop', 'url'];
|
|
|
let result = {code: 500, message: '非法参数'};
|
|
|
|
|
|
// skn, article, shop, url
|
|
|
let type = ctx.request.body.type,
|
|
|
val = ctx.request.body.val,
|
|
|
key = ctx.request.body.key,
|
|
|
title = ctx.request.body.title || '',
|
|
|
keywords = ctx.request.body.keywords || '',
|
|
|
description = ctx.request.body.description || '';
|
|
|
|
|
|
|
|
|
// type 不合法, 返回错误
|
|
|
if (!_.find(typeList, type)) {
|
|
|
ctx.response.body = result;
|
|
|
if (!_.find(TYPE_LIST, ['type', type])) {
|
|
|
return ctx.response.body = result;
|
|
|
}
|
|
|
|
|
|
let hashKey = type === 'url' ? md5(key) : key;
|
|
|
|
|
|
await ctx.redis.multi([
|
|
|
['lpushx', `tdk:${type}:links`, hashKey],
|
|
|
['hmset', `tdk:${type}:${hashKey}`, 'key', key, 'title', title, 'keywords', keywords,
|
|
|
'description', description, 'modify_time', Date.parse(new Date()) / 1000]
|
|
|
]).execAsync().then(function(res) {
|
|
|
if (res[1]) {
|
|
|
if (!res[0]) {
|
|
|
ctx.redis.lpush(`tdk:${type}:links`, hashKey);
|
|
|
}
|
|
|
Object.assign(result, {code:200, message: 'success'});
|
|
|
} else {
|
|
|
Object.assign(result, {code:400, message: 'failed'});
|
|
|
}
|
|
|
});
|
|
|
|
|
|
ctx.response.body = result;
|
|
|
},
|
|
|
|
|
|
edit: async(ctx, next) => {
|
|
|
let result = {code: 500, message: '非法参数'};
|
|
|
|
|
|
// skn, article, shop, url
|
|
|
let type = ctx.request.body.type,
|
|
|
key = ctx.request.body.key,
|
|
|
title = ctx.request.body.title || '',
|
|
|
keywords = ctx.request.body.keywords || '',
|
|
|
description = ctx.request.body.description || '';
|
|
|
|
|
|
// type 不合法, 返回错误
|
|
|
if (!_.find(TYPE_LIST, ['type', type])) {
|
|
|
return ctx.response.body = result;
|
|
|
}
|
|
|
|
|
|
ctx.redis.multi([
|
|
|
["lpush", `tdk:${type}:links`, val],
|
|
|
["hmset", `tdk:${type}:${val}`, "title", title, "keywords", keywords, "description", description]
|
|
|
let hashKey = type === 'url' ? md5(key) : key;
|
|
|
|
|
|
await ctx.redis.multi([
|
|
|
['hmset', `tdk:${type}:${hashKey}`, 'title', title, 'keywords', keywords,
|
|
|
'description', description, 'modify_time', Date.parse(new Date()) / 1000]
|
|
|
]).execAsync().then(function(res) {
|
|
|
Object.assign(result, {code:200, message: 'success'})
|
|
|
if (res[0]) {
|
|
|
Object.assign(result, {code:200, message: 'success'});
|
|
|
} else {
|
|
|
Object.assign(result, {code:400, message: 'failed'});
|
|
|
}
|
|
|
});
|
|
|
|
|
|
ctx.response.body = result;
|
|
|
},
|
|
|
delete: async(ctx, next) => {
|
|
|
let result = {code: 500, message: '非法参数'};
|
|
|
let list = ctx.request.body.list;
|
|
|
let delArr = ['del']
|
|
|
let multiArr = [];
|
|
|
|
|
|
// list 不合法, 返回错误
|
|
|
if (!list || !list.length) {
|
|
|
return ctx.response.body = result;
|
|
|
}
|
|
|
|
|
|
_.forEach(list, value => {
|
|
|
let hashKey = value.type === 'url' ? md5(value.key) : value.key;
|
|
|
|
|
|
if (!_.find(TYPE_LIST, ['type', value.type])) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
multiArr.push(['lrem', `tdk:${value.type}:links`, 1, hashKey]);
|
|
|
|
|
|
delArr.push(`tdk:${value.type}:${hashKey}`)
|
|
|
});
|
|
|
|
|
|
if (multiArr.length) {
|
|
|
multiArr.push(delArr);
|
|
|
|
|
|
await ctx.redis.multi(multiArr).execAsync().then(function(res) {
|
|
|
Object.assign(result, {code:200, message: 'success'});
|
|
|
});
|
|
|
}
|
|
|
|
|
|
ctx.response.body = result;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
r.get('/', tdk.index);
|
|
|
r.get('/tdk', tdk.index);
|
|
|
r.post('/tdk/add', tdk.add);
|
|
|
r.post('/tdk/edit', tdk.edit);
|
|
|
r.post('/tdk/delete', tdk.delete);
|
|
|
|
|
|
|
|
|
module.exports = r; |
...
|
...
|
|