postarticle.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <script setup lang="ts">
  2. import service from '../../plugins/axios'
  3. import { onMounted, ref } from 'vue'
  4. import { useRoute } from 'vue-router'
  5. import { token } from '../../plugins/pinia'
  6. import { ElMessage } from 'element-plus'
  7. const data = defineProps({
  8. postid: {
  9. type: String,
  10. required: true,
  11. },
  12. plateId: {
  13. type: String,
  14. required: true,
  15. },
  16. auth_id: {
  17. type: String,
  18. required: true,
  19. },
  20. title: {
  21. type: String,
  22. required: true,
  23. },
  24. content: {
  25. type: String,
  26. required: true,
  27. },
  28. updatedAt: {
  29. type: String,
  30. required: true,
  31. },
  32. Istop: {
  33. type: Boolean,
  34. required: true,
  35. },
  36. })
  37. const edit = () => {
  38. window.location.href = `/posting?plate=${data.plateId}&postid=${data.postid}`
  39. }
  40. const del = async () => {
  41. if (token().token) {
  42. await service({
  43. url: `/post?postid=${data.postid}`,
  44. method: 'delete',
  45. headers: {
  46. Authorization: `Bearer ${token().token}`,
  47. },
  48. })
  49. ElMessage({
  50. message: '删除成功',
  51. type: 'success',
  52. })
  53. window.location.href = `/home`
  54. }
  55. }
  56. const jurisdiction = ref()
  57. async function IsMypost() {
  58. if (token().token === '') {
  59. return false
  60. }
  61. const res = await (
  62. await service({
  63. url: `/auth/islogin`,
  64. method: 'get',
  65. headers: {
  66. Authorization: `Bearer ${token().token}`,
  67. },
  68. })
  69. ).data
  70. if (token().token) {
  71. jurisdiction.value = await (
  72. await service({
  73. url: `/auth/Permissions`,
  74. method: 'get',
  75. headers: {
  76. Authorization: `Bearer ${token().token}`,
  77. },
  78. })
  79. ).data
  80. }
  81. if (
  82. data.auth_id === res ||
  83. jurisdiction.value.msg === '管理员' ||
  84. jurisdiction.value.msg === '超级管理员'
  85. ) {
  86. return true
  87. }
  88. return false
  89. }
  90. const Istop = ref(data.Istop)
  91. const toppost = async () => {
  92. if (!token().token) {
  93. ElMessage({
  94. message: '请先登陆',
  95. type: 'success',
  96. })
  97. return
  98. }
  99. const res = (
  100. await service({
  101. url: `post/top?id=${data.postid}`,
  102. method: 'put',
  103. headers: {
  104. Authorization: `Bearer ${token().token}`,
  105. },
  106. })
  107. ).data
  108. if (res.cod === 200) {
  109. ElMessage({
  110. message: res.message,
  111. type: 'success',
  112. })
  113. Istop.value = res.data
  114. }
  115. }
  116. onMounted(() => {
  117. const md = document.querySelector('.vuepress-markdown-body') as HTMLElement
  118. // 每秒检测一次
  119. const html = document.querySelector('html') as HTMLElement
  120. //每秒检测一次
  121. const timer = setInterval(() => {
  122. if (md) {
  123. if (html.className === 'dark') {
  124. md.style.background = '#1d1e1f'
  125. md.style.color = '#fff'
  126. } else {
  127. md.style.background = '#fff'
  128. md.style.color = '#000'
  129. }
  130. }
  131. }, 100)
  132. })
  133. const isMypost = ref(await IsMypost())
  134. function formatDate(date: any) {
  135. const d = new Date(date)
  136. const now = Date.now()
  137. const diff = (now - d.getTime()) / 1000
  138. if (diff < 30) {
  139. return '刚刚'
  140. } else if (diff < 3600) {
  141. // less 1 hour
  142. return Math.ceil(diff / 60) + '分钟前'
  143. } else if (diff < 3600 * 24) {
  144. return Math.ceil(diff / 3600) + '小时前'
  145. } else if (diff < 3600 * 24 * 2) {
  146. return '1天前'
  147. }
  148. return Math.ceil(diff / (3600 * 24)) + '天前'
  149. }
  150. const isadmin = ref()
  151. if (jurisdiction.value) {
  152. isadmin.value = ref(
  153. jurisdiction.value.msg === '管理员' ||
  154. jurisdiction.value.msg === '超级管理员'
  155. )
  156. }
  157. </script>
  158. <template>
  159. <el-card style="margin: 10px 0px;"
  160. class="post-card">
  161. <template #header>
  162. <div class="card-header">
  163. <span>{{ data.title }} <el-tag type="danger"
  164. v-show="Istop">置顶</el-tag></span>
  165. <div v-show="isMypost">
  166. <el-button type="text"
  167. v-show="isadmin"
  168. @click="toppost">{{ Istop?`取消置顶`:`置顶帖子` }}</el-button>
  169. <el-divider v-show="isadmin"
  170. direction="vertical" />
  171. <el-button type="text"
  172. @click="edit">编辑</el-button>
  173. <el-divider direction="vertical" />
  174. <el-popconfirm title="确定要删除这篇帖子吗?"
  175. @confirm="del">
  176. <template #reference>
  177. <el-button type="text">删除</el-button>
  178. </template>
  179. </el-popconfirm>
  180. </div>
  181. </div>
  182. </template>
  183. <el-alert :title="`本帖最后更新与${formatDate(data.updatedAt)},请注意时效性`"
  184. type="info"
  185. :closable="false" />
  186. <br />
  187. <el-alert :title="`本帖所有观点均与作者有关,并不代表论坛观点`"
  188. type="info" />
  189. <v-md-editor :model-value="data.content"
  190. mode="preview"
  191. class="md">
  192. </v-md-editor>
  193. </el-card>
  194. </template>
  195. <style scoped>
  196. .card-header {
  197. display: flex;
  198. justify-content: space-between;
  199. }
  200. </style>