feat: 机器列表新增运行状态 & refactor: 登录账号信息存储与context

This commit is contained in:
meilin.huang
2023-11-07 21:05:21 +08:00
parent d9adf0fd25
commit eddda41291
74 changed files with 915 additions and 652 deletions

View File

@@ -1,6 +1,7 @@
package application
import (
"context"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/pkg/base"
@@ -17,11 +18,11 @@ type Account interface {
GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
Create(account *entity.Account) error
Create(ctx context.Context, account *entity.Account) error
Update(account *entity.Account) error
Update(ctx context.Context, account *entity.Account) error
Delete(id uint64) error
Delete(ctx context.Context, id uint64) error
}
func newAccountApp(accountRepo repository.Account) Account {
@@ -36,17 +37,17 @@ func (a *accountAppImpl) GetPageList(condition *entity.Account, pageParam *model
return a.GetRepo().GetPageList(condition, pageParam, toEntity)
}
func (a *accountAppImpl) Create(account *entity.Account) error {
func (a *accountAppImpl) Create(ctx context.Context, account *entity.Account) error {
if a.GetBy(&entity.Account{Username: account.Username}) == nil {
return errorx.NewBiz("该账号用户名已存在")
}
// 默认密码为账号用户名
account.Password = cryptox.PwdHash(account.Username)
account.Status = entity.AccountEnableStatus
return a.Insert(account)
return a.Insert(ctx, account)
}
func (a *accountAppImpl) Update(account *entity.Account) error {
func (a *accountAppImpl) Update(ctx context.Context, account *entity.Account) error {
if account.Username != "" {
unAcc := &entity.Account{Username: account.Username}
err := a.GetBy(unAcc)
@@ -55,14 +56,14 @@ func (a *accountAppImpl) Update(account *entity.Account) error {
}
}
return a.UpdateById(account)
return a.UpdateById(ctx, account)
}
func (a *accountAppImpl) Delete(id uint64) error {
func (a *accountAppImpl) Delete(ctx context.Context, id uint64) error {
return gormx.Tx(
func(db *gorm.DB) error {
// 删除account信息
return a.DeleteByIdWithDb(db, id)
return a.DeleteByIdWithDb(ctx, db, id)
},
func(db *gorm.DB) error {
// 删除账号关联的角色信息

View File

@@ -1,6 +1,7 @@
package application
import (
"context"
"encoding/json"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
@@ -20,7 +21,7 @@ type Config interface {
GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
Save(config *entity.Config) error
Save(ctx context.Context, config *entity.Config) error
// GetConfig 获取指定key的配置信息, 不会返回nil, 若不存在则值都默认值即空字符串
GetConfig(key string) *entity.Config
@@ -41,9 +42,9 @@ func (a *configAppImpl) GetPageList(condition *entity.Config, pageParam *model.P
return a.GetRepo().GetPageList(condition, pageParam, toEntity)
}
func (a *configAppImpl) Save(config *entity.Config) error {
func (a *configAppImpl) Save(ctx context.Context, config *entity.Config) error {
if config.Id == 0 {
if err := a.Insert(config); err != nil {
if err := a.Insert(ctx, config); err != nil {
return err
}
} else {
@@ -52,7 +53,7 @@ func (a *configAppImpl) Save(config *entity.Config) error {
return errorx.NewBiz("您无权修改该配置")
}
if err := a.UpdateById(config); err != nil {
if err := a.UpdateById(ctx, config); err != nil {
return err
}
}

View File

@@ -1,6 +1,7 @@
package application
import (
"context"
"mayfly-go/internal/common/consts"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
@@ -15,13 +16,13 @@ import (
type Resource interface {
base.App[*entity.Resource]
Save(entity *entity.Resource) error
Save(ctx context.Context, entity *entity.Resource) error
Delete(id uint64) error
Delete(ctx context.Context, id uint64) error
ChangeStatus(resourceId uint64, status int8) error
ChangeStatus(ctx context.Context, resourceId uint64, status int8) error
Sort(re *entity.Resource) error
Sort(ctx context.Context, re *entity.Resource) error
GetAccountResources(accountId uint64, toEntity any) error
}
@@ -36,7 +37,7 @@ type resourceAppImpl struct {
base.AppImpl[*entity.Resource, repository.Resource]
}
func (r *resourceAppImpl) Save(resource *entity.Resource) error {
func (r *resourceAppImpl) Save(ctx context.Context, resource *entity.Resource) error {
// 更新操作
if resource.Id != 0 {
if resource.Code != "" {
@@ -51,7 +52,7 @@ func (r *resourceAppImpl) Save(resource *entity.Resource) error {
}
}
}
return gormx.UpdateById(resource)
return r.UpdateById(ctx, resource)
}
// 生成随机八位唯一标识符
@@ -73,10 +74,10 @@ func (r *resourceAppImpl) Save(resource *entity.Resource) error {
return err
}
resource.Weight = int(time.Now().Unix())
return gormx.Insert(resource)
return r.Insert(ctx, resource)
}
func (r *resourceAppImpl) ChangeStatus(resourceId uint64, status int8) error {
func (r *resourceAppImpl) ChangeStatus(ctx context.Context, resourceId uint64, status int8) error {
resource, err := r.GetById(new(entity.Resource), resourceId)
if err != nil {
return errorx.NewBiz("资源不存在")
@@ -85,7 +86,7 @@ func (r *resourceAppImpl) ChangeStatus(resourceId uint64, status int8) error {
return r.GetRepo().UpdateByUiPathLike(resource)
}
func (r *resourceAppImpl) Sort(sortResource *entity.Resource) error {
func (r *resourceAppImpl) Sort(ctx context.Context, sortResource *entity.Resource) error {
resource, err := r.GetById(new(entity.Resource), sortResource.Id)
if err != nil {
return errorx.NewBiz("资源不存在")
@@ -95,7 +96,7 @@ func (r *resourceAppImpl) Sort(sortResource *entity.Resource) error {
if sortResource.Pid == resource.Pid {
saveE := &entity.Resource{Weight: sortResource.Weight}
saveE.Id = sortResource.Id
return r.Save(saveE)
return r.Save(ctx, saveE)
}
// 若资源原本唯一标识路径为xxxx/yyyy/zzzz/,则获取其父节点路径标识 xxxx/yyyy/ 与自身节点标识 zzzz/
@@ -131,7 +132,7 @@ func (r *resourceAppImpl) Sort(sortResource *entity.Resource) error {
} else {
updateUiPath.UiPath = strings.ReplaceAll(v.UiPath, parentResourceUiPath, newParentResourceUiPath)
}
r.Save(updateUiPath)
r.Save(ctx, updateUiPath)
}
// 更新零值使用map因为pid=0表示根节点
@@ -155,7 +156,7 @@ func (r *resourceAppImpl) checkCode(code string) error {
return nil
}
func (r *resourceAppImpl) Delete(id uint64) error {
func (r *resourceAppImpl) Delete(ctx context.Context, id uint64) error {
resource, err := r.GetById(new(entity.Resource), id)
if err != nil {
return errorx.NewBiz("资源不存在")
@@ -164,7 +165,7 @@ func (r *resourceAppImpl) Delete(id uint64) error {
// 删除当前节点及其所有子节点
children := r.GetRepo().GetChildren(resource.UiPath)
for _, v := range children {
r.GetRepo().DeleteById(v.Id)
r.GetRepo().DeleteById(ctx, v.Id)
// 删除角色关联的资源信息
gormx.DeleteBy(&entity.RoleResource{ResourceId: v.Id})
}

View File

@@ -17,9 +17,9 @@ import (
type Role interface {
GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
SaveRole(role *entity.Role) error
SaveRole(ctx context.Context, role *entity.Role) error
DeleteRole(id uint64) error
DeleteRole(ctx context.Context, id uint64) error
GetRoleResourceIds(roleId uint64) []uint64
@@ -29,7 +29,7 @@ type Role interface {
SaveRoleResource(ctx context.Context, roleId uint64, resourceIds []uint64)
// 删除角色资源关联记录
DeleteRoleResource(roleId uint64, resourceId uint64)
DeleteRoleResource(ctx context.Context, roleId uint64, resourceId uint64)
// 获取账号角色id列表
GetAccountRoleIds(accountId uint64) []uint64
@@ -37,7 +37,7 @@ type Role interface {
// 保存账号角色关联信息
SaveAccountRole(ctx context.Context, accountId uint64, roleIds []uint64)
DeleteAccountRole(accountId, roleId uint64)
DeleteAccountRole(ctx context.Context, accountId, roleId uint64)
GetAccountRoles(accountId uint64, toEntity any)
}
@@ -56,7 +56,7 @@ func (m *roleAppImpl) GetPageList(condition *entity.Role, pageParam *model.PageP
return m.roleRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
func (m *roleAppImpl) SaveRole(role *entity.Role) error {
func (m *roleAppImpl) SaveRole(ctx context.Context, role *entity.Role) error {
role.Code = strings.ToUpper(role.Code)
if role.Id != 0 {
// code不可更改防止误传
@@ -68,11 +68,11 @@ func (m *roleAppImpl) SaveRole(role *entity.Role) error {
return gormx.Insert(role)
}
func (m *roleAppImpl) DeleteRole(id uint64) error {
func (m *roleAppImpl) DeleteRole(ctx context.Context, id uint64) error {
// 删除角色与资源的关联关系
return gormx.Tx(
func(db *gorm.DB) error {
return m.roleRepo.DeleteByIdWithDb(db, id)
return m.roleRepo.DeleteByIdWithDb(ctx, db, id)
},
func(db *gorm.DB) error {
return gormx.DeleteByWithDb(db, &entity.RoleResource{RoleId: id})
@@ -110,11 +110,11 @@ func (m *roleAppImpl) SaveRoleResource(ctx context.Context, roleId uint64, resou
m.roleRepo.SaveRoleResource(addVals)
for _, v := range delIds {
m.DeleteRoleResource(roleId, v)
m.DeleteRoleResource(ctx, roleId, v)
}
}
func (m *roleAppImpl) DeleteRoleResource(roleId uint64, resourceId uint64) {
func (m *roleAppImpl) DeleteRoleResource(ctx context.Context, roleId uint64, resourceId uint64) {
m.roleRepo.DeleteRoleResource(roleId, resourceId)
}
@@ -140,11 +140,11 @@ func (m *roleAppImpl) SaveAccountRole(ctx context.Context, accountId uint64, rol
m.roleRepo.SaveAccountRole(rr)
}
for _, v := range delIds {
m.DeleteAccountRole(accountId, v)
m.DeleteAccountRole(ctx, accountId, v)
}
}
func (m *roleAppImpl) DeleteAccountRole(accountId, roleId uint64) {
func (m *roleAppImpl) DeleteAccountRole(ctx context.Context, accountId, roleId uint64) {
m.roleRepo.DeleteAccountRole(accountId, roleId)
}

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/pkg/contextx"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
@@ -34,7 +35,7 @@ func (m *syslogAppImpl) GetPageList(condition *entity.SysLogQuery, pageParam *mo
}
func (m *syslogAppImpl) SaveFromReq(req *req.Ctx) {
lg := req.LoginAccount
lg := contextx.GetLoginAccount(req.MetaCtx)
if lg == nil {
lg = &model.LoginAccount{Id: 0, Username: "-"}
}
@@ -74,5 +75,5 @@ func (m *syslogAppImpl) SaveFromReq(req *req.Ctx) {
syslog.Type = entity.SyslogTypeNorman
}
m.syslogRepo.Insert(syslog)
m.syslogRepo.Insert(req.MetaCtx, syslog)
}