hot-keywords.js
5.33 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
/**
*
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 18/5/10
*/
const _ = require('lodash');
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 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;
})
module.exports = r;