commentitem.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <script setup lang="ts">
  2. import { defineProps, ref } from 'vue'
  3. import service from '../../plugins/axios'
  4. import { API_URL } from '../../config'
  5. import { token } from '../../plugins/pinia'
  6. const data = defineProps({
  7. commentid: {
  8. type: String,
  9. required: true,
  10. },
  11. postid: {
  12. type: String,
  13. required: true,
  14. },
  15. content: {
  16. type: String,
  17. required: true,
  18. },
  19. authorid: {
  20. type: String,
  21. required: true,
  22. },
  23. time: {
  24. type: String,
  25. required: true,
  26. },
  27. })
  28. const edit = () => {
  29. window.location.href = `/reply?postid=${data.postid}&commentid=${data.commentid}`
  30. }
  31. const del = async () => {
  32. const res = (
  33. await service({
  34. url: '/comment',
  35. method: 'delete',
  36. headers: {
  37. Authorization: `Bearer ${token().token}`,
  38. },
  39. data: {
  40. Commentid: data.commentid,
  41. },
  42. })
  43. ).data
  44. if (res.cod === 200) {
  45. window.location.href = `/post/${data.postid}`
  46. }
  47. }
  48. async function IsMycomment() {
  49. if (token().token === '') {
  50. return false
  51. }
  52. const res = await (
  53. await service({
  54. url: `/auth/islogin`,
  55. method: 'get',
  56. headers: {
  57. Authorization: `Bearer ${token().token}`,
  58. },
  59. })
  60. ).data
  61. const jurisdiction = await (
  62. await service({
  63. url: `/auth/Permissions`,
  64. method: 'get',
  65. headers: {
  66. Authorization: `Bearer ${token().token}`,
  67. },
  68. })
  69. ).data
  70. if (
  71. data.authorid === res ||
  72. jurisdiction.msg === '管理员' ||
  73. jurisdiction.msg === '超级管理员'
  74. ) {
  75. return true
  76. }
  77. return false
  78. }
  79. const isMyComment = ref(await IsMycomment())
  80. //格式化时间几天前
  81. function formatDate(date: any) {
  82. const d = new Date(date)
  83. const now = Date.now()
  84. const diff = (now - d.getTime()) / 1000
  85. if (diff < 30) {
  86. return '刚刚'
  87. } else if (diff < 3600) {
  88. // less 1 hour
  89. return Math.ceil(diff / 60) + '分钟前'
  90. } else if (diff < 3600 * 24) {
  91. return Math.ceil(diff / 3600) + '小时前'
  92. } else if (diff < 3600 * 24 * 2) {
  93. return '1天前'
  94. }
  95. return Math.ceil(diff / (3600 * 24)) + '天前'
  96. }
  97. const userdata = await (
  98. await service.get(`userinfo/getuser?id=${data.authorid}`)
  99. ).data.data
  100. </script>
  101. <template>
  102. <el-card style="margin: 10px 0;">
  103. <template #header>
  104. <div class="card-header">
  105. <div v-show="isMyComment">
  106. <el-button type="text"
  107. @click="edit">
  108. 编辑
  109. </el-button>
  110. <el-popconfirm @confirm="del()"
  111. title="确定要删除这条评论?">
  112. <template #reference>
  113. <el-button type="text">
  114. 删除
  115. </el-button>
  116. </template>
  117. </el-popconfirm>
  118. </div>
  119. <el-tag type="success">
  120. 发表时间:{{ formatDate(data.time)}}
  121. </el-tag>
  122. </div>
  123. </template>
  124. <el-row>
  125. <el-col :xs="24"
  126. :sm="24"
  127. :md="6"
  128. :lg="6"
  129. :xl="6">
  130. <div class="c">
  131. <el-avatar style="margin-right: 10px;"
  132. :size="100"
  133. :src="API_URL+userdata.user.avatar">
  134. </el-avatar>
  135. </div>
  136. <div class="c">
  137. <el-button type="text"
  138. style="margin-right: 10px;">
  139. {{ userdata.user.nickname?userdata.user.nickname:userdata.username }}
  140. </el-button>
  141. </div>
  142. </el-col>
  143. <el-col :xs="24"
  144. :sm="24"
  145. :md="18"
  146. :lg="18"
  147. :xl="18">
  148. <v-md-editor :model-value="data.content"
  149. mode="preview"
  150. class="md">
  151. </v-md-editor>
  152. </el-col>
  153. </el-row>
  154. </el-card>
  155. </template>
  156. <style scoped>
  157. .card-header {
  158. display: flex;
  159. justify-content: space-between;
  160. align-items: center;
  161. }
  162. /* 居中 */
  163. .c {
  164. display: flex;
  165. justify-content: center;
  166. align-items: center;
  167. }
  168. </style>