|
@@ -8,65 +8,55 @@ |
|
@@ -8,65 +8,55 @@ |
8
|
|
8
|
|
9
|
const Router = require('koa-router');
|
9
|
const Router = require('koa-router');
|
10
|
const moment = require('moment');
|
10
|
const moment = require('moment');
|
11
|
-const redisStore = require('koa-redis');
|
|
|
12
|
-const redis = require("redis");
|
|
|
13
|
-const client = redis.createClient();
|
|
|
14
|
-const multi = client.multi();
|
|
|
15
|
-
|
|
|
16
|
-client.on("error", function (err) {
|
|
|
17
|
- console.log("Error " + err);
|
|
|
18
|
-});
|
11
|
+const pager = require('../utils/pager');
|
19
|
|
12
|
|
20
|
const r = new Router();
|
13
|
const r = new Router();
|
21
|
|
14
|
|
22
|
-const multiAsync=()=>{
|
|
|
23
|
- return new Promise(function (resolve, reject) {
|
|
|
24
|
- multi.exec(async function(errors, results) {
|
|
|
25
|
- if(errors)reject(errors);
|
|
|
26
|
- let res = results;
|
|
|
27
|
- resolve(res);
|
|
|
28
|
- });
|
15
|
+const multiAsync=(multi)=>{
|
|
|
16
|
+ return multi.execAsync().then(function(res) {
|
|
|
17
|
+ return res;
|
29
|
});
|
18
|
});
|
30
|
}
|
19
|
}
|
31
|
|
20
|
|
32
|
//查看
|
21
|
//查看
|
33
|
-const getDataList = async(key, page)=>{
|
|
|
34
|
- let start = (page-1)*10;
|
|
|
35
|
- let end = (page)*10-1;
|
|
|
36
|
- multi.lrange(key, start, end);
|
|
|
37
|
- multi.LLEN(key);
|
|
|
38
|
- return multiAsync();
|
22
|
+const getDataList = async(ctx, key, page, limit)=>{
|
|
|
23
|
+ let multi = ctx.redis.multi();
|
|
|
24
|
+ let start = (page-1)*limit;
|
|
|
25
|
+ let end = (page)*limit-1;
|
|
|
26
|
+ multi.lrange(key, start, end).LLEN(key);
|
|
|
27
|
+ return multiAsync(multi);
|
39
|
}
|
28
|
}
|
40
|
|
29
|
|
41
|
-const getDataValues = async(arr)=>{
|
30
|
+const getDataValues = async(ctx, arr)=>{
|
|
|
31
|
+ let multi = ctx.redis.multi();
|
42
|
arr.forEach(item=>{
|
32
|
arr.forEach(item=>{
|
43
|
multi.get(item);
|
33
|
multi.get(item);
|
44
|
});
|
34
|
});
|
45
|
- return multiAsync();
|
35
|
+ return multiAsync(multi);
|
46
|
}
|
36
|
}
|
47
|
//删除
|
37
|
//删除
|
48
|
-const delDataValues = async(arr)=>{
|
38
|
+const delDataValues = async(ctx, arr)=>{
|
|
|
39
|
+ let multi = ctx.redis.multi();
|
49
|
arr.forEach(item=>{
|
40
|
arr.forEach(item=>{
|
50
|
let key = `keywords_mana:${item}`;
|
41
|
let key = `keywords_mana:${item}`;
|
51
|
- multi.del(key);
|
|
|
52
|
- multi.lrem('keywords_mana_list', 1, key);
|
42
|
+ multi.del(key).lrem('keywords_mana_list', 1, key);
|
53
|
});
|
43
|
});
|
54
|
- return multiAsync();
|
44
|
+ return multiAsync(multi);
|
55
|
}
|
45
|
}
|
56
|
//添加
|
46
|
//添加
|
57
|
-const addDataValues = async(value)=>{
|
47
|
+const addDataValues = async(ctx, value)=>{
|
|
|
48
|
+ let multi = ctx.redis.multi();
|
58
|
let key=`keywords_mana:${value}`;
|
49
|
let key=`keywords_mana:${value}`;
|
59
|
multi.set(key, value);
|
50
|
multi.set(key, value);
|
60
|
- multi.lrem('keywords_mana_list', 1, key);
|
|
|
61
|
- multi.lpush('keywords_mana_list', key);
|
51
|
+ multi.lrem('keywords_mana_list', 1, key).lpush('keywords_mana_list', key);
|
62
|
|
52
|
|
63
|
- return multiAsync();
|
53
|
+ return multiAsync(multi);
|
64
|
}
|
54
|
}
|
65
|
//编辑
|
55
|
//编辑
|
66
|
-const editDataValues = async(value, oldValue)=>{
|
56
|
+const editDataValues = async(ctx, value, oldValue)=>{
|
67
|
try{
|
57
|
try{
|
68
|
- await delDataValues([oldValue]);
|
|
|
69
|
- await addDataValues(value);
|
58
|
+ await delDataValues(ctx, [oldValue]);
|
|
|
59
|
+ await addDataValues(ctx, value);
|
70
|
return true;
|
60
|
return true;
|
71
|
}catch(e){
|
61
|
}catch(e){
|
72
|
return false;
|
62
|
return false;
|
|
@@ -74,34 +64,32 @@ const editDataValues = async(value, oldValue)=>{ |
|
@@ -74,34 +64,32 @@ const editDataValues = async(value, oldValue)=>{ |
74
|
|
64
|
|
75
|
}
|
65
|
}
|
76
|
//搜索
|
66
|
//搜索
|
77
|
-const searchDataValues = async(value)=>{
|
67
|
+const searchDataValues = async(ctx, value)=>{
|
|
|
68
|
+ let multi = ctx.redis.multi();
|
78
|
let key=`keywords_mana:*${value}*`;
|
69
|
let key=`keywords_mana:*${value}*`;
|
79
|
multi.KEYS(key);
|
70
|
multi.KEYS(key);
|
80
|
- return multiAsync();
|
71
|
+ return multiAsync(multi);
|
81
|
}
|
72
|
}
|
82
|
|
73
|
|
83
|
r.get('/', async(ctx) => {
|
74
|
r.get('/', async(ctx) => {
|
84
|
- await ctx.render('action/keywords');
|
|
|
85
|
-});
|
|
|
86
|
-
|
|
|
87
|
-r.get('/getKeywords', async(ctx) => {
|
|
|
88
|
- let q = ctx.request.query;
|
|
|
89
|
- let r = await getDataList("keywords_mana_list", q.currentPage, q.pageCount);
|
75
|
+ let resData = {};
|
|
|
76
|
+ let q = ctx.request.query,
|
|
|
77
|
+ page = parseInt(`0${ctx.query.page}`, 10) || 1,
|
|
|
78
|
+ limit = parseInt(`0${ctx.query.limit}`, 10) || 10;
|
|
|
79
|
+ let r = await getDataList(ctx, "keywords_mana_list", page, limit);
|
90
|
let result = [];
|
80
|
let result = [];
|
91
|
- if(r[0].length)result = await getDataValues(r[0]);
|
|
|
92
|
- ctx.body = {
|
|
|
93
|
- code: 200,
|
|
|
94
|
- message: 'success',
|
|
|
95
|
- data: result,
|
|
|
96
|
- total:r[1]
|
|
|
97
|
- };
|
81
|
+ if(r[0].length)result = await getDataValues(ctx, r[0]);
|
|
|
82
|
+ let total = r[1];
|
|
|
83
|
+ resData.pager = pager(Math.floor((total - 1) / limit) + 1, ctx.query);
|
|
|
84
|
+ resData.tabs = result;
|
|
|
85
|
+ await ctx.render('action/keywords', resData);
|
98
|
});
|
86
|
});
|
99
|
|
87
|
|
100
|
r.get('/delKeywords', async(ctx) => {
|
88
|
r.get('/delKeywords', async(ctx) => {
|
101
|
let q = ctx.request.query;
|
89
|
let q = ctx.request.query;
|
102
|
let keys = JSON.parse(q['keywords']);
|
90
|
let keys = JSON.parse(q['keywords']);
|
103
|
try{
|
91
|
try{
|
104
|
- await delDataValues(keys);
|
92
|
+ await delDataValues(ctx, keys);
|
105
|
ctx.body = {
|
93
|
ctx.body = {
|
106
|
code: 200,
|
94
|
code: 200,
|
107
|
message: 'success',
|
95
|
message: 'success',
|
|
@@ -120,7 +108,7 @@ r.get('/addKeywords', async(ctx) => { |
|
@@ -120,7 +108,7 @@ r.get('/addKeywords', async(ctx) => { |
120
|
let q = ctx.request.query;
|
108
|
let q = ctx.request.query;
|
121
|
let key = q['keywords'];
|
109
|
let key = q['keywords'];
|
122
|
try{
|
110
|
try{
|
123
|
- await addDataValues(key);
|
111
|
+ await addDataValues(ctx, key);
|
124
|
ctx.body = {
|
112
|
ctx.body = {
|
125
|
code: 200,
|
113
|
code: 200,
|
126
|
message: 'success',
|
114
|
message: 'success',
|
|
@@ -139,7 +127,7 @@ r.get('/editKeywords', async(ctx) => { |
|
@@ -139,7 +127,7 @@ r.get('/editKeywords', async(ctx) => { |
139
|
let q = ctx.request.query;
|
127
|
let q = ctx.request.query;
|
140
|
let key = JSON.parse(q['keywords']);
|
128
|
let key = JSON.parse(q['keywords']);
|
141
|
try{
|
129
|
try{
|
142
|
- await editDataValues(key[0], key[1]);
|
130
|
+ await editDataValues(ctx, key[0], key[1]);
|
143
|
ctx.body = {
|
131
|
ctx.body = {
|
144
|
code: 200,
|
132
|
code: 200,
|
145
|
message: 'success',
|
133
|
message: 'success',
|
|
@@ -157,9 +145,9 @@ r.get('/editKeywords', async(ctx) => { |
|
@@ -157,9 +145,9 @@ r.get('/editKeywords', async(ctx) => { |
157
|
r.get('/searchKeywords', async(ctx) => {
|
145
|
r.get('/searchKeywords', async(ctx) => {
|
158
|
let q = ctx.request.query;
|
146
|
let q = ctx.request.query;
|
159
|
let key = q['association'];
|
147
|
let key = q['association'];
|
160
|
- let r = await searchDataValues(key);
|
148
|
+ let r = await searchDataValues(ctx, key);
|
161
|
let result = [];
|
149
|
let result = [];
|
162
|
- if(r[0].length)result = await getDataValues(r[0]);
|
150
|
+ if(r[0].length)result = await getDataValues(ctx, r[0]);
|
163
|
ctx.body = {
|
151
|
ctx.body = {
|
164
|
code: 200,
|
152
|
code: 200,
|
165
|
message: 'success',
|
153
|
message: 'success',
|