channel.js
3.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
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
/**
* 频道页面 model
* @author: Bi Kai<kai.bi@yoho.cn>
* @date: 2016/05/09
*/
'use strict';
const _ = require('lodash');
const API = require('../../../library/api');
const sign = require('../../../library/sign');
const camelCase = require('../../../library/camel-case');
const api = new API();
const genderData = {
boy: '1,3',
girl: '2,3'
};
const contentCode = {
boy: '8512bf0755cc549ac323f852c9fd945d',
girl: '8512bf0755cc549ac323f852c9fd945d'
};
/**
* 获取二级菜单顶部颜色
* @param {[string]} choosed
* @return {[string]}
*/
const getSidebarColor = (choosed) => {
let color = false;
if (choosed === 'girls') {
color = '#FF88AE';
} else if (choosed === 'kids') {
color = '#7ad9f9';
} else if (choosed === 'lifestyle') {
color = '#4f4138';
}
return color;
};
/**
* 处理侧边栏数据
* @param {[array]} list
* @return {[array]}
*/
const processSideBar = (list, choosed) => {
const formatData = [];
let offset = 0; // 分割数组用到的游标
list = list || [];
list = camelCase(list);
_.forEach(list, (item, i) => {
if (item.sub) {
item.sub.unshift({
sortName: item.sortName,
sortNameEn: item.sortNameEn,
back: true,
isSelect: false,
bgColor: getSidebarColor(choosed)
});
}
// 如果有分隔符,分割数组
if (item.separativeSign === 'Y') {
formatData.push(list.slice(offset, i));
offset = i;
}
});
// 数组被分割剩余的部分
formatData.push(list.slice(offset));
return formatData;
};
/**
* 处理楼层数据
* @param {[array]} list
* @return {[array]}
*/
const processFloor = (list) => {
const formatData = [];
list = list || [];
list = camelCase(list);
_.forEach(list, (floor) => {
floor[_.camelCase(floor.templateName)] = true;
formatData.push(floor);
});
return formatData;
};
/**
* 获取频道页面资源位
* @param {[object]} gender
* @return {[type]}
*/
const getChannelResource = (gender) => {
gender = gender || 'boy';
return api.get('operations/api/v5/resource/home', sign.apiSign({
new_device: true, // eslint-disable-line
gender: genderData[gender],
content_code: contentCode[gender], // eslint-disable-line
page: 1,
limit: 30
})).then(result => {
result = JSON.parse(result);
if (result.code === 200) {
return processFloor(result.data.list);
} else {
return result;
}
});
};
/**
* 获取左侧边栏数据
* @param {[string]} choosed
* @return {[object]}
*/
const getLeftNav = (choosed) => {
choosed = choosed || 'all';
return api.get('operations/api/v6/category/getCategory', sign.apiSign({})).then(result => {
result = JSON.parse(result);
if (result.code === 200) {
return processSideBar(result.data, choosed);
} else {
return result;
}
});
};
/**
* 获取频道页面数据
* @param {[object]} params
* @return {[object]}
*/
exports.getChannelDate = (params) => {
var channelData = {};
return Promise.all([getChannelResource(params.gender), getLeftNav(params.gender)]).then((data) => {
channelData.content = data[0]; // 资源位数据
channelData.sideNav = data[1]; // 侧边栏数据
return channelData;
});
};