通用评论

App.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _message2 = require("antd/es/message");
  6. var _message3 = _interopRequireDefault(_message2);
  7. 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; };
  8. 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; }; }();
  9. require("antd/es/message/style/css");
  10. var _react = require("react");
  11. var _react2 = _interopRequireDefault(_react);
  12. var _axios = require("./axios");
  13. var _axios2 = _interopRequireDefault(_axios);
  14. var _constant = require("./constant");
  15. var _Comment = require("./Comment");
  16. var _helper = require("./helper");
  17. var _CommentInput = require("./components/CommentInput");
  18. var _CommentInput2 = _interopRequireDefault(_CommentInput);
  19. var _CommentList = require("./components/CommentList");
  20. var _CommentList2 = _interopRequireDefault(_CommentList);
  21. require("./App.css");
  22. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  23. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  24. 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; }
  25. 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; }
  26. // import * as mock from "./mock";
  27. var App = function (_Component) {
  28. _inherits(App, _Component);
  29. function App(props) {
  30. _classCallCheck(this, App);
  31. var _this = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this, props));
  32. _this.state = {
  33. loading: {},
  34. // oss 配置
  35. oss: {},
  36. // 评论数据
  37. list: [],
  38. page: 1,
  39. total: 0,
  40. // 是否没有更多评论了
  41. isNoMoreComment: false
  42. };
  43. _this.handleChangeLoading = _this.handleChangeLoading.bind(_this);
  44. _this.sGetComment = _this.sGetComment.bind(_this);
  45. _this.sGetReply = _this.sGetReply.bind(_this);
  46. _this.sCreateComment = _this.sCreateComment.bind(_this);
  47. _this.sCreateReply = _this.sCreateReply.bind(_this);
  48. _this.sCommentFavor = _this.sCommentFavor.bind(_this);
  49. _this.sOssSts = _this.sOssSts.bind(_this);
  50. return _this;
  51. }
  52. _createClass(App, [{
  53. key: "componentDidMount",
  54. value: function componentDidMount() {
  55. this.sGetComment();
  56. }
  57. /**
  58. * 改变 loading 状态
  59. * @param {string} key key
  60. * @param {string} value value
  61. */
  62. }, {
  63. key: "handleChangeLoading",
  64. value: function handleChangeLoading(key, value) {
  65. var loading = this.state.loading;
  66. loading[key] = value;
  67. this.setState({ loading: loading });
  68. }
  69. /**
  70. * 获取评论列表
  71. */
  72. }, {
  73. key: "sGetComment",
  74. value: function sGetComment() {
  75. var _this2 = this;
  76. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  77. _ref$type = _ref.type,
  78. type = _ref$type === undefined ? 1 : _ref$type,
  79. _ref$businessId = _ref.businessId,
  80. businessId = _ref$businessId === undefined ? 1 : _ref$businessId,
  81. _ref$page = _ref.page,
  82. page = _ref$page === undefined ? 1 : _ref$page;
  83. this.handleChangeLoading("sGetComment", true);
  84. // 测试数据列表
  85. // const { comments } = mock;
  86. // this.setState({
  87. // list: comments.list,
  88. // page: 1,
  89. // total: 100
  90. // });
  91. // this.handleChangeLoading("sGetComment", false);
  92. // return;
  93. _axios2.default.get(_constant.URL + "/comments?type=" + type + "&business_id=" + businessId + "&page=" + page + "&limit=" + _constant.LIMIT).then(function (response) {
  94. var _response$data = response.data,
  95. list = _response$data.list,
  96. page = _response$data.page,
  97. total = _response$data.total;
  98. if (list) {
  99. var newList = list;
  100. if (page > 1) {
  101. var oldList = _this2.state.list;
  102. // 删除临时数据
  103. oldList = oldList.filter(function (o) {
  104. return !o.isTemporary;
  105. });
  106. newList = oldList.concat(newList);
  107. }
  108. _this2.setState({
  109. list: newList,
  110. page: page,
  111. total: total
  112. });
  113. } else {
  114. _message3.default.info("没有更多评论了");
  115. _this2.setState({
  116. isNoMoreComment: true
  117. });
  118. }
  119. }).catch(function (error) {
  120. if (error.response && error.response.data && error.response.data.msg) {
  121. _message3.default.error(error.response.data.msg || _constant.ERROR_DEFAULT);
  122. return;
  123. }
  124. _message3.default.error(error.message || _constant.ERROR_DEFAULT);
  125. }).finally(function () {
  126. _this2.handleChangeLoading("sGetComment", false);
  127. });
  128. }
  129. /**
  130. * 获取更多回复
  131. */
  132. }, {
  133. key: "sGetReply",
  134. value: function sGetReply() {
  135. var _this3 = this;
  136. var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  137. commentId = _ref2.commentId,
  138. _ref2$page = _ref2.page,
  139. page = _ref2$page === undefined ? 1 : _ref2$page;
  140. this.handleChangeLoading("sGetReply", true);
  141. _axios2.default.get(_constant.URL + "/replies?comment_id=" + commentId + "&page=" + page + "&limit=" + _constant.LIMIT).then(function (response) {
  142. var replies = response.data.list;
  143. if (!replies) {
  144. _message3.default.info("没有更多数据了!");
  145. }
  146. var list = _this3.state.list.map(function (item) {
  147. if (item.id === commentId) {
  148. if (!item.replies) item.replies = [];
  149. if (replies) {
  150. if (page === 1) {
  151. // 如果当前页数为第一页,则清空当前所有的 replies
  152. // 并将获取到的 replies 存放在 state
  153. item.replies = replies;
  154. } else {
  155. item.replies = item.replies.concat(replies);
  156. // 如果当前页数非第一页,则合并 replies
  157. }
  158. } else {
  159. item.isNoMoreReply = true;
  160. }
  161. }
  162. return item;
  163. });
  164. _this3.setState({ list: list });
  165. }).catch(function (error) {
  166. if (error.response && error.response.data && error.response.data.msg) {
  167. _message3.default.error(error.response.data.msg || _constant.ERROR_DEFAULT);
  168. return;
  169. }
  170. _message3.default.error(error.message || _constant.ERROR_DEFAULT);
  171. }).finally(function () {
  172. _this3.handleChangeLoading("sGetReply", false);
  173. });
  174. }
  175. /**
  176. * 添加评论
  177. * @param {object} data { type, business_id, content }
  178. */
  179. }, {
  180. key: "sCreateComment",
  181. value: function sCreateComment(data) {
  182. var _this4 = this;
  183. if (!data.content) return _message3.default.error("评论内容不能为空 ");
  184. this.handleChangeLoading("sCreateComment", true);
  185. (0, _axios2.default)(_constant.URL + "/comments", {
  186. method: "post",
  187. data: data,
  188. withCredentials: true
  189. }).then(function (response) {
  190. _message3.default.success("评论成功!");
  191. // 将数据写入到 list 中
  192. // 临时插入
  193. // 等到获取数据之后,删除临时数据
  194. var list = _this4.state.list;
  195. list.unshift(_extends({}, response.data, {
  196. isTemporary: true // 临时的数据
  197. }));
  198. _this4.setState({ list: list });
  199. }).catch(function (error) {
  200. if (error.response && error.response.data && error.response.data.msg) {
  201. _message3.default.error(error.response.data.msg || _constant.ERROR_DEFAULT);
  202. return;
  203. }
  204. _message3.default.error(error.message || _constant.ERROR_DEFAULT);
  205. }).finally(function () {
  206. _this4.handleChangeLoading("sCreateComment", false);
  207. });
  208. }
  209. /**
  210. * 添加回复
  211. * 回复评论/回复回复
  212. * @param {object} data { comment_id, content, [reply_id] }
  213. */
  214. }, {
  215. key: "sCreateReply",
  216. value: function sCreateReply(data, cb) {
  217. var _this5 = this;
  218. if (!data.content) return _message3.default.error("回复内容不能为空 ");
  219. this.handleChangeLoading("sCreateReply", true);
  220. (0, _axios2.default)(_constant.URL + "/replies", {
  221. method: "post",
  222. data: data,
  223. withCredentials: true
  224. }).then(function (response) {
  225. // console.log("response: ", response.data);
  226. // // 将该条数据插入到 list 中
  227. // const list = this.state.list.map(item => {
  228. // if (item.id === data.comment_id) {
  229. // if (!item.replies) item.replies = [];
  230. // item.reply_count += 1
  231. // item.replies.unshift(response.data);
  232. // }
  233. // return item;
  234. // });
  235. // this.setState({ list });
  236. _this5.sGetReply({ commentId: data.comment_id });
  237. _message3.default.success("回复成功!");
  238. if ((0, _helper.isFunction)(cb)) cb();
  239. }).catch(function (error) {
  240. if (error.response && error.response.data && error.response.data.msg) {
  241. _message3.default.error(error.response.data.msg || _constant.ERROR_DEFAULT);
  242. return;
  243. }
  244. _message3.default.error(error.message || _constant.ERROR_DEFAULT);
  245. }).finally(function () {
  246. _this5.handleChangeLoading("sCreateReply", false);
  247. });
  248. }
  249. /**
  250. * 点赞/取消点赞
  251. * @param {string} commentId { commentId }
  252. * @param {boolean} favored 是否已经点过赞
  253. */
  254. }, {
  255. key: "sCommentFavor",
  256. value: function sCommentFavor(commentId, favored) {
  257. var _this6 = this;
  258. this.handleChangeLoading("sCommentFavor", true);
  259. (0, _axios2.default)(_constant.URL + "/comments/" + commentId + "/favor", {
  260. method: favored ? "delete" : "put",
  261. withCredentials: true
  262. }).then(function (response) {
  263. _message3.default.success(favored ? "取消点赞成功!" : "点赞成功!");
  264. // 更新 list 中的该项数据的 favored
  265. var list = _this6.state.list.map(function (item) {
  266. if (item.id === commentId) {
  267. item.favored = !favored;
  268. item.favor_count += favored ? -1 : 1;
  269. }
  270. return item;
  271. });
  272. _this6.setState({ list: list });
  273. }).catch(function (error) {
  274. if (error.response && error.response.data && error.response.data.msg) {
  275. _message3.default.error(error.response.data.msg || _constant.ERROR_DEFAULT);
  276. return;
  277. }
  278. _message3.default.error(error.message || _constant.ERROR_DEFAULT);
  279. }).finally(function () {
  280. _this6.handleChangeLoading("sCommentFavor", false);
  281. });
  282. }
  283. /**
  284. * 获取 OSS 上传的参数
  285. */
  286. }, {
  287. key: "sOssSts",
  288. value: function sOssSts() {
  289. var _this7 = this;
  290. this.handleChangeLoading("sOssSts", true);
  291. _axios2.default.get(_constant.URL + "/oss/sts").then(function (response) {
  292. _this7.setState({ oss: _extends({}, response.data) });
  293. }).catch(function (error) {
  294. if (error.response && error.response.data && error.response.data.msg) {
  295. _message3.default.error(error.response.data.msg || _constant.ERROR_DEFAULT);
  296. return;
  297. }
  298. _message3.default.error(error.message || _constant.ERROR_DEFAULT);
  299. }).finally(function () {
  300. _this7.handleChangeLoading("sOssSts", false);
  301. });
  302. }
  303. }, {
  304. key: "render",
  305. value: function render() {
  306. // 添加到 Context 的数据
  307. var value = _extends({}, this.state, {
  308. sCreateComment: this.sCreateComment,
  309. sGetComment: this.sGetComment,
  310. sCommentFavor: this.sCommentFavor,
  311. sCreateReply: this.sCreateReply,
  312. sGetReply: this.sGetReply,
  313. sOssSts: this.sOssSts
  314. });
  315. return _react2.default.createElement(
  316. _Comment.CommentContext.Provider,
  317. { value: value },
  318. _react2.default.createElement(
  319. "div",
  320. { className: "comment" },
  321. _react2.default.createElement(_CommentInput2.default, null),
  322. _react2.default.createElement(
  323. "div",
  324. { style: { marginTop: 20 } },
  325. _react2.default.createElement(_CommentList2.default, null)
  326. )
  327. )
  328. );
  329. }
  330. }]);
  331. return App;
  332. }(_react.Component);
  333. exports.default = App;
  334. //# sourceMappingURL=App.js.map