From 22e218fc5f625096d58fddbd1a9462c175187fbe Mon Sep 17 00:00:00 2001 From: "meilin.huang" <954537473@qq.com> Date: Tue, 10 Oct 2023 17:39:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=B3=BB=E7=BB=9F=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mayfly_go_web/src/common/request.ts | 4 +- server/internal/db/api/db.go | 10 ++-- server/internal/machine/api/machine_file.go | 10 ++-- .../internal/msg/application/dto/sys_msg.go | 47 +++++++++++++++++++ server/internal/msg/application/msg.go | 9 ++-- server/pkg/ws/msg.go | 28 ----------- server/pkg/ws/ws.go | 4 +- 7 files changed, 66 insertions(+), 46 deletions(-) create mode 100644 server/internal/msg/application/dto/sys_msg.go diff --git a/mayfly_go_web/src/common/request.ts b/mayfly_go_web/src/common/request.ts index a644dbb2..0f20bbb9 100755 --- a/mayfly_go_web/src/common/request.ts +++ b/mayfly_go_web/src/common/request.ts @@ -29,7 +29,7 @@ enum ResultEnum { } const baseUrl: string = config.baseApiUrl; -const baseWsUrl: string = config.baseWsUrl; +// const baseWsUrl: string = config.baseWsUrl; /** * 通知错误消息 @@ -43,7 +43,7 @@ function notifyErrorMsg(msg: string) { // create an axios instance const service = Axios.create({ baseURL: baseUrl, // url = base url + request url - timeout: 20000, // request timeout + timeout: 60000, // request timeout }); // request interceptor diff --git a/server/internal/db/api/db.go b/server/internal/db/api/db.go index fd23cd96..4db5aa20 100644 --- a/server/internal/db/api/db.go +++ b/server/internal/db/api/db.go @@ -9,6 +9,7 @@ import ( "mayfly-go/internal/db/application" "mayfly-go/internal/db/domain/entity" msgapp "mayfly-go/internal/msg/application" + msgdto "mayfly-go/internal/msg/application/dto" tagapp "mayfly-go/internal/tag/application" "mayfly-go/pkg/biz" "mayfly-go/pkg/ginx" @@ -16,7 +17,6 @@ import ( "mayfly-go/pkg/model" "mayfly-go/pkg/req" "mayfly-go/pkg/utils/stringx" - "mayfly-go/pkg/ws" "regexp" "strconv" "strings" @@ -182,7 +182,7 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) { errInfo = t.Error() } if len(errInfo) > 0 { - d.MsgApp.CreateAndSend(rc.LoginAccount, ws.ErrSysMsg("sql脚本执行失败", fmt.Sprintf("[%s]%s执行失败: [%s]", filename, dbConn.Info.GetLogDesc(), errInfo))) + d.MsgApp.CreateAndSend(rc.LoginAccount, msgdto.ErrSysMsg("sql脚本执行失败", fmt.Sprintf("[%s]%s执行失败: [%s]", filename, dbConn.Info.GetLogDesc(), errInfo))) } } }() @@ -207,11 +207,11 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) { } if err != nil { - d.MsgApp.CreateAndSend(rc.LoginAccount, ws.ErrSysMsg("sql脚本执行失败", fmt.Sprintf("[%s][%s] -> sql=[%s] 执行失败: [%s]", filename, dbConn.Info.GetLogDesc(), sql, err.Error()))) + d.MsgApp.CreateAndSend(rc.LoginAccount, msgdto.ErrSysMsg("sql脚本执行失败", fmt.Sprintf("[%s][%s] -> sql=[%s] 执行失败: [%s]", filename, dbConn.Info.GetLogDesc(), sql, err.Error()))) return } } - d.MsgApp.CreateAndSend(rc.LoginAccount, ws.SuccessSysMsg("sql脚本执行成功", fmt.Sprintf("[%s]执行完成 -> %s", filename, dbConn.Info.GetLogDesc()))) + d.MsgApp.CreateAndSend(rc.LoginAccount, msgdto.SuccessSysMsg("sql脚本执行成功", fmt.Sprintf("[%s]执行完成 -> %s", filename, dbConn.Info.GetLogDesc()))) }() } @@ -265,7 +265,7 @@ func (d *Db) DumpSql(rc *req.Ctx) { if len(msg) > 0 { msg = "数据库导出失败: " + msg writer.WriteString(msg) - d.MsgApp.CreateAndSend(rc.LoginAccount, ws.ErrSysMsg("数据库导出失败", msg)) + d.MsgApp.CreateAndSend(rc.LoginAccount, msgdto.ErrSysMsg("数据库导出失败", msg)) } writer.Close() }() diff --git a/server/internal/machine/api/machine_file.go b/server/internal/machine/api/machine_file.go index 0f78b2c4..2dadc31f 100644 --- a/server/internal/machine/api/machine_file.go +++ b/server/internal/machine/api/machine_file.go @@ -10,6 +10,7 @@ import ( "mayfly-go/internal/machine/domain/entity" "mayfly-go/internal/machine/infrastructure/machine" msgapp "mayfly-go/internal/msg/application" + msgdto "mayfly-go/internal/msg/application/dto" "mayfly-go/pkg/biz" "mayfly-go/pkg/ginx" "mayfly-go/pkg/logx" @@ -17,7 +18,6 @@ import ( "mayfly-go/pkg/utils/collx" "mayfly-go/pkg/utils/jsonx" "mayfly-go/pkg/utils/timex" - "mayfly-go/pkg/ws" "mime/multipart" "path/filepath" "sort" @@ -187,7 +187,7 @@ func (m *MachineFile) UploadFile(rc *req.Ctx) { logx.Errorf("文件上传失败: %s", err) switch t := err.(type) { case biz.BizError: - m.MsgApp.CreateAndSend(la, ws.ErrSysMsg("文件上传失败", fmt.Sprintf("执行文件上传失败:\n<-e errCode: %d, errMsg: %s", t.Code(), t.Error()))) + m.MsgApp.CreateAndSend(la, msgdto.ErrSysMsg("文件上传失败", fmt.Sprintf("执行文件上传失败:\n<-e errCode: %d, errMsg: %s", t.Code(), t.Error()))) } } }() @@ -196,7 +196,7 @@ func (m *MachineFile) UploadFile(rc *req.Ctx) { rc.ReqParam = jsonx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", path, fileheader.Filename)) biz.ErrIsNilAppendErr(err, "创建文件失败: %s") // 保存消息并发送文件上传成功通知 - m.MsgApp.CreateAndSend(la, ws.SuccessSysMsg("文件上传成功", fmt.Sprintf("[%s]文件已成功上传至 %s[%s:%s]", fileheader.Filename, mi.Name, mi.Ip, path))) + m.MsgApp.CreateAndSend(la, msgdto.SuccessSysMsg("文件上传成功", fmt.Sprintf("[%s]文件已成功上传至 %s[%s:%s]", fileheader.Filename, mi.Name, mi.Ip, path))) } type FolderFile struct { @@ -262,7 +262,7 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) { logx.Errorf("文件上传失败: %s", err) switch t := err.(type) { case biz.BizError: - m.MsgApp.CreateAndSend(la, ws.ErrSysMsg("文件上传失败", fmt.Sprintf("执行文件上传失败:\n<-e errCode: %d, errMsg: %s", t.Code(), t.Error()))) + m.MsgApp.CreateAndSend(la, msgdto.ErrSysMsg("文件上传失败", fmt.Sprintf("执行文件上传失败:\n<-e errCode: %d, errMsg: %s", t.Code(), t.Error()))) } } }() @@ -286,7 +286,7 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) { // 等待所有协程执行完成 wg.Wait() // 保存消息并发送文件上传成功通知 - m.MsgApp.CreateAndSend(rc.LoginAccount, ws.SuccessSysMsg("文件上传成功", fmt.Sprintf("[%s]文件夹已成功上传至 %s[%s:%s]", folderName, mi.Name, mi.Ip, basePath))) + m.MsgApp.CreateAndSend(rc.LoginAccount, msgdto.SuccessSysMsg("文件上传成功", fmt.Sprintf("[%s]文件夹已成功上传至 %s[%s:%s]", folderName, mi.Name, mi.Ip, basePath))) } func (m *MachineFile) RemoveFile(rc *req.Ctx) { diff --git a/server/internal/msg/application/dto/sys_msg.go b/server/internal/msg/application/dto/sys_msg.go new file mode 100644 index 00000000..da94ee61 --- /dev/null +++ b/server/internal/msg/application/dto/sys_msg.go @@ -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)} +} diff --git a/server/internal/msg/application/msg.go b/server/internal/msg/application/msg.go index fd51ec9e..3328a0a3 100644 --- a/server/internal/msg/application/msg.go +++ b/server/internal/msg/application/msg.go @@ -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) } diff --git a/server/pkg/ws/msg.go b/server/pkg/ws/msg.go index e1abcd29..59a2fd08 100644 --- a/server/pkg/ws/msg.go +++ b/server/pkg/ws/msg.go @@ -16,31 +16,3 @@ type Msg struct { Type MsgType // 消息类型 } - -// ************** 系统消息 ************** - -const SuccessSysMsgType = 1 -const ErrorSysMsgType = 0 -const InfoSysMsgType = 2 - -// websocket消息 -type SysMsg struct { - Type int `json:"type"` // 消息类型 - Title string `json:"title"` // 消息标题 - SysMsg string `json:"msg"` // 消息内容 -} - -// 普通消息 -func NewSysMsg(title, msg string) *SysMsg { - return &SysMsg{Type: InfoSysMsgType, Title: title, SysMsg: msg} -} - -// 成功消息 -func SuccessSysMsg(title, msg string) *SysMsg { - return &SysMsg{Type: SuccessSysMsgType, Title: title, SysMsg: msg} -} - -// 错误消息 -func ErrSysMsg(title, msg string) *SysMsg { - return &SysMsg{Type: ErrorSysMsgType, Title: title, SysMsg: msg} -} diff --git a/server/pkg/ws/ws.go b/server/pkg/ws/ws.go index ddd298fd..3b9d04fe 100644 --- a/server/pkg/ws/ws.go +++ b/server/pkg/ws/ws.go @@ -32,7 +32,7 @@ func CloseClient(userid uint64) { Manager.CloseByUid(UserId(userid)) } -// 对指定用户发送消息 -func SendMsg(userId uint64, msg *SysMsg) { +// 对指定用户发送json类型消息 +func SendJsonMsg(userId uint64, msg any) { Manager.SendJsonMsg(UserId(userId), msg) }