categoryBActions.js
5.59 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
'use strict';
import ReactNative from 'react-native';
import CategoryBService from '../../services/CategoryBService';
import Immutable, {Map} from 'immutable';
const {
SET_TYPE,
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 getCategoryBList(channel_id) {
return (dispatch, getState) => {
let {app, classify} = getState();
dispatch(getCategoryBListRequest());
return new CategoryBService(app.host).getCategoryBList()
.then(json => {
dispatch(getCategoryBListSuccess(json));
dispatch(getCategoryBFirstSubCategoryDetail(channel_id));
})
.catch(error => {
dispatch(getCategoryBListFailure());
});
};
}
/**
* 获取子分类的数据,逻辑上先判断缓存数据是否存在,不存在则从网络获取
**/
export function getCategoryBSubCategoryDetail(category_id, category_value){
return (dispatch, getState) => {
let {categoryB} = getState();
let currentChannelId = categoryB.currentChannelId;
//检查缓存是否存在数据,如果不存在则获取
let cache = categoryB.cacheSubCateData;
let key = getSubDetailCacheKey(currentChannelId, category_id);
let categoryData = cache.get(key);
//设置当前的类别信息
dispatch(setCurrentCateB(category_id, category_value));
//未命中,访问网络数据
if (!categoryData) {
// console.log("chenlin", "未命中缓存,调用接口访问数据:" + key);
dispatch(getCategoryBSubDetail(currentChannelId, category_id, category_value));
}
//已命中,展示缓存数据
else{
// console.log("chenlin", "命中缓存:" + JSON.stringify(categoryData));
dispatch(getCategoryBSubDetailData(categoryData));
}
};
}
/**
* 获取子分类
**/
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} = getState();
dispatch(getCategoryBSubDetailRequest());
return new CategoryBService(app.host).getCategoryBSubDetail(channel_id, category_id)
.then(json => {
let payload = parseCategoryBSubDetail(channel_id, category_id, json);
dispatch(getCategoryBSubDetailSuccess(payload));
})
.catch(error => {
dispatch(getCategoryBSubDetailFailure());
});
};
}
/**
*获取指定channel的第一个分类数据
**/
export function getCategoryBFirstSubCategoryDetail(channel_id) {
return (dispatch, getState) => {
let {categoryB} = getState();
let categoryList = categoryB.categoryList;
let category = null;
if(channel_id == '1'){
category = categoryList.get('boy').get(0);
}
else if(channel_id == '2'){
category = categoryList.get('girl').get(0);
}
else if(channel_id == '3'){
category = categoryList.get('kids').get(0);
}
else if(channel_id == '4'){
category = categoryList.get('lifestyle').get(0);
}
dispatch(getCategoryBSubCategoryDetail(category.get('category_id'), category.get('category_name')));
};
}
/**
*在尾部添加一个MORE
**/
function parseCategoryBSubDetail(channel_id, category_id, data) {
if(!data){
return;
}
if(data.sortInfo){
let more = {
category_name: 'more',
parent_id: category_id,
relation_parameter:{},
data_type:'text',
};
data.sortInfo.push(more);
}
let key = getSubDetailCacheKey(channel_id, category_id);
// let newData = {
// bannerInfo: data.bannerInfo,
// sortInfo: data.sortInfo,
// brandInfo: data.brandInfo,
// }
// console.log("chenlin", "parseCategoryBSubDetail处理后的JSON数据:" + JSON.stringify(data));
return {key: key, data: 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 getCategoryBSubDetailData(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
}
}