2 Star 0 Fork 0

hero/momo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
role.go 2.66 KB
一键复制 编辑 原始数据 按行查看 历史
hero 提交于 2024-11-06 13:43 . upd:权限code
package controller
import (
"fmt"
"strconv"
"gitee.com/linqwen/momo/app/rbac/model"
"gitee.com/linqwen/momo/app/rbac/schema"
"gitee.com/linqwen/momo/app/rbac/service"
"gitee.com/linqwen/momo/base"
"gitee.com/linqwen/momo/rbac"
"gitee.com/linqwen/momo/utils"
"github.com/gin-gonic/gin"
"gitee.com/linqwen/momo/igin"
)
type roleController struct {
base.BaseController[model.RoleEntity, schema.RoleDTO, schema.RoleDTO, schema.RoleVO]
}
func RoleRouter(r igin.IRouterGroup) {
c := roleController{BaseController: *base.NewBaseController[model.RoleEntity, schema.RoleDTO, schema.RoleDTO, schema.RoleVO](service.NewRoleService())}
router := r.Group("role")
{
router.GET("/", rbac.Auth.HasPermit("rbac_role"), c.PageByDTO)
router.POST("/", rbac.Auth.HasPermit("rbac_role_add"), c.Create)
router.PUT("/:id", rbac.Auth.HasPermit("rbac_role_edit"), c.UpdateByMap)
router.GET("/:id", rbac.Auth.HasPermit("rbac_role_view"), c.GetById)
router.POST("/deletes", rbac.Auth.HasPermit("rbac_role_delete"), c.BulkDelete)
router.GET("/tree-all", rbac.Auth.HasPermit("rbac_role_view"), c.GetAllRole)
router.GET("/tree/:id", rbac.Auth.HasPermit("rbac_role_view"), c.GetTreeById)
router.POST("/setmenus/", rbac.Auth.HasPermit("rbac_role_edit"), c.setMenus)
}
}
var roleService = service.NewRoleService()
func (bc *roleController) setMenus(c igin.IContext) {
var postData struct {
Ids []string `json:"ids"`
MenuIds string `json:"MenuIds"`
}
if err := c.ShouldBindJSON(&postData); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
// 将字符串形式的用户ID转换为int64
userIdsInt64 := make([]int64, len(postData.Ids))
for i, idStr := range postData.Ids {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
c.JSON(400, gin.H{"error": "Invalid user Id format"})
return
}
userIdsInt64[i] = id
}
fmt.Printf("updatemap is %+v\n", postData)
err := roleService.SetMenus(userIdsInt64, postData.MenuIds)
if err != nil {
utils.ResponseApiError(c, 400, "login error", err.Error())
return
}
utils.ResponseApiOk(c, "ok", nil)
}
func (bc *roleController) GetAllRole(c igin.IContext) {
result, err := roleService.GetAllRoles()
if err != nil {
utils.ResponseApiError(c, 400, "no menurecord", err.Error())
}
var vos []schema.RoleVO
err = utils.SliceToSlice(result, &vos)
if err != nil {
utils.ResponseApiError(c, 400, "modeltovo error", err.Error())
}
var volist1 []interface{}
for _, vo := range vos {
volist1 = append(volist1, vo.GetListVO())
}
// 检查是否需要缓存数据
if cacheEnabled, exists := c.Get("cacheEnabled"); exists && cacheEnabled.(bool) {
c.Set("data", volist1)
}
utils.ResponseApiOk(c, "ok", volist1)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/linqwen/momo.git
git@gitee.com:linqwen/momo.git
linqwen
momo
momo
v1.1.23

搜索帮助

0d507c66 1850385 C8b1a773 1850385