通用评论

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