通用评论

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