...
|
...
|
@@ -307,6 +307,323 @@ 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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
const friendLink = {
|
|
|
index: async(ctx, next) => {
|
|
|
let type = ctx.query.type || 'text';
|
|
|
let resData = {
|
|
|
typeList: [
|
|
|
{type: 'text', name: '文字友链'},
|
|
|
{type: 'img', name: '图片友链'},
|
|
|
]
|
|
|
};
|
|
|
|
|
|
let typeObj = _.find(resData.typeList, {'type': type});
|
|
|
|
|
|
if (_.isEmpty(typeObj)) {
|
|
|
resData.typeName = '文字友链';
|
|
|
type = 'text';
|
|
|
} else {
|
|
|
resData.typeName = typeObj.name;
|
|
|
type = typeObj.type;
|
|
|
}
|
|
|
|
|
|
let res = await ctx.redis.getAsync(`friend:${type}:links`);
|
|
|
|
|
|
try {
|
|
|
res = JSON.parse(res);
|
|
|
} catch (e) {
|
|
|
console.error(`friend links parse error : ${JSON.stringify(e)}`)
|
|
|
res = [];
|
|
|
}
|
|
|
|
|
|
await ctx.render('action/seo_friendlink', Object.assign(resData, {
|
|
|
title: '友链管理',
|
|
|
linkList: res,
|
|
|
type: type
|
|
|
}));
|
|
|
},
|
|
|
add: async(ctx, next) => {
|
|
|
let result = {code: 500, message: '非法参数'};
|
|
|
|
|
|
let {type, name, img, sort, link} = ctx.request.body;
|
|
|
|
|
|
type = type || 'text';
|
|
|
|
|
|
if (!type) {
|
|
|
return ctx.response.body = result;
|
|
|
}
|
|
|
|
|
|
let rkey = `friend:${type}:links`;
|
|
|
let list = await ctx.redis.getAsync(rkey);
|
|
|
|
|
|
try {
|
|
|
list = JSON.parse(list);
|
|
|
list = list || [];
|
|
|
} catch (e) {
|
|
|
console.error(`friend links parse error : ${JSON.stringify(e)}`);
|
|
|
return ctx.response.body = result;
|
|
|
}
|
|
|
|
|
|
let linkInfo = {
|
|
|
name: name,
|
|
|
sort: sort || 0,
|
|
|
link: link,
|
|
|
modify_time: Date.parse(new Date()) / 1000
|
|
|
}
|
|
|
|
|
|
if (type === 'img' && img) {
|
|
|
linkInfo.img = img;
|
|
|
}
|
|
|
|
|
|
linkInfo.cid = md5(JSON.stringify(linkInfo));
|
|
|
|
|
|
list.push(linkInfo);
|
|
|
|
|
|
await ctx.redis.setAsync(rkey, JSON.stringify(list)).then(function(res) {
|
|
|
if (res === 'OK') {
|
|
|
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 {type, name, img, sort, link, cid} = ctx.request.body;
|
|
|
|
|
|
type = type || 'text';
|
|
|
|
|
|
if (!type) {
|
|
|
return ctx.response.body = result;
|
|
|
}
|
|
|
|
|
|
let rkey = `friend:${type}:links`;
|
|
|
let list = await ctx.redis.getAsync(rkey);
|
|
|
|
|
|
try {
|
|
|
list = JSON.parse(list);
|
|
|
list = list || [];
|
|
|
} catch (e) {
|
|
|
console.error(`friend links parse error : ${JSON.stringify(e)}`);
|
|
|
return ctx.response.body = result;
|
|
|
}
|
|
|
|
|
|
_.forEach(list, value => {
|
|
|
if (value.cid === cid) {
|
|
|
Object.assign(value, {
|
|
|
name: name,
|
|
|
sort: sort || 0,
|
|
|
link: link
|
|
|
});
|
|
|
|
|
|
img && (value.img = img);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
await ctx.redis.setAsync(rkey, JSON.stringify(list)).then(function(res) {
|
|
|
if (res === 'OK') {
|
|
|
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 delArr = ctx.request.body.list;
|
|
|
|
|
|
let type = _.get(delArr, '[0].type');
|
|
|
|
|
|
if (!delArr || !delArr.length || !type) {
|
|
|
return ctx.response.body = result;
|
|
|
}
|
|
|
|
|
|
let rkey = `friend:${type}:links`;
|
|
|
let list = await ctx.redis.getAsync(rkey);
|
|
|
|
|
|
try {
|
|
|
list = JSON.parse(list);
|
|
|
list = list || [];
|
|
|
} catch (e) {
|
|
|
console.error(`friend links parse error : ${JSON.stringify(e)}`);
|
|
|
return ctx.response.body = result;
|
|
|
}
|
|
|
|
|
|
_.remove(list, function(n) {
|
|
|
return _.findIndex(delArr, {cid: n.cid}) > -1;
|
|
|
});
|
|
|
|
|
|
await ctx.redis.setAsync(rkey, JSON.stringify(list)).then(function(res) {
|
|
|
if (res === 'OK') {
|
|
|
Object.assign(result, {code:200, message: 'success'});
|
|
|
} else {
|
|
|
Object.assign(result, {code:400, message: 'failed'});
|
|
|
}
|
|
|
});
|
|
|
|
|
|
ctx.response.body = result;
|
|
|
},
|
|
|
};
|
|
|
|
|
|
r.get('/', tdk.index);
|
|
|
r.get('/tdk', tdk.index);
|
|
|
r.post('/tdk/add', tdk.add);
|
...
|
...
|
@@ -321,5 +638,16 @@ 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);
|
|
|
|
|
|
// 品类描述管理
|
|
|
r.get('/friendlink', friendLink.index);
|
|
|
r.post('/friendlink/add', friendLink.add);
|
|
|
r.post('/friendlink/edit', friendLink.edit);
|
|
|
r.post('/friendlink/delete', friendLink.delete);
|
|
|
|
|
|
module.exports = r; |
...
|
...
|
|