代码拉取完成,页面将自动刷新
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。