refactor: 系统消息调整

This commit is contained in:
meilin.huang
2023-10-10 17:39:46 +08:00
parent 4da0d1abaa
commit 22e218fc5f
7 changed files with 66 additions and 46 deletions

View File

@@ -29,7 +29,7 @@ enum ResultEnum {
} }
const baseUrl: string = config.baseApiUrl; 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 // create an axios instance
const service = Axios.create({ const service = Axios.create({
baseURL: baseUrl, // url = base url + request url baseURL: baseUrl, // url = base url + request url
timeout: 20000, // request timeout timeout: 60000, // request timeout
}); });
// request interceptor // request interceptor

View File

@@ -9,6 +9,7 @@ import (
"mayfly-go/internal/db/application" "mayfly-go/internal/db/application"
"mayfly-go/internal/db/domain/entity" "mayfly-go/internal/db/domain/entity"
msgapp "mayfly-go/internal/msg/application" msgapp "mayfly-go/internal/msg/application"
msgdto "mayfly-go/internal/msg/application/dto"
tagapp "mayfly-go/internal/tag/application" tagapp "mayfly-go/internal/tag/application"
"mayfly-go/pkg/biz" "mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx" "mayfly-go/pkg/ginx"
@@ -16,7 +17,6 @@ import (
"mayfly-go/pkg/model" "mayfly-go/pkg/model"
"mayfly-go/pkg/req" "mayfly-go/pkg/req"
"mayfly-go/pkg/utils/stringx" "mayfly-go/pkg/utils/stringx"
"mayfly-go/pkg/ws"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@@ -182,7 +182,7 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
errInfo = t.Error() errInfo = t.Error()
} }
if len(errInfo) > 0 { 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 { 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 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 { if len(msg) > 0 {
msg = "数据库导出失败: " + msg msg = "数据库导出失败: " + msg
writer.WriteString(msg) writer.WriteString(msg)
d.MsgApp.CreateAndSend(rc.LoginAccount, ws.ErrSysMsg("数据库导出失败", msg)) d.MsgApp.CreateAndSend(rc.LoginAccount, msgdto.ErrSysMsg("数据库导出失败", msg))
} }
writer.Close() writer.Close()
}() }()

View File

@@ -10,6 +10,7 @@ import (
"mayfly-go/internal/machine/domain/entity" "mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/infrastructure/machine" "mayfly-go/internal/machine/infrastructure/machine"
msgapp "mayfly-go/internal/msg/application" msgapp "mayfly-go/internal/msg/application"
msgdto "mayfly-go/internal/msg/application/dto"
"mayfly-go/pkg/biz" "mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx" "mayfly-go/pkg/ginx"
"mayfly-go/pkg/logx" "mayfly-go/pkg/logx"
@@ -17,7 +18,6 @@ import (
"mayfly-go/pkg/utils/collx" "mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/jsonx" "mayfly-go/pkg/utils/jsonx"
"mayfly-go/pkg/utils/timex" "mayfly-go/pkg/utils/timex"
"mayfly-go/pkg/ws"
"mime/multipart" "mime/multipart"
"path/filepath" "path/filepath"
"sort" "sort"
@@ -187,7 +187,7 @@ func (m *MachineFile) UploadFile(rc *req.Ctx) {
logx.Errorf("文件上传失败: %s", err) logx.Errorf("文件上传失败: %s", err)
switch t := err.(type) { switch t := err.(type) {
case biz.BizError: 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)) rc.ReqParam = jsonx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", path, fileheader.Filename))
biz.ErrIsNilAppendErr(err, "创建文件失败: %s") 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 { type FolderFile struct {
@@ -262,7 +262,7 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) {
logx.Errorf("文件上传失败: %s", err) logx.Errorf("文件上传失败: %s", err)
switch t := err.(type) { switch t := err.(type) {
case biz.BizError: 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() 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) { func (m *MachineFile) RemoveFile(rc *req.Ctx) {

View 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)}
}

View File

@@ -1,6 +1,7 @@
package application package application
import ( import (
"mayfly-go/internal/msg/application/dto"
"mayfly-go/internal/msg/domain/entity" "mayfly-go/internal/msg/domain/entity"
"mayfly-go/internal/msg/domain/repository" "mayfly-go/internal/msg/domain/repository"
"mayfly-go/pkg/model" "mayfly-go/pkg/model"
@@ -14,7 +15,7 @@ type Msg interface {
Create(msg *entity.Msg) Create(msg *entity.Msg)
// 创建消息并通过ws发送 // 创建消息并通过ws发送
CreateAndSend(la *model.LoginAccount, msg *ws.SysMsg) CreateAndSend(la *model.LoginAccount, msg *dto.SysMsg)
} }
func newMsgApp(msgRepo repository.Msg) Msg { func newMsgApp(msgRepo repository.Msg) Msg {
@@ -35,9 +36,9 @@ func (a *msgAppImpl) Create(msg *entity.Msg) {
a.msgRepo.Insert(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() 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) a.msgRepo.Insert(msg)
ws.SendMsg(la.Id, wmsg) ws.SendJsonMsg(la.Id, wmsg)
} }

View File

@@ -16,31 +16,3 @@ type Msg struct {
Type MsgType // 消息类型 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}
}

View File

@@ -32,7 +32,7 @@ func CloseClient(userid uint64) {
Manager.CloseByUid(UserId(userid)) Manager.CloseByUid(UserId(userid))
} }
// 对指定用户发送消息 // 对指定用户发送json类型消息
func SendMsg(userId uint64, msg *SysMsg) { func SendJsonMsg(userId uint64, msg any) {
Manager.SendJsonMsg(UserId(userId), msg) Manager.SendJsonMsg(UserId(userId), msg)
} }