通用评论 vedio

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