refactor: 组件升级、代码优化

This commit is contained in:
meilin.huang
2023-05-24 12:32:17 +08:00
parent 4fa52412c1
commit 9900b236ef
48 changed files with 777 additions and 345 deletions

View File

@@ -0,0 +1,78 @@
package application
import (
"encoding/json"
"fmt"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils"
"time"
)
type Syslog interface {
GetPageList(condition *entity.Syslog, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult
// 从请求上下文的参数保存系统日志
SaveFromReq(req *req.Ctx)
}
func newSyslogApp(syslogRepo repository.Syslog) Syslog {
return &syslogAppImpl{
syslogRepo: syslogRepo,
}
}
type syslogAppImpl struct {
syslogRepo repository.Syslog
}
func (m *syslogAppImpl) GetPageList(condition *entity.Syslog, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return m.syslogRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
func (m *syslogAppImpl) SaveFromReq(req *req.Ctx) {
lg := req.LoginAccount
if lg == nil {
return
}
syslog := new(entity.Syslog)
syslog.CreateTime = time.Now()
syslog.Creator = lg.Username
syslog.CreatorId = lg.Id
syslog.Description = req.LogInfo.Description
if req.LogInfo.LogResp {
respB, _ := json.Marshal(req.ResData)
syslog.Resp = string(respB)
}
reqParam := req.ReqParam
if !utils.IsBlank(reqParam) {
// 如果是字符串类型则不使用json序列化
if reqStr, ok := reqParam.(string); ok {
syslog.ReqParam = reqStr
} else {
reqB, _ := json.Marshal(reqParam)
syslog.ReqParam = string(reqB)
}
}
if err := req.Err; err != nil {
syslog.Type = entity.SyslogTypeError
var errMsg string
switch t := err.(type) {
case *biz.BizError:
errMsg = fmt.Sprintf("errCode: %d, errMsg: %s", t.Code(), t.Error())
case error:
errMsg = t.Error()
}
syslog.Resp = errMsg
} else {
syslog.Type = entity.SyslogTypeNorman
}
m.syslogRepo.Insert(syslog)
}