Нет описания

comment.vue 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. <script lang="ts" setup>
  2. import { onMounted, ref } from 'vue';
  3. import { Plus } from '@element-plus/icons-vue';
  4. import {
  5. ElButton,
  6. ElIcon,
  7. ElInput,
  8. ElMessage,
  9. ElRate,
  10. ElUpload,
  11. } from 'element-plus';
  12. // @ts-ignore
  13. import { addComment, querycomment } from '#/api/knowledge/comment';
  14. // @ts-ignore
  15. import AttachmentViewer from '#/components/AttachmentViewer.vue';
  16. // @ts-ignore
  17. import { getSingleImageUploadConfig } from '#/components/upload/config';
  18. // @ts-ignore
  19. const props = defineProps({
  20. taskId: {
  21. type: Number,
  22. default: 0,
  23. },
  24. taskDetail: {
  25. type: Object,
  26. default: () => {},
  27. },
  28. });
  29. // 模拟评论数据
  30. const comments: any = ref([]);
  31. // 评分
  32. const rating = ref(0);
  33. // 评价内容
  34. const commentContent = ref('');
  35. const uploadRef = ref(null) as any;
  36. // 评论图片
  37. const commentImages = ref<any[]>([]);
  38. // 图片预览相关
  39. // const previewVisible = ref(false);
  40. // const previewImages = ref<string[]>([]);
  41. // 上传图片成功
  42. const handleUploadSuccess = (file: any) => {
  43. if (file.code === 200) {
  44. commentImages.value.push({
  45. fileName: file.data.fileName,
  46. url: file.data.url,
  47. });
  48. }
  49. };
  50. // 移除上传的图片
  51. // const removeImage = (index: number) => {
  52. // commentImages.value = [];
  53. // uploadRef?.value?.clearFiles();
  54. // };
  55. // 提交评论
  56. const submitComment = async () => {
  57. if (commentContent.value.trim() === '' || commentImages.value.length === 0) {
  58. ElMessage.warning('请输入评论内容或上传图片');
  59. return;
  60. }
  61. try {
  62. await addComment({
  63. targetId: Number(props.taskId),
  64. targetType: 'task',
  65. attachments: commentImages.value.map((item) => item.fileName).join(','),
  66. content: commentContent.value,
  67. });
  68. commentContent.value = '';
  69. commentImages.value = [];
  70. querycommentfn();
  71. } catch {
  72. ElMessage.error('评论提交失败');
  73. }
  74. };
  75. const commentsloading = ref(true);
  76. const querycommentfn = async () => {
  77. commentsloading.value = true;
  78. try {
  79. const res = await querycomment({
  80. targetId: Number(props.taskId),
  81. targetType: 'task',
  82. });
  83. if (res.code === 200) {
  84. comments.value = res.data || [];
  85. }
  86. } catch {
  87. // console.log(error);
  88. } finally {
  89. commentsloading.value = false;
  90. }
  91. };
  92. // 当前选中的tab
  93. const activeTab = ref('comment');
  94. const replyOpen = ref({}) as any;
  95. const toggleReply = (index: any) => {
  96. replyOpen.value[index] = !replyOpen.value[index];
  97. };
  98. const childContent = ref('');
  99. const replayEvent = async (item: any, index: any) => {
  100. if (!childContent.value.trim()) {
  101. ElMessage.warning('请输入评论内容');
  102. return;
  103. }
  104. try {
  105. await addComment({
  106. targetId: Number(props.taskId),
  107. targetType: 'task',
  108. parentId: item.id,
  109. // attachments: commentImages.value.map((item) => item.fileName).join(','),
  110. content: childContent.value,
  111. });
  112. childContent.value = '';
  113. replyOpen.value[index] = !replyOpen.value[index];
  114. querycommentfn();
  115. } catch {
  116. ElMessage.error('评论提交失败');
  117. }
  118. };
  119. // 查阅统计数据
  120. const consultStats = ref({
  121. readCount: props.taskDetail.viewerName?.split(',').length || 0,
  122. unreadCount: props.taskDetail.unreadUserName?.split(',').length || 0,
  123. readers: props.taskDetail.viewerName?.split(',') || [],
  124. unreaders: props.taskDetail.unreadUserName?.split(',') || [],
  125. });
  126. // 处理tab切换
  127. const handleTabChange = (tab: string) => {
  128. activeTab.value = tab;
  129. };
  130. // 获取名字的最后两个字
  131. const getLastTwoChars = (name: string) => {
  132. return name?.slice(-2);
  133. };
  134. onMounted(() => {
  135. querycommentfn();
  136. });
  137. </script>
  138. <template>
  139. <div class="comment-container">
  140. <!-- 评价表单 -->
  141. <div class="comment-form">
  142. <div class="form-header">
  143. <div class="rating-section">
  144. <span class="rating-text">评价:</span>
  145. <ElRate
  146. v-model="rating"
  147. :texts="['1分', '2分', '3分', '4分', '5分']"
  148. show-text
  149. />
  150. </div>
  151. </div>
  152. <div class="form-content">
  153. <div class="input-section">
  154. <div class="rating-section">
  155. <span class="rating-text">评价:</span>
  156. <ElInput
  157. v-model="commentContent"
  158. type="textarea"
  159. :rows="3"
  160. placeholder="请对执行任务的情况进行点评,您的点评会帮助执行人更好的完善工作哦~"
  161. class="comment-input"
  162. />
  163. </div>
  164. <div class="rating-section">
  165. <span class="rating-text">照片:</span>
  166. <ElUpload
  167. ref="uploadRef"
  168. v-bind="getSingleImageUploadConfig()"
  169. :on-success="handleUploadSuccess"
  170. >
  171. <!-- <img
  172. v-if="commentImages.length > 0"
  173. :src="commentImages[0].url"
  174. class="avatar"
  175. /> -->
  176. <div class="flex h-full w-full items-center justify-center">
  177. <ElIcon class="el-upload__icon">
  178. <Plus size="18" />
  179. </ElIcon>
  180. </div>
  181. </ElUpload>
  182. </div>
  183. </div>
  184. </div>
  185. <ElButton style="margin-top: 8px" @click="submitComment" type="primary">
  186. 提交
  187. </ElButton>
  188. </div>
  189. <div>
  190. <div class="consult-container">
  191. <!-- Tabs切换 -->
  192. <ElTabs v-model="activeTab" @tab-change="handleTabChange" class="mb-4">
  193. <ElTabPane label="评论" name="comment" />
  194. <ElTabPane :label="`${consultStats.readCount}人已阅`" name="read" />
  195. <ElTabPane
  196. :label="`${consultStats.unreadCount}人未阅`"
  197. name="unread"
  198. />
  199. </ElTabs>
  200. <!-- 人员列表 -->
  201. <div v-if="activeTab === 'comment'" class="readers-list">
  202. <div
  203. v-for="(item, index) in comments"
  204. :key="index"
  205. class="comment_item"
  206. >
  207. <div class="reader-item mb-2 mr-2" style="flex-shrink: 0">
  208. {{ getLastTwoChars(item.username) }}
  209. </div>
  210. <div class="comment_content">
  211. <span style="font-size: 15px; font-weight: 900">{{
  212. item.username
  213. }}</span>
  214. <div>{{ item.content }}</div>
  215. <div>
  216. <AttachmentViewer :files="item.attachmentsUrl" />
  217. </div>
  218. <div
  219. style="
  220. display: flex;
  221. justify-content: space-between;
  222. align-items: center;
  223. "
  224. >
  225. <span style="color: #a4aab2">{{ item.createTime }}</span>
  226. <span class="reply_btn" @click="toggleReply(index)">回复</span>
  227. </div>
  228. <div
  229. v-if="item.children && item.children.length > 0"
  230. style="
  231. margin-left: 16px;
  232. margin-top: 8px;
  233. border-left: 2px solid #dcdfe6;
  234. padding-left: 8px;
  235. "
  236. >
  237. <div v-for="value in item.children" :key="value.id">
  238. <div>
  239. <span>{{ value?.username }}:</span>
  240. <span>{{ value?.content }}</span>
  241. </div>
  242. </div>
  243. </div>
  244. <div
  245. style="margin-left: 16px; margin-top: 8px"
  246. v-if="replyOpen[index]"
  247. >
  248. <ElInput
  249. v-model="childContent"
  250. maxlength="200"
  251. style="width: 240px"
  252. placeholder="请输入内容"
  253. show-word-limit
  254. type="textarea"
  255. />
  256. <div style="margin-top: 8px">
  257. <ElButton
  258. type="primary"
  259. size="small"
  260. @click="replayEvent(item, index)"
  261. >
  262. 提交
  263. </ElButton>
  264. <ElButton size="small" @click="toggleReply(index)">
  265. 收起
  266. </ElButton>
  267. </div>
  268. </div>
  269. </div>
  270. </div>
  271. </div>
  272. <div v-else-if="activeTab === 'read'" class="readers-list">
  273. <div
  274. v-for="(reader, index) in consultStats.readers"
  275. :key="index"
  276. class="reader-item mb-2 mr-2"
  277. >
  278. {{ getLastTwoChars(reader) }}
  279. </div>
  280. </div>
  281. <div v-else class="readers-list">
  282. <div
  283. v-for="(unreader, index) in consultStats.unreaders"
  284. :key="index"
  285. class="reader-item unread mb-2 mr-2"
  286. >
  287. {{ getLastTwoChars(unreader) }}
  288. </div>
  289. </div>
  290. </div>
  291. </div>
  292. </div>
  293. <!-- 图片预览组件 -->
  294. <!-- <ElImageViewer
  295. v-if="previewVisible"
  296. :url-list="previewImages"
  297. @close="closePreview"
  298. /> -->
  299. </template>
  300. <style scoped lang="scss">
  301. .comment-container {
  302. width: 100%;
  303. font-size: 14px;
  304. .comment-form {
  305. margin-bottom: 24px;
  306. padding: 16px 0;
  307. .form-header {
  308. margin-bottom: 12px;
  309. }
  310. .rating-section {
  311. display: flex;
  312. align-items: center;
  313. .rating-text {
  314. color: var(--text-color-secondary);
  315. font-size: 14px;
  316. margin-right: 8px;
  317. white-space: nowrap;
  318. }
  319. .rating-stars {
  320. margin-right: 0;
  321. }
  322. }
  323. .form-content {
  324. .input-section {
  325. width: 100%;
  326. .upload-section {
  327. margin-bottom: 12px;
  328. :deep(.el-upload--picture-card) {
  329. width: 60px;
  330. height: 60px;
  331. }
  332. :deep(.el-upload-list--picture-card .el-upload-list__item) {
  333. width: 60px;
  334. height: 60px;
  335. }
  336. }
  337. .comment-input {
  338. margin-bottom: 12px;
  339. :deep(.el-textarea__inner) {
  340. border-radius: 4px;
  341. resize: none;
  342. }
  343. }
  344. .form-footer {
  345. .footer-left {
  346. display: flex;
  347. align-items: center;
  348. .cc-select {
  349. width: 200px;
  350. margin-right: 12px;
  351. }
  352. .comment-btn {
  353. padding: 4px 12px;
  354. }
  355. }
  356. }
  357. }
  358. }
  359. }
  360. .comment-list {
  361. .comment-item {
  362. margin-bottom: 20px;
  363. padding: 0;
  364. background-color: transparent;
  365. border: none;
  366. font-size: 14px;
  367. .comment-header {
  368. margin-bottom: 8px;
  369. .username-section {
  370. display: flex;
  371. align-items: center;
  372. .username {
  373. font-weight: 500;
  374. color: var(--text-color-primary);
  375. margin-right: 8px;
  376. }
  377. .department {
  378. color: var(--text-color-secondary);
  379. font-size: 13px;
  380. margin-right: 16px;
  381. }
  382. .rating-section {
  383. display: flex;
  384. align-items: center;
  385. .rating-stars {
  386. margin-right: 8px;
  387. }
  388. .rating-level {
  389. color: #f7ba2a;
  390. font-size: 13px;
  391. }
  392. }
  393. }
  394. }
  395. .comment-content {
  396. color: var(--text-color-primary);
  397. line-height: 1.5;
  398. margin-bottom: 12px;
  399. padding-right: 20px;
  400. }
  401. .comment-images {
  402. display: flex;
  403. gap: 12px;
  404. margin-bottom: 12px;
  405. .comment-image {
  406. width: 60px;
  407. height: 60px;
  408. border-radius: 4px;
  409. object-fit: cover;
  410. cursor: pointer;
  411. transition: transform 0.2s;
  412. &:hover {
  413. transform: scale(1.05);
  414. }
  415. }
  416. }
  417. /* 评论时间样式 */
  418. .comment-time {
  419. font-size: 12px;
  420. color: #999999;
  421. margin-top: 8px;
  422. }
  423. /* 评论操作样式 */
  424. .comment-footer {
  425. display: flex;
  426. justify-content: flex-start;
  427. align-items: center;
  428. margin-top: 4px;
  429. .comment-actions {
  430. display: flex;
  431. align-items: center;
  432. .reply-icon {
  433. margin-right: 4px;
  434. color: #409eff;
  435. font-size: 12px;
  436. }
  437. .reply-btn {
  438. color: #409eff;
  439. font-size: 12px;
  440. cursor: pointer;
  441. &:hover {
  442. text-decoration: underline;
  443. }
  444. }
  445. }
  446. }
  447. /* 回复表单样式 */
  448. .reply-form {
  449. margin-top: 12px;
  450. padding: 12px;
  451. background-color: var(--bg-color-light);
  452. border-radius: 4px;
  453. border: 1px solid var(--border-color);
  454. .reply-input {
  455. margin-bottom: 12px;
  456. :deep(.el-textarea__inner) {
  457. border-radius: 4px;
  458. resize: none;
  459. }
  460. }
  461. .reply-footer {
  462. display: flex;
  463. justify-content: flex-end;
  464. gap: 8px;
  465. :deep(.el-button) {
  466. padding: 4px 12px;
  467. }
  468. }
  469. }
  470. /* 回复列表样式 */
  471. .reply-list {
  472. margin-top: 12px;
  473. margin-left: 40px;
  474. .reply-item {
  475. margin-bottom: 12px;
  476. padding: 8px 12px;
  477. background-color: var(--bg-color-light);
  478. border-radius: 4px;
  479. border-left: 3px solid #409eff;
  480. .reply-header {
  481. margin-bottom: 4px;
  482. .reply-username {
  483. font-weight: 500;
  484. color: var(--text-color-primary);
  485. font-size: 13px;
  486. margin-right: 4px;
  487. }
  488. .reply-department {
  489. color: var(--text-color-secondary);
  490. font-size: 13px;
  491. }
  492. }
  493. .reply-content {
  494. color: var(--text-color-primary);
  495. font-size: 13px;
  496. line-height: 1.4;
  497. margin-bottom: 4px;
  498. }
  499. .reply-time {
  500. font-size: 12px;
  501. color: #999999;
  502. text-align: left;
  503. }
  504. }
  505. }
  506. }
  507. }
  508. }
  509. .comment_item {
  510. display: flex;
  511. // align-items: center;
  512. // justify-content: space-between;
  513. width: 100%;
  514. // padding: 8px 16px;
  515. border-radius: 4px;
  516. // background-color: #f5f7fa;
  517. margin-bottom: 8px;
  518. font-size: 12px;
  519. color: #606266;
  520. .comment_content {
  521. margin-top: 6px;
  522. display: flex;
  523. flex-direction: column;
  524. gap: 8px;
  525. .reply_btn {
  526. color: #409eff;
  527. cursor: pointer;
  528. &:hover {
  529. text-decoration: underline;
  530. }
  531. }
  532. }
  533. }
  534. .consult-container {
  535. width: 100%;
  536. padding: 16px 0;
  537. font-size: 14px;
  538. .readers-list {
  539. display: flex;
  540. flex-wrap: wrap;
  541. }
  542. .reader-item {
  543. display: flex;
  544. align-items: center;
  545. justify-content: center;
  546. width: 32px;
  547. height: 32px;
  548. border-radius: 50%;
  549. background-color: #409eff;
  550. color: white;
  551. font-size: 12px;
  552. font-weight: 500;
  553. cursor: pointer;
  554. transition: all 0.3s ease;
  555. &:hover {
  556. background-color: #66b1ff;
  557. transform: scale(1.1);
  558. }
  559. &.unread {
  560. background-color: #dcdfe6;
  561. color: #606266;
  562. &:hover {
  563. background-color: #e4e7ed;
  564. }
  565. }
  566. }
  567. }
  568. </style>