326 lines
9.5 KiB
Go
326 lines
9.5 KiB
Go
package logic
|
|
|
|
import (
|
|
"admin/internal/dao"
|
|
"admin/internal/model"
|
|
"admin/validator"
|
|
"context"
|
|
"encoding/json"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"admin/admin"
|
|
"admin/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"pkg.local/modelbase"
|
|
"pkg.local/utils"
|
|
)
|
|
|
|
type AuthorityEditLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewAuthorityEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthorityEditLogic {
|
|
return &AuthorityEditLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *AuthorityEditLogic) AuthorityEdit(in *admin.AuthorityEditRequest) (*admin.Response, error) {
|
|
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
|
|
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)
|
|
}
|
|
|
|
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)
|
|
_, err = modelObj.Edit(upW, edit)
|
|
|
|
} else {
|
|
_, 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)
|
|
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
|
|
}
|
|
}
|
|
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
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 := json.Unmarshal([]byte(in.DataBaseStr), &check)
|
|
if err != nil {
|
|
return utils.ErrorJsonDataError, data
|
|
}
|
|
// 解析JSON字符串到map
|
|
err = json.Unmarshal([]byte(in.DataAll), &check)
|
|
if err != nil {
|
|
return utils.ErrorJsonDataError, data
|
|
}
|
|
// 解析JSON字符串到map
|
|
err = json.Unmarshal([]byte(in.DataDepartment), &check)
|
|
if err != nil {
|
|
return utils.ErrorJsonDataError, data
|
|
}
|
|
// 解析JSON字符串到map
|
|
err = json.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
|
|
}
|