通用评论

App.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.RenderText = exports.Editor = undefined;
  6. var _tag = require('antd/es/tag');
  7. var _tag2 = _interopRequireDefault(_tag);
  8. var _message2 = require('antd/es/message');
  9. var _message3 = _interopRequireDefault(_message2);
  10. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  11. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  12. require('antd/es/tag/style/css');
  13. require('antd/es/message/style/css');
  14. var _react = require('react');
  15. var _react2 = _interopRequireDefault(_react);
  16. var _propTypes = require('prop-types');
  17. var _propTypes2 = _interopRequireDefault(_propTypes);
  18. var _axios = require('axios');
  19. var _axios2 = _interopRequireDefault(_axios);
  20. var _constant = require('./constant');
  21. var _Comment = require('./Comment');
  22. var _helper = require('./helper');
  23. var _CommentInput = require('./components/CommentInput');
  24. var _CommentInput2 = _interopRequireDefault(_CommentInput);
  25. var _CommentList = require('./components/CommentList');
  26. var _CommentList2 = _interopRequireDefault(_CommentList);
  27. var _Editor = require('./components/Editor');
  28. var _Editor2 = _interopRequireDefault(_Editor);
  29. var _RenderText = require('./components/RenderText');
  30. var _RenderText2 = _interopRequireDefault(_RenderText);
  31. var _lang = require('./lang');
  32. var _lang2 = _interopRequireDefault(_lang);
  33. require('./App.css');
  34. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  35. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  36. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  37. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  38. var App = function (_Component) {
  39. _inherits(App, _Component);
  40. function App(props) {
  41. _classCallCheck(this, App);
  42. var _this = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this, props));
  43. _this.state = {
  44. loading: {},
  45. // oss 配置
  46. oss: {},
  47. // 评论数据
  48. list: [],
  49. page: 1,
  50. total: 0,
  51. // 是否没有更多评论了
  52. isNoMoreComment: false
  53. };
  54. _this.handleChangeLoading = _this.handleChangeLoading.bind(_this);
  55. _this.sGetComment = _this.sGetComment.bind(_this);
  56. _this.sGetReply = _this.sGetReply.bind(_this);
  57. _this.sCreateComment = _this.sCreateComment.bind(_this);
  58. _this.sCreateReply = _this.sCreateReply.bind(_this);
  59. _this.sCommentFavor = _this.sCommentFavor.bind(_this);
  60. _this.sReplyFavor = _this.sReplyFavor.bind(_this);
  61. _this.sOssSts = _this.sOssSts.bind(_this);
  62. return _this;
  63. }
  64. _createClass(App, [{
  65. key: 'componentWillMount',
  66. value: function componentWillMount() {
  67. this.axios = _axios2.default;
  68. this.axios.defaults.withCredentials = true;
  69. if (this.props.token) {
  70. this.axios.defaults.headers.common['Authorization'] = 'Bearer ' + this.props.token;
  71. }
  72. }
  73. }, {
  74. key: 'componentDidMount',
  75. value: function componentDidMount() {}
  76. /**
  77. * 改变 loading 状态
  78. * @param {string} key key
  79. * @param {string} value value
  80. */
  81. }, {
  82. key: 'handleChangeLoading',
  83. value: function handleChangeLoading(key, value) {
  84. var loading = this.state.loading;
  85. loading[key] = value;
  86. this.setState({ loading: loading });
  87. }
  88. /**
  89. * 获取评论列表
  90. */
  91. }, {
  92. key: 'sGetComment',
  93. value: function sGetComment() {
  94. var _this2 = this;
  95. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  96. _ref$page = _ref.page,
  97. page = _ref$page === undefined ? 1 : _ref$page;
  98. this.handleChangeLoading('sGetComment', true);
  99. var _props = this.props,
  100. API = _props.API,
  101. type = _props.type,
  102. businessId = _props.businessId;
  103. this.axios.get(API + '/comments?type=' + type + '&business_id=' + businessId + '&page=' + page + '&limit=' + _constant.LIMIT).then(function (response) {
  104. var _response$data = response.data,
  105. list = _response$data.list,
  106. page = _response$data.page,
  107. total = _response$data.total;
  108. if (list) {
  109. var newList = list;
  110. if (page > 1) {
  111. var oldList = _this2.state.list;
  112. // 删除临时数据
  113. oldList = oldList.filter(function (o) {
  114. return !o.isTemporary;
  115. });
  116. newList = oldList.concat(newList);
  117. }
  118. _this2.setState({
  119. list: newList,
  120. page: page,
  121. total: total
  122. });
  123. } else {
  124. _message3.default.info('没有更多评论了');
  125. _this2.setState({
  126. isNoMoreComment: true
  127. });
  128. }
  129. }).catch(function (error) {
  130. if (error.response && error.response.data && error.response.data.msg) {
  131. _message3.default.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
  132. return;
  133. }
  134. _message3.default.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
  135. }).finally(function () {
  136. _this2.handleChangeLoading('sGetComment', false);
  137. });
  138. }
  139. /**
  140. * 获取更多回复
  141. */
  142. }, {
  143. key: 'sGetReply',
  144. value: function sGetReply() {
  145. var _this3 = this;
  146. var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  147. commentId = _ref2.commentId,
  148. _ref2$page = _ref2.page,
  149. page = _ref2$page === undefined ? 1 : _ref2$page;
  150. this.handleChangeLoading('sGetReply', true);
  151. var API = this.props.API;
  152. this.axios.get(API + '/replies?comment_id=' + commentId + '&page=' + page + '&limit=' + _constant.LIMIT).then(function (response) {
  153. if (!response.data.list) {
  154. _message3.default.info('没有更多数据了!');
  155. }
  156. var list = _this3.state.list.map(function (item) {
  157. if (item.id === commentId) {
  158. if (!item.replies) item.replies = [];
  159. if (response.data.list) {
  160. if (page === 1) {
  161. // 如果当前页数为第一页,则清空当前所有的 replies
  162. // 并将获取到的 replies 存放在 state
  163. item.replies = response.data.list;
  164. } else {
  165. item.replies = item.replies.filter(function (o) {
  166. return !o.isTemporary;
  167. }).concat(response.data.list);
  168. // 如果当前页数非第一页,则合并 replies
  169. }
  170. item.reply_count = response.data.total;
  171. item.reply_page = response.data.page;
  172. } else {
  173. item.isNoMoreReply = true;
  174. }
  175. }
  176. return item;
  177. });
  178. _this3.setState({ list: list });
  179. }).catch(function (error) {
  180. if (error.response && error.response.data && error.response.data.msg) {
  181. _message3.default.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
  182. return;
  183. }
  184. _message3.default.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
  185. }).finally(function () {
  186. _this3.handleChangeLoading('sGetReply', false);
  187. });
  188. }
  189. /**
  190. * 添加评论
  191. * @param {object} {content} comment content
  192. */
  193. }, {
  194. key: 'sCreateComment',
  195. value: function sCreateComment() {
  196. var _this4 = this;
  197. var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  198. content = _ref3.content;
  199. var cb = arguments[1];
  200. if (!content) return _message3.default.error('评论内容不能为空 ');
  201. this.handleChangeLoading('sCreateComment', true);
  202. var _props2 = this.props,
  203. API = _props2.API,
  204. type = _props2.type,
  205. businessId = _props2.businessId;
  206. this.axios(API + '/comments', {
  207. method: 'post',
  208. data: {
  209. type: type,
  210. business_id: businessId,
  211. content: content
  212. },
  213. withCredentials: true
  214. }).then(function (response) {
  215. if (_this4.props.showAlertComment) {
  216. _message3.default.success('评论成功!');
  217. }
  218. if ((0, _helper.isFunction)(cb)) cb();
  219. // 将数据写入到 list 中
  220. // 临时插入
  221. // 等到获取数据之后,删除临时数据
  222. var _state = _this4.state,
  223. list = _state.list,
  224. total = _state.total;
  225. list.unshift(_extends({}, response.data, {
  226. isTemporary: true // 临时的数据
  227. }));
  228. _this4.setState({ list: list, total: total + 1 });
  229. }).catch(function (error) {
  230. if (error.response && error.response.data && error.response.data.msg) {
  231. _message3.default.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
  232. return;
  233. }
  234. _message3.default.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
  235. }).finally(function () {
  236. _this4.handleChangeLoading('sCreateComment', false);
  237. });
  238. }
  239. /**
  240. * 添加回复
  241. * 回复评论/回复回复
  242. * @param {object} data { comment_id, content, [reply_id] }
  243. */
  244. }, {
  245. key: 'sCreateReply',
  246. value: function sCreateReply(data, cb) {
  247. var _this5 = this;
  248. if (!data.content) return _message3.default.error('回复内容不能为空 ');
  249. this.handleChangeLoading('sCreateReply', true);
  250. var API = this.props.API;
  251. this.axios(API + '/replies', {
  252. method: 'post',
  253. data: data,
  254. withCredentials: true
  255. }).then(function (response) {
  256. if (_this5.props.showAlertReply) {
  257. _message3.default.success('回复成功!');
  258. }
  259. if ((0, _helper.isFunction)(cb)) cb();
  260. // 将数据写入到 list 中
  261. // 临时插入
  262. // 等到获取数据之后,删除临时数据
  263. var list = _this5.state.list.map(function (item) {
  264. if (item.id === data.comment_id) {
  265. if (!item.replies) item.replies = [];
  266. item.replies.push(_extends({}, response.data, {
  267. isTemporary: true // 临时的数据
  268. }));
  269. item.reply_count += 1;
  270. }
  271. return item;
  272. });
  273. _this5.setState({ list: list });
  274. }).catch(function (error) {
  275. if (error.response && error.response.data && error.response.data.msg) {
  276. _message3.default.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
  277. return;
  278. }
  279. _message3.default.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
  280. }).finally(function () {
  281. _this5.handleChangeLoading('sCreateReply', false);
  282. });
  283. }
  284. /**
  285. * 评论 点赞/取消点赞
  286. * @param {string} commentId { commentId }
  287. * @param {boolean} favored 是否已经点过赞
  288. */
  289. }, {
  290. key: 'sCommentFavor',
  291. value: function sCommentFavor(commentId, favored) {
  292. var _this6 = this;
  293. this.handleChangeLoading('sCommentFavor', true);
  294. var API = this.props.API;
  295. this.axios(API + '/comments/' + commentId + '/favor', {
  296. method: favored ? 'delete' : 'put',
  297. withCredentials: true
  298. }).then(function (response) {
  299. if (_this6.props.showAlertFavor) {
  300. _message3.default.success(favored ? '取消点赞成功!' : '点赞成功!');
  301. }
  302. // 更新 list 中的该项数据的 favored
  303. var list = _this6.state.list.map(function (item) {
  304. if (item.id === commentId) {
  305. item.favored = !favored;
  306. item.favor_count += favored ? -1 : 1;
  307. }
  308. return item;
  309. });
  310. _this6.setState({ list: list });
  311. }).catch(function (error) {
  312. if (error.response && error.response.data && error.response.data.msg) {
  313. _message3.default.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
  314. return;
  315. }
  316. _message3.default.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
  317. }).finally(function () {
  318. _this6.handleChangeLoading('sCommentFavor', false);
  319. });
  320. }
  321. /**
  322. * 回复 点赞/取消点赞
  323. * @param {string} replyId replyId
  324. * @param {string} commentId commentId
  325. * @param {boolean} favored 是否已经点过赞
  326. */
  327. }, {
  328. key: 'sReplyFavor',
  329. value: function sReplyFavor(replyId, commentId, favored) {
  330. var _this7 = this;
  331. this.handleChangeLoading('sReplyFavor', true);
  332. var API = this.props.API;
  333. this.axios(API + '/replies/' + replyId + '/favor', {
  334. method: favored ? 'delete' : 'put',
  335. data: {
  336. comment_id: commentId
  337. },
  338. withCredentials: true
  339. }).then(function (response) {
  340. _message3.default.success(favored ? '取消点赞成功!' : '点赞成功!');
  341. // 更新 list 中的该项数据的 favored
  342. var list = _this7.state.list.map(function (item) {
  343. if (item.id === commentId) {
  344. item.replies = item.replies.map(function (r) {
  345. if (r.id === replyId) {
  346. r.favored = !favored;
  347. // r.favor_count = response.data.favor_count;
  348. // 点赞数 +1,而不是使用后端返回的点赞数
  349. // 不然如果返回的不是增加 1,用户可能以为程序错误
  350. r.favor_count += favored ? -1 : 1;
  351. }
  352. return r;
  353. });
  354. }
  355. return item;
  356. });
  357. _this7.setState({ list: list });
  358. }).catch(function (error) {
  359. if (error.response && error.response.data && error.response.data.msg) {
  360. _message3.default.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
  361. return;
  362. }
  363. _message3.default.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
  364. }).finally(function () {
  365. _this7.handleChangeLoading('sReplyFavor', false);
  366. });
  367. }
  368. /**
  369. * 获取 OSS 上传的参数
  370. */
  371. }, {
  372. key: 'sOssSts',
  373. value: function sOssSts() {
  374. var _this8 = this;
  375. this.handleChangeLoading('sOssSts', true);
  376. this.axios.get('http://api.links123.net/comment/v1/oss/sts').then(function (response) {
  377. _this8.setState({ oss: _extends({}, response.data) });
  378. }).catch(function (error) {
  379. if (error.response && error.response.data && error.response.data.msg) {
  380. _message3.default.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT);
  381. return;
  382. }
  383. _message3.default.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT);
  384. }).finally(function () {
  385. _this8.handleChangeLoading('sOssSts', false);
  386. });
  387. }
  388. }, {
  389. key: 'render',
  390. value: function render() {
  391. // 添加到 Context 的数据
  392. var value = _extends({}, this.state, this.props, {
  393. sCreateComment: this.sCreateComment,
  394. sGetComment: this.sGetComment,
  395. sCommentFavor: this.sCommentFavor,
  396. sReplyFavor: this.sReplyFavor,
  397. sCreateReply: this.sCreateReply,
  398. sGetReply: this.sGetReply,
  399. sOssSts: this.sOssSts
  400. });
  401. return _react2.default.createElement(
  402. _Comment.CommentContext.Provider,
  403. { value: value },
  404. _react2.default.createElement(
  405. 'div',
  406. { className: 'comment' },
  407. this.props.showHeader && _react2.default.createElement(
  408. 'div',
  409. { style: { marginBottom: 15 } },
  410. _react2.default.createElement(
  411. _tag2.default,
  412. { className: 'comment-header-tag' },
  413. '\u7559\u8A00'
  414. ),
  415. _react2.default.createElement(
  416. 'span',
  417. { className: 'comment-header-tip' },
  418. '\u53E3\u7891'
  419. ),
  420. _react2.default.createElement(
  421. 'span',
  422. { className: 'comment-header-text' },
  423. '(\u5168\u7AD9\u6311\u51FA\u6BDB\u75C5\u6216\u63D0\u51FA\u5408\u7406\u5EFA\u8BAE\uFF0C\u5956\u52B110\u5230100\u5143\u7EA2\u5305)'
  424. )
  425. ),
  426. this.props.showEditor && _react2.default.createElement(_CommentInput2.default, { content: this.props.children }),
  427. this.props.showList && _react2.default.createElement(
  428. 'div',
  429. { style: { marginTop: 20 } },
  430. _react2.default.createElement(_CommentList2.default, null)
  431. )
  432. )
  433. );
  434. }
  435. }]);
  436. return App;
  437. }(_react.Component);
  438. App.propTypes = {
  439. type: _propTypes2.default.number.isRequired, // 评论的 type
  440. businessId: _propTypes2.default.string.isRequired, // 评论的 business_id
  441. API: _propTypes2.default.string, // 评论的 API 前缀
  442. showList: _propTypes2.default.bool, // 是否显示评论列表
  443. showEditor: _propTypes2.default.bool, // 是否显示评论输入框
  444. showHeader: _propTypes2.default.bool, // 是否显示评论顶部的提示
  445. showAlertComment: _propTypes2.default.bool, // 评论成功之后,是否通过 Antd 的 Message 组件进行提示
  446. showAlertReply: _propTypes2.default.bool, // 回复成功之后,是否通过 Antd 的 Message 组件进行提示
  447. showAlertFavor: _propTypes2.default.bool // 点赞/取消点赞成功之后,是否通过 Antd 的 Message 组件进行提示
  448. };
  449. App.defaultProps = {
  450. API: 'http://api.links123.net/comment/v1',
  451. showList: true,
  452. showEditor: true,
  453. showHeader: true,
  454. showAlertComment: false,
  455. showAlertReply: false,
  456. showAlertFavor: false
  457. };
  458. exports.Editor = _Editor2.default;
  459. exports.RenderText = _RenderText2.default;
  460. exports.default = App;
  461. //# sourceMappingURL=App.js.map