diff --git a/internal/models/app_log.go b/internal/models/app_log.go index 7601148..aadc557 100644 --- a/internal/models/app_log.go +++ b/internal/models/app_log.go @@ -6,16 +6,17 @@ import ( // AppLog 统一应用日志与通知记录 type AppLog struct { - ID string `json:"id" gorm:"primaryKey;size:20"` - Category string `json:"category" gorm:"size:50;index;not null"` // 大类:constant.LogCategorySystemNotice(系统通知), constant.LogCategoryPushLog(推送记录) 等 - Title string `json:"title" gorm:"size:255"` // 消息标题 - Content BigText `json:"content"` // 详细内容/Payload - Level string `json:"level" gorm:"size:20;index"` // 级别:constant.LogLevelInfo, constant.LogLevelWarning, constant.LogLevelError - Status string `json:"status" gorm:"size:20;index"` // 状态:系统通知为 constant.LogStatusRead/constant.LogStatusUnread,推送为 constant.LogStatusSuccess/constant.LogStatusFailed - RefID string `json:"ref_id" gorm:"size:50;index"` // 关联对象ID(选填,比如绑定的通知渠道ID、任务ID等) - ErrorMsg BigText `json:"error_msg"` // 执行错误信息详情 - CreatedAt LocalTime `json:"created_at" gorm:"index"` - ReadAt *LocalTime `json:"read_at"` // 已读时间(仅对通知生效) + ID string `json:"id" gorm:"primaryKey;size:20"` + Category string `json:"category" gorm:"size:50;index;not null"` // 大类:constant.LogCategorySystemNotice(系统通知), constant.LogCategoryPushLog(推送记录) 等 + Title string `json:"title" gorm:"size:255"` // 消息标题 + Content BigText `json:"content"` // 详细内容/Payload + Level string `json:"level" gorm:"size:20;index"` // 级别:constant.LogLevelInfo, constant.LogLevelWarning, constant.LogLevelError + Status string `json:"status" gorm:"size:20;index"` // 状态:系统通知为 constant.LogStatusRead/constant.LogStatusUnread,推送为 constant.LogStatusSuccess/constant.LogStatusFailed + RefID string `json:"ref_id" gorm:"size:50;index"` // 关联对象ID(选填,比如绑定的通知渠道ID、任务ID等) + ErrorMsg BigText `json:"error_msg"` // 执行错误信息详情 + CreatedAt LocalTime `json:"created_at" gorm:"index"` + ReadAt *LocalTime `json:"read_at"` // 已读时间(仅对通知生效) + ChannelName string `json:"channel_name" gorm:"-"` // 推送记录的关联渠道名称(动态查询) } func (AppLog) TableName() string { diff --git a/internal/services/app_log_service.go b/internal/services/app_log_service.go index 421c9e1..76be9df 100644 --- a/internal/services/app_log_service.go +++ b/internal/services/app_log_service.go @@ -38,24 +38,53 @@ func (s *AppLogService) Add(log *models.AppLog) error { func (s *AppLogService) List(category string, status string, level string, page, pageSize int, keyword string) ([]models.AppLog, int64, error) { var logs []models.AppLog var total int64 - query := database.DB.Model(&models.AppLog{}) + db := database.DB + + // 如果是推送日志,尝试关联查询渠道名称 + if category == constant.LogCategoryPushLog { + db = db.Table(models.AppLog{}.TableName() + " AS al"). + Select("al.*, nw.name as channel_name"). + Joins(fmt.Sprintf("LEFT JOIN %s AS nw ON al.ref_id = nw.id", models.NotifyWay{}.TableName())) + } else { + db = db.Model(&models.AppLog{}) + } if category != "" { - query = query.Where("category = ?", category) + if category == constant.LogCategoryPushLog { + db = db.Where("al.category = ?", category) + } else { + db = db.Where("category = ?", category) + } } if status != "" { - query = query.Where("status = ?", status) + field := "status" + if category == constant.LogCategoryPushLog { + field = "al.status" + } + db = db.Where(field+" = ?", status) } if level != "" { - query = query.Where("level = ?", level) + field := "level" + if category == constant.LogCategoryPushLog { + field = "al.level" + } + db = db.Where(field+" = ?", level) } if keyword != "" { - query = query.Where("(title LIKE ? OR content LIKE ?)", "%"+keyword+"%", "%"+keyword+"%") + if category == constant.LogCategoryPushLog { + db = db.Where("(al.title LIKE ? OR al.content LIKE ?)", "%"+keyword+"%", "%"+keyword+"%") + } else { + db = db.Where("(title LIKE ? OR content LIKE ?)", "%"+keyword+"%", "%"+keyword+"%") + } } - query.Count(&total) + db.Count(&total) offset := (page - 1) * pageSize - err := query.Order("created_at desc").Offset(offset).Limit(pageSize).Find(&logs).Error + order := "created_at desc" + if category == constant.LogCategoryPushLog { + order = "al.created_at desc" + } + err := db.Order(order).Offset(offset).Limit(pageSize).Scan(&logs).Error return logs, total, err } @@ -171,7 +200,7 @@ func (s *AppLogService) SubscribeEvents(bus *eventbus.EventBus) { content, _ := payload["content"].(string) success, _ := payload["success"].(bool) errorMsg, _ := payload["error_msg"].(string) - channelName, _ := payload["channel_name"].(string) + channelID, _ := payload["channel_id"].(string) status := constant.LogStatusSuccess level := constant.LogLevelInfo @@ -182,10 +211,11 @@ func (s *AppLogService) SubscribeEvents(bus *eventbus.EventBus) { s.Add(&models.AppLog{ Category: constant.LogCategoryPushLog, - Title: fmt.Sprintf("[%s] %s", channelName, title), + Title: title, Content: models.BigText(content), Level: level, Status: status, + RefID: channelID, ErrorMsg: models.BigText(errorMsg), }) }) diff --git a/internal/services/notification_service.go b/internal/services/notification_service.go index 4fd1f25..bab1763 100644 --- a/internal/services/notification_service.go +++ b/internal/services/notification_service.go @@ -203,15 +203,16 @@ func (s *NotificationService) SendToChannel(channel NotifyChannel, msg *NotifyMe Title: msg.Title, Text: msg.Text, }) - + payload := map[string]interface{}{ "title": msg.Title, "content": msg.Text, + "channel_id": channel.ID, "channel_name": channel.Name, "success": false, "error_msg": "", } - + if err != nil { payload["error_msg"] = err.Error() eventbus.DefaultBus.Publish(eventbus.Event{ diff --git a/web/src/api/index.ts b/web/src/api/index.ts index a023c2a..45dd918 100644 --- a/web/src/api/index.ts +++ b/web/src/api/index.ts @@ -589,6 +589,7 @@ export interface AppLog { level: string status: string ref_id: string + channel_name?: string error_msg: string created_at: string read_at: string | null diff --git a/web/src/views/loginlogs/tabs/SystemEventTab.vue b/web/src/views/loginlogs/tabs/SystemEventTab.vue index 304af36..e2d03a4 100644 --- a/web/src/views/loginlogs/tabs/SystemEventTab.vue +++ b/web/src/views/loginlogs/tabs/SystemEventTab.vue @@ -289,9 +289,11 @@ function onDialogClose(open: boolean) { 内容详情
-
{{ detailDialogProps.content }}
-
无内容
+
+ {{ detailDialogProps.content }} +
+
无内容
diff --git a/web/src/views/notify/components/PushLog.vue b/web/src/views/notify/components/PushLog.vue index fdc2542..9342855 100644 --- a/web/src/views/notify/components/PushLog.vue +++ b/web/src/views/notify/components/PushLog.vue @@ -226,8 +226,10 @@ function onDialogClose(open: boolean) { class="flex items-center gap-2 sm:gap-4 px-3 sm:px-4 py-2 hover:bg-muted/50 transition-colors cursor-pointer group" :class="[selectedLogId === log.id && 'bg-accent/50']" @click="showDetail(log)"> #{{ getLogIndex(index) }} - {{ log.title - }} + + [{{ log.channel_name }}]{{ log.title + }} + @@ -270,6 +272,10 @@ function onDialogClose(open: boolean) { 标题 {{ detailDialogProps.title }} +
+ 发送渠道 + {{ selectedLog.channel_name }} +
发生时间 {{ selectedLog ? formatDate(selectedLog.created_at) @@ -284,9 +290,11 @@ function onDialogClose(open: boolean) { 推送内容
-
{{ detailDialogProps.content }}
-
无推送内容
+
+ {{ detailDialogProps.content }} +
+
无推送内容