seo.js
6.48 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
'use strict';
const Router = require('koa-router');
const _ = require('lodash');
const md5 = require('md5');
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 = {
_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,
query = ctx.query.query;
let listKey = `tdk:${type}:links`,
startId = (page - 1) * limit,
typeObj = _.find(TYPE_LIST, {'type': type}) || {};
let hmget = [];
if (query) {
query = _.trim(query);
resData.query = query;
query = type === 'url' ? md5(query) : query;
await ctx.redis.multi([
['exists', `tdk:${type}:${query}`],
['hmget', `tdk:${type}:${query}`, 'key', 'title', 'keywords', 'description']
]).execAsync().then(function(res) {
if (!res[0]) {
return;
}
resData.tdkList = [{
id: 1,
typeName: typeObj.name,
typeLt: typeObj.lt,
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']);
});
});
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: '非法参数'};
// 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;
}
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;
}
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) {
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;