seo.js
3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
const Router = require('koa-router');
const _ = require('lodash');
const pager = require('../utils/pager');
let r = new Router();
const TYPE_LIST = [
{name: '商品', type: 'skn', lt: 'SKN'},
{name: '逛', type: 'article', lt: 'ID'},
{name: '店铺', type: 'shop', lt: 'ShopId'},
{name: '链接', type: 'url', lt: 'URL'}
];
const tdk = {
// 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;
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;
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']);
});
});
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管理',
typeList: TYPE_LIST,
type: typeObj.type,
typeName: typeObj.name
}));
},
// 添加tdk
add: async(ctx, next) => {
let result = {code: 500, message: '非法参数'},
typeList = ['skn', 'article', 'shop', 'url'];
// skn, article, shop, url
let type = ctx.request.body.type,
val = ctx.request.body.val,
title = ctx.request.body.title || '',
keywords = ctx.request.body.keywords || '',
description = ctx.request.body.description || '';
// type 不合法, 返回错误
if (!_.find(typeList, type)) {
ctx.response.body = result;
}
ctx.redis.multi([
["lpush", `tdk:${type}:links`, val],
["hmset", `tdk:${type}:${val}`, "title", title, "keywords", keywords, "description", description]
]).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);
module.exports = r;