outlet.js
8.75 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* 频道页面 model
* @author: 赵彪<bill.zhao@yoho.cn>
* @date: 2016/05/09
*/
'use strict';
const utils = '../../../utils';
const contentCodeConfig = require('../../../config/content-code');
const _ = require('lodash');
const log = global.yoho.logger;
const serviceAPI = global.yoho.ServiceAPI;
const resourcesProcess = require(`${utils}/resources-process`);
const dateFormate = (str) => {
var time = new Date(str * 1000);
var y = time.getFullYear();
var m = time.getMonth() + 1;
var d = time.getDate();
var h = time.getHours();
return y + '年' + m + '月' + d + '日' + h + '时';
};
// 为了活动卡片特殊样式,将折扣信息拆分开来
const _transDiscountToArr = (discount) => {
return discount.replace(/(?:\d+[.\d]?)([\u4e00-\u9fa5]{1})/g, function(fullMatch, capture) {
if (capture) {
const arr = [];
arr.push(fullMatch.replace(capture, ''));
arr.push(capture);
return arr;
} else {
return fullMatch;
}
}).split(',');
};
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 获取资源位
* @param {String} channel 频道
* @param {String} contentcode 内容码
* @return {Promise}
*/
_getOutletResource(channel, contentcode) {
const params = {
content_code: contentcode || contentCodeConfig.outlet,
limit: 25,
yh_channel: channel || ''
};
return this.get({
api: serviceAPI,
url: 'operations/api/v5/resource/home',
data: params,
param: {
cache: true
}
}).then(result => {
if (result && result.code === 200) {
return resourcesProcess(result.data.list);
} else {
log.error('the response code of outlet "operations/api/v5/resource/home" is NOT 200', result);
return [];
}
});
}
/**
* 转换导航数据
* @param {[Object]} 原始导航数据
* @return {Object} 转换后的数据
*/
_convertNavData(list) {
const formatData = [];
list = list || [];
// list = camelCase(list);
_.forEach(list, (item) => {
formatData.push({
id: item.id,
name: item.sort_name,
url: encodeURI(item.sort_url)
});
});
return {
data: formatData
};
}
/**
* 获取导航数据
* @param {String} 导航类型id
* @return {Promise}
*/
_getNavData(categoryId) {
const params = {
parent_id: categoryId
};
return this.get({
api: serviceAPI,
url: 'operations/api/v6/category/getCategory',
data: params,
param: {
cache: true
}
}).then(result => {
if (result && result.code === 200) {
let data = this._convertNavData(result.data);
data.category = categoryId;
return data;
} else {
log.error('the response code of "operations/api/v6/category/getCategory" is NOT 200', result);
return [];
}
});
}
/**
* 转换奥莱活动数据
* @param {Object} data 原始数据
* @return {Object} 转换后的数据
*/
_convertActicityData(data) {
const formatData = [];
let discountArr = [],
discountNum = 0,
discountText = 0;
data = data || [];
_.forEach(data, (item) => {
if (item.promotionName) {
discountArr = item.promotionName.split('~');
if (discountArr.length === 1) {
discountNum = _transDiscountToArr(discountArr[0])[0];
discountText = _transDiscountToArr(discountArr[0])[1];
} else {
discountNum = discountArr[0] + '~' + _transDiscountToArr(discountArr[1])[0];
discountText = _transDiscountToArr(discountArr[1])[1];
}
formatData.push({
activityUrl: '/product/outlet/activity?id=' + item.id,
coverUrl: item.coverUrl,
logoUrl: item.logoUrl,
title: item.title,
discountNum: discountNum,
discountText: discountText,
productPoolId: item.productPoolId || '',
leftTime: item.startLeftTime > 0 ? dateFormate(item.startTime) : item.endLeftTime,
hide: false
});
}
});
return formatData;
}
/**
* 获取奥莱活动详情
* @param {String} id 活动id
* @return {Promise} 调用接口的Promise
*/
_getActivityDetail(id) {
var params = {
method: 'app.outlets.activityGet',
sort: 1, // 接口规定传1
platform: 3, // h5平台代号
id: id,
type: 0 // 接口规定传0
};
return this.get({
data: params,
param: {
cache: true
}
}).then(res => {
if (res.code === 200) {
return this._convertActicityData(res.data);
} else {
log.error('the response code of "app.outlets.activityGet" is NOT 200', res);
return {};
}
});
}
/**
* 获取奥莱资频道页活动列表
* @param {Object} data 请求接口所需的参数
* @return {Promise} 调用接口的Promise
*/
_getHomeActivity(data) {
var params = {
method: 'app.outlets.activityGet',
platform: 3 // h5平台代号
};
return this.get({
data: _.assign(params, data),
param: {
cache: true
}
}).then(res => {
return this._convertActicityData(res.data);
});
}
/**
* 获取奥莱资首页内容
* @param {String} categoryId 父级菜单id,用于标明当前页面是奥莱页面
* @param {Strting} channel 奥莱频道
* @param {Strting} code 内容码
* @return {Promise} 调用接口的Promise
*/
getContent(categoryId, channel, code) {
let params = {
type: 0, // 获取全部奥莱活动列表, 不区分是否将开始或结束
yh_channel: channel
};
return Promise.all([
this._getNavData(categoryId),
this._getOutletResource(channel, code),
this._getHomeActivity(params)
]).then(data => {
return {
nav: data[0] || [],
content: data[1] || [],
activity: data[2]
};
});
}
/**
* 获取奥莱活动详情
* @param {String} id 活动id
* @return {Promise} 调用接口的Promise
*/
getActivity(id) {
return this._getActivityDetail(id).then(res => {
return {
activity: res,
productPool: res[0] && res[0].productPoolId || '',
activityTitle: res[0] && res[0].title || '奥莱',
saleType: 4 // 促销类型, 奥莱为4
};
});
}
/**
* 获取即将开始或即将结束的活动列表
* @param {Number} type 标明是上线预告还是即将结束
* @param {String} categoryId 父级菜单id,用于标明当前页面是奥莱页面
* @return {Object} 活动列表数据
*/
getRecentActivity(type, categoryId) {
var params = {
type: type
};
return Promise.all([
this._getNavData(categoryId),
this._getHomeActivity(params)
]).then(res => {
return {
nav: res[0] || [],
activity: res[1]
};
});
}
// pageCache 单独获取活动时间
getActivityTime(params) {
return this.get({
data: _.assign({
method: 'app.outlets.activityGet',
platform: 3 // h5平台代号
}, params),
param: {
cache: true
}
}).then(res => {
var times = [];
if (res && res.code === 200) {
_.forEach(res.data, item => {
times.push(item.startLeftTime > 0 ? dateFormate(item.startTime) : item.endLeftTime);
});
return times;
} else {
log.error('the response code of "app.outlets.activityGet" is NOT 200', res);
return {};
}
});
}
};