通用评论

App.js 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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 _jsCookie = require("js-cookie");
  18. var _jsCookie2 = _interopRequireDefault(_jsCookie);
  19. var _reactIntlUniversal = require("react-intl-universal");
  20. var _reactIntlUniversal2 = _interopRequireDefault(_reactIntlUniversal);
  21. var _constant = require("./constant");
  22. var _Comment = require("./Comment");
  23. var _helper = require("./helper");
  24. var _CommentInput = require("./components/CommentInput");
  25. var _CommentInput2 = _interopRequireDefault(_CommentInput);
  26. var _CommentList = require("./components/CommentList");
  27. var _CommentList2 = _interopRequireDefault(_CommentList);
  28. var _Editor = require("./components/Editor");
  29. var _Editor2 = _interopRequireDefault(_Editor);
  30. var _RenderText = require("./components/RenderText");
  31. var _RenderText2 = _interopRequireDefault(_RenderText);
  32. var _EditComment = require("./components/EditComment/EditComment");
  33. var _EditComment2 = _interopRequireDefault(_EditComment);
  34. var _lang = require("./lang");
  35. require("./App.css");
  36. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  37. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  38. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  39. 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; }
  40. 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; }
  41. var App = function (_Component) {
  42. _inherits(App, _Component);
  43. function App(props) {
  44. _classCallCheck(this, App);
  45. var _this = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this, props));
  46. _this.state = {
  47. loading: {},
  48. // oss 配置
  49. oss: {},
  50. // 评论数据
  51. list: [],
  52. page: 1,
  53. total: 0,
  54. // 是否没有更多评论了
  55. isNoMoreComment: false,
  56. initDone: false,
  57. locale: "zh-CN",
  58. editModalVisible: false,
  59. action: "",
  60. replyId: "",
  61. commentId: "",
  62. userId: "",
  63. content: "",
  64. replyPage: 1
  65. };
  66. _this.handleChangeLoading = _this.handleChangeLoading.bind(_this);
  67. _this.sCreateComment = _this.sCreateComment.bind(_this);
  68. _this.sUpdateComment = _this.sUpdateComment.bind(_this);
  69. _this.sDeleteComment = _this.sDeleteComment.bind(_this);
  70. _this.sCommentFavor = _this.sCommentFavor.bind(_this);
  71. _this.sCreateReply = _this.sCreateReply.bind(_this);
  72. _this.sDeleteReply = _this.sDeleteReply.bind(_this);
  73. _this.errorHandler = _this.errorHandler.bind(_this);
  74. _this.sGetComment = _this.sGetComment.bind(_this);
  75. _this.sReplyFavor = _this.sReplyFavor.bind(_this);
  76. _this.sGetReply = _this.sGetReply.bind(_this);
  77. _this.sUpdateReply = _this.sUpdateReply.bind(_this);
  78. _this.sOssSts = _this.sOssSts.bind(_this);
  79. _this.handleEdit = _this.handleEdit.bind(_this);
  80. _this.handleClose = _this.handleClose.bind(_this);
  81. return _this;
  82. }
  83. _createClass(App, [{
  84. key: "componentWillMount",
  85. value: function componentWillMount() {
  86. this.axios = _axios2.default;
  87. this.axios.defaults.withCredentials = true;
  88. if (this.props.token) {
  89. this.axios.defaults.headers.common["Authorization"] = "Bearer " + this.props.token;
  90. }
  91. }
  92. }, {
  93. key: "componentDidMount",
  94. value: function componentDidMount() {
  95. this.loadLocales();
  96. }
  97. /**
  98. * 加载语言包
  99. * 只能根据url或者传入的props来确定加载哪个语言包
  100. * 优先级:传入的props > url
  101. */
  102. }, {
  103. key: "loadLocales",
  104. value: function loadLocales() {
  105. var _this2 = this;
  106. var currentLocale = this.props.locales;
  107. var cookieLang = _jsCookie2.default.get("lnk_lang");
  108. if (!currentLocale) {
  109. currentLocale = cookieLang || _reactIntlUniversal2.default.determineLocale({
  110. urlLocaleKey: "lang"
  111. });
  112. }
  113. currentLocale = _lang.SUPPORT_LOCALES.find(function (item) {
  114. return item.value === currentLocale;
  115. }) ? currentLocale : "zh-CN";
  116. var version = require("./version.json").hash;
  117. // 使用绝对路径
  118. var languageURL = _constant.LANGUAGE_LINK + "/" + currentLocale + ".json?v=" + version;
  119. return fetch(languageURL).then(function (response) {
  120. if (response.status >= 400) {
  121. throw new Error("Bad response from server");
  122. }
  123. return response.json();
  124. }).then(function (data) {
  125. // console.log('data: ', data);
  126. _reactIntlUniversal2.default.init({
  127. currentLocale: currentLocale,
  128. locales: _defineProperty({}, currentLocale, data)
  129. }).then(function () {
  130. _this2.setState({ initDone: true, locale: currentLocale });
  131. });
  132. });
  133. }
  134. }, {
  135. key: "handleEdit",
  136. value: function handleEdit(_ref) {
  137. var replyId = _ref.replyId,
  138. commentId = _ref.commentId,
  139. userId = _ref.userId,
  140. content = _ref.content,
  141. replyPage = _ref.replyPage;
  142. this.setState({
  143. editModalVisible: true,
  144. action: content.replies ? "comment" : content.reply ? "replyToReply" : "reply",
  145. replyId: replyId,
  146. commentId: commentId,
  147. userId: userId,
  148. content: content,
  149. replyPage: replyPage
  150. });
  151. }
  152. }, {
  153. key: "handleClose",
  154. value: function handleClose() {
  155. this.setState({
  156. editModalVisible: false
  157. });
  158. }
  159. }, {
  160. key: "error",
  161. value: function error(msg) {
  162. var info = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  163. if (this.props.showError) {
  164. _message3.default.error(msg);
  165. }
  166. if (this.props.onError) {
  167. this.props.onError(msg, info);
  168. }
  169. }
  170. }, {
  171. key: "errorHandler",
  172. value: function errorHandler(error) {
  173. var locale = this.state.locale;
  174. var localResponse = _lang.LOCALES_RESPONSE[locale];
  175. if (error.response && error.response.data && error.response.data.msg) {
  176. this.error(localResponse[error.response.data.msg] || _constant.ERROR_DEFAULT, {
  177. response: error.response
  178. });
  179. return;
  180. }
  181. this.error(localResponse[error.response.data.msg] || _constant.ERROR_DEFAULT, {
  182. response: error.response
  183. });
  184. }
  185. /**
  186. * 改变 loading 状态
  187. * @param {string} key key
  188. * @param {string} value value
  189. */
  190. }, {
  191. key: "handleChangeLoading",
  192. value: function handleChangeLoading(key, value) {
  193. var loading = this.state.loading;
  194. loading[key] = value;
  195. this.setState({ loading: loading });
  196. }
  197. /**
  198. * 获取评论列表
  199. */
  200. }, {
  201. key: "sGetComment",
  202. value: function sGetComment() {
  203. var _this3 = this;
  204. var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  205. _ref2$page = _ref2.page,
  206. page = _ref2$page === undefined ? 1 : _ref2$page;
  207. var pageType = this.props.pageType;
  208. this.handleChangeLoading("sGetComment", true);
  209. var _props = this.props,
  210. API = _props.API,
  211. type = _props.type,
  212. businessId = _props.businessId,
  213. limit = _props.limit;
  214. this.axios.get(API + "/comments?type=" + type + "&business_id=" + businessId + "&page=" + page + "&limit=" + limit).then(function (response) {
  215. var _response$data = response.data,
  216. list = _response$data.list,
  217. page = _response$data.page,
  218. total = _response$data.total;
  219. if (list) {
  220. var newList = list;
  221. var oldList = _this3.state.list;
  222. if (pageType === "more") {
  223. if (page > 1) {
  224. // 删除临时数据
  225. oldList = oldList.filter(function (o) {
  226. return !o.isTemporary;
  227. });
  228. newList = oldList.concat(newList);
  229. }
  230. } else if (pageType === "pagination") {
  231. // TODO 滚动到顶部
  232. window.scrollTo(0, 0);
  233. }
  234. _this3.setState({
  235. list: newList,
  236. page: page,
  237. total: total
  238. });
  239. _this3.props.onCountChange(total);
  240. } else {
  241. _message3.default.info(_reactIntlUniversal2.default.get("message.noMoreComment"));
  242. _this3.setState({
  243. isNoMoreComment: true
  244. });
  245. }
  246. }).catch(this.errorHandler).finally(function () {
  247. _this3.handleChangeLoading("sGetComment", false);
  248. });
  249. }
  250. /**
  251. * 获取更多回复
  252. */
  253. }, {
  254. key: "sGetReply",
  255. value: function sGetReply() {
  256. var _this4 = this;
  257. var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  258. commentId = _ref3.commentId,
  259. _ref3$page = _ref3.page,
  260. page = _ref3$page === undefined ? 1 : _ref3$page;
  261. this.handleChangeLoading("sGetReply", true);
  262. var _props2 = this.props,
  263. API = _props2.API,
  264. limit = _props2.limit;
  265. this.axios.get(API + "/replies?comment_id=" + commentId + "&page=" + page + "&limit=" + limit).then(function (response) {
  266. if (!response.data.list) {
  267. _message3.default.info(_reactIntlUniversal2.default.get("message.noMoreData"));
  268. }
  269. var list = _this4.state.list.map(function (item) {
  270. if (item.id === commentId) {
  271. if (!item.replies) item.replies = [];
  272. if (response.data.list) {
  273. if (page === 1) {
  274. // 如果当前页数为第一页,则清空当前所有的 replies
  275. // 并将获取到的 replies 存放在 state
  276. item.replies = response.data.list;
  277. } else {
  278. item.replies = item.replies.filter(function (o) {
  279. return !o.isTemporary;
  280. }).concat(response.data.list);
  281. // 如果当前页数非第一页,则合并 replies
  282. }
  283. item.reply_count = response.data.total;
  284. item.reply_page = response.data.page;
  285. } else {
  286. item.isNoMoreReply = true;
  287. }
  288. }
  289. return item;
  290. });
  291. _this4.setState({ list: list });
  292. }).catch(this.errorHandler).finally(function () {
  293. _this4.handleChangeLoading("sGetReply", false);
  294. });
  295. }
  296. /**
  297. * 添加评论
  298. * @param {object} {content} comment content
  299. */
  300. }, {
  301. key: "sCreateComment",
  302. value: function sCreateComment() {
  303. var _this5 = this;
  304. var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  305. content = _ref4.content;
  306. var cb = arguments[1];
  307. if (!content) return this.error(_reactIntlUniversal2.default.get("message.notNull"));
  308. this.handleChangeLoading("sCreateComment", true);
  309. var _props3 = this.props,
  310. API = _props3.API,
  311. type = _props3.type,
  312. businessId = _props3.businessId,
  313. businessUserId = _props3.businessUserId;
  314. this.axios(API + "/comments", {
  315. method: "post",
  316. data: {
  317. type: type,
  318. business_id: businessId,
  319. business_user_id: businessUserId,
  320. content: content
  321. },
  322. withCredentials: true
  323. }).then(function (response) {
  324. if (_this5.props.showAlertComment) {
  325. _message3.default.success(_reactIntlUniversal2.default.get("message.success"));
  326. }
  327. if ((0, _helper.isFunction)(cb)) cb(response.data);
  328. // 将数据写入到 list 中
  329. // 临时插入
  330. // 等到获取数据之后,删除临时数据
  331. var _state = _this5.state,
  332. list = _state.list,
  333. total = _state.total;
  334. list.unshift(_extends({}, response.data, {
  335. replies: [],
  336. isTemporary: true // 临时的数据
  337. }));
  338. _this5.setState({ list: list, total: total + 1 });
  339. _this5.props.onCountChange(total + 1);
  340. }).catch(this.errorHandler).finally(function () {
  341. _this5.handleChangeLoading("sCreateComment", false);
  342. });
  343. }
  344. /**
  345. * 删除评论
  346. */
  347. }, {
  348. key: "sDeleteComment",
  349. value: function sDeleteComment(commentId) {
  350. var _this6 = this;
  351. this.handleChangeLoading("sDeleteComment", true);
  352. var API = this.props.API;
  353. this.axios(API + "/comments/" + commentId, {
  354. method: "delete",
  355. withCredentials: true
  356. }).then(function () {
  357. var _state2 = _this6.state,
  358. list = _state2.list,
  359. total = _state2.total;
  360. var res = list.filter(function (item) {
  361. return item.id !== commentId;
  362. });
  363. var deletedItem = list.find(function (item) {
  364. return item.id === commentId;
  365. });
  366. _this6.setState({ list: res, total: total - 1 });
  367. _this6.props.onDelete(_constant.COMMENT_TYPE.COMMENT, deletedItem);
  368. _this6.props.onCountChange(total - 1);
  369. }).catch(this.errorHandler).finally(function () {
  370. _this6.handleChangeLoading("sDeleteComment", false);
  371. });
  372. }
  373. /**
  374. * 更新评论
  375. * @param {object} {content} comment content
  376. */
  377. }, {
  378. key: "sUpdateComment",
  379. value: function sUpdateComment(_ref5) {
  380. var _this7 = this;
  381. var commentId = _ref5.commentId,
  382. content = _ref5.content;
  383. this.handleChangeLoading("sUpdateComment", true);
  384. var API = this.props.API;
  385. this.axios(API + "/comments/" + commentId, {
  386. method: "post",
  387. data: {
  388. content: content
  389. },
  390. withCredentials: true
  391. }).then(function () {
  392. var list = _this7.state.list;
  393. list = list.map(function (it) {
  394. if (it.id === commentId) {
  395. return _extends({}, it, {
  396. content: content
  397. });
  398. }
  399. return it;
  400. });
  401. _this7.props.onUpdateComment("comment");
  402. _this7.setState({ list: list });
  403. }).catch(this.errorHandler).finally(function () {
  404. _this7.handleChangeLoading("sUpdateComment", false);
  405. });
  406. }
  407. /**
  408. * 添加回复
  409. * 回复评论/回复回复
  410. * @param {object} data { comment_id, content, [reply_id] }
  411. */
  412. }, {
  413. key: "sCreateReply",
  414. value: function sCreateReply(data, cb) {
  415. var _this8 = this;
  416. if (!data.content) return this.error(_reactIntlUniversal2.default.get("message.replyNoNull"));
  417. this.handleChangeLoading("sCreateReply", true);
  418. var API = this.props.API;
  419. this.axios(API + "/replies", {
  420. method: "post",
  421. data: data,
  422. withCredentials: true
  423. }).then(function (response) {
  424. if (_this8.props.showAlertReply) {
  425. _message3.default.success(_reactIntlUniversal2.default.get("message.replySuccess"));
  426. }
  427. if ((0, _helper.isFunction)(cb)) cb(response.data);
  428. // 将数据写入到 list 中
  429. // 临时插入
  430. // 等到获取数据之后,删除临时数据
  431. var list = _this8.state.list.map(function (item) {
  432. if (item.id === data.comment_id) {
  433. if (!item.replies) item.replies = [];
  434. item.replies.push(_extends({}, response.data, {
  435. isTemporary: true // 临时的数据
  436. }));
  437. item.reply_count += 1;
  438. }
  439. return item;
  440. });
  441. _this8.setState({ list: list });
  442. }).catch(this.errorHandler).finally(function () {
  443. _this8.handleChangeLoading("sCreateReply", false);
  444. });
  445. }
  446. /**
  447. * 删除回复
  448. * @param {*} replyId
  449. * @param {*} commentId
  450. */
  451. }, {
  452. key: "sDeleteReply",
  453. value: function sDeleteReply(replyId, commentId) {
  454. var _this9 = this;
  455. this.handleChangeLoading("sDeleteReply", true);
  456. var API = this.props.API;
  457. this.axios(API + "/replies/" + replyId + "?CommentID=" + commentId, {
  458. method: "delete",
  459. withCredentials: true
  460. }).then(function () {
  461. var deletedItem = null;
  462. var list = _this9.state.list.map(function (item) {
  463. if (item.id === commentId) {
  464. var replies = item.replies.filter(function (item) {
  465. return item.id !== replyId;
  466. });
  467. deletedItem = item.replies.find(function (item) {
  468. return item.id === replyId;
  469. });
  470. item.replies = replies;
  471. item.reply_count -= 1;
  472. }
  473. return item;
  474. });
  475. _this9.setState({ list: list });
  476. _this9.props.onDelete(_constant.COMMENT_TYPE.REPLY, deletedItem);
  477. }).catch(this.errorHandler).finally(function () {
  478. _this9.handleChangeLoading("sDeleteReply", false);
  479. });
  480. }
  481. /**
  482. * 更新回复
  483. * 回复评论/回复回复
  484. * @param {object} data { comment_id, content, reply_id }
  485. */
  486. }, {
  487. key: "sUpdateReply",
  488. value: function sUpdateReply(_ref6) {
  489. var _this10 = this;
  490. var commentId = _ref6.commentId,
  491. content = _ref6.content,
  492. replyId = _ref6.replyId,
  493. replyPage = _ref6.replyPage;
  494. this.handleChangeLoading("sUpdateReply", true);
  495. var API = this.props.API;
  496. this.axios(API + "/replies/" + replyId, {
  497. method: "post",
  498. data: {
  499. comment_id: commentId,
  500. content: content
  501. },
  502. withCredentials: true
  503. }).then(function () {
  504. for (var i = 1; i <= replyPage; i++) {
  505. _this10.sGetReply({ commentId: commentId, page: i });
  506. }
  507. _this10.props.onUpdateComment("reply");
  508. }).catch(this.errorHandler).finally(function () {
  509. _this10.handleChangeLoading("sUpdateReply", false);
  510. });
  511. }
  512. /**
  513. * 评论 点赞/取消点赞
  514. * @param {string} commentId { commentId }
  515. * @param {boolean} favored 是否已经点过赞
  516. */
  517. }, {
  518. key: "sCommentFavor",
  519. value: function sCommentFavor(commentId, favored) {
  520. var _this11 = this;
  521. this.handleChangeLoading("sCommentFavor", true);
  522. var API = this.props.API;
  523. this.axios(API + "/comments/" + commentId + "/favor", {
  524. method: favored ? "delete" : "put",
  525. withCredentials: true
  526. }).then(function (response) {
  527. if (_this11.props.showAlertFavor) {
  528. _message3.default.success(favored ? _reactIntlUniversal2.default.get("message.cancelLickSuccess") : _reactIntlUniversal2.default.get("message.likeSuccess"));
  529. }
  530. // 更新 list 中的该项数据的 favored
  531. var list = _this11.state.list.map(function (item) {
  532. if (item.id === commentId) {
  533. item.favored = !favored;
  534. item.favor_count += favored ? -1 : 1;
  535. }
  536. return item;
  537. });
  538. _this11.setState({ list: list });
  539. }).catch(this.errorHandler).finally(function () {
  540. _this11.handleChangeLoading("sCommentFavor", false);
  541. });
  542. }
  543. /**
  544. * 回复 点赞/取消点赞
  545. * @param {string} replyId replyId
  546. * @param {string} commentId commentId
  547. * @param {boolean} favored 是否已经点过赞
  548. */
  549. }, {
  550. key: "sReplyFavor",
  551. value: function sReplyFavor(replyId, commentId, favored) {
  552. var _this12 = this;
  553. this.handleChangeLoading("sReplyFavor", true);
  554. var API = this.props.API;
  555. this.axios(API + "/replies/" + replyId + "/favor", {
  556. method: favored ? "delete" : "put",
  557. data: {
  558. comment_id: commentId
  559. },
  560. withCredentials: true
  561. }).then(function (response) {
  562. _message3.default.success(favored ? _reactIntlUniversal2.default.get("message.cancelLickSuccess") : _reactIntlUniversal2.default.get("message.likeSuccess"));
  563. // 更新 list 中的该项数据的 favored
  564. var list = _this12.state.list.map(function (item) {
  565. if (item.id === commentId) {
  566. item.replies = item.replies.map(function (r) {
  567. if (r.id === replyId) {
  568. r.favored = !favored;
  569. // r.favor_count = response.data.favor_count;
  570. // 点赞数 +1,而不是使用后端返回的点赞数
  571. // 不然如果返回的不是增加 1,用户可能以为程序错误
  572. r.favor_count += favored ? -1 : 1;
  573. }
  574. return r;
  575. });
  576. }
  577. return item;
  578. });
  579. _this12.setState({ list: list });
  580. }).catch(this.errorHandler).finally(function () {
  581. _this12.handleChangeLoading("sReplyFavor", false);
  582. });
  583. }
  584. /**
  585. * 获取 OSS 上传的参数
  586. */
  587. }, {
  588. key: "sOssSts",
  589. value: function sOssSts() {
  590. var _this13 = this;
  591. this.handleChangeLoading("sOssSts", true);
  592. var API = this.props.API;
  593. this.axios.get(API + "/oss/sts").then(function (response) {
  594. _this13.setState({ oss: _extends({}, response.data) });
  595. }).catch(this.errorHandler).finally(function () {
  596. _this13.handleChangeLoading("sOssSts", false);
  597. });
  598. }
  599. }, {
  600. key: "render",
  601. value: function render() {
  602. // 添加到 Context 的数据
  603. var value = _extends({}, this.state, this.props, {
  604. sCreateComment: this.sCreateComment,
  605. sGetComment: this.sGetComment,
  606. sCommentFavor: this.sCommentFavor,
  607. sReplyFavor: this.sReplyFavor,
  608. sCreateReply: this.sCreateReply,
  609. sGetReply: this.sGetReply,
  610. sOssSts: this.sOssSts,
  611. sDeleteComment: this.sDeleteComment,
  612. sDeleteReply: this.sDeleteReply,
  613. sUpdateReply: this.sUpdateReply,
  614. sUpdateComment: this.sUpdateComment,
  615. handleEdit: this.handleEdit
  616. });
  617. return this.state.initDone && _react2.default.createElement(
  618. _Comment.CommentContext.Provider,
  619. { value: value },
  620. _react2.default.createElement(
  621. "div",
  622. { className: "comment" },
  623. this.props.showEditor && _react2.default.createElement(_CommentInput2.default, { content: this.props.children }),
  624. this.props.showList && _react2.default.createElement(
  625. "div",
  626. { style: { marginTop: 20 } },
  627. _react2.default.createElement(_CommentList2.default, null)
  628. )
  629. ),
  630. this.state.editModalVisible && _react2.default.createElement(_EditComment2.default, {
  631. visible: this.state.editModalVisible,
  632. action: this.state.action,
  633. replyId: this.state.replyId,
  634. replyPage: this.state.replyPage,
  635. commentId: this.state.commentId,
  636. userId: this.state.content.user_id,
  637. content: this.state.content,
  638. handleClose: this.handleClose
  639. })
  640. );
  641. }
  642. }]);
  643. return App;
  644. }(_react.Component);
  645. App.propTypes = {
  646. type: _propTypes2.default.number.isRequired, // 评论的 type
  647. businessId: _propTypes2.default.string.isRequired, // 评论的 business_id
  648. businessUserId: _propTypes2.default.number,
  649. API: _propTypes2.default.string, // 评论的 API 前缀
  650. showList: _propTypes2.default.bool, // 是否显示评论列表
  651. showEditor: _propTypes2.default.bool, // 是否显示评论输入框
  652. showAlertComment: _propTypes2.default.bool, // 评论成功之后,是否通过 Antd 的 Message 组件进行提示
  653. showAlertReply: _propTypes2.default.bool, // 回复成功之后,是否通过 Antd 的 Message 组件进行提示
  654. showAlertFavor: _propTypes2.default.bool, // 点赞/取消点赞成功之后,是否通过 Antd 的 Message 组件进行提示
  655. showError: _propTypes2.default.bool, // 是否使用Antd的Message组件提示错误信息
  656. onError: _propTypes2.default.func, // 错误回调, 出错了会被调用
  657. userId: _propTypes2.default.number, // 用户id, comment内部不维护用户id, 调用组件时传递过来, 目前用于判断是否显示删除按钮
  658. pageType: _propTypes2.default.string, // 分页类型
  659. page: _propTypes2.default.number, // 页码
  660. limit: _propTypes2.default.number, // 一次加载评论数量
  661. onPageChange: _propTypes2.default.func, // 页码变化回调
  662. onGetMoreBtnClick: _propTypes2.default.func, // 点击查看更多按钮回调
  663. onDelete: _propTypes2.default.func,
  664. onUpdateComment: _propTypes2.default.func,
  665. locales: _propTypes2.default.string, // 传入的语言环境, en-US/zh-CN
  666. onCountChange: _propTypes2.default.func // 评论数量变更时的回调
  667. };
  668. App.defaultProps = {
  669. businessUserId: 0,
  670. API: "//api.links123.net/comment/v1",
  671. showList: true,
  672. showEditor: true,
  673. showAlertComment: false,
  674. showAlertReply: false,
  675. showAlertFavor: false,
  676. showError: true,
  677. showEdit: false,
  678. pageType: "more",
  679. limit: _constant.LIMIT,
  680. onGetMoreBtnClick: function onGetMoreBtnClick() {},
  681. onPageChange: function onPageChange(page) {},
  682. onDelete: function onDelete() {},
  683. onUpdateComment: function onUpdateComment() {},
  684. onBeforeUpdateComment: function onBeforeUpdateComment() {},
  685. onCountChange: function onCountChange() {}
  686. };
  687. exports.Editor = _Editor2.default;
  688. exports.RenderText = _RenderText2.default;
  689. exports.default = App;
  690. //# sourceMappingURL=App.js.map