add action,login log
This commit is contained in:
@@ -2,9 +2,17 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
admin "lone-services/rpc/admin/pb"
|
||||
"lone-services/services/admin/internal/dao"
|
||||
"lone-services/services/admin/internal/model"
|
||||
"lone-services/services/admin/internal/svc"
|
||||
"lone-services/services/admin/validator"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -12,6 +20,7 @@ type AuthorityEditLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
BaseLogic
|
||||
}
|
||||
|
||||
func NewAuthorityEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthorityEditLogic {
|
||||
@@ -23,7 +32,305 @@ func NewAuthorityEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Aut
|
||||
}
|
||||
|
||||
func (l *AuthorityEditLogic) AuthorityEdit(in *admin.AuthorityEditRequest) (*admin.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
var v validator.AuthorityEditValidator
|
||||
if fail := l.checkParams(in, &v); fail != nil {
|
||||
return fail, nil
|
||||
}
|
||||
adminInfo := utils.GetUserFromCtx(l.ctx)
|
||||
if adminInfo.ID < utils.NumberOne {
|
||||
return l.fail(utils.ErrorNoLoginInfo)
|
||||
}
|
||||
edit, dErr := l.getData(adminInfo, in)
|
||||
if dErr != utils.Ok {
|
||||
return l.fail(dErr)
|
||||
}
|
||||
modelObj := model.AuthorityModel{}.Init()
|
||||
var err error
|
||||
jErr, jsonData := l.getJsonData(in)
|
||||
if jErr != utils.Ok {
|
||||
return l.fail(jErr)
|
||||
}
|
||||
id := in.Id
|
||||
var row int64
|
||||
var action utils.ActionAdd
|
||||
if id > utils.NumberZero {
|
||||
w := modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(in.Id, utils.NumberTen)}}
|
||||
info := dao.Authority{}
|
||||
err = modelObj.GetOne(w, &info)
|
||||
if err != nil {
|
||||
l.Logger.Error(err)
|
||||
return l.fail(utils.Fail)
|
||||
}
|
||||
if info.Id < utils.NumberOne {
|
||||
return l.fail(utils.ErrorNotFund)
|
||||
}
|
||||
action.Type = utils.LogActionTypeEdit
|
||||
action.OldContent = info
|
||||
edit.Id = info.Id
|
||||
edit.Type = info.Type
|
||||
edit.Sort = info.Sort
|
||||
edit.ServiceId = info.ServiceId
|
||||
edit.Status = info.Status
|
||||
w = modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(info.Id, utils.NumberTen)}}
|
||||
if edit.Type == dao.TypeData {
|
||||
edit.DataIds = info.DataIds
|
||||
ids := strings.Split(info.DataIds, ",")
|
||||
eId, _ := strconv.Atoi(ids[utils.NumberZero])
|
||||
eId1, _ := strconv.Atoi(ids[utils.NumberOne])
|
||||
eId2, _ := strconv.Atoi(ids[utils.NumberTwo])
|
||||
upW := modelbase.Params{Eq: map[string]string{"id": strconv.Itoa(eId)}}
|
||||
l.setJsonData(&edit, int64(eId), "base", jsonData, in)
|
||||
_, err = modelObj.Edit(upW, edit)
|
||||
l.setJsonData(&edit, int64(eId1), "department", jsonData, in)
|
||||
upW.Eq["id"] = strconv.Itoa(eId1)
|
||||
_, err = modelObj.Edit(upW, edit)
|
||||
l.setJsonData(&edit, int64(eId2), "oneself", jsonData, in)
|
||||
upW.Eq["id"] = strconv.Itoa(eId2)
|
||||
row, err = modelObj.Edit(upW, edit)
|
||||
|
||||
return &admin.Response{}, nil
|
||||
} else {
|
||||
row, err = modelObj.Edit(w, edit)
|
||||
}
|
||||
} else {
|
||||
if edit.Type == dao.TypeData {
|
||||
var ids []string
|
||||
l.setJsonData(&edit, utils.NumberZero, "base", jsonData, in)
|
||||
err = modelObj.Create(&edit)
|
||||
ids = append(ids, strconv.FormatInt(edit.Id, utils.NumberTen))
|
||||
l.setJsonData(&edit, utils.NumberZero, "department", jsonData, in)
|
||||
err = modelObj.Create(&edit)
|
||||
ids = append(ids, strconv.FormatInt(edit.Id, utils.NumberTen))
|
||||
l.setJsonData(&edit, utils.NumberZero, "oneself", jsonData, in)
|
||||
err = modelObj.Create(&edit)
|
||||
row = edit.Id
|
||||
ids = append(ids, strconv.FormatInt(edit.Id, utils.NumberTen))
|
||||
|
||||
jsonIds := dao.AuthorityUpJsonIds{
|
||||
DataIds: strings.Join(ids, ","),
|
||||
}
|
||||
jsonW := modelbase.Params{In: map[string][]string{"id": ids}}
|
||||
modelObj.Edit(jsonW, &jsonIds)
|
||||
|
||||
} else {
|
||||
err = modelObj.Create(&edit)
|
||||
id = edit.Id
|
||||
row = id
|
||||
}
|
||||
action.Type = utils.LogActionTypeAdd
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
l.Logger.Error(err)
|
||||
return l.fail(utils.Fail)
|
||||
}
|
||||
|
||||
if row > utils.NumberZero {
|
||||
action.NewContent = edit
|
||||
action.ModuleName = utils.LogActionModuleAuthority
|
||||
utils.SetActionLog(adminInfo, action)
|
||||
}
|
||||
|
||||
return l.ok(id)
|
||||
}
|
||||
|
||||
func (l *AuthorityEditLogic) getData(adminInfo utils.UserInfo, in *admin.AuthorityEditRequest) (dao.Authority, utils.Status) {
|
||||
edit := dao.Authority{
|
||||
Name: in.Name,
|
||||
Type: uint8(in.Type),
|
||||
Sort: l.getSort(in.ParentId, in.ServiceId),
|
||||
Description: in.Description,
|
||||
Icon: in.Icon,
|
||||
Identification: in.Identification,
|
||||
ServiceId: in.ServiceId,
|
||||
ParentId: in.ParentId,
|
||||
Api: dao.NoData,
|
||||
ViewPath: in.ViewPath,
|
||||
Path: in.Path,
|
||||
IsShow: uint8(in.IsShow),
|
||||
AdminId: adminInfo.ID,
|
||||
AdminName: adminInfo.Name,
|
||||
}
|
||||
|
||||
edit.Path = "/" + strings.TrimLeft(edit.Path, "/")
|
||||
|
||||
if edit.ParentId < utils.NumberOne {
|
||||
edit.ParentId = utils.NumberZero
|
||||
}
|
||||
|
||||
check := l.checkName(in.Id, map[string]string{
|
||||
"service_id": strconv.FormatInt(edit.ServiceId, utils.NumberTen),
|
||||
"name": edit.Name,
|
||||
})
|
||||
if !check {
|
||||
return edit, utils.SetError(utils.ErrorMissingParams, "权限名称已存在")
|
||||
}
|
||||
|
||||
check = l.check(edit.Id, map[string]string{
|
||||
"service_id": strconv.FormatInt(edit.ServiceId, utils.NumberTen),
|
||||
"identification": edit.Identification,
|
||||
})
|
||||
if !check {
|
||||
return edit, utils.SetError(utils.ErrorMissingParams, "权限标识已存在")
|
||||
}
|
||||
|
||||
if edit.Type == dao.TypeWeb {
|
||||
if len(edit.ViewPath) < utils.NumberOne {
|
||||
return edit, utils.SetError(utils.ErrorMissingParams, "组件路径不能为空")
|
||||
}
|
||||
check := l.check(in.Id, map[string]string{"path": edit.Path, "service_id": strconv.FormatInt(edit.ServiceId, utils.NumberTen)})
|
||||
if !check {
|
||||
return edit, utils.SetError(utils.ErrorMissingParams, "PATH已存在")
|
||||
}
|
||||
} else if edit.Type == dao.TypeButton {
|
||||
edit.Icon = utils.StringEmpty
|
||||
if len(edit.Api) < utils.NumberOne {
|
||||
return edit, utils.SetError(utils.ErrorMissingParams, "API不能为空")
|
||||
}
|
||||
edit.Api = in.Api
|
||||
edit.Path = dao.NoData
|
||||
edit.ViewPath = dao.NoData
|
||||
} else if edit.Type == dao.TypeDir {
|
||||
edit.ViewPath = dao.NoData
|
||||
check := l.check(in.Id, map[string]string{"path": edit.Path, "service_id": strconv.FormatInt(edit.ServiceId, utils.NumberTen)})
|
||||
if !check {
|
||||
return edit, utils.SetError(utils.ErrorMissingParams, "PATH已存在")
|
||||
}
|
||||
} else if edit.Type == dao.TypeData {
|
||||
if len(edit.Api) < utils.NumberOne {
|
||||
return edit, utils.SetError(utils.ErrorMissingParams, "API不能为空")
|
||||
}
|
||||
edit.Icon = utils.StringEmpty
|
||||
edit.Api = in.Api
|
||||
edit.Path = dao.NoData
|
||||
edit.ViewPath = dao.NoData
|
||||
}
|
||||
|
||||
if edit.ParentId == utils.NumberZero {
|
||||
edit.ParentIds = utils.StringZero
|
||||
} else {
|
||||
w := modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(edit.ParentId, utils.NumberTen)}}
|
||||
info := dao.Authority{}
|
||||
err := model.AuthorityModel{}.Init().GetOne(w, &info)
|
||||
if err != nil {
|
||||
return edit, utils.Ok
|
||||
}
|
||||
if info.Id < utils.NumberOne {
|
||||
return edit, utils.ErrorNotFund
|
||||
}
|
||||
edit.ParentIds = info.ParentIds + "," + strconv.FormatInt(edit.ParentId, utils.NumberTen)
|
||||
}
|
||||
|
||||
idsArr := strings.SplitAfter(edit.ParentIds, ",")
|
||||
edit.Level = uint8(len(idsArr))
|
||||
|
||||
return edit, utils.Ok
|
||||
}
|
||||
|
||||
func (l *AuthorityEditLogic) check(id int64, check map[string]string) bool {
|
||||
w := modelbase.Params{Eq: check}
|
||||
info := dao.AuthorityCheck{}
|
||||
_ = model.AuthorityModel{}.Init().GetOne(w, &info)
|
||||
if id > utils.NumberZero && info.Id == id {
|
||||
return true
|
||||
}
|
||||
if info.Id > utils.NumberZero {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (l *AuthorityEditLogic) checkName(id int64, check map[string]string) bool {
|
||||
w := modelbase.Params{Eq: check}
|
||||
info := dao.AuthorityCheck{}
|
||||
_ = model.AuthorityModel{}.Init().GetOne(w, &info)
|
||||
if id > utils.NumberZero && info.Id == id {
|
||||
return true
|
||||
}
|
||||
if info.Id > utils.NumberZero {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (l *AuthorityEditLogic) getSort(pid, serviceId int64) uint8 {
|
||||
if pid < utils.NumberZero {
|
||||
pid = utils.NumberZero
|
||||
}
|
||||
w := modelbase.Params{
|
||||
Eq: map[string]string{
|
||||
"parent_id": strconv.FormatInt(pid, utils.NumberTen),
|
||||
"project_id": strconv.FormatInt(serviceId, utils.NumberTen)},
|
||||
Order: "sort DESC",
|
||||
}
|
||||
info := dao.Authority{}
|
||||
err := model.AuthorityModel{}.Init().GetOne(w, &info)
|
||||
if err != nil || info.Id < utils.NumberOne {
|
||||
return utils.NumberOne
|
||||
}
|
||||
return info.Sort + utils.NumberOne
|
||||
}
|
||||
|
||||
func (l *AuthorityEditLogic) setJsonData(edit *dao.Authority, id int64, name string, data map[string]string, in *admin.AuthorityEditRequest) {
|
||||
edit.Id = id
|
||||
if name == "base" {
|
||||
edit.Name = in.Name + "-全部"
|
||||
edit.JsonDataBase = data["base"]
|
||||
edit.JsonData = data["all"]
|
||||
edit.JsonTreeData = data["tree_all"]
|
||||
} else if name == "department" {
|
||||
edit.JsonData = data["department"]
|
||||
edit.Name = in.Name + "-部门"
|
||||
edit.JsonTreeData = data["tree_department"]
|
||||
edit.Identification = in.Identification + ":department"
|
||||
} else if name == "oneself" {
|
||||
edit.JsonData = data["oneself"]
|
||||
edit.Name = in.Name + "-个人"
|
||||
edit.JsonTreeData = data["tree_oneself"]
|
||||
edit.Identification = in.Identification + ":oneself"
|
||||
}
|
||||
if id == utils.NumberZero {
|
||||
edit.Sort += utils.NumberOne
|
||||
} else {
|
||||
info := dao.AuthoritySort{}
|
||||
w := modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(id, utils.NumberTen)}}
|
||||
_ = model.AuthorityModel{}.Init().GetOne(w, &info)
|
||||
edit.Sort = info.Sort
|
||||
}
|
||||
}
|
||||
func (l *AuthorityEditLogic) getJsonData(in *admin.AuthorityEditRequest) (utils.Status, map[string]string) {
|
||||
data := map[string]string{}
|
||||
if in.Type != dao.TypeData {
|
||||
return utils.Ok, data
|
||||
}
|
||||
var check map[string]interface{}
|
||||
// 解析JSON字符串到map
|
||||
err := jsoniter.Unmarshal([]byte(in.DataBaseStr), &check)
|
||||
if err != nil {
|
||||
return utils.ErrorJsonDataError, data
|
||||
}
|
||||
// 解析JSON字符串到map
|
||||
err = jsoniter.Unmarshal([]byte(in.DataAll), &check)
|
||||
if err != nil {
|
||||
return utils.ErrorJsonDataError, data
|
||||
}
|
||||
// 解析JSON字符串到map
|
||||
err = jsoniter.Unmarshal([]byte(in.DataDepartment), &check)
|
||||
if err != nil {
|
||||
return utils.ErrorJsonDataError, data
|
||||
}
|
||||
// 解析JSON字符串到map
|
||||
err = jsoniter.Unmarshal([]byte(in.DataOneself), &check)
|
||||
if err != nil {
|
||||
return utils.ErrorJsonDataError, data
|
||||
}
|
||||
data["base"] = in.DataBaseStr
|
||||
data["all"] = in.DataAll
|
||||
data["department"] = in.DataDepartment
|
||||
data["oneself"] = in.DataOneself
|
||||
data["tree_all"] = in.DataTreeAll
|
||||
data["tree_department"] = in.DataTreeDepartment
|
||||
data["tree_oneself"] = in.DataTreeOneself
|
||||
return utils.Ok, data
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user