通用评论

App.js 18KB

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