seoModel.js
5.7 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
'use strict';
const _ = require('lodash');
const seoHelper = require('../utils/seoHelper');
const model = require('../../../lib/model');
const mysqlPromise = require('../../../lib/mysql-promise');
const config = require('../../../config/config');
const singleBrandKeyPre = config.singleBrandKeyPre;
const singleSortKeyPre = config.singleSortKeyPre;
const brandUrl = `${config.domains.search}brand/list.json`;
const sortUrl = `${config.domains.search}sortgroup.json`;
class seoModel extends model {
constructor(ctx) {
super(ctx);
this.mysql = new mysqlPromise();
}
/**
* 获取所有品牌(redis || api)
*/
getBrands() {
let brandsRedisKey = 'global:yoho:brands';
// 先取redis
return this.ctx.redis.getAsync(brandsRedisKey).then(brands => {
if (!brands) {
// 调用接口
return this.get(brandUrl).then(res => {
if (res) {
this.ctx.redis.setAsync(brandsRedisKey, JSON.stringify(res));
brands = res;
this.storeBrands(res);
}
return brands;
})
} else {
return JSON.parse(brands);
}
}).catch(()=>{
return {};
});
}
/**
* 根据品牌id存储品牌名称到redis
*/
storeBrands(brands) {
let operation = [];
_.forEach(_.get(brands, 'data'), val => {
operation.push(['set', `${singleBrandKeyPre}${val.id}`, val.brand_name]);
});
this.ctx.redis.multi(operation).execAsync();
}
/**
* 根据品类id存储品牌名称到redis
*/
storeSorts(sort) {
let operation = [];
_.forEach(_.get(sort, 'data.sort'), (msort) => {
operation.push(['set', `${singleSortKeyPre}${msort.sort_id}`, msort.sort_name]);
_.forEach(_.get(msort, 'sub'), (misort) => {
operation.push(['set', `${singleSortKeyPre}${misort.sort_id}`, misort.sort_name]);
_.forEach(_.get(misort, 'sub'), (sort) => {
operation.push(['set', `${singleSortKeyPre}${sort.sort_id}`, sort.sort_name]);
})
})
})
this.ctx.redis.multi(operation).execAsync();
}
/**
* 获取所有分类 (redis || api)
*/
getSort() {
let sortsRedisKey = 'global:yoho:sorts';
// 先取redis
return this.ctx.redis.getAsync(sortsRedisKey).then(sorts => {
if (!sorts) {
// 调用接口
return this.get(sortUrl,{needAllSort:1, needSmallSort: 1}).then(res => {
if (res) {
this.ctx.redis.setAsync(sortsRedisKey, JSON.stringify(res));
sorts = res;
this.storeSorts(sorts);
}
return sorts;
})
} else {
return JSON.parse(sorts);
}
}).catch(()=>{
return {};
});
}
/**
* 添加词根数据
*/
addRootWordsData() {
return Promise.all([this.getBrands(), this.getSort()]).then(result => {
return {brand: seoHelper.sortBrands(result[0]), sort: seoHelper.getSubSorts(result[1])};
})
}
/**
* 词根列表数据
*/
rootWordsData(page, limit) {
let sql = `select * from seo_keywords_root where status=1 order by id desc limit ${(page-1)*limit}, ${limit};`,
countSql = `select count(id) from seo_keywords_root where status=1;`;
return Promise.all([this.mysql.query(countSql), this.mysql.query(sql)]).then(res => {
let list = _.get(res, '1'),
operate = [];
_.forEach(list, val => {
operate.push(['get', `${singleBrandKeyPre}${val.brands_id}`]);
operate.push(['get', `${singleSortKeyPre}${val.sort_id}`]);
})
return this.ctx.redis.multi(operate).execAsync().then(result => {
_.forEach(list, (val, key) => {
list[key].brand_name = result[2*key];
list[key].sort_name = result[2*key+1];
})
return {total: res[0], list: list};
});
});
}
/**
* 获取子分类
*/
getSubSorts(sortId) {
return this.getSort().then(sorts => {
return seoHelper.getSubSorts(sorts, sortId);
});
}
/**
* 查看词根是否存在
*/
queryKeyword(keyword) {
return this.mysql.query(`select * from seo_keywords_root where keyword = '${keyword}' and status = 1 limit 1`).then(res => {
return res;
});
}
/**
* 添加词根
*/
addRootWord(rootWord, brand, sort, filterWord, isKeyword) {
let result = {};
let sql = `insert into seo_keywords_root (keyword, brands_id, sort_id, filter_words, is_expand, is_keyword, add_time, status) values
('${rootWord}', ${brand}, ${sort}, '${filterWord}', 0, ${isKeyword}, ${Math.floor(new Date().getTime()/1000)}, 1)`;
return this.mysql.query(sql).then(res => {
return result = {code:200};
}).catch(err=>{
return result = {code:201, message: '插入失败'};
})
}
/**
* 删除词根
*/
delRoorWord(ids) {
let sql = `update seo_keywords_root set status=0 where id in (${ids});`;
return this.mysql.query(sql);
}
/**
* 词根设为关键词
*/
rootToKeywords(ids) {
let sql = `update seo_keywords_root set is_keyword=1 where id in (${ids});`;
return this.mysql.query(sql);
}
}
module.exports = seoModel;