hot-keywords.js
7.08 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
/**
*
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 18/5/10
*/
const _ = require('lodash');
const fs = require('fs');
const Req = require('request-promise');
const moment = require('moment');
const Router = require('koa-router');
const r = new Router();
const Mysql = require('../../../lib/mysql-promise');
const pager = require('../utils/pager');
const xlsx = require('xlsx');
const config = require('../../../config/config');
const singleBrandKeyPre = config.singleBrandKeyPre;
const singleSortKeyPre = config.singleSortKeyPre;
//通过小分类同步中分类和大分类
const getMsort = async(ctx) => {
let redis = ctx.redis;
return redis.getAsync(`global:yoho:sorts`).then((sorts) => {
let sData = {};
sorts = JSON.parse(sorts) || [];
_.forEach(_.get(sorts, 'data.sort'), (msort) => {
_.forEach(_.get(msort, 'sub'), (misort) => {
_.forEach(_.get(misort, 'sub'), (sort) => {
if (!sData[sort.sort_id]) {
sData[sort.sort_id] = {
msort: msort.sort_id,
msort_name: msort.sort_name,
misort: misort.sort_id,
misort_name: misort.sort_name,
sort_id: sort.sort_id,
sort_name: sort.sort_name,
};
}
})
})
})
return sData;
});
};
r.get('/list', async(ctx) => {
let resData = {};
let q = ctx.request.query || {};
let query = q.query || '';
let page = parseInt(`0${q.page}`, 10) || 1;
let limit = parseInt(`0${q.limit}`, 10) || 10;
let mysql = new Mysql();
let total = 0;
let typeList = [
{
type: 'keyword',
name: '关键词'
},
{
type: 'wordroot',
name: '词根ID'
},
{
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 = [];
if (q.hot) {
wheres += ' AND is_hot = 1';
}
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;
}
let d = await mysql.query(`SELECT COUNT(*) as total FROM seo_keywords WHERE status = 1 ${wheres}`, conditions);
resData.total = d[0] && d[0].total || 0;
conditions.push((page - 1) * limit, limit);
d = await mysql.query(`SELECT * FROM seo_keywords WHERE status = 1 ${wheres} ORDER BY id DESC limit ?, ?`, conditions);
let sortIds = [];
sortIds = await getMsort(ctx);
resData.keywords = _.map(d, (elem) => {
const _sortId = sortIds[elem.sort_id];
return Object.assign({}, elem, {
sortName: _sortId && _sortId.sort_name,
misortName: _sortId && _sortId.misort_name,
msortName: _sortId && _sortId.msort_name,
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.body = {
code: 200,
message: 'success',
data: resData
};
});
r.post('/save', async (ctx) => {
let mysql = new Mysql();
let id = ctx.request.body.id || 0;
let keyword = ctx.request.body.keywords;
let brand = ctx.request.body.brand || 0;
let msort = ctx.request.body.msort || 0;
let misort = ctx.request.body.misort || 0;
let sort = ctx.request.body.sort || 0;
let describe = ctx.request.body.describe || '';
let goods_img = ctx.request.body.goodsImg || '';
let is_hot = ctx.request.body.isHot || 0;
let addTime = Date.parse(new Date())/1000;
if (id) {
return mysql.query(`UPDATE seo_keywords SET \`describe\` = '${describe}', goods_img = '${goods_img}' WHERE id = ${id}`).then(d => {
return ctx.body = {
code: 200,
message: 'success',
data: d
};
});
}
let result = {code: 400, message: ''};
if (!keyword) {
result.message = '关键词必填';
return ctx.response.body = result;
}
let select = await mysql.query(`select id from seo_keywords where keyword='${keyword}' limit 1`);
if (_.get(select, '[0].id', 0) > 0) {
result.message = '关键词已经存在';
return ctx.response.body = result;
}
await mysql.query(`insert into seo_keywords (keyword, brand_id, msort, misort, sort_id, add_time, \`describe\`, goods_img, is_hot) values ('${keyword}', ${brand}, ${msort}, ${misort}, ${sort}, ${addTime}, '${describe}', '${goods_img}', ${is_hot})`);
result.code = 200;
return ctx.response.body = result;
});
r.post('/upload', async(ctx) => { // 导入关键词EXCEL
if (ctx.request.body._files) {
let file = ctx.request.body._files.up_excel;
const workbook = xlsx.readFile(file.path);
const sheetNames = workbook.Props.SheetNames;
const worksheet = workbook.Sheets[sheetNames[0]];
let json_data = xlsx.utils.sheet_to_json(worksheet);
console.log(json_data);
let post_data = [];
_.each(json_data, (obj, index) => {
if (obj.keyword) {
post_data.push({
id: index,
keyword: obj.keyword,
describe: obj.describe || ''
})
}
});
console.log(post_data);
if (post_data.length > 0) {
return Req({ // 传数据给爬虫接口
method: 'POST',
uri: 'http://spiderwebhook.yoho.cn/importApiHot', // 'http://172.16.6.84:9100/importApiHot',
body: {
keywords: post_data
},
json: true,
timeout: 5000
}).then(res => {
// console.log('res:', res);
return ctx.response.body = res;
}).catch(err => {
// console.log(err);
return ctx.response.body = {
code: 301,
message: '接口数据处理错误'
}
});
} else {
return ctx.response.body = {
code: 400,
data: post_data,
message: 'excel无数据或数据格式不正确!'
}
}
}
});
module.exports = r;