feat(notice): 新增公告优先级和未读状态功能,优化公告展示逻辑

This commit is contained in:
2025-12-30 22:49:03 +08:00
parent 76c324b0df
commit 88a4e58130
21 changed files with 1520 additions and 13 deletions

0
login-notice-popup.js Normal file
View File

0
notice-popup-utils.js Normal file
View File

0
notice-popup.js Normal file
View File

0
notice-popup.vue Normal file
View File

View File

@@ -61,22 +61,44 @@ public class SysNoticeController extends BaseController {
/**
* 获取当前用户的通知列表(公开接口)
* 通知类型:通常 noticeType = '1' 代表通知
* 通知类型:通常 noticeType = '1' 代表通知noticeType = '2' 代表公告
* 返回已发布且状态正常的所有公告和通知,并标注已读状态
* 按优先级排序,高优先级在前
*/
@GetMapping("/public/notice")
public AjaxResult getUserNotices() {
// 获取当前用户信息
LoginUser loginUser = getLoginUser();
SysUser currentUser = loginUser.getUser();
// 查询状态正常0且已发布1的通知
// 查询已发布且状态正常的所有公告和通知
SysNotice notice = new SysNotice();
notice.setStatus("0");
notice.setPublishStatus("1");
// 通知类型设置为 '1'(通知)
notice.setNoticeType("1");
List<SysNotice> list = noticeService.selectNoticeList(notice);
// 按优先级排序1高 2中 3低相同优先级按创建时间降序
list.sort((a, b) -> {
String priorityA = a.getPriority() != null ? a.getPriority() : "3";
String priorityB = b.getPriority() != null ? b.getPriority() : "3";
int priorityCompare = priorityA.compareTo(priorityB);
if (priorityCompare != 0) {
return priorityCompare;
}
// 相同优先级,按创建时间降序
return b.getCreateTime().compareTo(a.getCreateTime());
});
// 获取用户已读的公告/通知ID列表
List<Long> readIds = noticeReadService.selectReadNoticeIdsByUserId(currentUser.getUserId());
// 为每个公告/通知添加已读状态
for (SysNotice item : list) {
boolean isRead = readIds.contains(item.getNoticeId());
item.setIsRead(isRead);
}
return success(list);
}
@@ -143,6 +165,10 @@ public class SysNoticeController extends BaseController {
if (notice.getPublishStatus() == null || notice.getPublishStatus().isEmpty()) {
notice.setPublishStatus("0");
}
// 设置默认优先级为中2
if (notice.getPriority() == null || notice.getPriority().isEmpty()) {
notice.setPriority("2");
}
return toAjax(noticeService.insertNotice(notice));
}

View File

@@ -34,6 +34,12 @@ public class SysNotice extends BaseEntity {
/** 发布状态0未发布 1已发布 */
private String publishStatus;
/** 优先级1高 2中 3低 */
private String priority;
/** 是否已读(前端展示用,不映射到数据库) */
private Boolean isRead;
public Long getNoticeId() {
return noticeId;
}
@@ -85,6 +91,22 @@ public class SysNotice extends BaseEntity {
this.publishStatus = publishStatus;
}
public String getPriority() {
return priority;
}
public void setPriority(String priority) {
this.priority = priority;
}
public Boolean getIsRead() {
return isRead;
}
public void setIsRead(Boolean isRead) {
this.isRead = isRead;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("noticeId", getNoticeId())

View File

@@ -11,6 +11,7 @@
<result property="noticeContent" column="notice_content"/>
<result property="status" column="status"/>
<result property="publishStatus" column="publish_status"/>
<result property="priority" column="priority"/>
<result property="createBy" column="create_by"/>
<result property="createTime" column="create_time"/>
<result property="updateBy" column="update_by"/>
@@ -25,6 +26,7 @@
convert_from(notice_content, 'UTF8') as notice_content,
status,
publish_status,
priority,
create_by,
create_time,
update_by,
@@ -67,6 +69,7 @@
<if test="noticeType != null and noticeType != '' ">notice_type,</if>
<if test="noticeContent != null and noticeContent != '' ">notice_content,</if>
<if test="status != null and status != '' ">status,</if>
<if test="priority != null and priority != '' ">priority,</if>
<if test="remark != null and remark != ''">remark,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
create_time
@@ -76,6 +79,7 @@
<if test="noticeType != null and noticeType != ''">#{noticeType},</if>
<if test="noticeContent != null and noticeContent != ''">cast(#{noticeContent} as bytea),</if>
<if test="status != null and status != ''">#{status},</if>
<if test="priority != null and priority != ''">#{priority},</if>
<if test="remark != null and remark != ''">#{remark},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
now()
@@ -90,6 +94,7 @@
<if test="noticeContent != null">notice_content = cast(#{noticeContent} as bytea),</if>
<if test="status != null and status != ''">status = #{status},</if>
<if test="publishStatus != null and publishStatus != ''">publish_status = #{publishStatus},</if>
<if test="priority != null and priority != ''">priority = #{priority},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
update_time = now()
</set>

View File

@@ -6,7 +6,7 @@ spring:
druid:
# 主库数据源
master:
url: jdbc:postgresql://192.168.110.252:15432/postgresql?currentSchema=hisdev&characterEncoding=UTF-8&client_encoding=UTF-8
url: jdbc:postgresql://47.116.196.11:15432/postgresql?currentSchema=hisdev&characterEncoding=UTF-8&client_encoding=UTF-8
username: postgresql
password: Jchl1528
# 从库数据源
@@ -64,9 +64,9 @@ spring:
# redis 配置
redis:
# 地址
host: 192.168.110.252
host: 47.116.196.11
# 端口默认为6379
port: 6379
port: 26379
# 数据库索引
database: 1
# 密码

View File

@@ -0,0 +1,10 @@
-- 为 sys_notice 表添加优先级字段
-- 执行前请先备份数据库
ALTER TABLE sys_notice ADD COLUMN priority VARCHAR(10) DEFAULT '2';
-- 添加注释
COMMENT ON COLUMN sys_notice.priority IS '优先级1高 2中 3低';
-- 为现有数据设置默认优先级
UPDATE sys_notice SET priority = '2' WHERE priority IS NULL;

View File

@@ -0,0 +1,557 @@
<template>
<el-dialog
v-model="dialogVisible"
title="公告/通知"
width="800px"
:close-on-click-modal="true"
:close-on-press-escape="true"
class="notice-panel-dialog"
@close="handleClose"
>
<div class="notice-panel-content">
<!-- 公告列表 -->
<div class="notice-list">
<div class="notice-list-header">
<span class="notice-list-title">公告列表</span>
<el-button text type="primary" @click="markAllAsRead" v-if="unreadCount > 0">
全部标记为已读
</el-button>
</div>
<el-scrollbar max-height="400px">
<div
v-for="notice in noticeList"
:key="notice.noticeId"
class="notice-item"
:class="{ 'unread': !notice.isRead, 'active': activeNoticeId === notice.noticeId }"
@click="handleSelectNotice(notice)"
>
<div class="notice-item-left">
<el-icon class="notice-type-icon" :class="getNoticeTypeClass(notice.noticeType)">
<component :is="getNoticeTypeIcon(notice.noticeType)" />
</el-icon>
<div class="notice-item-content">
<div class="notice-title">
<el-tag
:class="getPriorityClass(notice.priority)"
size="small"
effect="plain"
>
{{ getPriorityText(notice.priority) }}
</el-tag>
<span class="notice-title-text">{{ notice.noticeTitle }}</span>
<el-tag v-if="!notice.isRead" type="danger" size="small" effect="dark">未读</el-tag>
</div>
<div class="notice-meta">
<span class="notice-date">{{ formatDate(notice.createTime) }}</span>
<span class="notice-creator">{{ notice.createBy }}</span>
</div>
</div>
</div>
<el-icon class="arrow-icon" v-if="activeNoticeId === notice.noticeId">
<ArrowRight />
</el-icon>
</div>
<!-- 空状态 -->
<div v-if="noticeList.length === 0" class="empty-state">
<el-empty description="暂无公告" :image-size="100" />
</div>
</el-scrollbar>
</div>
<!-- 公告详情 -->
<div class="notice-detail" v-if="activeNotice">
<div class="notice-detail-header">
<h3 class="notice-detail-title">{{ activeNotice.noticeTitle }}</h3>
<div class="notice-detail-meta">
<el-tag :type="getPriorityTagType(activeNotice.priority)" size="small" effect="dark">
{{ getPriorityText(activeNotice.priority) }}
</el-tag>
<el-tag :type="getNoticeTypeTagType(activeNotice.noticeType)" size="small">
{{ getNoticeTypeText(activeNotice.noticeType) }}
</el-tag>
<span class="notice-detail-date">{{ formatFullDate(activeNotice.createTime) }}</span>
</div>
</div>
<el-divider />
<div class="notice-detail-body" v-html="activeNotice.noticeContent"></div>
</div>
<!-- 未选择状态 -->
<div v-else class="no-selection">
<el-empty description="请选择公告查看详情" :image-size="120" />
</div>
</div>
<template #footer>
<div class="dialog-footer">
<el-button @click="handleClose">关闭</el-button>
</div>
</template>
</el-dialog>
</template>
<script setup>
import { ref, computed, defineEmits } from 'vue'
import { getUserNotices, markAsRead, markAllAsRead as markAllReadApi } from '@/api/system/notice'
import { Bell, Warning, InfoFilled, ArrowRight, CircleCheck } from '@element-plus/icons-vue'
const emit = defineEmits(['updateUnreadCount'])
const dialogVisible = ref(false)
const noticeList = ref([])
const activeNoticeId = ref(null)
const activeNotice = computed(() => {
return noticeList.value.find(n => n.noticeId === activeNoticeId.value)
})
const unreadCount = computed(() => {
return noticeList.value.filter(n => !n.isRead).length
})
// 获取公告类型图标
const getNoticeTypeIcon = (type) => {
const iconMap = {
'1': Bell, // 通知
'2': Warning, // 紧急
'3': InfoFilled, // 信息
'4': CircleCheck // 成功
}
return iconMap[type] || InfoFilled
}
// 获取优先级样式类
const getPriorityClass = (priority) => {
const classMap = {
'1': 'priority-high',
'2': 'priority-medium',
'3': 'priority-low'
}
return classMap[priority] || 'priority-medium'
}
// 获取优先级文本
const getPriorityText = (priority) => {
const textMap = {
'1': '高',
'2': '中',
'3': '低'
}
return textMap[priority] || '中'
}
// 获取优先级标签类型
const getPriorityTagType = (priority) => {
const typeMap = {
'1': 'danger', // 高优先级 - 红色
'2': 'warning', // 中优先级 - 橙色
'3': 'info' // 低优先级 - 灰色
}
return typeMap[priority] || 'info'
}
// 获取公告类型样式类
const getNoticeTypeClass = (type) => {
const classMap = {
'1': 'type-notice',
'2': 'type-urgent',
'3': 'type-info',
'4': 'type-success'
}
return classMap[type] || 'type-info'
}
// 获取公告类型标签类型
const getNoticeTypeTagType = (type) => {
const typeMap = {
'1': '',
'2': 'danger',
'3': 'info',
'4': 'success'
}
return typeMap[type] || ''
}
// 获取公告类型文本
const getNoticeTypeText = (type) => {
const textMap = {
'1': '通知',
'2': '紧急',
'3': '信息',
'4': '成功'
}
return textMap[type] || '信息'
}
// 格式化相对时间
const formatDate = (dateStr) => {
if (!dateStr) return ''
const date = new Date(dateStr)
const now = new Date()
const diff = now - date
if (diff < 60000) { // 小于1分钟
return '刚刚'
} else if (diff < 3600000) { // 小于1小时
return Math.floor(diff / 60000) + '分钟前'
} else if (diff < 86400000) { // 小于1天
return Math.floor(diff / 3600000) + '小时前'
} else if (diff < 604800000) { // 小于1周
return Math.floor(diff / 86400000) + '天前'
} else {
return dateStr.substring(0, 10)
}
}
// 格式化完整时间
const formatFullDate = (dateStr) => {
if (!dateStr) return ''
return dateStr.replace('T', ' ').substring(0, 19)
}
// 加载公告列表
const loadNotices = async () => {
try {
const res = await getUserNotices()
if (res.code === 200) {
noticeList.value = res.data || []
}
} catch (error) {
console.error('加载公告失败:', error)
noticeList.value = []
}
}
// 打开面板
const open = async () => {
dialogVisible.value = true
await loadNotices()
// 如果有未读公告,默认选中第一个未读公告
const firstUnread = noticeList.value.find(n => !n.isRead)
if (firstUnread) {
activeNoticeId.value = firstUnread.noticeId
} else if (noticeList.value.length > 0) {
activeNoticeId.value = noticeList.value[0].noticeId
}
}
// 选择公告
const handleSelectNotice = async (notice) => {
activeNoticeId.value = notice.noticeId
// 标记为已读
if (!notice.isRead) {
try {
await markAsRead(notice.noticeId)
notice.isRead = true
// 通知父组件更新未读数量
emit('updateUnreadCount')
} catch (error) {
console.error('标记已读失败:', error)
}
}
}
// 全部标记为已读
const markAllAsRead = async () => {
try {
const unreadIds = noticeList.value.filter(n => !n.isRead).map(n => n.noticeId)
if (unreadIds.length > 0) {
await markAllReadApi(unreadIds)
noticeList.value.forEach(n => {
if (unreadIds.includes(n.noticeId)) {
n.isRead = true
}
})
// 通知父组件更新未读数量
emit('updateUnreadCount')
}
} catch (error) {
console.error('全部标记已读失败:', error)
}
}
// 关闭对话框
const handleClose = () => {
dialogVisible.value = false
}
// 暴露方法
defineExpose({
open
})
</script>
<style lang="scss" scoped>
.notice-panel-dialog {
:deep(.el-dialog__header) {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
.el-dialog__title {
font-size: 18px;
font-weight: 600;
}
.el-dialog__headerbtn .el-dialog__close {
color: white;
font-size: 20px;
&:hover {
color: rgba(255, 255, 255, 0.8);
}
}
}
:deep(.el-dialog__body) {
padding: 0;
}
:deep(.el-dialog__footer) {
padding: 15px 20px;
background: #f5f7fa;
border-top: 1px solid #e4e7ed;
}
}
.notice-panel-content {
display: flex;
min-height: 400px;
max-height: 500px;
overflow: hidden;
}
.notice-list {
width: 360px;
border-right: 1px solid #e4e7ed;
background: #fafafa;
display: flex;
flex-direction: column;
.notice-list-header {
padding: 16px;
border-bottom: 1px solid #e4e7ed;
display: flex;
justify-content: space-between;
align-items: center;
.notice-list-title {
font-size: 15px;
font-weight: 600;
color: #333;
}
}
.notice-item {
padding: 14px 16px;
cursor: pointer;
border-bottom: 1px solid #e4e7ed;
transition: all 0.3s;
&:hover {
background: #f0f2f5;
}
&.active {
background: #e6f7ff;
border-left: 3px solid #1890ff;
}
&.unread {
background: #fffbe6;
&:hover {
background: #fff7b8;
}
&.active {
background: #fff7b8;
border-left: 3px solid #faad14;
}
}
}
.notice-item-left {
display: flex;
gap: 10px;
.notice-type-icon {
flex-shrink: 0;
width: 36px;
height: 36px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
&.type-notice {
background: #e6f7ff;
color: #1890ff;
}
&.type-urgent {
background: #fff1f0;
color: #ff4d4f;
}
&.type-info {
background: #f6ffed;
color: #52c41a;
}
&.type-success {
background: #f9f0ff;
color: #722ed1;
}
}
.notice-item-content {
flex: 1;
min-width: 0;
.notice-title {
display: flex;
align-items: center;
gap: 6px;
margin-bottom: 5px;
.notice-title-text {
font-size: 13px;
font-weight: 500;
color: #333;
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
.notice-meta {
display: flex;
gap: 10px;
font-size: 11px;
color: #999;
.notice-date,
.notice-creator {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
}
}
.arrow-icon {
position: absolute;
right: 16px;
top: 50%;
transform: translateY(-50%);
color: #1890ff;
font-size: 14px;
}
.empty-state {
padding: 40px 20px;
text-align: center;
}
}
.notice-detail {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
.notice-detail-header {
padding: 20px;
.notice-detail-title {
margin: 0 0 12px 0;
font-size: 16px;
font-weight: 600;
color: #333;
line-height: 1.5;
}
.notice-detail-meta {
display: flex;
align-items: center;
gap: 12px;
.notice-detail-date {
font-size: 12px;
color: #999;
}
}
}
.notice-detail-body {
flex: 1;
padding: 0 20px 20px 20px;
overflow-y: auto;
font-size: 14px;
line-height: 1.8;
color: #666;
:deep(img) {
max-width: 100%;
height: auto;
border-radius: 4px;
margin: 10px 0;
}
:deep(p) {
margin: 10px 0;
}
}
}
.no-selection {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
}
.dialog-footer {
display: flex;
justify-content: flex-end;
}
// 优先级样式
.priority-high {
background: #fff1f0;
color: #ff4d4f;
border-color: #ffccc7;
&:hover {
background: #ffccc7;
color: #fff;
}
}
.priority-medium {
background: #fff7e6;
color: #faad14;
border-color: #ffe58f;
&:hover {
background: #ffe58f;
color: #fff;
}
}
.priority-low {
background: #f0f2f5;
color: #909399;
border-color: #dcdfe6;
&:hover {
background: #dcdfe6;
color: #fff;
}
}
</style>

View File

@@ -0,0 +1,592 @@
<template>
<el-dialog
v-model="dialogVisible"
title="系统公告"
width="800px"
:close-on-click-modal="false"
:close-on-press-escape="false"
:show-close="true"
class="notice-popup-dialog"
@close="handleClose"
>
<div class="notice-popup-content">
<!-- 公告列表 -->
<div class="notice-list">
<el-scrollbar max-height="500px">
<div
v-for="notice in noticeList"
:key="notice.noticeId"
class="notice-item"
:class="{ 'unread': !notice.isRead, 'active': activeNoticeId === notice.noticeId }"
@click="handleSelectNotice(notice)"
>
<div class="notice-item-left">
<el-icon class="notice-type-icon" :class="getNoticeTypeClass(notice.noticeType)">
<component :is="getNoticeTypeIcon(notice.noticeType)" />
</el-icon>
<div class="notice-item-content">
<div class="notice-title">
<el-tag
:class="getPriorityClass(notice.priority)"
size="small"
effect="plain"
>
{{ getPriorityText(notice.priority) }}
</el-tag>
<span class="notice-title-text">{{ notice.noticeTitle }}</span>
<el-tag v-if="!notice.isRead" type="danger" size="small" effect="dark">未读</el-tag>
</div>
<div class="notice-meta">
<span class="notice-date">{{ formatDate(notice.createTime) }}</span>
<span class="notice-creator">{{ notice.createBy }}</span>
</div>
</div>
</div>
<el-icon class="arrow-icon" v-if="activeNoticeId === notice.noticeId">
<ArrowRight />
</el-icon>
</div>
</el-scrollbar>
</div>
<!-- 公告详情 -->
<div class="notice-detail" v-if="activeNotice">
<div class="notice-detail-header">
<h3 class="notice-detail-title">{{ activeNotice.noticeTitle }}</h3>
<div class="notice-detail-meta">
<el-tag :type="getPriorityTagType(activeNotice.priority)" size="small" effect="dark">
{{ getPriorityText(activeNotice.priority) }}
</el-tag>
<el-tag :type="getNoticeTypeTagType(activeNotice.noticeType)" size="small">
{{ getNoticeTypeText(activeNotice.noticeType) }}
</el-tag>
<span class="notice-detail-date">{{ formatDate(activeNotice.createTime) }}</span>
</div>
</div>
<el-divider />
<div class="notice-detail-body" v-html="activeNotice.noticeContent"></div>
</div>
<!-- 空状态 -->
<div v-else class="empty-state">
<el-empty description="暂无公告" :image-size="120" />
</div>
</div>
<template #footer>
<div class="dialog-footer">
<el-button @click="markAllAsRead" v-if="unreadCount > 0" type="info">
全部标记为已读
</el-button>
<el-button type="primary" @click="handleClose">
{{ hasUnread ? '稍后再看' : '知道了' }}
</el-button>
</div>
</template>
</el-dialog>
</template>
<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { getUserNotices, markAsRead, markAllAsRead as markAllReadApi } from '@/api/system/notice'
import { Bell, Warning, InfoFilled, ArrowRight, CircleCheck } from '@element-plus/icons-vue'
// 监听全局触发公告弹窗事件
onMounted(() => {
window.addEventListener('trigger-notice-popup', handleTriggerNoticePopup)
})
onUnmounted(() => {
window.removeEventListener('trigger-notice-popup', handleTriggerNoticePopup)
})
function handleTriggerNoticePopup(event) {
// 显示公告弹窗
dialogVisible.value = true
// 如果没有加载过公告,则加载
if (noticeList.value.length === 0) {
loadNotices()
}
}
const dialogVisible = ref(false)
const noticeList = ref([])
const activeNoticeId = ref(null)
const readNoticeIds = ref([])
const activeNotice = computed(() => {
return noticeList.value.find(n => n.noticeId === activeNoticeId.value)
})
const unreadCount = computed(() => {
return noticeList.value.filter(n => !n.isRead).length
})
const hasUnread = computed(() => {
return unreadCount.value > 0
})
// 获取公告类型图标
const getNoticeTypeIcon = (type) => {
const iconMap = {
'1': Bell, // 通知
'2': Warning, // 紧急
'3': InfoFilled, // 信息
'4': CircleCheck // 成功
}
return iconMap[type] || InfoFilled
}
// 获取优先级样式类
const getPriorityClass = (priority) => {
const classMap = {
'1': 'priority-high',
'2': 'priority-medium',
'3': 'priority-low'
}
return classMap[priority] || 'priority-medium'
}
// 获取优先级文本
const getPriorityText = (priority) => {
const textMap = {
'1': '高',
'2': '中',
'3': '低'
}
return textMap[priority] || '中'
}
// 获取优先级标签类型
const getPriorityTagType = (priority) => {
const typeMap = {
'1': 'danger', // 高优先级 - 红色
'2': 'warning', // 中优先级 - 橙色
'3': 'info' // 低优先级 - 灰色
}
return typeMap[priority] || 'info'
}
// 获取优先级图标
const getPriorityIcon = (priority) => {
const iconMap = {
'1': Warning, // 高优先级
'2': InfoFilled, // 中优先级
'3': CircleCheck // 低优先级
}
return iconMap[priority] || InfoFilled
}
// 获取公告类型样式类
const getNoticeTypeClass = (type) => {
const classMap = {
'1': 'type-notice',
'2': 'type-urgent',
'3': 'type-info',
'4': 'type-success'
}
return classMap[type] || 'type-info'
}
// 获取公告类型标签类型
const getNoticeTypeTagType = (type) => {
const typeMap = {
'1': '',
'2': 'danger',
'3': 'info',
'4': 'success'
}
return typeMap[type] || ''
}
// 获取公告类型文本
const getNoticeTypeText = (type) => {
const textMap = {
'1': '通知',
'2': '紧急',
'3': '信息',
'4': '成功'
}
return textMap[type] || '信息'
}
// 格式化日期
const formatDate = (dateStr) => {
if (!dateStr) return ''
const date = new Date(dateStr)
const now = new Date()
const diff = now - date
if (diff < 60000) { // 小于1分钟
return '刚刚'
} else if (diff < 3600000) { // 小于1小时
return Math.floor(diff / 60000) + '分钟前'
} else if (diff < 86400000) { // 小于1天
return Math.floor(diff / 3600000) + '小时前'
} else if (diff < 604800000) { // 小于1周
return Math.floor(diff / 86400000) + '天前'
} else {
return dateStr.substring(0, 10)
}
}
// 格式化完整时间
const formatFullDate = (dateStr) => {
if (!dateStr) return ''
return dateStr.replace('T', ' ').substring(0, 19)
}
// 加载公告列表
const loadNotices = async () => {
try {
const res = await getUserNotices()
if (res.code === 200) {
noticeList.value = res.data || []
// 设置第一个未读公告为激活状态,如果没有未读则设置第一个
const firstUnread = noticeList.value.find(n => !n.isRead)
if (firstUnread) {
activeNoticeId.value = firstUnread.noticeId
} else if (noticeList.value.length > 0) {
activeNoticeId.value = noticeList.value[0].noticeId
}
// 检查是否有未读公告,有未读则自动弹出
const hasUnreadNotices = noticeList.value.some(n => !n.isRead)
if (hasUnreadNotices) {
// 延迟弹出,避免与登录加载冲突
setTimeout(() => {
dialogVisible.value = true
}, 500)
}
}
} catch (error) {
console.error('加载公告失败:', error)
}
}
// 选择公告
const handleSelectNotice = async (notice) => {
activeNoticeId.value = notice.noticeId
// 标记为已读
if (!notice.isRead) {
try {
await markAsRead(notice.noticeId)
notice.isRead = true
} catch (error) {
console.error('标记已读失败:', error)
}
}
}
// 全部标记为已读
const markAllAsRead = async () => {
try {
const unreadIds = noticeList.value.filter(n => !n.isRead).map(n => n.noticeId)
if (unreadIds.length > 0) {
await markAllReadApi(unreadIds)
noticeList.value.forEach(n => {
if (unreadIds.includes(n.noticeId)) {
n.isRead = true
}
})
}
} catch (error) {
console.error('全部标记已读失败:', error)
}
}
// 关闭对话框
const handleClose = () => {
dialogVisible.value = false
}
// 初始化
onMounted(() => {
// 延迟加载,避免影响页面渲染
setTimeout(() => {
loadNotices()
}, 1000)
})
// 暴露方法供外部调用
defineExpose({
showNotice: () => {
dialogVisible.value = true
if (noticeList.value.length === 0) {
loadNotices()
}
}
})
</script>
<style lang="scss" scoped>
.notice-popup-dialog {
:deep(.el-dialog__header) {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
.el-dialog__title {
font-size: 20px;
font-weight: 600;
}
.el-dialog__headerbtn .el-dialog__close {
color: white;
font-size: 20px;
&:hover {
color: rgba(255, 255, 255, 0.8);
}
}
}
:deep(.el-dialog__body) {
padding: 0;
}
:deep(.el-dialog__footer) {
padding: 15px 20px;
background: #f5f7fa;
border-top: 1px solid #e4e7ed;
}
}
.notice-popup-content {
display: flex;
min-height: 500px;
max-height: 600px;
overflow: hidden;
}
.notice-list {
width: 380px;
border-right: 1px solid #e4e7ed;
background: #fafafa;
.notice-item {
padding: 16px;
cursor: pointer;
border-bottom: 1px solid #e4e7ed;
transition: all 0.3s;
&:hover {
background: #f0f2f5;
}
&.active {
background: #e6f7ff;
border-left: 3px solid #1890ff;
}
&.unread {
background: #fffbe6;
&:hover {
background: #fff7b8;
}
&.active {
background: #fff7b8;
border-left: 3px solid #faad14;
}
}
}
.notice-item-left {
display: flex;
gap: 12px;
.notice-type-icon {
flex-shrink: 0;
width: 40px;
height: 40px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
&.type-notice {
background: #e6f7ff;
color: #1890ff;
}
&.type-urgent {
background: #fff1f0;
color: #ff4d4f;
}
&.type-info {
background: #f6ffed;
color: #52c41a;
}
&.type-success {
background: #f9f0ff;
color: #722ed1;
}
}
.notice-item-content {
flex: 1;
min-width: 0;
.notice-title {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 6px;
.notice-title-text {
font-size: 14px;
font-weight: 500;
color: #333;
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
// 优先级标签样式
.priority-high {
color: #f56c6c;
border-color: #f56c6c;
background: #fef0f0;
}
.priority-medium {
color: #e6a23c;
border-color: #e6a23c;
background: #fdf6ec;
}
.priority-low {
color: #909399;
border-color: #909399;
background: #f4f4f5;
}
.notice-meta {
display: flex;
gap: 12px;
font-size: 12px;
color: #999;
.notice-date,
.notice-creator {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
}
}
.arrow-icon {
position: absolute;
right: 16px;
top: 50%;
transform: translateY(-50%);
color: #1890ff;
font-size: 16px;
}
}
.notice-detail {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
.notice-detail-header {
padding: 20px;
.notice-detail-title {
margin: 0 0 12px 0;
font-size: 18px;
font-weight: 600;
color: #333;
line-height: 1.5;
}
.notice-detail-meta {
display: flex;
align-items: center;
gap: 12px;
.notice-detail-date {
font-size: 13px;
color: #999;
}
}
}
.notice-detail-body {
flex: 1;
padding: 0 20px 20px 20px;
overflow-y: auto;
font-size: 14px;
line-height: 1.8;
color: #666;
:deep(img) {
max-width: 100%;
height: auto;
border-radius: 4px;
margin: 10px 0;
}
:deep(p) {
margin: 10px 0;
}
}
}
.empty-state {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
}
.dialog-footer {
display: flex;
justify-content: flex-end;
gap: 10px;
}
// 优先级样式
.priority-high {
background: #fff1f0;
color: #ff4d4f;
border-color: #ffccc7;
&:hover {
background: #ffccc7;
color: #fff;
}
}
.priority-medium {
background: #fff7e6;
color: #faad14;
border-color: #ffe58f;
&:hover {
background: #ffe58f;
color: #fff;
}
}
.priority-low {
background: #f0f2f5;
color: #909399;
border-color: #dcdfe6;
&:hover {
background: #dcdfe6;
color: #fff;
}
}
</style>

View File

@@ -16,6 +16,8 @@
<app-main />
<settings ref="settingRef" />
</div>
<!-- 公告弹窗组件 -->
<notice-popup ref="noticePopupRef" />
</div>
</template>
@@ -23,6 +25,7 @@
import {useWindowSize} from '@vueuse/core';
import Sidebar from './components/Sidebar/index.vue';
import {AppMain, Settings, TagsView} from './components';
import NoticePopup from '@/components/NoticePopup/index.vue';
import useAppStore from '@/store/modules/app';
import useSettingsStore from '@/store/modules/settings';
@@ -62,9 +65,16 @@ function handleClickOutside() {
}
const settingRef = ref(null);
const noticePopupRef = ref(null);
function setLayout() {
settingRef.value.openSetting();
}
// 暴露公告弹窗引用,供其他组件调用
defineExpose({
noticePopupRef
});
</script>
<style lang="scss" scoped>

View File

@@ -96,4 +96,13 @@ app.use(ElementPlus, {
size: Cookies.get('size') || 'default',
});
// 导入公告帮助工具
import { initNoticePopupAfterLogin } from '@/utils/noticeHelper'
app.mount('#app');
// 应用启动后初始化公告弹窗功能
import { nextTick } from 'vue'
nextTick(() => {
initNoticePopupAfterLogin()
})

View File

@@ -9,6 +9,9 @@ import useUserStore from '@/store/modules/user'
import useSettingsStore from '@/store/modules/settings'
import usePermissionStore from '@/store/modules/permission'
// 全局变量,用于控制公告弹窗只显示一次
let hasShownNoticePopup = false
NProgress.configure({ showSpinner: false });
const whiteList = ['/login', '/register'];
@@ -65,4 +68,36 @@ router.beforeEach((to, from, next) => {
router.afterEach(() => {
NProgress.done()
})
// 登录成功后显示公告弹窗(仅限非登录页面且未显示过)
const token = getToken()
const isLoginPage = router.currentRoute.value.path === '/login'
if (token && !isLoginPage && !hasShownNoticePopup) {
// 延迟显示,确保页面完全加载
setTimeout(() => {
showNoticePopupGlobally()
hasShownNoticePopup = true
}, 1500)
}
})
// 全局函数:显示公告弹窗
function showNoticePopupGlobally() {
try {
// 通过多种方式尝试获取并显示公告弹窗
const layouts = document.querySelectorAll('.app-wrapper')
for (const layout of layouts) {
const noticePopupRef = layout.__vue_app__?.config.globalProperties.$refs?.noticePopupRef
if (noticePopupRef && noticePopupRef.showNotice) {
noticePopupRef.showNotice()
return
}
}
// 如果直接获取失败,尝试通过事件总线方式
window.dispatchEvent(new CustomEvent('show-notice-popup'))
} catch (error) {
console.error('显示公告弹窗失败:', error)
}
}

View File

@@ -0,0 +1,38 @@
// 公告帮助工具类
// 用于登录后自动显示公告弹窗
export function initNoticePopupAfterLogin() {
// 监听路由变化,在登录成功后显示公告
const token = localStorage.getItem('Admin-Token') || sessionStorage.getItem('Admin-Token')
if (!token) return
// 检查是否已经显示过公告
if (sessionStorage.getItem('notice_popup_shown')) return
// 延迟执行,确保页面完全加载
setTimeout(() => {
try {
// 查找公告弹窗组件并触发显示
const noticeElements = document.querySelectorAll('[class*="notice-popup"]')
if (noticeElements.length > 0) {
// 触发点击事件或查找内部方法
const event = new CustomEvent('trigger-notice-popup', { detail: { autoShow: true } })
window.dispatchEvent(event)
sessionStorage.setItem('notice_popup_shown', 'true')
}
} catch (error) {
console.error('自动显示公告弹窗失败:', error)
}
}, 2000)
}
// 重置公告弹窗显示状态(用于测试)
export function resetNoticePopupState() {
sessionStorage.removeItem('notice_popup_shown')
}
// 手动触发公告弹窗
export function triggerNoticePopup() {
const event = new CustomEvent('trigger-notice-popup', { detail: { autoShow: false } })
window.dispatchEvent(event)
}

View File

@@ -128,7 +128,7 @@
href="https://open.tntlinking.com/communityTreaty"
target="_blank"
>
>Copyright © 2025 湖北天天数链技术有限公司 本系统软件源代码许可来源于
Copyright © 2025 湖北天天数链技术有限公司 本系统软件源代码许可来源于
天天开源软件社区版许可协议 https://open.tntlinking.com/communityTreaty
</el-link>
</span>
@@ -137,7 +137,7 @@
</template>
<script setup>
import {getCurrentInstance, onMounted, ref, watch} from 'vue';
import {getCurrentInstance, onMounted, ref, watch, nextTick} from 'vue';
import settings from '@/settings';
import {getCodeImg, getUserBindTenantList, sign} from '@/api/login';
import {invokeYbPlugin5001} from '@/api/public';

View File

@@ -89,6 +89,13 @@
<dict-tag :options="sys_notice_type" :value="scope.row.noticeType" class="dict-tag" />
</template>
</el-table-column>
<el-table-column label="优先级" align="center" prop="priority" width="90">
<template #default="scope">
<el-tag v-if="scope.row.priority === '1'" type="danger"></el-tag>
<el-tag v-else-if="scope.row.priority === '2'" type="warning"></el-tag>
<el-tag v-else type="info"></el-tag>
</template>
</el-table-column>
<el-table-column label="状态" align="center" prop="status" width="90">
<template #default="scope">
<dict-tag :options="sys_notice_status" :value="scope.row.status" class="dict-tag" />
@@ -163,6 +170,15 @@
</el-select>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="优先级" prop="priority">
<el-select v-model="form.priority" placeholder="请选择优先级">
<el-option label="高" value="1" />
<el-option label="中" value="2" />
<el-option label="低" value="3" />
</el-select>
</el-form-item>
</el-col>
<el-col :span="24">
<el-form-item label="状态">
<el-radio-group v-model="form.status">
@@ -246,7 +262,8 @@ function reset() {
noticeType: undefined,
noticeContent: undefined,
status: "0",
publishStatus: "0"
publishStatus: "0",
priority: "2"
};
proxy.resetForm("noticeRef");
}

View File

@@ -0,0 +1,178 @@
# 公告通知弹窗功能说明
## 功能概述
用户登录后,系统会自动弹出公告通知窗口,显示未读的系统公告和通知。
## 功能特性
### 1. 自动弹出
- 登录后 1 秒自动加载公告列表
- 如果有未读公告,自动弹出弹窗显示
- 延迟加载避免影响页面初始渲染
### 2. 优先级显示
- **高优先级**1红色背景 `#fff1f0`,红色文字 `#ff4d4f`
- **中优先级**2橙色背景 `#fff7e6`,橙色文字 `#faad14`
- **低优先级**3灰色背景 `#f0f2f5`,灰色文字 `#909399`
### 3. 排序规则
- 按优先级升序排列(高 -> 中 -> 低)
- 相同优先级按创建时间降序排列
### 4. 已读状态
- 未读公告:黄色背景高亮显示
- 已读公告:默认白色背景
- 点击公告自动标记为已读
### 5. 分类显示
- **通知**1蓝色图标
- **紧急**2红色警告图标
- **信息**3绿色信息图标
- **成功**4紫色成功图标
## 文件结构
```
src/
├── components/
│ ├── NoticePopup/
│ │ └── index.vue # 登录后自动弹窗组件
│ └── NoticePanel/
│ └── index.vue # 手动打开的公告面板
├── layout/
│ └── index.vue # 主布局,引入 NoticePopup
└── views/
└── system/
└── notice/
└── index.vue # 公告管理页面
```
## API 接口
### 前端 API
```javascript
// 获取用户公告列表
getUserNotices()
// 标记为已读
markAsRead(noticeId)
// 全部标记为已读
markAllAsRead(noticeIds)
// 获取未读数量
getUnreadCount()
```
### 后端接口
```java
// GET /system/notice/public/notice
// 获取当前用户的公告列表(含已读状态)
// GET /system/notice/public/unread/count
// 获取未读公告数量
// POST /system/notice/public/read/{noticeId}
// 标记公告为已读
// POST /system/notice/public/read/all
// 批量标记为已读
```
## 数据库字段
### sys_notice 表新增字段
```sql
-- 优先级字段
priority VARCHAR(1) DEFAULT '3'
-- 发布状态字段
publish_status VARCHAR(1) DEFAULT '0'
```
## 使用说明
### 1. 数据库迁移
执行 SQL 脚本添加必要的字段:
```bash
# 添加优先级字段
ALTER TABLE sys_notice ADD COLUMN priority VARCHAR(1) DEFAULT '3';
# 添加发布状态字段
ALTER TABLE sys_notice ADD COLUMN publish_status VARCHAR(1) DEFAULT '0';
```
### 2. 后台管理
`系统管理 > 公告管理` 中:
1. 点击"新增"创建公告
2. 选择优先级:高/中/低
3. 选择公告类型:通知/紧急/信息/成功
4. 填写标题和内容
5. 点击"发布"按钮
### 3. 用户端显示
- 登录后自动弹出未读公告
- 点击顶部导航栏铃铛图标可手动打开
- 点击公告查看详情
- 支持全部标记为已读
## 样式说明
### 弹窗样式
- 宽度800px
- 高度:最大 600px
- 标题栏:渐变紫色背景 `linear-gradient(135deg, #667eea 0%, #764ba2 100%)`
- 左右分栏布局
### 列表样式
- 列表宽度380px
- 滚动区域:最大高度 500px
- 未读高亮:黄色背景 `#fffbe6`
- 激活项:蓝色左边框 `#1890ff`
### 详情样式
- 自适应宽度
- 标题字号18px
- 内容字号14px
- 行高1.8
## 注意事项
1. **权限控制**
- 只有已发布的公告才会显示
- 只有正常状态的公告才会显示
2. **性能优化**
- 使用延迟加载1秒
- 使用虚拟滚动el-scrollbar
- 按需加载详情
3. **用户体验**
- 支持点击空白处关闭
- 支持ESC键关闭
- 未读状态醒目标识
- 优先级颜色区分明显
## 故障排查
### 弹窗不显示
1. 检查后端接口 `/system/notice/public/notice` 是否正常返回
2. 检查是否有未读公告
3. 检查浏览器控制台是否有错误
4. 清除浏览器缓存重试
### 样式错乱
1. 检查 Element Plus 版本是否兼容
2. 检查样式是否被其他 CSS 覆盖
3. 使用浏览器开发者工具检查样式
### 数据不更新
1. 检查后端返回的数据格式
2. 检查 API 调用是否成功
3. 检查响应拦截器处理
## 版本信息
- 创建日期2025-12-30
- 功能版本v1.0
- 前端框架Vue 3 + Element Plus
- 后端框架Spring Boot + MyBatis Plus

View File

View File

@@ -0,0 +1,8 @@
-- 添加公告/通知优先级字段
ALTER TABLE sys_notice ADD COLUMN priority VARCHAR(1) DEFAULT '3';
-- 添加字段注释
COMMENT ON COLUMN sys_notice.priority IS '优先级1高 2中 3低';
-- 更新现有数据的优先级为中等
UPDATE sys_notice SET priority = '2' WHERE priority IS NULL;