通用评论 vedio

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