Authored by yyq

品类管理

... ... @@ -307,6 +307,158 @@ const rootWords = {
}
}
const category = {
index: async(ctx, next) => {
let page = ctx.query.page || 1,
limit = 20,
startId = (page - 1) * limit;
let query = _.trim(ctx.query.query);
let resData = {};
if (query) {
resData.query = query;
let queryKey = md5(query);
await ctx.redis.multi([
['exists', `category:description:${queryKey}`],
['hmget', `category:description:${queryKey}`, 'category', 'description']
]).execAsync().then(function(res) {
if (!res[0]) {
return;
}
resData.categoryList = [{
id: 1,
key: md5(res[1][0]),
category: res[1][0],
description: res[1][1]
}];
});
} else {
let hmget = [];
await ctx.redis.multi([
['llen', 'category:description:links'],
['lrange', 'category:description:links', 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', `category:description:${value}`, 'category', 'description']);
});
});
await ctx.redis.multi(hmget).execAsync().then(function(res) {
let categoryList = [];
_.forEach(res, value => {
categoryList.push({
id: ++startId,
key: md5(value[0]),
category: value[0],
description: value[1]
});
});
if (categoryList.length) {
resData.categoryList = categoryList;
}
});
}
await ctx.render('action/seo_category', Object.assign(resData, {
title: '品类描述管理'
}));
},
// 添加品类描述
add: async(ctx, next) => {
let result = {code: 500, message: '非法参数'};
let category = ctx.request.body.category || '',
description = ctx.request.body.description || '';
if (!category || !description) {
return ctx.response.body = result;
}
let hashKey = md5(category);
await ctx.redis.multi([
['lpushx', 'category:description:links', hashKey],
['hmset', `category:description:${hashKey}`, 'category', category,
'description', description, 'modify_time', Date.parse(new Date()) / 1000]
]).execAsync().then(function(res) {
if (res[1]) {
if (!res[0]) {
ctx.redis.lpush(`category:description: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: '非法参数'};
let key = ctx.request.body.key,
category = ctx.request.body.category || '',
description = ctx.request.body.description || '';
if (!key) {
return ctx.response.body = result;
}
await ctx.redis.multi([
['hmset', `category:description:${key}`, 'description', description, 'modify_time', Date.parse(new Date()) / 1000]
]).execAsync().then(function(res) {
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.key;
multiArr.push(['lrem', `category:description:links`, 1, hashKey]);
delArr.push(`category:description:${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);
... ... @@ -321,5 +473,10 @@ r.post('/rootwords/delete', rootWords.delete);
r.post('/rootwords/getsubsorts', rootWords.getSubSorts);
r.post('/rootwords/rootToKeywords', rootWords.rootToKeywords);
// 品类描述管理
r.get('/category', category.index);
r.post('/category/add', category.add);
r.post('/category/edit', category.edit);
r.post('/category/delete', category.delete);
module.exports = r;
... ...
... ... @@ -75,6 +75,8 @@
<li><a href="/seo/rootwords"> <span>词根管理</span></a></li>
<li><a href="/keywords/expand"> <span>关键词管理</span></a></li>
<li><a href="/seo/tdk"><span>TDK管理</span></a></li>
<li><a href="/seo/category"><span>品类描述管理</span></a></li>
<li><a href="/seo/tdk"><span>友链管理</span></a></li>
</ul>
</li>
... ...