Home.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <script setup lang="ts">
  2. import { ref, onMounted, computed, onUnmounted } from 'vue'
  3. import { ElMessage } from 'element-plus'
  4. import { useRouter } from 'vue-router'
  5. import { GlobalStore } from '@/stores/index'
  6. import type { TabsPaneContext } from 'element-plus'
  7. import { User_News_See } from '@/api/user/index'
  8. interface NoticeType {
  9. CreateTime: string
  10. Id: number
  11. T_Tag: number
  12. T_Title: string
  13. T_Url: string
  14. T_uuid: string
  15. }
  16. let tabs__content: HTMLDivElement
  17. const router = useRouter()
  18. const globalStore = GlobalStore()
  19. const activeName = ref('first')
  20. const newsList = computed<NoticeType[]>(() => {
  21. return globalStore.noticeList
  22. })
  23. console.log(newsList.value)
  24. const handleClick = (tab: TabsPaneContext) => {
  25. const { props } = tab
  26. globalStore.SET_NoticeList([], true)
  27. globalStore.SET_NoticePage(1)
  28. switch (props.name) {
  29. case 'first':
  30. globalStore.SET_User_News_List()
  31. break
  32. case 'second':
  33. globalStore.SET_User_News_List(1)
  34. break
  35. }
  36. }
  37. /**
  38. * 查看通知消息
  39. */
  40. const previewNotice = async (item: NoticeType) => {
  41. router.push(item.T_Url)
  42. const res: any = await User_News_See({ User_tokey: globalStore.GET_User_tokey, Id: item.Id })
  43. if (res.Code) {
  44. ElMessage({
  45. type: 'success',
  46. message: '确认查看消息!'
  47. })
  48. newsList.value.filter((notice: NoticeType) => item.Id !== notice.Id)
  49. }
  50. }
  51. const ScrollHandle = (e: any) => {
  52. const { target } = e
  53. if (
  54. target.scrollHeight - target.scrollTop === target.clientHeight &&
  55. globalStore.noticeTotal !== newsList.value.length
  56. ) {
  57. console.log('/home')
  58. globalStore.SET_NoticePage()
  59. globalStore.SET_User_News_List()
  60. }
  61. }
  62. onMounted(() => {
  63. tabs__content = document.querySelector('#home')?.getElementsByClassName('el-tabs__content')[0] as HTMLDivElement
  64. tabs__content?.addEventListener('scroll', ScrollHandle)
  65. if (globalStore.noticeList.length <= 0) {
  66. globalStore.SET_User_News_List()
  67. }
  68. })
  69. onUnmounted(() => {
  70. console.log('组件销毁')
  71. tabs__content.removeEventListener('scroll', ScrollHandle)
  72. })
  73. </script>
  74. <template>
  75. <div class="home">
  76. <div class="notice">
  77. <h1>消息通知</h1>
  78. <el-tabs v-model="activeName" @tab-click="handleClick" id="home">
  79. <transition-group
  80. appear
  81. leave-active-class="animate__animated animate__fadeInLeft"
  82. enter-active-class="animate__animated animate__fadeInLeft"
  83. >
  84. <el-tab-pane label="未读" name="first" key="first">
  85. <div class="item" v-for="item in newsList" :key="item.T_uuid" @click="previewNotice(item)">
  86. <div class="item-box">
  87. <el-badge :value="item.T_Tag === 0 ? 'new' : ''">{{ item.T_Title }}</el-badge>
  88. </div>
  89. <span>{{ item.CreateTime.split(' ')[0] }}</span>
  90. </div>
  91. </el-tab-pane>
  92. <el-tab-pane label="全部" name="second" key="second">
  93. <div class="item" v-for="item in newsList" :key="item.T_uuid" @click="previewNotice(item)">
  94. <div class="item-box">
  95. <el-badge :value="item.T_Tag === 0 ? 'new' : ''">{{ item.T_Title }}</el-badge>
  96. </div>
  97. <span>{{ item.CreateTime.split(' ')[0] }}</span>
  98. </div>
  99. </el-tab-pane>
  100. </transition-group>
  101. </el-tabs>
  102. </div>
  103. </div>
  104. </template>
  105. <style scoped lang="scss">
  106. .home {
  107. width: 100%;
  108. height: 100%;
  109. padding-top: 50px;
  110. display: flex;
  111. justify-content: center;
  112. // align-items: center;
  113. .notice {
  114. width: 50%;
  115. height: 50%;
  116. padding: 15px;
  117. display: flex;
  118. flex-direction: column;
  119. align-items: center;
  120. box-shadow: 0px 0px 6px #ccc;
  121. .el-tabs {
  122. width: 100%;
  123. height: calc(100% - 25px);
  124. :deep(.el-tabs__content) {
  125. padding-top: 10px;
  126. height: calc(100% - 80px);
  127. overflow-y: scroll;
  128. }
  129. }
  130. .item {
  131. width: 100%;
  132. display: flex;
  133. padding: 5px 16px;
  134. cursor: pointer;
  135. justify-content: space-between;
  136. &:hover {
  137. background-color: #ecf5ff;
  138. color: #409eff;
  139. }
  140. .item-box {
  141. width: 80%;
  142. .el-badge {
  143. max-width: 100%;
  144. .item-content {
  145. overflow: hidden;
  146. white-space: nowrap;
  147. text-overflow: ellipsis;
  148. max-width: 100%;
  149. display: inline-block;
  150. }
  151. }
  152. }
  153. span {
  154. white-space: nowrap;
  155. }
  156. }
  157. }
  158. h1 {
  159. font-size: 2rem;
  160. margin-bottom: 20px;
  161. }
  162. }
  163. </style>