seoModel.js
2.46 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
'use strict';
const _ = require('lodash');
const seoHelper = require('../utils/seoHelper');
const model = require('../../../lib/model');
class seoModel extends model {
constructor(ctx) {
super(ctx);
}
/**
* 获取所有品牌(redis || api)
*/
getBrands() {
let brandsRedisKey = 'global:yoho:brands';
// 先取redis
return this.ctx.redis.getAsync(brandsRedisKey).then(brands => {
if (!brands) {
// 调用接口
return this.get('http://192.168.102.216:8080/yohosearch/brand/list.json').then(res => {
if (res) {
this.ctx.redis.setAsync(brandsRedisKey, JSON.stringify(res));
brands = res;
}
return brands;
})
} else {
return JSON.parse(brands);
}
}).catch(()=>{
return {};
});
}
/**
* 获取所有分类 (redis || api)
*/
getSort() {
let sortsRedisKey = 'global:yoho:sorts';
// 先取redis
return this.ctx.redis.getAsync(sortsRedisKey).then(sorts => {
if (!sorts) {
// 调用接口
return this.get('http://192.168.102.216:8080/yohosearch/sortgroup.json',{needAllSort:1, needSmallSort: 1}).then(res => {
if (res) {
this.ctx.redis.setAsync(sortsRedisKey, JSON.stringify(res));
sorts = res;
}
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() {
return Promise.all([this.getBrands(), this.getSort()]).then(result => {
return {brand: seoHelper.sortBrands(result[0]), sort: seoHelper.getSubSorts(result[1])};
})
}
/**
* 获取子分类
*/
getSubSorts(sortId) {
return this.getSort().then(sorts => {
return seoHelper.getSubSorts(sorts, sortId);
});
}
}
module.exports = seoModel;