2023-11-15 07:16:27 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2023-11-15 07:50:18 +00:00
|
|
|
"errors"
|
|
|
|
"fusenapi/model/gmodel"
|
2023-11-15 07:16:27 +00:00
|
|
|
"fusenapi/utils/auth"
|
|
|
|
"fusenapi/utils/basic"
|
2023-11-15 07:50:18 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2023-11-15 07:16:27 +00:00
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fusenapi/server/ldap-admin/internal/svc"
|
|
|
|
"fusenapi/server/ldap-admin/internal/types"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SaveMenuLogic struct {
|
|
|
|
logx.Logger
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSaveMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveMenuLogic {
|
|
|
|
return &SaveMenuLogic{
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理进入前逻辑w,r
|
|
|
|
// func (l *SaveMenuLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// }
|
|
|
|
|
|
|
|
func (l *SaveMenuLogic) SaveMenu(req *types.SaveMenuReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
2023-11-15 07:50:18 +00:00
|
|
|
req.Path = strings.Trim(req.Path, " ")
|
|
|
|
req.Name = strings.Trim(req.Name, " ")
|
|
|
|
req.Title = strings.Trim(req.Title, " ")
|
|
|
|
if req.Path == "" {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数path不能为空")
|
|
|
|
}
|
|
|
|
if req.Name == "" {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数name不能为空")
|
|
|
|
}
|
|
|
|
if req.Title == "" {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数title不能为空")
|
|
|
|
}
|
|
|
|
now := time.Now().UTC()
|
|
|
|
data := &gmodel.LdapMenus{
|
|
|
|
Name: &req.Name,
|
|
|
|
Title: &req.Title,
|
|
|
|
Icon: &req.Icon,
|
|
|
|
Path: &req.Path,
|
|
|
|
Sort: &req.Sort,
|
|
|
|
Status: &req.Status,
|
|
|
|
ParentId: &req.ParentId,
|
|
|
|
Utime: &now,
|
|
|
|
}
|
|
|
|
//查询重复
|
|
|
|
info, err := l.svcCtx.AllModels.LdapMenus.FindByPath(l.ctx, req.Path)
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "获取检查重复路径失败")
|
|
|
|
}
|
|
|
|
//没有
|
|
|
|
} else { //有
|
|
|
|
//是新增/或者是更新但是id不等
|
|
|
|
if req.Id == 0 || (info.Id != req.Id) {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "该路径已经存在")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if req.Id > 0 { //更新
|
|
|
|
//更新
|
|
|
|
if err = l.svcCtx.AllModels.LdapMenus.Update(l.ctx, req.Id, data); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "更新失败")
|
|
|
|
}
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "更新成功")
|
|
|
|
}
|
|
|
|
//添加
|
|
|
|
data.Ctime = &now
|
|
|
|
if err = l.svcCtx.AllModels.LdapMenus.Create(l.ctx, data); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "添加失败")
|
|
|
|
}
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "添加成功")
|
2023-11-15 07:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
|
|
// func (l *SaveMenuLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
|
// }
|