通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. _this5.props.onDelete(_constant.COMMENT_TYPE.COMMENT);
  273. }).catch(this.errorHandler).finally(function () {
  274. _this5.handleChangeLoading("sDeleteComment", false);
  275. });
  276. }
  277. /**
  278. * 添加回复
  279. * 回复评论/回复回复
  280. * @param {object} data { comment_id, content, [reply_id] }
  281. */
  282. }, {
  283. key: "sCreateReply",
  284. value: function sCreateReply(data, cb) {
  285. var _this6 = this;
  286. if (!data.content) return this.error("回复内容不能为空 ");
  287. this.handleChangeLoading("sCreateReply", true);
  288. var API = this.props.API;
  289. this.axios(API + "/replies", {
  290. method: "post",
  291. data: data,
  292. withCredentials: true
  293. }).then(function (response) {
  294. if (_this6.props.showAlertReply) {
  295. _message3.default.success("回复成功!");
  296. }
  297. if ((0, _helper.isFunction)(cb)) cb();
  298. // 将数据写入到 list 中
  299. // 临时插入
  300. // 等到获取数据之后,删除临时数据
  301. var list = _this6.state.list.map(function (item) {
  302. if (item.id === data.comment_id) {
  303. if (!item.replies) item.replies = [];
  304. item.replies.push(_extends({}, response.data, {
  305. isTemporary: true // 临时的数据
  306. }));
  307. item.reply_count += 1;
  308. }
  309. return item;
  310. });
  311. _this6.setState({ list: list });
  312. }).catch(this.errorHandler).finally(function () {
  313. _this6.handleChangeLoading("sCreateReply", false);
  314. });
  315. }
  316. /**
  317. * 删除回复
  318. * @param {*} replyId
  319. * @param {*} commentId
  320. */
  321. }, {
  322. key: "sDeleteReply",
  323. value: function sDeleteReply(replyId, commentId) {
  324. var _this7 = this;
  325. this.handleChangeLoading("sDeleteReply", true);
  326. var API = this.props.API;
  327. this.axios(API + "/replies/" + replyId + "?CommentID=" + commentId, {
  328. method: "delete",
  329. withCredentials: true
  330. }).then(function () {
  331. var list = _this7.state.list.map(function (item) {
  332. if (item.id === commentId) {
  333. var replies = item.replies.filter(function (item) {
  334. return item.id !== replyId;
  335. });
  336. item.replies = replies;
  337. item.reply_count -= 1;
  338. }
  339. return item;
  340. });
  341. _this7.setState({ list: list });
  342. _this7.props.onDelete(_constant.COMMENT_TYPE.REPLY);
  343. }).catch(this.errorHandler).finally(function () {
  344. _this7.handleChangeLoading("sDeleteReply", false);
  345. });
  346. }
  347. /**
  348. * 评论 点赞/取消点赞
  349. * @param {string} commentId { commentId }
  350. * @param {boolean} favored 是否已经点过赞
  351. */
  352. }, {
  353. key: "sCommentFavor",
  354. value: function sCommentFavor(commentId, favored) {
  355. var _this8 = this;
  356. this.handleChangeLoading("sCommentFavor", true);
  357. var API = this.props.API;
  358. this.axios(API + "/comments/" + commentId + "/favor", {
  359. method: favored ? "delete" : "put",
  360. withCredentials: true
  361. }).then(function (response) {
  362. if (_this8.props.showAlertFavor) {
  363. _message3.default.success(favored ? "取消点赞成功!" : "点赞成功!");
  364. }
  365. // 更新 list 中的该项数据的 favored
  366. var list = _this8.state.list.map(function (item) {
  367. if (item.id === commentId) {
  368. item.favored = !favored;
  369. item.favor_count += favored ? -1 : 1;
  370. }
  371. return item;
  372. });
  373. _this8.setState({ list: list });
  374. }).catch(this.errorHandler).finally(function () {
  375. _this8.handleChangeLoading("sCommentFavor", false);
  376. });
  377. }
  378. /**
  379. * 回复 点赞/取消点赞
  380. * @param {string} replyId replyId
  381. * @param {string} commentId commentId
  382. * @param {boolean} favored 是否已经点过赞
  383. */
  384. }, {
  385. key: "sReplyFavor",
  386. value: function sReplyFavor(replyId, commentId, favored) {
  387. var _this9 = this;
  388. this.handleChangeLoading("sReplyFavor", true);
  389. var API = this.props.API;
  390. this.axios(API + "/replies/" + replyId + "/favor", {
  391. method: favored ? "delete" : "put",
  392. data: {
  393. comment_id: commentId
  394. },
  395. withCredentials: true
  396. }).then(function (response) {
  397. _message3.default.success(favored ? "取消点赞成功!" : "点赞成功!");
  398. // 更新 list 中的该项数据的 favored
  399. var list = _this9.state.list.map(function (item) {
  400. if (item.id === commentId) {
  401. item.replies = item.replies.map(function (r) {
  402. if (r.id === replyId) {
  403. r.favored = !favored;
  404. // r.favor_count = response.data.favor_count;
  405. // 点赞数 +1,而不是使用后端返回的点赞数
  406. // 不然如果返回的不是增加 1,用户可能以为程序错误
  407. r.favor_count += favored ? -1 : 1;
  408. }
  409. return r;
  410. });
  411. }
  412. return item;
  413. });
  414. _this9.setState({ list: list });
  415. }).catch(this.errorHandler).finally(function () {
  416. _this9.handleChangeLoading("sReplyFavor", false);
  417. });
  418. }
  419. /**
  420. * 获取 OSS 上传的参数
  421. */
  422. }, {
  423. key: "sOssSts",
  424. value: function sOssSts() {
  425. var _this10 = this;
  426. this.handleChangeLoading("sOssSts", true);
  427. var API = this.props.API;
  428. this.axios.get(API + "/oss/sts").then(function (response) {
  429. _this10.setState({ oss: _extends({}, response.data) });
  430. }).catch(this.errorHandler).finally(function () {
  431. _this10.handleChangeLoading("sOssSts", false);
  432. });
  433. }
  434. }, {
  435. key: "render",
  436. value: function render() {
  437. // 添加到 Context 的数据
  438. var value = _extends({}, this.state, this.props, {
  439. sCreateComment: this.sCreateComment,
  440. sGetComment: this.sGetComment,
  441. sCommentFavor: this.sCommentFavor,
  442. sReplyFavor: this.sReplyFavor,
  443. sCreateReply: this.sCreateReply,
  444. sGetReply: this.sGetReply,
  445. sOssSts: this.sOssSts,
  446. sDeleteComment: this.sDeleteComment,
  447. sDeleteReply: this.sDeleteReply
  448. });
  449. return _react2.default.createElement(
  450. _Comment.CommentContext.Provider,
  451. { value: value },
  452. _react2.default.createElement(
  453. "div",
  454. { className: "comment" },
  455. this.props.showEditor && _react2.default.createElement(_CommentInput2.default, { content: this.props.children }),
  456. this.props.showList && _react2.default.createElement(
  457. "div",
  458. { style: { marginTop: 20 } },
  459. _react2.default.createElement(_CommentList2.default, null)
  460. )
  461. )
  462. );
  463. }
  464. }]);
  465. return App;
  466. }(_react.Component);
  467. App.propTypes = {
  468. type: _propTypes2.default.number.isRequired, // 评论的 type
  469. businessId: _propTypes2.default.string.isRequired, // 评论的 business_id
  470. API: _propTypes2.default.string, // 评论的 API 前缀
  471. showList: _propTypes2.default.bool, // 是否显示评论列表
  472. showEditor: _propTypes2.default.bool, // 是否显示评论输入框
  473. showAlertComment: _propTypes2.default.bool, // 评论成功之后,是否通过 Antd 的 Message 组件进行提示
  474. showAlertReply: _propTypes2.default.bool, // 回复成功之后,是否通过 Antd 的 Message 组件进行提示
  475. showAlertFavor: _propTypes2.default.bool, // 点赞/取消点赞成功之后,是否通过 Antd 的 Message 组件进行提示
  476. showError: _propTypes2.default.bool, // 是否使用Antd的Message组件提示错误信息
  477. onError: _propTypes2.default.func, // 错误回调, 出错了会被调用
  478. userId: _propTypes2.default.number, // 用户id, comment内部不维护用户id, 调用组件时传递过来, 目前用于判断是否显示删除按钮
  479. pageType: _propTypes2.default.string, // 分页类型
  480. page: _propTypes2.default.number, // 页码
  481. onPageChange: _propTypes2.default.func, // 页码变化回调
  482. onDelete: _propTypes2.default.func
  483. };
  484. App.defaultProps = {
  485. API: "//api.links123.net/comment/v1",
  486. showList: true,
  487. showEditor: true,
  488. showAlertComment: false,
  489. showAlertReply: false,
  490. showAlertFavor: false,
  491. showError: true,
  492. pageType: "more",
  493. onPageChange: function onPageChange(page) {},
  494. onDelete: function onDelete() {}
  495. };
  496. exports.Editor = _Editor2.default;
  497. exports.RenderText = _RenderText2.default;
  498. exports.default = App;
  499. //# sourceMappingURL=App.js.map