cc-comment.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. <template>
  2. <view>
  3. <view class="c_total">评论 {{ tableTotal }}</view>
  4. <template v-if="dataList && dataList.length">
  5. <view class="c_comment" v-for="(item1, index1) in dataList" :key="item1.id">
  6. <!-- 一级评论 -->
  7. <CommonComp :data="item1" @likeClick="() => likeClick({ item1, index1 })"
  8. @replyClick="() => replyClick({ item1, index1 })"
  9. @deleteClick="() => deleteClick({ item1, index1 })" />
  10. <view class="children_item" v-if="item1.children && item1.children.length">
  11. <!-- 二级评论 -->
  12. <CommonComp v-for="(item2, index2) in item1.childrenShow" :key="item2.id" :data="item2"
  13. :pData="item1" @likeClick="() => likeClick({ item1, index1, item2, index2 })"
  14. @replyClick="() => replyClick({ item1, index1, item2, index2 })"
  15. @deleteClick="() => deleteClick({ item1, index1, item2, index2 })" />
  16. <!-- 展开二级评论 -->
  17. <view class="expand_reply" v-if="expandTxtShow({ item1, index1 })"
  18. @tap="() => expandReplyFun({ item1, index1 })">
  19. <span class="txt"> 展开{{ item1.children.length - item1.childrenShow.length }}条回复 </span>
  20. <uni-icons type="down" size="24" color="#007aff"></uni-icons>
  21. </view>
  22. <!-- 折叠二级评论 -->
  23. <view class="shrink_reply" v-if="shrinkTxtShow({ item1, index1 })"
  24. @tap="() => shrinkReplyFun({ item1, index1 })">
  25. <span class="txt"> 收起回复内容 </span>
  26. <uni-icons type="up" size="24" color="#007aff"></uni-icons>
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. <!-- 空盒子 -->
  32. <view class="empty_box" v-else>
  33. <!-- <uni-icons type="chatboxes" size="36" color="#c0c0c0"></uni-icons> -->
  34. <image src="@/static/icon/quexing_01.png" style="width: 380rpx; height: 308rpx;"></image>
  35. <view>
  36. <span class="txt"> 来鼓励一下作者吧~ </span>
  37. <span class="txt click" @click="() => newCommentFun()">说点什么...</span>
  38. </view>
  39. </view>
  40. <!-- 评论弹窗 -->
  41. <uni-popup ref="cPopupRef" type="bottom" @change="popChange">
  42. <view class="c_popup_box">
  43. <view class="reply_text">
  44. <template v-if="Object.keys(replyTemp).length">
  45. <span class="text_aid">回复给</span>
  46. <img class="user_avatar"
  47. :src="replyTemp.item2 ? replyTemp.item2.user_avatar : replyTemp.item1.user_avatar" />
  48. <span class="text_main">{{ replyTemp.item2 ? replyTemp.item2.user_name :
  49. replyTemp.item1.user_name }}</span>
  50. </template>
  51. <span v-else class="text_main">发表新评论</span>
  52. </view>
  53. <view class="content">
  54. <view class="text_area">
  55. <textarea class="textarea" v-model="commentValue" :placeholder="commentPlaceholder"
  56. :focus="focus" maxlength="300" auto-height @focus="onTextareaFocus"
  57. @keydown="handleKeydown"></textarea>
  58. <view class="emoji-trigger" @tap="toggleEmojiPanel">
  59. <text class="fa fa-smile-o"></text>
  60. </view>
  61. </view>
  62. <view class="send_btn" @tap="() => sendClick()">发送</view>
  63. </view>
  64. <!-- 表情面板 -->
  65. <view class="emoji-panel" v-if="showEmojiPanel">
  66. <view class="emoji-grid">
  67. <view class="emoji-item" v-for="(emoji, index) in emojiList" :key="index"
  68. @tap="selectEmoji(emoji)">
  69. {{ emoji }}
  70. </view>
  71. </view>
  72. </view>
  73. </view>
  74. </uni-popup>
  75. <!-- 删除弹窗 -->
  76. <!-- <uni-popup ref="delPopupRef" type="dialog">
  77. <uni-popup-dialog mode="base" title="" content="确定删除这条评论吗?" :before-close="true" @close="delCloseFun"
  78. @confirm="delConfirmFun"></uni-popup-dialog>
  79. </uni-popup> -->
  80. <DialogBox ref="DialogBox"></DialogBox>
  81. </view>
  82. </template>
  83. <script>
  84. import CommonComp from "./componets/common";
  85. export default {
  86. components: {
  87. CommonComp
  88. },
  89. props: {
  90. /** 登陆用户信息
  91. * id: number // 登陆用户id
  92. * user_name: number // 登陆用户名
  93. * user_avatar: string // 登陆用户头像地址
  94. */
  95. myInfo: {
  96. type: Object,
  97. default: () => { },
  98. },
  99. /** 文章作者信息
  100. * id: number // 文章作者id
  101. * user_name: number // 文章作者名
  102. * user_avatar: string // 文章作者头像地址
  103. */
  104. userInfo: {
  105. type: Object,
  106. default: () => { },
  107. },
  108. /** 评论列表
  109. * id: number // 评论id
  110. * parent_id: number // 父级评论id
  111. * reply_id: number // 被回复人评论id
  112. * reply_name: string // 被回复人名称
  113. * user_name: string // 用户名
  114. * user_avatar: string // 评论者头像地址
  115. * user_content: string // 评论内容
  116. * is_like: boolean // 是否点赞
  117. * like_count: number // 点赞数统计
  118. * create_time: string // 创建时间
  119. */
  120. tableData: {
  121. type: Array,
  122. default: () => [],
  123. },
  124. // 评论总数
  125. tableTotal: {
  126. type: Number,
  127. default: 0,
  128. },
  129. // 评论删除模式
  130. // bind - 当被删除的一级评论存在回复评论, 那么该评论内容变更显示为[当前评论内容已被移除]
  131. // only - 仅删除当前评论(后端删除相关联的回复评论, 否则总数显示不对)
  132. // all - 删除所有评论包括回复评论
  133. deleteMode: {
  134. type: String,
  135. default: "all",
  136. },
  137. },
  138. data() {
  139. return {
  140. dataList: [], // 渲染数据(前端的格式)
  141. replyTemp: {}, // 回复临时数据
  142. isNewComment: false, // 是否为新评论
  143. focus: false, // 评论弹窗
  144. commentValue: "", // 输入框值
  145. commentPlaceholder: "说点什么...", // 输入框占位符
  146. delTemp: {}, // 删除临时数据
  147. showEmojiPanel: false, // 是否显示表情面板
  148. // emojiList: [
  149. // '😊', '😂', '🤣', '❤️', '😍', '🥰', '😘', '😭',
  150. // '😅', '😉', '🤔', '😤', '😡', '🥺', '😴', '😷',
  151. // '👍', '👎', '👏', '🙌', '🤝', '🙏', '💪', '🎉',
  152. // '✨', '💫', '⭐', '🌟', '💥', '💯', '♥️', '💕'
  153. // ], // 表情列表
  154. emojiList: ['😀', '😃', '😄', '😁', '😆', '😅', '😂', '🤣', '🥲', '😊', '😇', '🙂', '🙃', '😉', '😌', '😍', '🥰', '😘', '😗', '😙', '😚', '😋', '😛', '😝', '😜', '🤪', '🤨', '🧐', '🤓', '😎', '🥸', '🤩', '🥳', '😏', '😒', '😞', '😔', '😟', '😕', '🙁', '☹️', '😣', '😖', '😫', '😩', '🥺', '😢', '😭', '😤', '😠', '😡', '🤬', '🤯', '😳', '🥵', '🥶', '😱', '😨', '😰', '😥', '😓', '🤗', '🤔', '🤭', '🤫', '🤥', '😶', '😐', '😑', '😬', '🙄', '😯', '😦', '😧', '😮', '😲', '🥱', '😴', '🤤', '😪', '😵', '🤐', '🥴', '🤢', '🤮', '🤧', '😷', '🤒', '🤕', '🤑', '🤠'
  155. ]
  156. };
  157. },
  158. watch: {
  159. tableData: {
  160. handler(newVal) {
  161. if (newVal.length !== this.dataList.length) {
  162. this.dataList = this.treeTransForm(newVal);
  163. }
  164. },
  165. deep: true,
  166. immediate: true,
  167. },
  168. },
  169. mounted() { },
  170. methods: {
  171. // 数据转换
  172. treeTransForm(data) {
  173. let newData = JSON.parse(JSON.stringify(data));
  174. let result = [];
  175. let map = {};
  176. newData.forEach((item, i) => {
  177. item.owner = item.user_id === this.myInfo.user_id; // 是否为当前登陆用户
  178. item.author = item.user_id === this.userInfo.user_id; // 是否为作者
  179. map[item.id] = item;
  180. });
  181. newData.forEach((item) => {
  182. let parent = map[item.parent_id];
  183. if (parent) {
  184. (parent.children || (parent.children = [])).push(item); // 所有回复
  185. if (parent.children.length === 1) {
  186. (parent.childrenShow = []).push(item); // 显示的回复
  187. }
  188. } else {
  189. result.push(item);
  190. }
  191. });
  192. return result;
  193. },
  194. // 点赞
  195. setLike(item) {
  196. item.is_like = !item.is_like;
  197. item.like_count = item.is_like ? item.like_count + 1 : item.like_count - 1;
  198. },
  199. likeClick({
  200. item1,
  201. index1,
  202. item2,
  203. index2
  204. }) {
  205. let item = item2 || item1;
  206. let that = this;
  207. this.setLike(item);
  208. this.$emit("likeFun", {
  209. params: item
  210. }, (res) => {
  211. // 请求后端失败, 重置点赞
  212. that.setLike(item);
  213. });
  214. },
  215. // 回复
  216. replyClick({
  217. item1,
  218. index1,
  219. item2,
  220. index2
  221. }) {
  222. this.replyTemp = JSON.parse(JSON.stringify({
  223. item1,
  224. index1,
  225. item2,
  226. index2
  227. }));
  228. this.$refs["cPopupRef"].open();
  229. },
  230. // 发起新评论
  231. newCommentFun() {
  232. this.isNewComment = true;
  233. this.$refs["cPopupRef"].open();
  234. },
  235. // 评论弹窗
  236. popChange(e) {
  237. // 关闭弹窗
  238. if (!e.show) {
  239. this.commentValue = ""; // 清空输入框值
  240. this.replyTemp = {}; // 清空被回复人信息
  241. this.isNewComment = false; // 恢复是否为新评论默认值
  242. this.showEmojiPanel = false; // 隐藏表情面板
  243. }
  244. this.focus = e.show;
  245. },
  246. // 切换表情面板显示状态
  247. toggleEmojiPanel() {
  248. this.showEmojiPanel = !this.showEmojiPanel;
  249. },
  250. // 选择表情
  251. selectEmoji(emoji) {
  252. this.commentValue += emoji;
  253. },
  254. // 输入框获取焦点时
  255. onTextareaFocus() {
  256. // 可以选择不关闭表情面板,让用户同时使用键盘和表情
  257. // this.showEmojiPanel = false;
  258. },
  259. // 发送评论
  260. sendClick({
  261. item1,
  262. index1,
  263. item2,
  264. index2
  265. } = this.replyTemp) {
  266. let item = item2 || item1;
  267. let params = {};
  268. // 新评论
  269. if (this.isNewComment) {
  270. params = {
  271. id: Math.random(), // 评论id
  272. parent_id: null, // 父级评论id
  273. reply_id: null, // 被回复评论id
  274. reply_name: null, // 被回复人名称
  275. };
  276. } else {
  277. // 回复评论
  278. params = {
  279. id: Math.random(), // 评论id
  280. parent_id: item && item.parent_id ? item.parent_id : item.id, // 父级评论id
  281. reply_id: item.id, // 被回复评论id
  282. reply_name: item.user_name, // 被回复人名称
  283. };
  284. }
  285. params = {
  286. ...params,
  287. user_id: this.myInfo.user_id, // 用户id
  288. user_name: this.myInfo.user_name, // 用户名
  289. user_avatar: this.myInfo.user_avatar, // 用户头像地址
  290. user_content: this.commentValue, // 用户评论内容
  291. is_like: false, // 是否点赞
  292. like_count: 0, // 点赞数统计
  293. create_time: "刚刚", // 创建时间
  294. owner: true, // 是否为所有者 所有者可以进行删除 管理员默认true
  295. };
  296. uni.showLoading({
  297. title: "正在发送",
  298. mask: true,
  299. });
  300. this.$emit("replyFun", {
  301. params
  302. }, (res) => {
  303. uni.hideLoading();
  304. // 拿到后端返回的id赋值, 因为删除要用到id
  305. params = {
  306. ...params,
  307. id: res.id
  308. };
  309. // 新评论
  310. if (this.isNewComment) {
  311. this.dataList.push(params);
  312. } else {
  313. // 回复
  314. let c_data = this.dataList[index1];
  315. (c_data.children || (c_data.children = [])).push(params);
  316. // 如果已展开所有回复, 那么此时插入children长度会大于childrenShow长度1, 所以就直接展开显示即可
  317. if (c_data.children.length === (c_data.childrenShow || (c_data.childrenShow = [])).length +
  318. 1) {
  319. c_data.childrenShow.push(params);
  320. }
  321. }
  322. this.$emit("update:tableTotal", this.tableTotal + 1);
  323. this.$refs["cPopupRef"].close();
  324. });
  325. },
  326. //删除
  327. deleteClick({
  328. item1,
  329. index1,
  330. item2,
  331. index2
  332. }) {
  333. this.delTemp = JSON.parse(JSON.stringify({
  334. item1,
  335. index1,
  336. item2,
  337. index2
  338. }));
  339. this.$refs['DialogBox'].confirm({
  340. title: '提示',
  341. content: '确定要删除该评论及其回复吗?',
  342. DialogType: 'inquiry',
  343. btn1: '取消',
  344. btn2: '确定',
  345. animation: 0
  346. }).then(() => {
  347. this.delConfirmFun()
  348. }).catch(() => {
  349. this.delCloseFun()
  350. })
  351. },
  352. // 关闭删除弹窗
  353. delCloseFun() {
  354. this.delTemp = {};
  355. // this.$refs["delPopupRef"].close();
  356. },
  357. // 确定删除
  358. delConfirmFun({
  359. item1,
  360. index1,
  361. item2,
  362. index2
  363. } = this.delTemp) {
  364. const deleteMode = this.deleteMode;
  365. let c_data = this.dataList[index1];
  366. uni.showLoading({
  367. title: "正在删除",
  368. mask: true,
  369. });
  370. // 删除二级评论
  371. if (index2 >= 0) {
  372. this.$emit("deleteFun", {
  373. params: [c_data.children[index2].id],
  374. mode: deleteMode
  375. }, (res) => {
  376. uni.hideLoading();
  377. this.$emit("update:tableTotal", this.tableTotal - 1);
  378. c_data.children.splice(index2, 1);
  379. c_data.childrenShow.splice(index2, 1);
  380. });
  381. } else {
  382. // 删除一级评论
  383. if (c_data?.children?.length) {
  384. // 如果一级评论包含回复评论
  385. switch (deleteMode) {
  386. case "bind":
  387. // 一级评论内容展示修改为: 当前评论内容已被移除
  388. this.$emit(
  389. "deleteFun", {
  390. params: [c_data.id],
  391. mode: deleteMode,
  392. },
  393. (res) => {
  394. uni.hideLoading();
  395. c_data.user_content = "当前评论内容已被移除";
  396. }
  397. );
  398. break;
  399. case "only":
  400. // 后端自行根据删除的一级评论id, 查找关联的子评论进行删除
  401. this.$emit(
  402. "deleteFun", {
  403. params: [c_data.id],
  404. mode: deleteMode,
  405. },
  406. (res) => {
  407. uni.hideLoading();
  408. this.$emit("update:tableTotal", this.tableTotal - c_data.children.length + 1);
  409. this.dataList.splice(index1, 1);
  410. }
  411. );
  412. break;
  413. default:
  414. // all
  415. // 收集子评论id, 提交给后端统一删除
  416. let delIdArr = [c_data.id];
  417. c_data.children.forEach((_, i) => {
  418. delIdArr.push(_.id);
  419. });
  420. this.$emit("deleteFun", {
  421. params: delIdArr,
  422. mode: deleteMode
  423. }, (res) => {
  424. uni.hideLoading();
  425. this.$emit("update:tableTotal", this.tableTotal - c_data.children.length + 1);
  426. this.dataList.splice(index1, 1);
  427. });
  428. break;
  429. }
  430. } else {
  431. // 一级评论无回复, 直接删除
  432. this.$emit("deleteFun", {
  433. params: [c_data.id],
  434. mode: deleteMode
  435. }, (res) => {
  436. uni.hideLoading();
  437. this.$emit("update:tableTotal", this.tableTotal - 1);
  438. this.dataList.splice(index1, 1);
  439. });
  440. }
  441. }
  442. this.delCloseFun();
  443. },
  444. // 展开评论if
  445. expandTxtShow({
  446. item1,
  447. index1
  448. }) {
  449. return item1.childrenShow?.length && item1.children.length - item1.childrenShow.length;
  450. },
  451. // 展开更多评论
  452. expandReplyFun({
  453. item1,
  454. index1
  455. }) {
  456. let csLen = this.dataList[index1].childrenShow.length;
  457. this.dataList[index1].childrenShow.push(
  458. ...this.dataList[index1].children.slice(csLen, csLen + 6) // 截取5条评论
  459. );
  460. },
  461. // 收起评论if
  462. shrinkTxtShow({
  463. item1,
  464. index1
  465. }) {
  466. return item1.childrenShow?.length >= 2 && item1.children.length - item1.childrenShow.length === 0;
  467. },
  468. // 收起更多评论
  469. shrinkReplyFun({
  470. item1,
  471. index1
  472. }) {
  473. this.dataList[index1].childrenShow = [];
  474. this.dataList[index1].childrenShow.push(
  475. ...this.dataList[index1].children.slice(0, 1) // 截取1条评论
  476. );
  477. },
  478. handleKeydown(event) { // 新增方法
  479. console.log(event.key, event.shiftKey);
  480. if (event.key === 'Enter' && !event.shiftKey) {
  481. event.preventDefault();
  482. this.sendClick();
  483. }
  484. },
  485. },
  486. };
  487. </script>
  488. <style lang="scss" scoped>
  489. @font-face {
  490. font-family: 'FontAwesome';
  491. src: url('https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/fonts/fontawesome-webfont.woff2') format('woff2');
  492. font-weight: normal;
  493. font-style: normal;
  494. }
  495. .fa {
  496. display: inline-block;
  497. font: normal normal normal 14px/1 FontAwesome;
  498. font-size: inherit;
  499. text-rendering: auto;
  500. -webkit-font-smoothing: antialiased;
  501. }
  502. .fa-smile-o:before {
  503. content: "\f118";
  504. }
  505. ////////////////////////
  506. .center {
  507. display: flex;
  508. align-items: center;
  509. }
  510. ////////////////////////
  511. .c_total {
  512. padding: 20rpx 30rpx 0 30rpx;
  513. font-size: 28rpx;
  514. }
  515. .empty_box {
  516. display: flex;
  517. justify-content: center;
  518. align-items: center;
  519. flex-direction: column;
  520. padding: 150rpx 10rpx;
  521. font-size: 28rpx;
  522. .txt {
  523. color: $uni-text-color-disable;
  524. }
  525. .click {
  526. color: $uni-color-primary;
  527. }
  528. }
  529. .c_comment {
  530. padding: 20rpx 30rpx;
  531. font-size: 28rpx;
  532. .children_item {
  533. padding: 10rpx 0rpx 10rpx 0rpx;
  534. margin-top: 10rpx;
  535. margin-left: 80rpx;
  536. background-color: $uni-bg-color-grey;
  537. .expand_reply,
  538. .shrink_reply {
  539. margin-top: 10rpx;
  540. margin-left: 80rpx;
  541. .txt {
  542. font-weight: 600;
  543. color: $uni-color-primary;
  544. font-size: 26rpx !important;
  545. }
  546. .uni-icons{
  547. font-size: 30rpx !important;
  548. }
  549. }
  550. }
  551. }
  552. .c_popup_box {
  553. background-color: #fff;
  554. margin-bottom: 0rpx;
  555. .reply_text {
  556. @extend .center;
  557. padding: 20rpx 20rpx 0 20rpx;
  558. font-size: 26rpx;
  559. .text_aid {
  560. color: $uni-text-color-grey;
  561. margin-right: 5rpx;
  562. }
  563. .user_avatar {
  564. width: 48rpx;
  565. height: 48rpx;
  566. border-radius: 50%;
  567. margin-right: 6rpx;
  568. margin-left: 12rpx;
  569. }
  570. }
  571. .content {
  572. @extend .center;
  573. .text_area {
  574. flex: 1;
  575. padding: 20rpx;
  576. position: relative;
  577. .textarea {
  578. width: 100%;
  579. min-height: 80rpx;
  580. font-size: 28rpx;
  581. color: #333;
  582. background: #f8f8f8;
  583. border: 2rpx solid #eee;
  584. border-radius: 8rpx;
  585. padding: 16rpx 50rpx 16rpx 16rpx;
  586. }
  587. .emoji-trigger {
  588. position: absolute;
  589. right: 30rpx;
  590. bottom: 30rpx;
  591. width: 60rpx;
  592. height: 60rpx;
  593. display: flex;
  594. align-items: center;
  595. justify-content: center;
  596. color: #666;
  597. font-size: 40rpx;
  598. &:active {
  599. opacity: 0.7;
  600. }
  601. }
  602. }
  603. .send_btn {
  604. @extend .center;
  605. justify-content: center;
  606. width: 120rpx;
  607. height: 60rpx;
  608. border-radius: 20rpx;
  609. font-size: 28rpx;
  610. color: #fff;
  611. background-color: $uni-color-primary;
  612. margin-right: 20rpx;
  613. margin-left: 5rpx;
  614. }
  615. }
  616. .emoji-panel {
  617. padding: 20rpx;
  618. background-color: #f8f8f8;
  619. border-top: 2rpx solid #eee;
  620. max-height: 40vh;
  621. overflow-y: scroll;
  622. .emoji-grid {
  623. display: flex;
  624. flex-wrap: wrap;
  625. // overflow-y: scroll;
  626. .emoji-item {
  627. width: 12.5%;
  628. height: 80rpx;
  629. display: flex;
  630. align-items: center;
  631. justify-content: center;
  632. font-size: 40rpx;
  633. &:active {
  634. background-color: rgba(0, 0, 0, 0.1);
  635. border-radius: 8rpx;
  636. }
  637. }
  638. }
  639. }
  640. }
  641. </style>