messageActions.js
10.2 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
'use strict';
import ReactNative from 'react-native';
import {Actions} from 'react-native-router-flux';
import MessageService from '../../services/MessageService';
import timeago from '../../utils/timeago';
import {number10KFormater} from '../../utils/numberFormater';
import {unread} from '../user/userActions';
const {
MESSAGE_INFO_REQUEST,
MESSAGE_INFO_SUCCESS,
MESSAGE_INFO_FAILURE,
MESSAGE_CENTER_REQUEST,
MESSAGE_CENTER_SUCCESS,
MESSAGE_CENTER_FAILURE,
MESSAGE_LIKE_REQUEST,
MESSAGE_LIKE_SUCCESS,
MESSAGE_LIKE_FAILURE,
MESSAGE_SYST_REQUEST,
MESSAGE_SYST_SUCCESS,
MESSAGE_SYST_FAILURE,
MESSAGE_CLEAN,
} = require('../../constants/actionTypes').default;
const LIMIT = 10;
export function messageClean() {
return {
type: MESSAGE_CLEAN,
};
}
export function likeMessageRequest(ptr) {
return {
payload: ptr,
type: MESSAGE_LIKE_REQUEST,
};
}
export function likeMessageSuccess(json) {
return {
payload: json,
type: MESSAGE_LIKE_SUCCESS,
};
}
export function likeMessageFailue(error) {
return {
type: MESSAGE_LIKE_FAILURE,
payload: error
};
}
export function messageInfoRequest(ptr) {
return {
type: MESSAGE_INFO_REQUEST,
payload: ptr,
};
}
export function messageInfoSuccess(json) {
return {
type: MESSAGE_INFO_SUCCESS,
payload: json,
};
}
export function messageInfoFailue(error) {
return {
type: MESSAGE_INFO_FAILURE,
payload: error,
};
}
export function replyMessageRequest(ptr) {
return {
type: MESSAGE_CENTER_REQUEST,
payload: ptr,
};
}
export function replyMessageSuccess(json) {
return {
type: MESSAGE_CENTER_SUCCESS,
payload: json,
};
}
export function replyMessageFailue(error) {
return {
type: MESSAGE_CENTER_FAILURE,
payload: error,
};
}
export function systemMessageRequest(ptr) {
return {
type: MESSAGE_SYST_REQUEST,
payload: ptr,
};
}
export function systemMessageSuccess(json) {
return {
type: MESSAGE_SYST_SUCCESS,
payload: json,
};
}
export function systemMessageFailue(error) {
return {
type: MESSAGE_SYST_FAILURE,
payload: error,
};
}
export function likeMessage(ptr = false) {
return (dispatch, getState) => {
let {user, message} = getState();
// 接口请求跳出的条件:
// 前置条件:下拉刷新优先级高于上拉加载
if (ptr) {
//下拉刷新直接执行
} else {
// 1.当次请求不是下拉刷新,同时正在进行下拉刷新的请求,跳出
// 2.当次请求不是下拉刷新,同时接口请求正在加载中, 跳出
// 3.当次请求不是下拉刷新,数据已全部加载完成,跳出
if (message.like.ptr || message.like.isFetching || message.like.endReached || message.like.error) {
return;
}
}
dispatch(likeMessageRequest(ptr));
let uid = user.profile.uid;
let lastedTime = 0;
if (!ptr) {
lastedTime = message.like.lastedTime;
}
return new MessageService().messageList(uid, 2, lastedTime, LIMIT)
.then(json => {
let payload = parseMessageList(json);
if (!ptr) {
let oldList = message.like.list.toJS();
// 首次读取消息列表成功后,再去刷新一次未读消息数量,
// 因为,接口会在读取消息首页数据后重置未读消息数量
if (oldList.length == 0) {
dispatch(unread());
}
let list = [...oldList, ...payload.list];
payload.list = list;
}
dispatch(likeMessageSuccess(payload));
});
}
}
export function messageInfo(ptr = false) {
return (dispatch, getState) => {
let {user, message} = getState();
if (message.centerMsg.isFetching) {
return;
}
dispatch(messageInfoRequest(ptr));
let uid = user.profile.uid;
return new MessageService().messageInfo(uid)
.then(json => {
let payload = parseMessageInfo(json);
dispatch(messageInfoSuccess(payload));
})
.catch(error => {
dispatch(messageInfoFailue(error));
});
};
}
export function replyMessage(ptr = false) {
return (dispatch, getState) => {
let {user, message} = getState();
if (!ptr) {
if (message.centerMsg.ptr || message.centerMsg.isListFetching || message.centerMsg.endReached || message.centerMsg.listError) {
return;
}
}
let uid = user.profile.uid;
let lastedTime = 0;
if (!ptr) {
lastedTime = message.centerMsg.lastedTime;
}
dispatch(replyMessageRequest(ptr));
return new MessageService().messageList(uid, 3, lastedTime, LIMIT)
.then(json => {
let payload = parseMessageList(json);
// 首次读取消息列表成功后,再去刷新一次未读消息数量,
// 因为,接口会在读取消息首页数据后重置未读消息数量
if (message.centerMsg.list.size == 0) {
dispatch(unread());
}
if (!ptr) {
let oldList = message.centerMsg.list.toJS();
let list = [...oldList, ...payload.list];
payload.list = list;
}
dispatch(replyMessageSuccess(payload));
})
.catch(error => {
dispatch(replyMessageFailue(error));
});
};
}
export function systemMessage(ptr = false) {
return (dispatch, getState) => {
let {user, message} = getState();
let uid = user.profile.uid;
let lastedTime = 0;
if (!ptr) {
lastedTime = message.system.lastedTime;
}
if (!ptr) {
if (message.system.ptr || message.system.isFetching || message.system.endReached || message.system.error) {
return;
}
}
dispatch(systemMessageRequest(ptr));
return new MessageService().messageList(uid, 1, lastedTime, LIMIT)
.then(json => {
let payload = parseMessageList(json);
if (!ptr) {
let oldList = message.system.list.toJS();
// 首次读取消息列表成功后,再去刷新一次未读消息数量,
// 因为,接口会在读取消息首页数据后重置未读消息数量
if (oldList.length == 0) {
dispatch(unread());
}
let list = [...oldList, ...payload.list];
payload.list = list;
}
dispatch(systemMessageSuccess(payload));
})
.catch(error => {
dispatch(systemMessageFailue(error));
});
};
}
export function goToLikeMessage() {
Actions.LikeMessage();
return {
type: GO_TO_LIKE_MESSAGE,
// payload:
};
}
export function goToSystemMessage() {
Actions.SystemMessage();
return {
type: GO_TO_SYS_MESSAGE,
// payload:
};
}
function parseMessageInfo(json) {
let {praiseMsg, systemMsg} = json;
let likeMsg = {
tips: praiseMsg && praiseMsg.title || '',
isRead: praiseMsg && (praiseMsg.isRead === 'N') ? false : true,
timeagoStr: praiseMsg && praiseMsg.createTime && timeago(praiseMsg.createTime) || '',
};
let sysMsg = {
tips: systemMsg && systemMsg.title || '',
isRead: systemMsg && (systemMsg.isRead === 'N') ? false : true,
timeagoStr: systemMsg && systemMsg.createTime && timeago(systemMsg.createTime) || '',
};
let result = {
likeMsg,
sysMsg,
};
return result;
}
function parseMessageList(json) {
let {lastedTime, list} = json;
let messages = [];
list && list.map((item, i) => {
let id = item.id ? item.id : 0;
let title = item.title ? item.title : '';
let type = item.type ? item.type : 0;
let isRead = item.isRead === 'Y' ? true : false;
let createTime = item.createTime;
let timeagoStr = timeago(createTime);
let subTitle = '';
let contentType = 0;
let user = {};
let post = {};
let {content} = item;
if (content) {
subTitle = content.subTitle ? content.subTitle : '';
contentType = content.type ? content.type : 0;
let {userInfo, postInfo} = content;
if (userInfo) {
let nickName = userInfo.nickName ? userInfo.nickName : '';
let avatar = userInfo.headIcon ? userInfo.headIcon : '';
let uid = userInfo.uid ? userInfo.uid : 0;
let sign = userInfo.signature ? userInfo.signature : '';
let backgroundImage = userInfo.bgPic ? userInfo.bgPic : '';
user = {
nickName,
avatar,
uid,
sign,
backgroundImage,
};
}
if (postInfo) {
let sectionId = postInfo.forumCode ? parseInt(postInfo.forumCode) : 0;
let postId = postInfo.postId ? postInfo.postId : 0;
let sectionName = postInfo.forumName ? postInfo.forumName : '';
postId = parseInt(postId);
let thumb = postInfo.content ? postInfo.content : '';
post = {
id: postId,
thumb,
sectionId,
sectionName,
};
}
}
let message = {
id,
title,
type,
isRead,
createTime,
timeago: timeagoStr,
user,
post,
subTitle,
contentType,
};
messages.push(message);
});
let endReached = messages.length < LIMIT;
return {
lastedTime,
list: messages,
endReached,
};
}