keywords.js
7.56 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
*
* @author: shijian<jian.shi@yoho.cn>
* @date: 17/5/31
*/
'use strict';
const _ = require('lodash');
const Router = require('koa-router');
const moment = require('moment');
const pager = require('../utils/pager');
const Mysql = require('../../../lib/mysql-promise');
const Promise = require('bluebird');
const r = new Router();
const rp = require('request-promise');
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
};
});
const insertWord = (keyword, mysql) => {
return mysql.query(`SELECT id FROM seo_keywords WHERE keyword = ?`, [keyword]).then(d => {
console.log(keyword, d, '--+++++++++++++++++++++++-');
if (d.length <= 0) {
return mysql.query(
`INSERT INTO seo_keywords SET keyword = ?, is_push = ?, yoho_goods_num= ?, add_time = ?`,
[keyword, 1, -2, moment().unix()]
);
}
return d;
});
}
//redis关键词同步到mysql
r.get('/syncWord', async(ctx) => {
let mysql = new Mysql();
return getDataList(ctx, "keywords_mana_list", 0, -1).then(d => {
return getDataValues(ctx, d[0]);
}).then(ddata => {
return Promise.map(ddata, (word) => insertWord(word, mysql), {concurrency: 15});
}).then(d => {
return ctx.body = {
code: 200,
message: 'success',
data: d
};
});
});
r.get('/expand', async(ctx) => {
let resData = {};
let q = ctx.request.query;
let query = q.query || '';
let page = parseInt(`0${ctx.query.page}`, 10) || 1;
let limit = parseInt(`0${ctx.query.limit}`, 10) || 10;
let mysql = new Mysql();
let total = 0;
let typeList = [
{
type: 'keyword',
name: '关键词'
},
{
type: 'wordroot',
name: '词 根'
},
{
type: 'goodnum',
name: '商品数'
}
];
let type = q.type || typeList[0].type;
let typeName = _.result(_.find(typeList, { 'type': type}), 'name') || typeList[0].name;
let wheres = '';
let conditions = [];
switch (query && type) {
case 'keyword':
wheres = ` AND keyword like '%%${query}%'`;
break;
case 'wordroot':
wheres += ' AND root_id = ?'
conditions.push(query);
break;
case 'brand':
wheres += ' AND brand_id = ?'
conditions.push(query);
break;
case 'sort':
wheres += ' AND sort_id = ?'
conditions.push(query);
break;
case 'goodnum':
wheres += ' AND yoho_goods_num >= ?'
conditions.push(query);
break;
}
return mysql.query(`SELECT COUNT(*) as total FROM seo_keywords WHERE 1 = 1 ${wheres}`, conditions).then(d => {
total = d[0] && d[0].total || 0;
conditions.push((page - 1) * limit, limit);
return mysql.query(`SELECT * FROM seo_keywords WHERE 1 = 1 ${wheres} limit ?, ?`, conditions);
}).then(d => {
resData.pager = pager(Math.floor((total - 1) / limit) + 1, ctx.query);
resData.tabs = _.map(d, (elem) => {
return Object.assign({}, elem, {
is_push: elem.is_push ? '是' : '否',
add_time: elem.add_time && moment(elem.add_time * 1000).format('YYYY-MM-DD HH:mm'),
});
});
resData.typeList = typeList;
resData.typeName = typeName;
resData.type = type;
resData.query = query;
return ctx.render('action/keywords/expand', resData);
});
});
r.post('/expand/del', async(ctx) => {
let q = ctx.request.body;
let ids = q.ids && JSON.parse(q.ids) || [];
let len = ids.length;
let marks = [];
if (len <= 0) {
return ctx.body = {
code: 400,
message: 'ids is empty',
data: ''
};
}
let mysql = new Mysql();
for(let i = 0; i < len; i++) {
marks.push('?');
}
return mysql.query(`DELETE FROM seo_keywords WHERE id IN (${marks.join(',')})`, ids).then(d => {
return ctx.body = {
code: 200,
message: 'success',
data: d
};
});
});
module.exports = r;