|
@@ -85,12 +85,15 @@ var App = function (_Component) {
|
85
|
85
|
isNoMoreComment: false
|
86
|
86
|
};
|
87
|
87
|
_this.handleChangeLoading = _this.handleChangeLoading.bind(_this);
|
88
|
|
- _this.sGetComment = _this.sGetComment.bind(_this);
|
89
|
|
- _this.sGetReply = _this.sGetReply.bind(_this);
|
90
|
88
|
_this.sCreateComment = _this.sCreateComment.bind(_this);
|
91
|
|
- _this.sCreateReply = _this.sCreateReply.bind(_this);
|
|
89
|
+ _this.sDeleteComment = _this.sDeleteComment.bind(_this);
|
92
|
90
|
_this.sCommentFavor = _this.sCommentFavor.bind(_this);
|
|
91
|
+ _this.sCreateReply = _this.sCreateReply.bind(_this);
|
|
92
|
+ _this.sDeleteReply = _this.sDeleteReply.bind(_this);
|
|
93
|
+ _this.errorHandler = _this.errorHandler.bind(_this);
|
|
94
|
+ _this.sGetComment = _this.sGetComment.bind(_this);
|
93
|
95
|
_this.sReplyFavor = _this.sReplyFavor.bind(_this);
|
|
96
|
+ _this.sGetReply = _this.sGetReply.bind(_this);
|
94
|
97
|
_this.sOssSts = _this.sOssSts.bind(_this);
|
95
|
98
|
return _this;
|
96
|
99
|
}
|
|
@@ -110,12 +113,23 @@ var App = function (_Component) {
|
110
|
113
|
}, {
|
111
|
114
|
key: "error",
|
112
|
115
|
value: function error(msg) {
|
|
116
|
+ var info = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
117
|
+
|
113
|
118
|
if (this.props.showError) {
|
114
|
119
|
_message3.default.error(msg);
|
115
|
120
|
}
|
116
|
121
|
if (this.props.onError) {
|
117
|
|
- this.props.onError(msg);
|
|
122
|
+ this.props.onError(msg, info);
|
|
123
|
+ }
|
|
124
|
+ }
|
|
125
|
+ }, {
|
|
126
|
+ key: "errorHandler",
|
|
127
|
+ value: function errorHandler(error) {
|
|
128
|
+ if (error.response && error.response.data && error.response.data.msg) {
|
|
129
|
+ this.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT, { response: error.response });
|
|
130
|
+ return;
|
118
|
131
|
}
|
|
132
|
+ this.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT, { response: error.response });
|
119
|
133
|
}
|
120
|
134
|
|
121
|
135
|
/**
|
|
@@ -180,13 +194,7 @@ var App = function (_Component) {
|
180
|
194
|
isNoMoreComment: true
|
181
|
195
|
});
|
182
|
196
|
}
|
183
|
|
- }).catch(function (error) {
|
184
|
|
- if (error.response && error.response.data && error.response.data.msg) {
|
185
|
|
- _this2.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
|
186
|
|
- return;
|
187
|
|
- }
|
188
|
|
- _this2.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
|
189
|
|
- }).finally(function () {
|
|
197
|
+ }).catch(this.errorHandler).finally(function () {
|
190
|
198
|
_this2.handleChangeLoading("sGetComment", false);
|
191
|
199
|
});
|
192
|
200
|
}
|
|
@@ -235,13 +243,7 @@ var App = function (_Component) {
|
235
|
243
|
return item;
|
236
|
244
|
});
|
237
|
245
|
_this3.setState({ list: list });
|
238
|
|
- }).catch(function (error) {
|
239
|
|
- if (error.response && error.response.data && error.response.data.msg) {
|
240
|
|
- _this3.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
|
241
|
|
- return;
|
242
|
|
- }
|
243
|
|
- _this3.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
|
244
|
|
- }).finally(function () {
|
|
246
|
+ }).catch(this.errorHandler).finally(function () {
|
245
|
247
|
_this3.handleChangeLoading("sGetReply", false);
|
246
|
248
|
});
|
247
|
249
|
}
|
|
@@ -292,17 +294,40 @@ var App = function (_Component) {
|
292
|
294
|
isTemporary: true // 临时的数据
|
293
|
295
|
}));
|
294
|
296
|
_this4.setState({ list: list, total: total + 1 });
|
295
|
|
- }).catch(function (error) {
|
296
|
|
- if (error.response && error.response.data && error.response.data.msg) {
|
297
|
|
- _this4.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
|
298
|
|
- return;
|
299
|
|
- }
|
300
|
|
- _this4.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
|
301
|
|
- }).finally(function () {
|
|
297
|
+ }).catch(this.errorHandler).finally(function () {
|
302
|
298
|
_this4.handleChangeLoading("sCreateComment", false);
|
303
|
299
|
});
|
304
|
300
|
}
|
305
|
301
|
|
|
302
|
+ /**
|
|
303
|
+ * 删除评论
|
|
304
|
+ */
|
|
305
|
+
|
|
306
|
+ }, {
|
|
307
|
+ key: "sDeleteComment",
|
|
308
|
+ value: function sDeleteComment(commentId) {
|
|
309
|
+ var _this5 = this;
|
|
310
|
+
|
|
311
|
+ this.handleChangeLoading("sDeleteComment", true);
|
|
312
|
+ var API = this.props.API;
|
|
313
|
+
|
|
314
|
+ this.axios(API + "/comments/" + commentId, {
|
|
315
|
+ method: "delete",
|
|
316
|
+ withCredentials: true
|
|
317
|
+ }).then(function () {
|
|
318
|
+ var _state2 = _this5.state,
|
|
319
|
+ list = _state2.list,
|
|
320
|
+ total = _state2.total;
|
|
321
|
+
|
|
322
|
+ var res = list.filter(function (item) {
|
|
323
|
+ return item.id !== commentId;
|
|
324
|
+ });
|
|
325
|
+ _this5.setState({ list: res, total: total - 1 });
|
|
326
|
+ }).catch(this.errorHandler).finally(function () {
|
|
327
|
+ _this5.handleChangeLoading("sDeleteComment", false);
|
|
328
|
+ });
|
|
329
|
+ }
|
|
330
|
+
|
306
|
331
|
/**
|
307
|
332
|
* 添加回复
|
308
|
333
|
* 回复评论/回复回复
|
|
@@ -312,7 +337,7 @@ var App = function (_Component) {
|
312
|
337
|
}, {
|
313
|
338
|
key: "sCreateReply",
|
314
|
339
|
value: function sCreateReply(data, cb) {
|
315
|
|
- var _this5 = this;
|
|
340
|
+ var _this6 = this;
|
316
|
341
|
|
317
|
342
|
if (!data.content) return this.error("回复内容不能为空 ");
|
318
|
343
|
this.handleChangeLoading("sCreateReply", true);
|
|
@@ -323,14 +348,14 @@ var App = function (_Component) {
|
323
|
348
|
data: data,
|
324
|
349
|
withCredentials: true
|
325
|
350
|
}).then(function (response) {
|
326
|
|
- if (_this5.props.showAlertReply) {
|
|
351
|
+ if (_this6.props.showAlertReply) {
|
327
|
352
|
_message3.default.success("回复成功!");
|
328
|
353
|
}
|
329
|
354
|
if ((0, _helper.isFunction)(cb)) cb();
|
330
|
355
|
// 将数据写入到 list 中
|
331
|
356
|
// 临时插入
|
332
|
357
|
// 等到获取数据之后,删除临时数据
|
333
|
|
- var list = _this5.state.list.map(function (item) {
|
|
358
|
+ var list = _this6.state.list.map(function (item) {
|
334
|
359
|
if (item.id === data.comment_id) {
|
335
|
360
|
if (!item.replies) item.replies = [];
|
336
|
361
|
item.replies.push(_extends({}, response.data, {
|
|
@@ -340,15 +365,43 @@ var App = function (_Component) {
|
340
|
365
|
}
|
341
|
366
|
return item;
|
342
|
367
|
});
|
343
|
|
- _this5.setState({ list: list });
|
344
|
|
- }).catch(function (error) {
|
345
|
|
- if (error.response && error.response.data && error.response.data.msg) {
|
346
|
|
- _this5.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
|
347
|
|
- return;
|
348
|
|
- }
|
349
|
|
- _this5.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
|
350
|
|
- }).finally(function () {
|
351
|
|
- _this5.handleChangeLoading("sCreateReply", false);
|
|
368
|
+ _this6.setState({ list: list });
|
|
369
|
+ }).catch(this.errorHandler).finally(function () {
|
|
370
|
+ _this6.handleChangeLoading("sCreateReply", false);
|
|
371
|
+ });
|
|
372
|
+ }
|
|
373
|
+
|
|
374
|
+ /**
|
|
375
|
+ * 删除回复
|
|
376
|
+ * @param {*} replyId
|
|
377
|
+ * @param {*} commentId
|
|
378
|
+ */
|
|
379
|
+
|
|
380
|
+ }, {
|
|
381
|
+ key: "sDeleteReply",
|
|
382
|
+ value: function sDeleteReply(replyId, commentId) {
|
|
383
|
+ var _this7 = this;
|
|
384
|
+
|
|
385
|
+ this.handleChangeLoading("sDeleteReply", true);
|
|
386
|
+ var API = this.props.API;
|
|
387
|
+
|
|
388
|
+ this.axios(API + "/replies/" + replyId + "?CommentID=" + commentId, {
|
|
389
|
+ method: "delete",
|
|
390
|
+ withCredentials: true
|
|
391
|
+ }).then(function () {
|
|
392
|
+ var list = _this7.state.list.map(function (item) {
|
|
393
|
+ if (item.id === commentId) {
|
|
394
|
+ var replies = item.replies.filter(function (item) {
|
|
395
|
+ return item.id !== replyId;
|
|
396
|
+ });
|
|
397
|
+ item.replies = replies;
|
|
398
|
+ item.reply_count -= 1;
|
|
399
|
+ }
|
|
400
|
+ return item;
|
|
401
|
+ });
|
|
402
|
+ _this7.setState({ list: list });
|
|
403
|
+ }).catch(this.errorHandler).finally(function () {
|
|
404
|
+ _this7.handleChangeLoading("sDeleteReply", false);
|
352
|
405
|
});
|
353
|
406
|
}
|
354
|
407
|
|
|
@@ -361,7 +414,7 @@ var App = function (_Component) {
|
361
|
414
|
}, {
|
362
|
415
|
key: "sCommentFavor",
|
363
|
416
|
value: function sCommentFavor(commentId, favored) {
|
364
|
|
- var _this6 = this;
|
|
417
|
+ var _this8 = this;
|
365
|
418
|
|
366
|
419
|
this.handleChangeLoading("sCommentFavor", true);
|
367
|
420
|
var API = this.props.API;
|
|
@@ -370,26 +423,20 @@ var App = function (_Component) {
|
370
|
423
|
method: favored ? "delete" : "put",
|
371
|
424
|
withCredentials: true
|
372
|
425
|
}).then(function (response) {
|
373
|
|
- if (_this6.props.showAlertFavor) {
|
|
426
|
+ if (_this8.props.showAlertFavor) {
|
374
|
427
|
_message3.default.success(favored ? "取消点赞成功!" : "点赞成功!");
|
375
|
428
|
}
|
376
|
429
|
// 更新 list 中的该项数据的 favored
|
377
|
|
- var list = _this6.state.list.map(function (item) {
|
|
430
|
+ var list = _this8.state.list.map(function (item) {
|
378
|
431
|
if (item.id === commentId) {
|
379
|
432
|
item.favored = !favored;
|
380
|
433
|
item.favor_count += favored ? -1 : 1;
|
381
|
434
|
}
|
382
|
435
|
return item;
|
383
|
436
|
});
|
384
|
|
- _this6.setState({ list: list });
|
385
|
|
- }).catch(function (error) {
|
386
|
|
- if (error.response && error.response.data && error.response.data.msg) {
|
387
|
|
- _this6.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
|
388
|
|
- return;
|
389
|
|
- }
|
390
|
|
- _this6.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
|
391
|
|
- }).finally(function () {
|
392
|
|
- _this6.handleChangeLoading("sCommentFavor", false);
|
|
437
|
+ _this8.setState({ list: list });
|
|
438
|
+ }).catch(this.errorHandler).finally(function () {
|
|
439
|
+ _this8.handleChangeLoading("sCommentFavor", false);
|
393
|
440
|
});
|
394
|
441
|
}
|
395
|
442
|
|
|
@@ -403,7 +450,7 @@ var App = function (_Component) {
|
403
|
450
|
}, {
|
404
|
451
|
key: "sReplyFavor",
|
405
|
452
|
value: function sReplyFavor(replyId, commentId, favored) {
|
406
|
|
- var _this7 = this;
|
|
453
|
+ var _this9 = this;
|
407
|
454
|
|
408
|
455
|
this.handleChangeLoading("sReplyFavor", true);
|
409
|
456
|
var API = this.props.API;
|
|
@@ -417,7 +464,7 @@ var App = function (_Component) {
|
417
|
464
|
}).then(function (response) {
|
418
|
465
|
_message3.default.success(favored ? "取消点赞成功!" : "点赞成功!");
|
419
|
466
|
// 更新 list 中的该项数据的 favored
|
420
|
|
- var list = _this7.state.list.map(function (item) {
|
|
467
|
+ var list = _this9.state.list.map(function (item) {
|
421
|
468
|
if (item.id === commentId) {
|
422
|
469
|
item.replies = item.replies.map(function (r) {
|
423
|
470
|
if (r.id === replyId) {
|
|
@@ -432,15 +479,9 @@ var App = function (_Component) {
|
432
|
479
|
}
|
433
|
480
|
return item;
|
434
|
481
|
});
|
435
|
|
- _this7.setState({ list: list });
|
436
|
|
- }).catch(function (error) {
|
437
|
|
- if (error.response && error.response.data && error.response.data.msg) {
|
438
|
|
- _this7.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
|
439
|
|
- return;
|
440
|
|
- }
|
441
|
|
- _this7.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
|
442
|
|
- }).finally(function () {
|
443
|
|
- _this7.handleChangeLoading("sReplyFavor", false);
|
|
482
|
+ _this9.setState({ list: list });
|
|
483
|
+ }).catch(this.errorHandler).finally(function () {
|
|
484
|
+ _this9.handleChangeLoading("sReplyFavor", false);
|
444
|
485
|
});
|
445
|
486
|
}
|
446
|
487
|
|
|
@@ -451,21 +492,15 @@ var App = function (_Component) {
|
451
|
492
|
}, {
|
452
|
493
|
key: "sOssSts",
|
453
|
494
|
value: function sOssSts() {
|
454
|
|
- var _this8 = this;
|
|
495
|
+ var _this10 = this;
|
455
|
496
|
|
456
|
497
|
this.handleChangeLoading("sOssSts", true);
|
457
|
498
|
var API = this.props.API;
|
458
|
499
|
|
459
|
500
|
this.axios.get(API + "/oss/sts").then(function (response) {
|
460
|
|
- _this8.setState({ oss: _extends({}, response.data) });
|
461
|
|
- }).catch(function (error) {
|
462
|
|
- if (error.response && error.response.data && error.response.data.msg) {
|
463
|
|
- _this8.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
|
464
|
|
- return;
|
465
|
|
- }
|
466
|
|
- _this8.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
|
467
|
|
- }).finally(function () {
|
468
|
|
- _this8.handleChangeLoading("sOssSts", false);
|
|
501
|
+ _this10.setState({ oss: _extends({}, response.data) });
|
|
502
|
+ }).catch(this.errorHandler).finally(function () {
|
|
503
|
+ _this10.handleChangeLoading("sOssSts", false);
|
469
|
504
|
});
|
470
|
505
|
}
|
471
|
506
|
}, {
|
|
@@ -479,7 +514,9 @@ var App = function (_Component) {
|
479
|
514
|
sReplyFavor: this.sReplyFavor,
|
480
|
515
|
sCreateReply: this.sCreateReply,
|
481
|
516
|
sGetReply: this.sGetReply,
|
482
|
|
- sOssSts: this.sOssSts
|
|
517
|
+ sOssSts: this.sOssSts,
|
|
518
|
+ sDeleteComment: this.sDeleteComment,
|
|
519
|
+ sDeleteReply: this.sDeleteReply
|
483
|
520
|
});
|
484
|
521
|
|
485
|
522
|
return _react2.default.createElement(
|
|
@@ -512,7 +549,8 @@ App.propTypes = {
|
512
|
549
|
showAlertReply: _propTypes2.default.bool, // 回复成功之后,是否通过 Antd 的 Message 组件进行提示
|
513
|
550
|
showAlertFavor: _propTypes2.default.bool, // 点赞/取消点赞成功之后,是否通过 Antd 的 Message 组件进行提示
|
514
|
551
|
showError: _propTypes2.default.bool, // 是否使用Antd的Message组件提示错误信息
|
515
|
|
- onError: _propTypes2.default.func // 错误回调, 出错了会被调用
|
|
552
|
+ onError: _propTypes2.default.func, // 错误回调, 出错了会被调用
|
|
553
|
+ userId: _propTypes2.default.number // 用户id, comment内部不维护用户id, 调用组件时传递过来, 目前用于判断是否显示删除按钮
|
516
|
554
|
};
|
517
|
555
|
|
518
|
556
|
App.defaultProps = {
|