mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-04 00:10:25 +08:00
refactor: 系统消息调整
This commit is contained in:
47
server/internal/msg/application/dto/sys_msg.go
Normal file
47
server/internal/msg/application/dto/sys_msg.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package dto
|
||||
|
||||
import "mayfly-go/pkg/utils/stringx"
|
||||
|
||||
// ************** 系统消息 **************
|
||||
|
||||
const SuccessSysMsgType = 1
|
||||
const ErrorSysMsgType = 0
|
||||
const InfoSysMsgType = 2
|
||||
|
||||
// websocket消息
|
||||
type SysMsg struct {
|
||||
Type int `json:"type"` // 消息类型
|
||||
Category int `json:"category"` // 消息类别
|
||||
Title string `json:"title"` // 消息标题
|
||||
Msg string `json:"msg"` // 消息内容
|
||||
}
|
||||
|
||||
func (sm *SysMsg) WithTitle(title string) *SysMsg {
|
||||
sm.Title = title
|
||||
return sm
|
||||
}
|
||||
|
||||
func (sm *SysMsg) WithCategory(category int) *SysMsg {
|
||||
sm.Category = category
|
||||
return sm
|
||||
}
|
||||
|
||||
func (sm *SysMsg) WithMsg(msg any) *SysMsg {
|
||||
sm.Msg = stringx.AnyToStr(msg)
|
||||
return sm
|
||||
}
|
||||
|
||||
// 普通消息
|
||||
func NewSysMsg(title string, msg any) *SysMsg {
|
||||
return &SysMsg{Type: InfoSysMsgType, Title: title, Msg: stringx.AnyToStr(msg)}
|
||||
}
|
||||
|
||||
// 成功消息
|
||||
func SuccessSysMsg(title string, msg any) *SysMsg {
|
||||
return &SysMsg{Type: SuccessSysMsgType, Title: title, Msg: stringx.AnyToStr(msg)}
|
||||
}
|
||||
|
||||
// 错误消息
|
||||
func ErrSysMsg(title string, msg any) *SysMsg {
|
||||
return &SysMsg{Type: ErrorSysMsgType, Title: title, Msg: stringx.AnyToStr(msg)}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"mayfly-go/internal/msg/application/dto"
|
||||
"mayfly-go/internal/msg/domain/entity"
|
||||
"mayfly-go/internal/msg/domain/repository"
|
||||
"mayfly-go/pkg/model"
|
||||
@@ -14,7 +15,7 @@ type Msg interface {
|
||||
Create(msg *entity.Msg)
|
||||
|
||||
// 创建消息,并通过ws发送
|
||||
CreateAndSend(la *model.LoginAccount, msg *ws.SysMsg)
|
||||
CreateAndSend(la *model.LoginAccount, msg *dto.SysMsg)
|
||||
}
|
||||
|
||||
func newMsgApp(msgRepo repository.Msg) Msg {
|
||||
@@ -35,9 +36,9 @@ func (a *msgAppImpl) Create(msg *entity.Msg) {
|
||||
a.msgRepo.Insert(msg)
|
||||
}
|
||||
|
||||
func (a *msgAppImpl) CreateAndSend(la *model.LoginAccount, wmsg *ws.SysMsg) {
|
||||
func (a *msgAppImpl) CreateAndSend(la *model.LoginAccount, wmsg *dto.SysMsg) {
|
||||
now := time.Now()
|
||||
msg := &entity.Msg{Type: 2, Msg: wmsg.SysMsg, RecipientId: int64(la.Id), CreateTime: &now, CreatorId: la.Id, Creator: la.Username}
|
||||
msg := &entity.Msg{Type: 2, Msg: wmsg.Msg, RecipientId: int64(la.Id), CreateTime: &now, CreatorId: la.Id, Creator: la.Username}
|
||||
a.msgRepo.Insert(msg)
|
||||
ws.SendMsg(la.Id, wmsg)
|
||||
ws.SendJsonMsg(la.Id, wmsg)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user