platePostItem.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <script setup lang="ts">
  2. import service from '../../plugins/axios'
  3. import { defineProps, ref, watch } from 'vue'
  4. import { API_URL } from '../../config'
  5. import { log } from 'console'
  6. const dp = defineProps({
  7. id: {
  8. type: String,
  9. required: true,
  10. },
  11. title: {
  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. numberOfReplies: {
  28. type: String,
  29. required: true,
  30. },
  31. views: {
  32. type: String,
  33. required: true,
  34. },
  35. istop: {
  36. type: Boolean,
  37. required: true,
  38. default: false,
  39. },
  40. })
  41. //格式化时间几天前
  42. function formatDate(date: any) {
  43. console.log(date);
  44. const d = new Date(date)
  45. const now = Date.now()
  46. const diff = (now - d.getTime()) / 1000
  47. if (diff < 30) {
  48. return '刚刚'
  49. } else if (diff < 3600) {
  50. // less 1 hour
  51. return Math.ceil(diff / 60) + '分钟前'
  52. } else if (diff < 3600 * 24) {
  53. return Math.ceil(diff / 3600) + '小时前'
  54. } else if (diff < 3600 * 24 * 2) {
  55. return '1天前'
  56. }
  57. return Math.ceil(diff / (3600 * 24)) + '天前'
  58. }
  59. const userinfo = ref(
  60. await (
  61. await service.get(`userinfo/getuser?id=${dp.authorid}`)
  62. ).data
  63. )
  64. //监听数据变化
  65. watch(
  66. dp,
  67. async (newVal) => {
  68. userinfo.value = await (
  69. await service.get(`userinfo/getuser?id=${dp.authorid}`)
  70. ).data
  71. userinfo.value.data.user.avatar = API_URL + userinfo.value.data.user.avatar
  72. },
  73. { deep: true }
  74. )
  75. const to_post = () => {
  76. window.location.href = `/post/${dp.id}`
  77. }
  78. if (userinfo.value.data.user.avatar) {
  79. userinfo.value.data.user.avatar = API_URL + userinfo.value.data.user.avatar
  80. } else {
  81. userinfo.value.data.user.avatar =
  82. 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
  83. }
  84. </script>
  85. <template>
  86. <el-card>
  87. <el-row>
  88. <el-col :span="4">
  89. <div class="c">
  90. <div style="margin-bottom: 6px;">
  91. <el-avatar :src="userinfo.data.user.avatar"
  92. :size="60" />
  93. </div>
  94. <el-tag type="primary">{{ userinfo.data.user.nickname?userinfo.data.user.nickname:userinfo.data.username }}</el-tag>
  95. </div>
  96. </el-col>
  97. <el-col :span="20">
  98. <span class="title"
  99. v-html="dp.title" />
  100. <el-tag type="danger"
  101. v-show="istop">置顶</el-tag>
  102. <p class="content">{{dp.content}}</p>
  103. <div type="text"
  104. style="float: left">
  105. <el-space wrap>
  106. <!-- {{ dp.time }} -->
  107. <!-- <el-tag type="info">{{ formatDate(dp.time) }}</el-tag> -->
  108. <el-tag type="success">回复{{ dp.numberOfReplies?numberOfReplies:0 }}</el-tag>
  109. <el-tag type="warning">浏览量{{ dp.views }}</el-tag>
  110. </el-space>
  111. </div>
  112. <el-button type="text"
  113. style="float: right"
  114. @click="to_post">
  115. 去看看
  116. </el-button>
  117. </el-col>
  118. </el-row>
  119. </el-card>
  120. </template>
  121. <style scoped>
  122. .c {
  123. display: flex;
  124. flex-direction: column;
  125. justify-content: center;
  126. align-items: center;
  127. height: 100%;
  128. }
  129. .title {
  130. font-size: 14px;
  131. color: #909399;
  132. margin-bottom: 6px;
  133. margin-right: 10px;
  134. }
  135. .content {
  136. font-size: 14px;
  137. color: #606266;
  138. }
  139. .title .content {
  140. overflow: hidden;
  141. text-overflow: ellipsis;
  142. white-space: nowrap;
  143. }
  144. </style>