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

@@ -35,7 +35,7 @@ type Account struct {
// 获取当前登录用户的菜单与权限码
func (a *Account) GetPermissions(rc *req.Ctx) {
account := rc.LoginAccount
account := rc.GetLoginAccount()
var resources vo.AccountResourceVOList
// 获取账号菜单资源
@@ -78,15 +78,13 @@ func (a *Account) ChangePassword(rc *req.Ctx) {
updateAccount := new(entity.Account)
updateAccount.Id = account.Id
updateAccount.Password = cryptox.PwdHash(originNewPwd)
biz.ErrIsNil(a.AccountApp.Update(updateAccount), "更新账号密码失败")
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, updateAccount), "更新账号密码失败")
// 赋值loginAccount 主要用于记录操作日志,因为操作日志保存请求上下文没有该信息不保存日志
if rc.LoginAccount == nil {
rc.LoginAccount = &model.LoginAccount{
Id: account.Id,
Username: account.Username,
}
}
contextx.WithLoginAccount(rc.MetaCtx, &model.LoginAccount{
Id: account.Id,
Username: account.Username,
})
}
// 获取个人账号信息
@@ -94,7 +92,7 @@ func (a *Account) AccountInfo(rc *req.Ctx) {
ap := new(vo.AccountPersonVO)
// 角色信息
roles := new([]vo.AccountRoleVO)
a.RoleApp.GetAccountRoles(rc.LoginAccount.Id, roles)
a.RoleApp.GetAccountRoles(rc.GetLoginAccount().Id, roles)
ap.Roles = *roles
rc.ResData = ap
@@ -104,7 +102,7 @@ func (a *Account) AccountInfo(rc *req.Ctx) {
func (a *Account) UpdateAccount(rc *req.Ctx) {
updateAccount := ginx.BindJsonAndCopyTo[*entity.Account](rc.GinCtx, new(form.AccountUpdateForm), new(entity.Account))
// 账号id为登录者账号
updateAccount.Id = rc.LoginAccount.Id
updateAccount.Id = rc.GetLoginAccount().Id
if updateAccount.Password != "" {
biz.IsTrue(utils.CheckAccountPasswordLever(updateAccount.Password), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
@@ -118,7 +116,7 @@ func (a *Account) UpdateAccount(rc *req.Ctx) {
// 禁止更新用户名,防止误传被更新
updateAccount.Username = ""
}
biz.ErrIsNil(a.AccountApp.Update(updateAccount))
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, updateAccount))
}
/** 后台账号操作 **/
@@ -139,10 +137,9 @@ func (a *Account) SaveAccount(rc *req.Ctx) {
form.Password = "*****"
rc.ReqParam = form
account.SetBaseInfo(rc.LoginAccount)
if account.Id == 0 {
biz.ErrIsNil(a.AccountApp.Create(account))
biz.ErrIsNil(a.AccountApp.Create(rc.MetaCtx, account))
} else {
if account.Password != "" {
biz.IsTrue(utils.CheckAccountPasswordLever(account.Password), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
@@ -150,7 +147,7 @@ func (a *Account) SaveAccount(rc *req.Ctx) {
}
// 更新操作不允许修改用户名、防止误传更新
account.Username = ""
biz.ErrIsNil(a.AccountApp.Update(account))
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, account))
}
}
@@ -161,7 +158,7 @@ func (a *Account) ChangeStatus(rc *req.Ctx) {
account.Id = uint64(ginx.PathParamInt(g, "id"))
account.Status = int8(ginx.PathParamInt(g, "status"))
rc.ReqParam = collx.Kvs("accountId", account.Id, "status", account.Status)
biz.ErrIsNil(a.AccountApp.Update(account))
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, account))
}
func (a *Account) DeleteAccount(rc *req.Ctx) {
@@ -172,7 +169,7 @@ func (a *Account) DeleteAccount(rc *req.Ctx) {
for _, v := range ids {
value, err := strconv.Atoi(v)
biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
biz.ErrIsNilAppendErr(a.AccountApp.Delete(uint64(value)), "删除失败:%s")
biz.ErrIsNilAppendErr(a.AccountApp.Delete(rc.MetaCtx, uint64(value)), "删除失败:%s")
}
}
@@ -207,7 +204,7 @@ func (a *Account) SaveRoles(rc *req.Ctx) {
return uint64(id)
})
a.RoleApp.SaveAccountRole(contextx.NewLoginAccount(rc.LoginAccount), form.Id, newIds)
a.RoleApp.SaveAccountRole(rc.MetaCtx, form.Id, newIds)
}
// 重置otp秘钥
@@ -216,5 +213,5 @@ func (a *Account) ResetOtpSecret(rc *req.Ctx) {
accountId := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
account.Id = accountId
rc.ReqParam = collx.Kvs("accountId", accountId)
biz.ErrIsNil(a.AccountApp.Update(account))
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, account))
}

View File

@@ -16,7 +16,7 @@ type Config struct {
func (c *Config) Configs(rc *req.Ctx) {
g := rc.GinCtx
condition := &entity.Config{Key: g.Query("key")}
condition.Permission = rc.LoginAccount.Username
condition.Permission = rc.GetLoginAccount().Username
res, err := c.ConfigApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Config))
biz.ErrIsNil(err)
rc.ResData = res
@@ -40,6 +40,5 @@ func (c *Config) SaveConfig(rc *req.Ctx) {
form := &form.ConfigForm{}
config := ginx.BindJsonAndCopyTo(rc.GinCtx, form, new(entity.Config))
rc.ReqParam = form
config.SetBaseInfo(rc.LoginAccount)
biz.ErrIsNil(c.ConfigApp.Save(config))
biz.ErrIsNil(c.ConfigApp.Save(rc.MetaCtx, config))
}

View File

@@ -39,19 +39,18 @@ func (r *Resource) SaveResource(rc *req.Ctx) {
bytes, _ := json.Marshal(form.Meta)
entity.Meta = string(bytes)
entity.SetBaseInfo(rc.LoginAccount)
biz.ErrIsNil(r.ResourceApp.Save(entity))
biz.ErrIsNil(r.ResourceApp.Save(rc.MetaCtx, entity))
}
func (r *Resource) DelResource(rc *req.Ctx) {
biz.ErrIsNil(r.ResourceApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id"))))
biz.ErrIsNil(r.ResourceApp.Delete(rc.MetaCtx, uint64(ginx.PathParamInt(rc.GinCtx, "id"))))
}
func (r *Resource) ChangeStatus(rc *req.Ctx) {
rid := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
status := int8(ginx.PathParamInt(rc.GinCtx, "status"))
rc.ReqParam = collx.Kvs("id", rid, "status", status)
biz.ErrIsNil(r.ResourceApp.ChangeStatus(rid, status))
biz.ErrIsNil(r.ResourceApp.ChangeStatus(rc.MetaCtx, rid, status))
}
func (r *Resource) Sort(rc *req.Ctx) {
@@ -62,6 +61,6 @@ func (r *Resource) Sort(rc *req.Ctx) {
for _, v := range rs {
sortE := &entity.Resource{Pid: v.Pid, Weight: v.Weight}
sortE.Id = uint64(v.Id)
r.ResourceApp.Sort(sortE)
r.ResourceApp.Sort(rc.MetaCtx, sortE)
}
}

View File

@@ -6,7 +6,6 @@ import (
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/contextx"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
@@ -32,9 +31,8 @@ func (r *Role) SaveRole(rc *req.Ctx) {
form := &form.RoleForm{}
role := ginx.BindJsonAndCopyTo(rc.GinCtx, form, new(entity.Role))
rc.ReqParam = form
role.SetBaseInfo(rc.LoginAccount)
r.RoleApp.SaveRole(role)
r.RoleApp.SaveRole(rc.MetaCtx, role)
}
// 删除角色及其资源关联关系
@@ -46,7 +44,7 @@ func (r *Role) DelRole(rc *req.Ctx) {
for _, v := range ids {
value, err := strconv.Atoi(v)
biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
r.RoleApp.DeleteRole(uint64(value))
r.RoleApp.DeleteRole(rc.MetaCtx, uint64(value))
}
}
@@ -77,5 +75,5 @@ func (r *Role) SaveResource(rc *req.Ctx) {
return uint64(id)
})
r.RoleApp.SaveRoleResource(contextx.NewLoginAccount(rc.LoginAccount), form.Id, newIds)
r.RoleApp.SaveRoleResource(rc.MetaCtx, form.Id, newIds)
}

View File

@@ -38,8 +38,6 @@ func (s *System) ConnectWs(g *gin.Context) {
biz.ErrIsNil(err, "sys websocket没有权限连接")
// 登录账号信息
la := rc.LoginAccount
if la != nil {
ws.AddClient(ws.UserId(la.Id), clientId, wsConn)
}
la := rc.GetLoginAccount()
ws.AddClient(ws.UserId(la.Id), clientId, wsConn)
}