categoryBActions.js
10.9 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
'use strict';
import ReactNative from 'react-native';
import CategoryBService from '../../services/CategoryBService';
import Immutable, {Map} from 'immutable';
const {
SET_TYPE,
SET_DELAY_EXPOSE_DATA,
GET_CATEGORY_B_LIST_REQUEST,
GET_CATEGORY_B_LIST_SUCCESS,
GET_CATEGORY_B_LIST_FAILURE,
GET_CATEGORY_B_SUBDETAIL_REQUEST,
GET_CATEGORY_B_SUBDETAIL_SUCCESS,
GET_CATEGORY_B_SUBDETAIL_FAILURE,
SET_CURRENT_CATEGORY_B,
SET_CURRENT_CHANNEL_B,
SET_CURRENT_CATEGORY_B_SUBDETAIL_FROM_CACHE,
JUMP_TO_CATEGORY,
} = require('../../constants/actionTypes').default;
export function setDelayExposeData(delayExposeData) {
return {
type: SET_DELAY_EXPOSE_DATA,
payload: delayExposeData,
}
}
export function getCategoryBList() {
return (dispatch, getState) => {
let {app, categoryB} = getState();
dispatch(getCategoryBListRequest());
return new CategoryBService(app.host).getCategoryBList()
.then(json => {
dispatch(getCategoryBListSuccess(json));
dispatch(getCategoryBFirstSubCategoryDetail(categoryB.currentChannelId));
})
.catch(error => {
dispatch(getCategoryBListFailure(error));
});
};
}
/**
* 点击More事件
**/
export function pressCategoryBMore(category_id){
return (dispatch, getState) => {
let {categoryB} = getState();
//获取当前频道下一级分类列表信息
let categoryData = categoryB.categoryList ? categoryB.categoryList.get(categoryB.currentChannelValue) : [];
//获取指定category_id分类信息
let category = null;
categoryData.map((item, i) => {
if(item && (category_id == item.get('category_id'))){
category = item;
}
});
let all = {
category_name: "全部" + (category ? category.get('category_name') : ""),
parent_id: category_id + '',
relation_parameter: category ? category.get('relation_parameter').toJS() : {},
node_count: category ? category.get('node_count') : 0,
};
//设置当前的类别信息
// dispatch(setCurrentCateB(category_id, category ? category.get('category_name') : ""));
dispatch(jumpToCategory(all, 0, categoryB.currentChannelId));
};
}
export function fetchSubCategory(categoryId, categoryValue, callback) {
return (dispatch, getState) => {
let {
categoryB
} = getState();
let {
currentChannelId,
currentCateId,
currentCateValue,
cacheSubCateData,
delayExposeData,
} = categoryB;
//检查缓存是否存在数据,如果不存在则获取
let key = getSubDetailCacheKey(currentChannelId, categoryId);
let categoryData = cacheSubCateData ? cacheSubCateData.get(key) : "";
//已命中,展示缓存数据
if (categoryData) {
dispatch(getCategoryBSubDetailFromCache(categoryData));
//曝光从缓存获取的数据
if (delayExposeData) {
} else {
//曝光从缓存获取的数据
dispatch(dataExposure(currentChannelId, categoryId, categoryValue, categoryData));
}
//未命中,访问网络数据
} else {
callback && callback();
}
};
}
/**
* 获取子分类的数据,逻辑上先判断缓存数据是否存在,不存在则从网络获取
**/
export function getCategoryBSubCategoryDetail(categoryId, categoryValue){
return (dispatch, getState) => {
let {categoryB} = getState();
let {
currentChannelId,
currentCateId,
currentCateValue,
cacheSubCateData,
contentError,
} = categoryB;
//检查缓存是否存在数据
let key = getSubDetailCacheKey(currentChannelId, categoryId);
let categoryData = cacheSubCateData ? cacheSubCateData.get(key) : "";
//已有缓存
if (categoryData && categoryData.size > 0) {
let lastTimestamp = categoryData.get('lrts', 0);
let currentTimestamp = new Date().getTime();
let diff = currentTimestamp - lastTimestamp;
if (diff < 400) {
__DEV__ && console.log('timestamp diff < 50, return');
return;
}
}
if (contentError) {
let lastTimestamp = contentError.lrts;
let currentTimestamp = new Date().getTime();
let diff = currentTimestamp - lastTimestamp;
if (diff < 400) {
__DEV__ && console.log('timestamp diff < 50, return');
return;
}
}
if (currentCateId == categoryId && categoryB.contentFetching) {
return;
}
dispatch(getCategoryBSubDetail(currentChannelId, categoryId, categoryValue));
};
}
/**
* 获取子分类的KEY值
**/
function getSubDetailCacheKey(channel_id, category_id){
let key = "CHA_" + channel_id + "_CAT_" + category_id;
return key;
}
/**
* 网络获取子分类的数据
**/
export function getCategoryBSubDetail(channel_id, category_id, category_value) {
return (dispatch, getState) => {
let {app, categoryB} = getState();
if (categoryB.contentFetching) {
return;
}
//各频道固定资源位
let content_code = '';
if(channel_id == '1'){
content_code = 'daaa8b1a5103a30419ebd79c06e6feac';
}
else if(channel_id == '2'){
content_code = '1a18449fa785631b302dc6d27388bf93';
}
else if(channel_id == '3'){
content_code = '591152bee9fddcb0b4f793aa38304cb5';
}
else if(channel_id == '4'){
content_code = 'e823582db5ead63c82e0d348c4e6a6bb';
}
dispatch(getCategoryBSubDetailRequest());
return new CategoryBService(app.host).getCategoryBSubDetail(channel_id, category_id, content_code)
.then(json => {
let payload = parseCategoryBSubDetail(channel_id, category_id, json, categoryB);
dispatch(getCategoryBSubDetailSuccess(payload));
if (categoryB.delayExposeData) {
} else {
//曝光从网络获取的数据
dispatch(dataExposure(channel_id, category_id, category_value, json));
}
})
.catch(error => {
error.lrts = new Date().getTime();
dispatch(getCategoryBSubDetailFailure(error));
});
};
}
/**
*获取指定channel的第一个分类数据
**/
export function getCategoryBFirstSubCategoryDetail() {
return (dispatch, getState) => {
let {categoryB} = getState();
let {currentChannelId, currentChannelValue, categoryList, contentFetching} = categoryB;
if (contentFetching) {
return;
}
let category = categoryList.get(currentChannelValue) ? categoryList.get(currentChannelValue).get(0) : "";
let category_id = category ? category.get('category_id') : "";
let category_name = category ? category.get('category_name') : "";
dispatch(setCurrentCateB(category_id, category_name))
if(category){
dispatch(getCategoryBSubCategoryDetail(category_id, category_name));
}
};
}
/**
*在子分类的数据尾部添加一个MORE,因为需要获取relation_parameter 和 node_count数据,所以不得不从分类列表中筛选找出
**/
function parseCategoryBSubDetail(channel_id, category_id, subcategory_data, props_data) {
if(!subcategory_data || !props_data){
return;
}
subcategory_data.lrts = new Date().getTime();
let key = getSubDetailCacheKey(channel_id, category_id);
return {key: key, data: subcategory_data};
}
export function getCategoryBListRequest() {
return {
type: GET_CATEGORY_B_LIST_REQUEST,
};
}
export function getCategoryBListSuccess(json) {
return {
type: GET_CATEGORY_B_LIST_SUCCESS,
payload: json
}
}
export function getCategoryBListFailure(error) {
return {
type: GET_CATEGORY_B_LIST_FAILURE,
payload: error
}
}
export function getCategoryBSubDetailRequest() {
return {
type: GET_CATEGORY_B_SUBDETAIL_REQUEST,
};
}
export function getCategoryBSubDetailSuccess(json) {
return {
type: GET_CATEGORY_B_SUBDETAIL_SUCCESS,
payload: json
}
}
export function getCategoryBSubDetailFailure(error) {
return {
type: GET_CATEGORY_B_SUBDETAIL_FAILURE,
payload: error
}
}
export function setCurrentCateB(categoryId, categoryValue){
return {
type: SET_CURRENT_CATEGORY_B,
payload: {id: categoryId, value : categoryValue}
}
}
export function setCurrentChannelB(channelId, channelValue){
return {
type: SET_CURRENT_CHANNEL_B,
payload: {id: channelId, value : channelValue}
}
}
export function getCategoryBSubDetailFromCache(categoryData) {
return {
type: SET_CURRENT_CATEGORY_B_SUBDETAIL_FROM_CACHE,
payload: categoryData
}
}
export function jumpToCategory(value, index, channelId){
ReactNative.NativeModules.YH_CommonHelper.jumpToCategory(value, parseInt(index), parseInt(channelId));
return {
type: JUMP_TO_CATEGORY,
payload: value
}
}
export function startExposeData() {
return (dispatch, getState) => {
let {
categoryB
} = getState();
let {
currentChannelId,
currentCateId,
currentCateValue,
cacheSubCateData,
delayExposeData,
} = categoryB;
//检查缓存是否存在数据,如果不存在则获取
let key = getSubDetailCacheKey(currentChannelId, currentCateId);
let categoryData = cacheSubCateData ? cacheSubCateData.get(key) : "";
//已命中,展示缓存数据
if (categoryData) {
//曝光从缓存获取的数据
dispatch(dataExposure(currentChannelId, currentCateId, currentCateValue, categoryData.toJS()));
}
}
}
function dataExposure(channel_id, category_id, category_value, json) {
return (dispatch, getState) => {
if (!json) {
return;
}
try {
let dataList = [];
//bannerInfo数据曝光
let bannerInfo = json.bannerInfo;
if(bannerInfo && bannerInfo.length > 0){
let bannerList = [];
bannerInfo.map((item, i) => {
bannerList.push({
I_INDEX: i + 1 + '',
F_URL: item.data[0].url,
});
})
let bannerData = {
L1_CATE: category_id + '',
F_ID: '1001',
F_NAME: 'BANNER',
LIST: bannerList,
}
dataList.push(bannerData);
}
//sortInfo数据
let sortInfo = json.sortInfo;
if(sortInfo && sortInfo.length > 0){
let sortList = [];
sortInfo.map((item, i) => {
sortList.push({
I_INDEX: i + 1 + '',
PRD_SKN: item.product_skn + '',
L2_CATE: item.category_id + '',
});
})
let sortData = {
L1_CATE: category_id + '',
F_ID: '1002',
F_NAME: '全部'+category_value,
LIST: sortList,
}
dataList.push(sortData);
}
//brandInfo数据
let brandInfo = json.brandInfo;
if(brandInfo && brandInfo.length > 0){
let brandList = [];
brandInfo.map((item, i) => {
brandList.push({
I_INDEX: i + 1 + '',
BRAND_ID: item.id + '',
});
})
let brandData = {
L1_CATE: category_id + '',
F_ID: '1003',
F_NAME: '热门品牌',
LIST: brandList,
}
dataList.push(brandData);
}
let params = {
TAB_ID: channel_id + '',
DATA: dataList,
};
ReactNative.NativeModules.YH_CommonHelper.logEvent('YB_SHOW_CATEGORY', params);
} catch (e) {
__DEV__ && console.error(e);
} finally {
}
};
}