announcement.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <div class="announcement-container">
  3. <head-title subTitle="了解更多安检知识" title="最新公告"></head-title>
  4. <!-- 列表为空时显示的状态 -->
  5. <div v-if="list.length === 0" class="empty-state">
  6. <view class="empty"></view>
  7. <text class="empty-text">暂无公告</text>
  8. </div>
  9. <!-- 有数据时显示列表 -->
  10. <div v-else>
  11. <div class="list">
  12. <div v-for="(item, index) in list.slice(0, 2)" :key="item.id"
  13. :class="[item.status, index === 0 ? 'first-item' : 'second-item']" class="list-item"
  14. @click="navigateToDetail(item)">
  15. <div class="list-item-title">
  16. <text class="text-grey1">{{ item.noticeTitle }}</text>
  17. </div>
  18. <div class="list-item-content">{{ stripHtmlTags(item.noticeContent) }}</div>
  19. </div>
  20. </div>
  21. <div class="more-list" @click="navigateToMore">查看全部</div>
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. import HeadTitle from "@/components/HeadTitle.vue";
  27. import { getNoticeList } from "@/api/announcement/announcement.js";
  28. export default {
  29. components: {
  30. HeadTitle
  31. },
  32. data() {
  33. return {
  34. list: []
  35. };
  36. },
  37. mounted() {
  38. // 组件首次加载时获取数据
  39. // this.getAnnouncementList();
  40. },
  41. methods: {
  42. // 提供给父组件调用的刷新方法
  43. refreshData() {
  44. this.getAnnouncementList();
  45. },
  46. navigateToDetail(item) {
  47. console.log(item);
  48. uni.navigateTo({
  49. url: '/pages/announcement/announcementDetail?id=' + item.noticeId
  50. });
  51. },
  52. // 去除HTML标签,只保留纯文本
  53. stripHtmlTags(html) {
  54. if (!html) return '';
  55. // 使用正则表达式去除HTML标签
  56. return html.replace(/<[^>]+>/g, '');
  57. },
  58. navigateToMore() {
  59. uni.navigateTo({
  60. url: '/pages/announcement/index'
  61. });
  62. },
  63. async getAnnouncementList() {
  64. try {
  65. const res = await getNoticeList({
  66. pageNum: 1,
  67. pageSize: 2,
  68. noticeType: 2,
  69. status: '0'
  70. });
  71. // 处理API返回的数据,转换为组件需要的格式
  72. if (res && res.rows) {
  73. this.list = res.rows
  74. }
  75. } catch (error) {
  76. console.error('获取公告列表失败:', error);
  77. // 出错时使用默认数据
  78. this.list = [];
  79. }
  80. },
  81. formatTime(timeString) {
  82. // 简单的时间格式化函数,根据实际API返回格式调整
  83. if (!timeString) return '';
  84. const now = new Date();
  85. const past = new Date(timeString);
  86. const diffMinutes = Math.floor((now - past) / (1000 * 60));
  87. if (diffMinutes < 60) {
  88. return `${diffMinutes}分钟之前`;
  89. } else if (diffMinutes < 60 * 24) {
  90. return `${Math.floor(diffMinutes / 60)}小时之前`;
  91. } else {
  92. return `${Math.floor(diffMinutes / (60 * 24))}天之前`;
  93. }
  94. }
  95. }
  96. };
  97. </script>
  98. <style lang="scss" scoped>
  99. .more-list {
  100. padding: 30rpx 30rpx 0 30rpx;
  101. font-weight: 400;
  102. font-size: 30rpx;
  103. color: #666666;
  104. text-align: center;
  105. }
  106. .announcement-container {
  107. border-radius: 16rpx;
  108. padding: 20rpx 20rpx;
  109. box-shadow: 0 8rpx 20rpx 0 rgba(0, 0, 0, 0.08);
  110. }
  111. // 空状态样式
  112. .empty-state {
  113. display: flex;
  114. flex-direction: column;
  115. align-items: center;
  116. justify-content: center;
  117. padding: 80rpx 0;
  118. background: #FFFFFF;
  119. //box-shadow: 0 8rpx 20rpx 0 rgba(0, 0, 0, 0.08);
  120. margin-top: 28rpx;
  121. margin-bottom: 28rpx;
  122. border-radius: 16rpx;
  123. .empty {
  124. margin: 0 auto;
  125. width: 160px;
  126. height: 155px;
  127. background: url("../../../static/images/Empty.png") no-repeat;
  128. background-size: cover;
  129. }
  130. .empty-text {
  131. font-size: 28rpx;
  132. color: #999999;
  133. }
  134. }
  135. // 列表样式
  136. .list {
  137. .list-item {
  138. margin-top: 28rpx;
  139. padding: 20rpx 34rpx;
  140. font-weight: bold;
  141. font-size: 28rpx;
  142. color: #222222;
  143. background: #FFFFFF;
  144. line-height: 32rpx;
  145. //box-shadow: 0 8rpx 20rpx 0 rgba(0, 0, 0, 0.08);
  146. border-radius: 0 24rpx 24rpx 0;
  147. // border-left: 20rpx solid #F0F8FF;
  148. // border-top: 2rpx solid #F0F8FF;
  149. // border-right: 2rpx solid #F0F8FF;
  150. // border-bottom: 2rpx solid #F0F8FF;
  151. // 第一个容器的边框颜色
  152. &.first-item {
  153. border-left: 10rpx solid #FD7474;
  154. }
  155. // 第二个容器的边框颜色
  156. &.second-item {
  157. border-left: 10rpx solid #2A70D2;
  158. }
  159. .list-item-title {
  160. margin-bottom: 20rpx;
  161. .text-grey2 {
  162. font-weight: 400;
  163. font-size: 24rpx;
  164. color: #666666;
  165. line-height: 28rpx;
  166. }
  167. }
  168. .list-item-content {
  169. font-weight: 400;
  170. font-size: 24rpx;
  171. color: #999999;
  172. line-height: 28rpx;
  173. }
  174. &.error {
  175. border-left: 5rpx solid #FD7474;
  176. }
  177. &.active {
  178. border-left: 5rpx solid #2A70D1;
  179. }
  180. }
  181. }
  182. </style>