fusenapi/server/ldap-admin/internal/logic/getmenuslogic.go
laodaming 1a2c9a8f49 fix
2023-11-15 16:32:07 +08:00

85 lines
2.1 KiB
Go

package logic
import (
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"math"
"context"
"fusenapi/server/ldap-admin/internal/svc"
"fusenapi/server/ldap-admin/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetMenusLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetMenusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenusLogic {
return &GetMenusLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *GetMenusLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *GetMenusLogic) GetMenus(req *types.GetMenusReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if req.CurrentPage <= 0 {
req.CurrentPage = constants.DEFAULT_PAGE
}
limit := constants.DEFAULT_PAGE_SIZE
status := int64(1)
menusReq := gmodel.GetMenuListByParamReq{
Name: req.Name,
Title: req.Title,
Path: req.Path,
Status: &status,
ParentId: req.ParentId,
Sort: "sort DESC",
Page: req.CurrentPage,
Limit: limit,
}
menus, total, err := l.svcCtx.AllModels.LdapMenus.GetMenuListByParam(l.ctx, menusReq)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "获取菜单列表失败")
}
list := make([]types.MenuItem, 0, len(menus))
for _, v := range menus {
list = append(list, types.MenuItem{
Id: v.Id,
Name: *v.Name,
Title: *v.Title,
Icon: *v.Icon,
Path: *v.Path,
Sort: *v.Sort,
ParentId: *v.ParentId,
Status: *v.Status,
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetMenusRsp{
List: list,
Meta: types.Meta{
TotalCount: total,
PageCount: int64(math.Ceil(float64(total) / float64(limit))),
CurrentPage: req.CurrentPage,
PerPage: limit,
},
})
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetMenusLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }