通用评论

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