通用评论

App.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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.sCreateComment = _this.sCreateComment.bind(_this);
  53. _this.sDeleteComment = _this.sDeleteComment.bind(_this);
  54. _this.sCommentFavor = _this.sCommentFavor.bind(_this);
  55. _this.sCreateReply = _this.sCreateReply.bind(_this);
  56. _this.sDeleteReply = _this.sDeleteReply.bind(_this);
  57. _this.errorHandler = _this.errorHandler.bind(_this);
  58. _this.sGetComment = _this.sGetComment.bind(_this);
  59. _this.sReplyFavor = _this.sReplyFavor.bind(_this);
  60. _this.sGetReply = _this.sGetReply.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. key: "error",
  78. value: function error(msg) {
  79. var info = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  80. if (this.props.showError) {
  81. _message3.default.error(msg);
  82. }
  83. if (this.props.onError) {
  84. this.props.onError(msg, info);
  85. }
  86. }
  87. }, {
  88. key: "errorHandler",
  89. value: function errorHandler(error) {
  90. if (error.response && error.response.data && error.response.data.msg) {
  91. this.error(_lang2.default[error.response.data.msg] || _constant.ERROR_DEFAULT, { response: error.response });
  92. return;
  93. }
  94. this.error(_lang2.default[error.message] || _constant.ERROR_DEFAULT, { response: error.response });
  95. }
  96. /**
  97. * 改变 loading 状态
  98. * @param {string} key key
  99. * @param {string} value value
  100. */
  101. }, {
  102. key: "handleChangeLoading",
  103. value: function handleChangeLoading(key, value) {
  104. var loading = this.state.loading;
  105. loading[key] = value;
  106. this.setState({ loading: loading });
  107. }
  108. /**
  109. * 获取评论列表
  110. */
  111. }, {
  112. key: "sGetComment",
  113. value: function sGetComment() {
  114. var _this2 = this;
  115. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  116. _ref$page = _ref.page,
  117. page = _ref$page === undefined ? 1 : _ref$page;
  118. this.handleChangeLoading("sGetComment", true);
  119. var _props = this.props,
  120. API = _props.API,
  121. type = _props.type,
  122. businessId = _props.businessId;
  123. this.axios.get(API + "/comments?type=" + type + "&business_id=" + businessId + "&page=" + page + "&limit=" + _constant.LIMIT).then(function (response) {
  124. var _response$data = response.data,
  125. list = _response$data.list,
  126. page = _response$data.page,
  127. total = _response$data.total;
  128. if (list) {
  129. var newList = list;
  130. if (page > 1) {
  131. var oldList = _this2.state.list;
  132. // 删除临时数据
  133. oldList = oldList.filter(function (o) {
  134. return !o.isTemporary;
  135. });
  136. newList = oldList.concat(newList);
  137. }
  138. _this2.setState({
  139. list: newList,
  140. page: page,
  141. total: total
  142. });
  143. } else {
  144. _message3.default.info("没有更多评论了");
  145. _this2.setState({
  146. isNoMoreComment: true
  147. });
  148. }
  149. }).catch(this.errorHandler).finally(function () {
  150. _this2.handleChangeLoading("sGetComment", false);
  151. });
  152. }
  153. /**
  154. * 获取更多回复
  155. */
  156. }, {
  157. key: "sGetReply",
  158. value: function sGetReply() {
  159. var _this3 = this;
  160. var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  161. commentId = _ref2.commentId,
  162. _ref2$page = _ref2.page,
  163. page = _ref2$page === undefined ? 1 : _ref2$page;
  164. this.handleChangeLoading("sGetReply", true);
  165. var API = this.props.API;
  166. this.axios.get(API + "/replies?comment_id=" + commentId + "&page=" + page + "&limit=" + _constant.LIMIT).then(function (response) {
  167. if (!response.data.list) {
  168. _message3.default.info("没有更多数据了!");
  169. }
  170. var list = _this3.state.list.map(function (item) {
  171. if (item.id === commentId) {
  172. if (!item.replies) item.replies = [];
  173. if (response.data.list) {
  174. if (page === 1) {
  175. // 如果当前页数为第一页,则清空当前所有的 replies
  176. // 并将获取到的 replies 存放在 state
  177. item.replies = response.data.list;
  178. } else {
  179. item.replies = item.replies.filter(function (o) {
  180. return !o.isTemporary;
  181. }).concat(response.data.list);
  182. // 如果当前页数非第一页,则合并 replies
  183. }
  184. item.reply_count = response.data.total;
  185. item.reply_page = response.data.page;
  186. } else {
  187. item.isNoMoreReply = true;
  188. }
  189. }
  190. return item;
  191. });
  192. _this3.setState({ list: list });
  193. }).catch(this.errorHandler).finally(function () {
  194. _this3.handleChangeLoading("sGetReply", false);
  195. });
  196. }
  197. /**
  198. * 添加评论
  199. * @param {object} {content} comment content
  200. */
  201. }, {
  202. key: "sCreateComment",
  203. value: function sCreateComment() {
  204. var _this4 = this;
  205. var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  206. content = _ref3.content;
  207. var cb = arguments[1];
  208. if (!content) return this.error("评论内容不能为空 ");
  209. this.handleChangeLoading("sCreateComment", true);
  210. var _props2 = this.props,
  211. API = _props2.API,
  212. type = _props2.type,
  213. businessId = _props2.businessId;
  214. this.axios(API + "/comments", {
  215. method: "post",
  216. data: {
  217. type: type,
  218. business_id: businessId,
  219. content: content
  220. },
  221. withCredentials: true
  222. }).then(function (response) {
  223. if (_this4.props.showAlertComment) {
  224. _message3.default.success("评论成功!");
  225. }
  226. if ((0, _helper.isFunction)(cb)) cb();
  227. // 将数据写入到 list 中
  228. // 临时插入
  229. // 等到获取数据之后,删除临时数据
  230. var _state = _this4.state,
  231. list = _state.list,
  232. total = _state.total;
  233. list.unshift(_extends({}, response.data, {
  234. isTemporary: true // 临时的数据
  235. }));
  236. _this4.setState({ list: list, total: total + 1 });
  237. }).catch(this.errorHandler).finally(function () {
  238. _this4.handleChangeLoading("sCreateComment", false);
  239. });
  240. }
  241. /**
  242. * 删除评论
  243. */
  244. }, {
  245. key: "sDeleteComment",
  246. value: function sDeleteComment(commentId) {
  247. var _this5 = this;
  248. this.handleChangeLoading("sDeleteComment", true);
  249. var API = this.props.API;
  250. this.axios(API + "/comments/" + commentId, {
  251. method: "delete",
  252. withCredentials: true
  253. }).then(function () {
  254. var _state2 = _this5.state,
  255. list = _state2.list,
  256. total = _state2.total;
  257. var res = list.filter(function (item) {
  258. return item.id !== commentId;
  259. });
  260. _this5.setState({ list: res, total: total - 1 });
  261. }).catch(this.errorHandler).finally(function () {
  262. _this5.handleChangeLoading("sDeleteComment", false);
  263. });
  264. }
  265. /**
  266. * 添加回复
  267. * 回复评论/回复回复
  268. * @param {object} data { comment_id, content, [reply_id] }
  269. */
  270. }, {
  271. key: "sCreateReply",
  272. value: function sCreateReply(data, cb) {
  273. var _this6 = this;
  274. if (!data.content) return this.error("回复内容不能为空 ");
  275. this.handleChangeLoading("sCreateReply", true);
  276. var API = this.props.API;
  277. this.axios(API + "/replies", {
  278. method: "post",
  279. data: data,
  280. withCredentials: true
  281. }).then(function (response) {
  282. if (_this6.props.showAlertReply) {
  283. _message3.default.success("回复成功!");
  284. }
  285. if ((0, _helper.isFunction)(cb)) cb();
  286. // 将数据写入到 list 中
  287. // 临时插入
  288. // 等到获取数据之后,删除临时数据
  289. var list = _this6.state.list.map(function (item) {
  290. if (item.id === data.comment_id) {
  291. if (!item.replies) item.replies = [];
  292. item.replies.push(_extends({}, response.data, {
  293. isTemporary: true // 临时的数据
  294. }));
  295. item.reply_count += 1;
  296. }
  297. return item;
  298. });
  299. _this6.setState({ list: list });
  300. }).catch(this.errorHandler).finally(function () {
  301. _this6.handleChangeLoading("sCreateReply", false);
  302. });
  303. }
  304. /**
  305. * 删除回复
  306. * @param {*} replyId
  307. * @param {*} commentId
  308. */
  309. }, {
  310. key: "sDeleteReply",
  311. value: function sDeleteReply(replyId, commentId) {
  312. var _this7 = this;
  313. this.handleChangeLoading("sDeleteReply", true);
  314. var API = this.props.API;
  315. this.axios(API + "/replies/" + replyId + "?CommentID=" + commentId, {
  316. method: "delete",
  317. withCredentials: true
  318. }).then(function () {
  319. var list = _this7.state.list.map(function (item) {
  320. if (item.id === commentId) {
  321. var replies = item.replies.filter(function (item) {
  322. return item.id !== replyId;
  323. });
  324. item.replies = replies;
  325. item.reply_count -= 1;
  326. }
  327. return item;
  328. });
  329. _this7.setState({ list: list });
  330. }).catch(this.errorHandler).finally(function () {
  331. _this7.handleChangeLoading("sDeleteReply", false);
  332. });
  333. }
  334. /**
  335. * 评论 点赞/取消点赞
  336. * @param {string} commentId { commentId }
  337. * @param {boolean} favored 是否已经点过赞
  338. */
  339. }, {
  340. key: "sCommentFavor",
  341. value: function sCommentFavor(commentId, favored) {
  342. var _this8 = this;
  343. this.handleChangeLoading("sCommentFavor", true);
  344. var API = this.props.API;
  345. this.axios(API + "/comments/" + commentId + "/favor", {
  346. method: favored ? "delete" : "put",
  347. withCredentials: true
  348. }).then(function (response) {
  349. if (_this8.props.showAlertFavor) {
  350. _message3.default.success(favored ? "取消点赞成功!" : "点赞成功!");
  351. }
  352. // 更新 list 中的该项数据的 favored
  353. var list = _this8.state.list.map(function (item) {
  354. if (item.id === commentId) {
  355. item.favored = !favored;
  356. item.favor_count += favored ? -1 : 1;
  357. }
  358. return item;
  359. });
  360. _this8.setState({ list: list });
  361. }).catch(this.errorHandler).finally(function () {
  362. _this8.handleChangeLoading("sCommentFavor", false);
  363. });
  364. }
  365. /**
  366. * 回复 点赞/取消点赞
  367. * @param {string} replyId replyId
  368. * @param {string} commentId commentId
  369. * @param {boolean} favored 是否已经点过赞
  370. */
  371. }, {
  372. key: "sReplyFavor",
  373. value: function sReplyFavor(replyId, commentId, favored) {
  374. var _this9 = this;
  375. this.handleChangeLoading("sReplyFavor", true);
  376. var API = this.props.API;
  377. this.axios(API + "/replies/" + replyId + "/favor", {
  378. method: favored ? "delete" : "put",
  379. data: {
  380. comment_id: commentId
  381. },
  382. withCredentials: true
  383. }).then(function (response) {
  384. _message3.default.success(favored ? "取消点赞成功!" : "点赞成功!");
  385. // 更新 list 中的该项数据的 favored
  386. var list = _this9.state.list.map(function (item) {
  387. if (item.id === commentId) {
  388. item.replies = item.replies.map(function (r) {
  389. if (r.id === replyId) {
  390. r.favored = !favored;
  391. // r.favor_count = response.data.favor_count;
  392. // 点赞数 +1,而不是使用后端返回的点赞数
  393. // 不然如果返回的不是增加 1,用户可能以为程序错误
  394. r.favor_count += favored ? -1 : 1;
  395. }
  396. return r;
  397. });
  398. }
  399. return item;
  400. });
  401. _this9.setState({ list: list });
  402. }).catch(this.errorHandler).finally(function () {
  403. _this9.handleChangeLoading("sReplyFavor", false);
  404. });
  405. }
  406. /**
  407. * 获取 OSS 上传的参数
  408. */
  409. }, {
  410. key: "sOssSts",
  411. value: function sOssSts() {
  412. var _this10 = this;
  413. this.handleChangeLoading("sOssSts", true);
  414. var API = this.props.API;
  415. this.axios.get(API + "/oss/sts").then(function (response) {
  416. _this10.setState({ oss: _extends({}, response.data) });
  417. }).catch(this.errorHandler).finally(function () {
  418. _this10.handleChangeLoading("sOssSts", false);
  419. });
  420. }
  421. }, {
  422. key: "render",
  423. value: function render() {
  424. // 添加到 Context 的数据
  425. var value = _extends({}, this.state, this.props, {
  426. sCreateComment: this.sCreateComment,
  427. sGetComment: this.sGetComment,
  428. sCommentFavor: this.sCommentFavor,
  429. sReplyFavor: this.sReplyFavor,
  430. sCreateReply: this.sCreateReply,
  431. sGetReply: this.sGetReply,
  432. sOssSts: this.sOssSts,
  433. sDeleteComment: this.sDeleteComment,
  434. sDeleteReply: this.sDeleteReply
  435. });
  436. return _react2.default.createElement(
  437. _Comment.CommentContext.Provider,
  438. { value: value },
  439. _react2.default.createElement(
  440. "div",
  441. { className: "comment" },
  442. this.props.showEditor && _react2.default.createElement(_CommentInput2.default, { content: this.props.children }),
  443. this.props.showList && _react2.default.createElement(
  444. "div",
  445. { style: { marginTop: 20 } },
  446. _react2.default.createElement(_CommentList2.default, null)
  447. )
  448. )
  449. );
  450. }
  451. }]);
  452. return App;
  453. }(_react.Component);
  454. App.propTypes = {
  455. type: _propTypes2.default.number.isRequired, // 评论的 type
  456. businessId: _propTypes2.default.string.isRequired, // 评论的 business_id
  457. API: _propTypes2.default.string, // 评论的 API 前缀
  458. showList: _propTypes2.default.bool, // 是否显示评论列表
  459. showEditor: _propTypes2.default.bool, // 是否显示评论输入框
  460. showAlertComment: _propTypes2.default.bool, // 评论成功之后,是否通过 Antd 的 Message 组件进行提示
  461. showAlertReply: _propTypes2.default.bool, // 回复成功之后,是否通过 Antd 的 Message 组件进行提示
  462. showAlertFavor: _propTypes2.default.bool, // 点赞/取消点赞成功之后,是否通过 Antd 的 Message 组件进行提示
  463. showError: _propTypes2.default.bool, // 是否使用Antd的Message组件提示错误信息
  464. onError: _propTypes2.default.func, // 错误回调, 出错了会被调用
  465. userId: _propTypes2.default.number // 用户id, comment内部不维护用户id, 调用组件时传递过来, 目前用于判断是否显示删除按钮
  466. };
  467. App.defaultProps = {
  468. API: "http://api.links123.net/comment/v1",
  469. showList: true,
  470. showEditor: true,
  471. showAlertComment: false,
  472. showAlertReply: false,
  473. showAlertFavor: false,
  474. showError: true
  475. };
  476. exports.Editor = _Editor2.default;
  477. exports.RenderText = _RenderText2.default;
  478. exports.default = App;
  479. //# sourceMappingURL=App.js.map