appActions.js
7.97 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
'use strict';
import ReactNative from 'react-native';
import AppService from '../../services/AppService';
import {historyList} from "../recommend/recommendActions";
import htmlparser from 'htmlparser2-without-node-native';
const Platform = require('Platform');
const {
SET_PLATFORM,
SET_CHANNEL,
SET_HOST,
SET_SERVICE_HOST,
SET_PRODUCT_POOL,
SET_SKNS,
PRODUCT_LIST_REQUEST,
PRODUCT_LIST_SUCCESS,
PRODUCT_LIST_FAILURE,
APP_SET_SELECTED_CATEGORY,
BANNER_INVITECODE_REQUEST,
BANNER_INVITECODE_SUCCESS,
BANNER_INVITECODE_FAILURE,
} = require('../../constants/actionTypes').default;
export function setAppSelectedCategory(productPool, skns, sort_id, sort_name, index) {
return (dispatch, getState) => {
dispatch({
type: APP_SET_SELECTED_CATEGORY,
payload: {index, sort_id, sort_name}
});
dispatch(getProductList(productPool,skns,sort_id));
};
}
export function setPlatform(platform) {
return {
type: SET_PLATFORM,
payload: platform
};
}
export function setChannel(channel) {
return {
type: SET_CHANNEL,
payload: channel
};
}
export function setHost(host) {
return {
type: SET_HOST,
payload: host,
}
}
export function setServiceHost(host) {
return {
type: SET_SERVICE_HOST,
payload: host
};
}
export function setproductPool(productPool) {
return {
type: SET_PRODUCT_POOL,
payload: productPool,
}
}
export function setSkns(skns) {
return {
type: SET_SKNS,
payload: skns
}
}
export function productListRequest() {
return {
type: PRODUCT_LIST_REQUEST,
};
}
export function productListSuccess(json) {
return {
type: PRODUCT_LIST_SUCCESS,
payload: json
};
}
export function productListFailure(error) {
return {
type: PRODUCT_LIST_FAILURE,
payload: error
};
}
export function inviteCodeRequest() {
return {
type:BANNER_INVITECODE_REQUEST
}
}
export function inviteCodeSuccess(json) {
return {
type: BANNER_INVITECODE_SUCCESS,
payload: json
};
}
export function inviteCodeFailure(error) {
return {
type: BANNER_INVITECODE_FAILURE,
payload: error
};
}
export function getInviteCode() {
return (dispatch, getState) => {
let {app} = getState();
let fetchInviteCode = () => {
dispatch(inviteCodeRequest());
return new AppService(app.host).fetchInvitedCodeInfo()
.then(json => {
exposeHtmlContent(json ,function (nodelist) {
let payload = json;
nodelist.forEach(item => {
payload[item.name] = item;
})
dispatch(inviteCodeSuccess(payload));
});
})
.catch(error => {
dispatch(inviteCodeFailure(error));
})
}
fetchInviteCode();
}
}
//解析html形式的内容为可用版本
function exposeHtmlContent(json, callbackFun) {
let res = json;
let context = res.context ? '<context>' + res.context + '</context>' : '<context></context>';
let shareMainTitle = res.shareMainTitle ? '<sharemaintitle>' + res.shareMainTitle + '</sharemaintitle>' : '<sharemaintitle></sharemaintitle>';
let shareSubTitle = res.shareSubTitle ? '<sharesubtitle>' + res.shareSubTitle + '</sharesubtitle>' : '<sharesubtitle></sharesubtitle>';
let parseStr = context + shareMainTitle + shareSubTitle;
let node = {
name : '',
textlist: [],
}
let nodelist = [];
let parser = new htmlparser.Parser({
onopentag: function(name, attribs){
switch (name) {
case 'context':
{
node.name = name;
node.textlist = [Object.assign({}, attribs)];
}
break;
case 'sharemaintitle':
{
node.name = 'shareMainTitle';
node.textlist = [Object.assign({}, attribs)];
}
break;
case 'sharesubtitle':
{
node.name = 'shareSubTitle';
node.textlist = [Object.assign({}, attribs)];
}
break;
default: {
node.textlist.push(Object.assign({}, attribs));
}
break;
}
},
ontext: function(text){
let dic = node.textlist.pop();
dic && (dic.text = text);
node.textlist.push(dic);
},
onclosetag: function(name){
switch (name) {
case 'context':
case 'sharemaintitle':
{
nodelist.push(Object.assign({}, node)) ;
node = {
name : '',
textlist: [],
};
}
break;
case 'sharesubtitle':
{
nodelist.push(Object.assign({}, node)) ;
callbackFun && callbackFun(nodelist)
}
break;
default: {
node.textlist.push({});
}
break;
}
}
})
parser.write(parseStr);
parser.end();
}
export function getProductList(product_pool = 0, skns = 0,msort = 0) {
return (dispatch, getState) => {
let {app} = getState();
let list = app.productList;
/**
* page: 0, //当前页面
* page_size: 20, //每页显示的数量
* total: 0, //总共多少条
* page_total: 0, //总共多少页
* endReached: false, //到达底部
*/
if (list.isFetching || list.endReached || list.error || (!list.endReached && list.page_total === 1)) {
return;
}
let page = list.page + 1;
let pageSize = list.page_size;
console.log(pageSize);
let fetchProductList = (uid) => {
dispatch(productListRequest());
return new AppService(app.host).fetchProductList(uid, page, pageSize, msort, product_pool, skns)
.then(json => {
let payload = json;
try {
payload = exposeProductListData(json);
} catch (e) {
}
payload.endReached = (payload.page === payload.page_total) && (payload.page_total !== 1);
if (payload.page > 1) {
let oldList = list.product_list.toJS();
let newList = [...oldList, ...payload.product_list];
payload.product_list = newList;
}
dispatch(productListSuccess(payload));
})
.catch(error => {
dispatch(productListFailure(error));
});
};
let uid = 0;
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(uid => {
fetchProductList(uid)
})
.catch(error => {
fetchProductList(uid)
});
};
}
function exposeProductListData(json) {
let list = json ? json.product_list : [];
let res = json;
if (!list) {
return list;
}
let pageName = 'iFP_Alliance';
if (Platform.OS === 'android') {
pageName = 'aFP_Alliance';
}
for (var i = 0; i < list.length; i++) {
let item = list[i];
let skn = item.product_skn;
let inFloorIndex = parseInt(i) + 1 + '';
item.yh_exposureData = {
P_NAME: pageName,
I_INDEX: inFloorIndex,
PRD_SKN: skn,
exposureEnd: 1,
};
}
res.product_list = list;
return res;
}