通用评论 vedio

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { message } from "antd";
  4. import axios from "axios";
  5. import intl from "react-intl-universal";
  6. import { ERROR_DEFAULT, LIMIT } from "./constant";
  7. import { CommentContext } from "./Comment";
  8. import { isFunction } from "./helper";
  9. import CommentInput from "./components/CommentInput";
  10. import CommentList from "./components/CommentList";
  11. import Editor from "./components/Editor";
  12. import RenderText from "./components/RenderText";
  13. // import lang from "./lang";
  14. import USdata from "./lang/en-US.js";
  15. import CNdata from "./lang/zh-CN.js";
  16. import "./App.css";
  17. // import styles from "./App.module.css";
  18. /**
  19. * 当前支持的语言
  20. */
  21. const SUPPORT_LOCALES = [
  22. {
  23. name: "English",
  24. value: "en-US"
  25. },
  26. {
  27. name: "简体中文",
  28. value: "zh-CN"
  29. }
  30. ];
  31. const LOCALES = {
  32. "zh-CN": CNdata,
  33. "en-US": USdata
  34. };
  35. const LOCALES_RESPONSE = {
  36. "zh-CN": {
  37. "not found": "没有数据",
  38. "auth failed": "请先登录",
  39. "create comment failed": "创建评论失败",
  40. "comment favor failed": "评论点赞失败",
  41. "delete comment favor failed": "评论取消点赞失败",
  42. "get comments failed": "获取评论列表失败",
  43. "create reply failed": "创建回复失败",
  44. "reply favor failed": "回复点赞失败",
  45. "delete reply favor failed": "删除回复点赞失败",
  46. "get replies failed": "获取回复列表失败"
  47. },
  48. "en-US": {
  49. "not found": "no data",
  50. "auth failed": "please log in first",
  51. "create comment failed": "Failed to create comment",
  52. "comment favor failed": "Comment likes failure",
  53. "delete comment favor failed": "评论取消点赞失败",
  54. "get comments failed": "Comment cancels praise failure",
  55. "create reply failed": "Create reply failed",
  56. "reply favor failed": "Reply to praise failed",
  57. "delete reply favor failed": "Delete reply clicks failed",
  58. "get replies failed": "Failed to get reply list"
  59. }
  60. };
  61. class App extends Component {
  62. constructor(props) {
  63. super(props);
  64. this.state = {
  65. loading: {},
  66. // oss 配置
  67. oss: {},
  68. // 评论数据
  69. list: [],
  70. page: 1,
  71. total: 0,
  72. // 是否没有更多评论了
  73. isNoMoreComment: false,
  74. initDone: false,
  75. locale: "zh-CN"
  76. };
  77. this.handleChangeLoading = this.handleChangeLoading.bind(this);
  78. this.sCreateComment = this.sCreateComment.bind(this);
  79. this.sDeleteComment = this.sDeleteComment.bind(this);
  80. this.sCommentFavor = this.sCommentFavor.bind(this);
  81. this.sCreateReply = this.sCreateReply.bind(this);
  82. this.sDeleteReply = this.sDeleteReply.bind(this);
  83. this.errorHandler = this.errorHandler.bind(this);
  84. this.sGetComment = this.sGetComment.bind(this);
  85. this.sReplyFavor = this.sReplyFavor.bind(this);
  86. this.sGetReply = this.sGetReply.bind(this);
  87. this.sOssSts = this.sOssSts.bind(this);
  88. }
  89. componentWillMount() {
  90. this.axios = axios;
  91. this.axios.defaults.withCredentials = true;
  92. if (this.props.token) {
  93. this.axios.defaults.headers.common["Authorization"] = `Bearer ${
  94. this.props.token
  95. }`;
  96. }
  97. }
  98. componentDidMount() {
  99. this.loadLocales();
  100. }
  101. /**
  102. * 加载语言包
  103. * 只能根据url或者传入的props来确定加载哪个语言包
  104. * 优先级:传入的props > url
  105. */
  106. loadLocales() {
  107. let { currentLocale } = this.props;
  108. if (!currentLocale) {
  109. currentLocale = intl.determineLocale({
  110. urlLocaleKey: "lang"
  111. });
  112. }
  113. console.log("currentLocale", currentLocale);
  114. currentLocale = SUPPORT_LOCALES.find(item => item.value === currentLocale)
  115. ? currentLocale
  116. : "zh-CN";
  117. console.log("locales is", LOCALES[currentLocale]);
  118. intl
  119. .init({
  120. currentLocale,
  121. locales: {
  122. [currentLocale]: LOCALES[currentLocale]
  123. }
  124. })
  125. .then(() => {
  126. this.setState({ initDone: true, locale: currentLocale });
  127. });
  128. }
  129. error(msg, info = {}) {
  130. if (this.props.showError) {
  131. message.error(msg);
  132. }
  133. if (this.props.onError) {
  134. this.props.onError(msg, info);
  135. }
  136. }
  137. errorHandler(error) {
  138. const { locale } = this.state;
  139. const localResponse = LOCALES_RESPONSE[locale];
  140. if (error.response && error.response.data && error.response.data.msg) {
  141. this.error(localResponse[error.response.data.msg] || ERROR_DEFAULT, {
  142. response: error.response
  143. });
  144. return;
  145. }
  146. this.error(localResponse[error.response.data.msg] || ERROR_DEFAULT, {
  147. response: error.response
  148. });
  149. }
  150. /**
  151. * 改变 loading 状态
  152. * @param {string} key key
  153. * @param {string} value value
  154. */
  155. handleChangeLoading(key, value) {
  156. const { loading } = this.state;
  157. loading[key] = value;
  158. this.setState({ loading });
  159. }
  160. /**
  161. * 获取评论列表
  162. */
  163. sGetComment({ page = 1 } = {}) {
  164. const { pageType } = this.props;
  165. this.handleChangeLoading("sGetComment", true);
  166. const { API, type, businessId } = this.props;
  167. this.axios
  168. .get(
  169. `${API}/comments?type=${type}&business_id=${businessId}&page=${page}&limit=${LIMIT}`
  170. )
  171. .then(response => {
  172. const { list, page, total } = response.data;
  173. if (list) {
  174. let newList = list;
  175. let { list: oldList } = this.state;
  176. if (pageType === "more") {
  177. if (page > 1) {
  178. // 删除临时数据
  179. oldList = oldList.filter(o => !o.isTemporary);
  180. newList = oldList.concat(newList);
  181. }
  182. } else if (pageType === "pagination") {
  183. // TODO 滚动到顶部
  184. window.scrollTo(0, 0);
  185. }
  186. this.setState({
  187. list: newList,
  188. page,
  189. total
  190. });
  191. } else {
  192. message.info(intl.get("message.noMoreComment"));
  193. this.setState({
  194. isNoMoreComment: true
  195. });
  196. }
  197. })
  198. .catch(this.errorHandler)
  199. .finally(() => {
  200. this.handleChangeLoading("sGetComment", false);
  201. });
  202. }
  203. /**
  204. * 获取更多回复
  205. */
  206. sGetReply({ commentId, page = 1 } = {}) {
  207. this.handleChangeLoading("sGetReply", true);
  208. const { API } = this.props;
  209. this.axios
  210. .get(`${API}/replies?comment_id=${commentId}&page=${page}&limit=${LIMIT}`)
  211. .then(response => {
  212. if (!response.data.list) {
  213. message.info(intl.get("message.noMoreData"));
  214. }
  215. const list = this.state.list.map(item => {
  216. if (item.id === commentId) {
  217. if (!item.replies) item.replies = [];
  218. if (response.data.list) {
  219. if (page === 1) {
  220. // 如果当前页数为第一页,则清空当前所有的 replies
  221. // 并将获取到的 replies 存放在 state
  222. item.replies = response.data.list;
  223. } else {
  224. item.replies = item.replies
  225. .filter(o => !o.isTemporary)
  226. .concat(response.data.list);
  227. // 如果当前页数非第一页,则合并 replies
  228. }
  229. item.reply_count = response.data.total;
  230. item.reply_page = response.data.page;
  231. } else {
  232. item.isNoMoreReply = true;
  233. }
  234. }
  235. return item;
  236. });
  237. this.setState({ list });
  238. })
  239. .catch(this.errorHandler)
  240. .finally(() => {
  241. this.handleChangeLoading("sGetReply", false);
  242. });
  243. }
  244. /**
  245. * 添加评论
  246. * @param {object} {content} comment content
  247. */
  248. sCreateComment({ content } = {}, cb) {
  249. if (!content) return this.error(intl.get("message.notNull"));
  250. this.handleChangeLoading("sCreateComment", true);
  251. const { API, type, businessId } = this.props;
  252. this.axios(`${API}/comments`, {
  253. method: "post",
  254. data: {
  255. type,
  256. business_id: businessId,
  257. content
  258. },
  259. withCredentials: true
  260. })
  261. .then(response => {
  262. if (this.props.showAlertComment) {
  263. message.success(intl.get("message.success"));
  264. }
  265. if (isFunction(cb)) cb();
  266. // 将数据写入到 list 中
  267. // 临时插入
  268. // 等到获取数据之后,删除临时数据
  269. const { list, total } = this.state;
  270. list.unshift({
  271. ...response.data,
  272. isTemporary: true // 临时的数据
  273. });
  274. this.setState({ list, total: total + 1 });
  275. })
  276. .catch(this.errorHandler)
  277. .finally(() => {
  278. this.handleChangeLoading("sCreateComment", false);
  279. });
  280. }
  281. /**
  282. * 删除评论
  283. */
  284. sDeleteComment(commentId) {
  285. this.handleChangeLoading("sDeleteComment", true);
  286. const { API } = this.props;
  287. this.axios(`${API}/comments/${commentId}`, {
  288. method: "delete",
  289. withCredentials: true
  290. })
  291. .then(() => {
  292. const { list, total } = this.state;
  293. const res = list.filter(item => item.id !== commentId);
  294. this.setState({ list: res, total: total - 1 });
  295. })
  296. .catch(this.errorHandler)
  297. .finally(() => {
  298. this.handleChangeLoading("sDeleteComment", false);
  299. });
  300. }
  301. /**
  302. * 添加回复
  303. * 回复评论/回复回复
  304. * @param {object} data { comment_id, content, [reply_id] }
  305. */
  306. sCreateReply(data, cb) {
  307. if (!data.content) return this.error(intl.get("message.replyNoNull"));
  308. this.handleChangeLoading("sCreateReply", true);
  309. const { API } = this.props;
  310. this.axios(`${API}/replies`, {
  311. method: "post",
  312. data,
  313. withCredentials: true
  314. })
  315. .then(response => {
  316. if (this.props.showAlertReply) {
  317. message.success(intl.get("message.replySuccess"));
  318. }
  319. if (isFunction(cb)) cb();
  320. // 将数据写入到 list 中
  321. // 临时插入
  322. // 等到获取数据之后,删除临时数据
  323. const list = this.state.list.map(item => {
  324. if (item.id === data.comment_id) {
  325. if (!item.replies) item.replies = [];
  326. item.replies.push({
  327. ...response.data,
  328. isTemporary: true // 临时的数据
  329. });
  330. item.reply_count += 1;
  331. }
  332. return item;
  333. });
  334. this.setState({ list });
  335. })
  336. .catch(this.errorHandler)
  337. .finally(() => {
  338. this.handleChangeLoading("sCreateReply", false);
  339. });
  340. }
  341. /**
  342. * 删除回复
  343. * @param {*} replyId
  344. * @param {*} commentId
  345. */
  346. sDeleteReply(replyId, commentId) {
  347. this.handleChangeLoading("sDeleteReply", true);
  348. const { API } = this.props;
  349. this.axios(`${API}/replies/${replyId}?CommentID=${commentId}`, {
  350. method: "delete",
  351. withCredentials: true
  352. })
  353. .then(() => {
  354. const list = this.state.list.map(item => {
  355. if (item.id === commentId) {
  356. const replies = item.replies.filter(item => item.id !== replyId);
  357. item.replies = replies;
  358. item.reply_count -= 1;
  359. }
  360. return item;
  361. });
  362. this.setState({ list });
  363. })
  364. .catch(this.errorHandler)
  365. .finally(() => {
  366. this.handleChangeLoading("sDeleteReply", false);
  367. });
  368. }
  369. /**
  370. * 评论 点赞/取消点赞
  371. * @param {string} commentId { commentId }
  372. * @param {boolean} favored 是否已经点过赞
  373. */
  374. sCommentFavor(commentId, favored) {
  375. this.handleChangeLoading("sCommentFavor", true);
  376. const { API } = this.props;
  377. this.axios(`${API}/comments/${commentId}/favor`, {
  378. method: favored ? "delete" : "put",
  379. withCredentials: true
  380. })
  381. .then(response => {
  382. if (this.props.showAlertFavor) {
  383. message.success(
  384. favored
  385. ? intl.get("message.cancelLickSuccess")
  386. : intl.get("message.likeSuccess")
  387. );
  388. }
  389. // 更新 list 中的该项数据的 favored
  390. const list = this.state.list.map(item => {
  391. if (item.id === commentId) {
  392. item.favored = !favored;
  393. item.favor_count += favored ? -1 : 1;
  394. }
  395. return item;
  396. });
  397. this.setState({ list });
  398. })
  399. .catch(this.errorHandler)
  400. .finally(() => {
  401. this.handleChangeLoading("sCommentFavor", false);
  402. });
  403. }
  404. /**
  405. * 回复 点赞/取消点赞
  406. * @param {string} replyId replyId
  407. * @param {string} commentId commentId
  408. * @param {boolean} favored 是否已经点过赞
  409. */
  410. sReplyFavor(replyId, commentId, favored) {
  411. this.handleChangeLoading("sReplyFavor", true);
  412. const { API } = this.props;
  413. this.axios(`${API}/replies/${replyId}/favor`, {
  414. method: favored ? "delete" : "put",
  415. data: {
  416. comment_id: commentId
  417. },
  418. withCredentials: true
  419. })
  420. .then(response => {
  421. message.success(
  422. favored
  423. ? intl.get("message.cancelLickSuccess")
  424. : intl.get("message.likeSuccess")
  425. );
  426. // 更新 list 中的该项数据的 favored
  427. const list = this.state.list.map(item => {
  428. if (item.id === commentId) {
  429. item.replies = item.replies.map(r => {
  430. if (r.id === replyId) {
  431. r.favored = !favored;
  432. // r.favor_count = response.data.favor_count;
  433. // 点赞数 +1,而不是使用后端返回的点赞数
  434. // 不然如果返回的不是增加 1,用户可能以为程序错误
  435. r.favor_count += favored ? -1 : 1;
  436. }
  437. return r;
  438. });
  439. }
  440. return item;
  441. });
  442. this.setState({ list });
  443. })
  444. .catch(this.errorHandler)
  445. .finally(() => {
  446. this.handleChangeLoading("sReplyFavor", false);
  447. });
  448. }
  449. /**
  450. * 获取 OSS 上传的参数
  451. */
  452. sOssSts() {
  453. this.handleChangeLoading("sOssSts", true);
  454. const { API } = this.props;
  455. this.axios
  456. .get(`${API}/oss/sts`)
  457. .then(response => {
  458. this.setState({ oss: { ...response.data } });
  459. })
  460. .catch(this.errorHandler)
  461. .finally(() => {
  462. this.handleChangeLoading("sOssSts", false);
  463. });
  464. }
  465. render() {
  466. // 添加到 Context 的数据
  467. const value = {
  468. ...this.state,
  469. ...this.props,
  470. sCreateComment: this.sCreateComment,
  471. sGetComment: this.sGetComment,
  472. sCommentFavor: this.sCommentFavor,
  473. sReplyFavor: this.sReplyFavor,
  474. sCreateReply: this.sCreateReply,
  475. sGetReply: this.sGetReply,
  476. sOssSts: this.sOssSts,
  477. sDeleteComment: this.sDeleteComment,
  478. sDeleteReply: this.sDeleteReply
  479. };
  480. return (
  481. this.state.initDone && (
  482. <CommentContext.Provider value={value}>
  483. <div className="comment">
  484. {this.props.showEditor && (
  485. <CommentInput content={this.props.children} />
  486. )}
  487. {this.props.showList && (
  488. <div style={{ marginTop: 20 }}>
  489. <CommentList />
  490. </div>
  491. )}
  492. </div>
  493. </CommentContext.Provider>
  494. )
  495. );
  496. }
  497. }
  498. App.propTypes = {
  499. type: PropTypes.number.isRequired, // 评论的 type
  500. businessId: PropTypes.string.isRequired, // 评论的 business_id
  501. API: PropTypes.string, // 评论的 API 前缀
  502. showList: PropTypes.bool, // 是否显示评论列表
  503. showEditor: PropTypes.bool, // 是否显示评论输入框
  504. showAlertComment: PropTypes.bool, // 评论成功之后,是否通过 Antd 的 Message 组件进行提示
  505. showAlertReply: PropTypes.bool, // 回复成功之后,是否通过 Antd 的 Message 组件进行提示
  506. showAlertFavor: PropTypes.bool, // 点赞/取消点赞成功之后,是否通过 Antd 的 Message 组件进行提示
  507. showError: PropTypes.bool, // 是否使用Antd的Message组件提示错误信息
  508. onError: PropTypes.func, // 错误回调, 出错了会被调用
  509. userId: PropTypes.number, // 用户id, comment内部不维护用户id, 调用组件时传递过来, 目前用于判断是否显示删除按钮
  510. pageType: PropTypes.string, // 分页类型
  511. page: PropTypes.number, // 页码
  512. onPageChange: PropTypes.func, // 页码变化回调
  513. currentLocale: PropTypes.string // 传入的语言包
  514. };
  515. App.defaultProps = {
  516. API: "//api.links123.net/comment/v1",
  517. showList: true,
  518. showEditor: true,
  519. showAlertComment: false,
  520. showAlertReply: false,
  521. showAlertFavor: false,
  522. showError: true,
  523. pageType: "more",
  524. onPageChange: page => {}
  525. };
  526. export { Editor, RenderText };
  527. export default App;