keywords.js
3.97 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
/**
*
* @author: shijian<jian.shi@yoho.cn>
* @date: 17/5/31
*/
'use strict';
const Router = require('koa-router');
const moment = require('moment');
const pager = require('../utils/pager');
const Mysql = require('../../../lib/mysql-promise');
const r = new Router();
const multiAsync=(multi)=>{
return multi.execAsync().then(function(res) {
return res;
});
}
//查看
const getDataList = async(ctx, key, page, limit)=>{
let multi = ctx.redis.multi();
let start = (page-1)*limit;
let end = (page)*limit-1;
multi.lrange(key, start, end).LLEN(key);
return multiAsync(multi);
}
const getDataValues = async(ctx, arr)=>{
let multi = ctx.redis.multi();
arr.forEach(item=>{
multi.get(item);
});
return multiAsync(multi);
}
//删除
const delDataValues = async(ctx, arr)=>{
let multi = ctx.redis.multi();
arr.forEach(item=>{
let key = `keywords_mana:${item}`;
multi.del(key).lrem('keywords_mana_list', 1, key);
});
return multiAsync(multi);
}
//添加
const addDataValues = async(ctx, value)=>{
let multi = ctx.redis.multi();
let key=`keywords_mana:${value}`;
multi.set(key, value);
multi.lrem('keywords_mana_list', 1, key).lpush('keywords_mana_list', key);
return multiAsync(multi);
}
//编辑
const editDataValues = async(ctx, value, oldValue)=>{
try{
await delDataValues(ctx, [oldValue]);
await addDataValues(ctx, value);
return true;
}catch(e){
return false;
}
}
//搜索
const searchDataValues = async(ctx, value)=>{
let multi = ctx.redis.multi();
let key=`keywords_mana:*${value}*`;
multi.KEYS(key);
return multiAsync(multi);
}
r.get('/', async(ctx) => {
let resData = {};
let q = ctx.request.query,
page = parseInt(`0${ctx.query.page}`, 10) || 1,
limit = parseInt(`0${ctx.query.limit}`, 10) || 10;
let r = await getDataList(ctx, "keywords_mana_list", page, limit);
let result = [];
if(r[0].length)result = await getDataValues(ctx, r[0]);
let total = r[1];
resData.pager = pager(Math.floor((total - 1) / limit) + 1, ctx.query);
resData.tabs = result;
await ctx.render('action/keywords', resData);
});
r.get('/delKeywords', async(ctx) => {
let q = ctx.request.query;
let keys = JSON.parse(q['keywords']);
try{
await delDataValues(ctx, keys);
ctx.body = {
code: 200,
message: 'success',
data: ''
};
}catch(e){
ctx.body = {
code: 500,
message: 'fail',
data: ''
};
}
});
r.get('/addKeywords', async(ctx) => {
let q = ctx.request.query;
let key = q['keywords'];
try{
await addDataValues(ctx, key);
ctx.body = {
code: 200,
message: 'success',
data: ''
};
}catch(e){
ctx.body = {
code: 500,
message: 'fail',
data: ''
};
}
});
r.get('/editKeywords', async(ctx) => {
let q = ctx.request.query;
let key = JSON.parse(q['keywords']);
try{
await editDataValues(ctx, key[0], key[1]);
ctx.body = {
code: 200,
message: 'success',
data: ''
};
}catch(e){
ctx.body = {
code: 500,
message: 'fail',
data: ''
};
}
});
r.get('/searchKeywords', async(ctx) => {
let q = ctx.request.query;
let key = q['association'];
let r = await searchDataValues(ctx, key);
let result = [];
if(r[0].length)result = await getDataValues(ctx, r[0]);
ctx.body = {
code: 200,
message: 'success',
data: result,
total:1
};
});
r.get('/syncWord', async(ctx) => {
let mysql = new Mysql();
let r = await mysql.query(`select * from seo_keywords`);
console.log(r[0].keyword, ' ===');
ctx.body = {
code: 200,
message: 'success',
data: r
};
});
module.exports = r;