通用评论

App.js 19KB

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