通用评论

App.js 11KB

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