notice-board.vue 2.65 KB
<template>
  <workbench-card title="通知公告" subtitle="最新系统公告" icon="el-icon-message-solid" tone="purple" :loading="loading">
    <template #extra>
      <el-button type="text" size="mini" @click="goMore">更多</el-button>
    </template>
    <div v-if="list.length" class="wb-list">
      <div
        v-for="item in list"
        :key="item.id"
        class="wb-list-item"
        @click="openDetail(item)"
      >
        <div class="wb-list-item__left">
          <span class="wb-list-item__dot" style="background:#8b5cf6" />
          <span class="wb-list-item__title">{{ item.fullName || item.title || '无' }}</span>
        </div>
        <span class="wb-list-item__meta">{{ formatTime(item.creatorTime || item.lastModifyTime) }}</span>
      </div>
    </div>
    <div v-else class="wb-empty">
      <div class="wb-empty__circle" style="background:#f5f3ff;color:#8b5cf6">
        <i class="el-icon-bell" />
      </div>
      <div class="wb-empty__title">暂无公告</div>
      <div class="wb-empty__desc">有新通知时会显示在这里</div>
    </div>
    <notice-detail-dialog ref="detailDialog" />
  </workbench-card>
</template>

<script>
import { getNotice } from '@/api/home'
import WorkbenchCard from '../workbench-card.vue'
import NoticeDetailDialog from './notice-detail-dialog.vue'

export default {
  name: 'WidgetNoticeBoard',
  components: { WorkbenchCard, NoticeDetailDialog },
  data() {
    return { loading: false, list: [] }
  },
  mounted() {
    this.load()
  },
  methods: {
    formatTime(val) {
      if (!val) return '无'
      const d = new Date(typeof val === 'number' ? val : val)
      if (Number.isNaN(d.getTime())) return '无'
      const y = d.getFullYear()
      const m = `${d.getMonth() + 1}`.padStart(2, '0')
      const day = `${d.getDate()}`.padStart(2, '0')
      return `${y}-${m}-${day}`
    },
    async load() {
      this.loading = true
      try {
        const res = await getNotice()
        const rows = (res.data && res.data.list) || res.data || []
        // 兜底:若后端未过滤,前端也排除草稿(enabledMark=0)
        this.list = (Array.isArray(rows) ? rows : [])
          .filter(x => x.enabledMark == null || x.enabledMark === 1 || x.EnabledMark === 1)
          .slice(0, 5)
      } catch (e) {
        this.list = []
      } finally {
        this.loading = false
      }
    },
    openDetail(item) {
      const id = item && (item.id || item.Id)
      if (!id) return
      this.$refs.detailDialog && this.$refs.detailDialog.open(id)
    },
    goMore() {
      this.$router.push('/messageRecord').catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../widget-common.scss';
</style>