瀏覽代碼

角色部分接口

terry 2 年之前
父節點
當前提交
3d62a1df85

+ 1 - 0
app/system/controller/menu.go

@@ -0,0 +1 @@
+package controller

+ 105 - 0
app/system/controller/role.go

@@ -1 +1,106 @@
 package controller
+
+import (
+	"github.com/gin-gonic/gin"
+	"iot_manager_service/app/system/dao"
+	"iot_manager_service/app/system/model"
+	"iot_manager_service/app/system/service"
+	"iot_manager_service/util"
+	"math"
+	"net/http"
+	"strconv"
+)
+
+var Role = new(role)
+
+type role struct{}
+
+func (c *role) GetDetail(ctx *gin.Context) {
+	id := ctx.Query("id")
+	iId, err := strconv.ParseInt(id, 10, 64)
+	if err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	role, err := service.RoleService.Get(iId)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, role))
+}
+
+func (c *role) List(ctx *gin.Context) {
+	current, _ := strconv.Atoi(ctx.Query("current"))
+	size, _ := strconv.Atoi(ctx.Query("size"))
+	roleName := ctx.Query("roleName")
+	tenantId := ctx.Query("tenantId")
+	roleAlias := ctx.Query("roleAlias")
+	if current == 0 {
+		current = 1
+	}
+	if size <= 0 || size > 100 {
+		size = 10
+	}
+
+	roles, err := service.RoleService.List(roleName, tenantId, roleAlias, current, size)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	pages := math.Ceil(float64(len(roles)) / float64(size))
+	rsp := model.RsqRoleList{
+		Current: current,
+		Size:    size,
+		Total:   len(roles),
+		Pages:   int(pages),
+		Records: roles,
+	}
+	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
+}
+
+func (c *role) Tree(ctx *gin.Context) {
+	err := service.RoleService.Tree()
+	ctx.JSON(http.StatusOK, err)
+}
+
+func (c *role) Submit(ctx *gin.Context) {
+	var req dao.Role
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.RoleService.Submit(req)
+	ctx.JSON(http.StatusOK, err)
+}
+
+func (c *role) Remove(ctx *gin.Context) {
+	var req *model.ReqRoleRemove
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.RoleService.Remove(req.IDs)
+	ctx.JSON(http.StatusOK, err)
+}
+
+func (c *role) GetList(ctx *gin.Context) {
+	roles, err := service.RoleService.GetList()
+	if err != nil {
+		ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
+	}
+	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, roles))
+}
+
+func (c *role) Grant(ctx *gin.Context) {
+	var req *model.ReqRoleGrant
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.RoleService.Grant(req)
+	if err != nil {
+		ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
+	}
+	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, nil))
+}

+ 1 - 0
app/system/dao/menu.go

@@ -0,0 +1 @@
+package dao

+ 54 - 1
app/system/dao/role.go

@@ -15,6 +15,59 @@ func (Role) TableName() string {
 	return "role"
 }
 
-func (c *Role) GetRole() error {
+func (c *Role) Get() error {
 	return Db.Model(&c).Where("id = ?", c.ID).Find(&c).Error
 }
+
+func (c *Role) GetRole() error {
+	return Db.Model(&c).Where("id = ? and is_deleted = 0", c.ID).Find(&c).Error
+}
+
+func (c Role) GetRoles(offset, limit int) ([]Role, error) {
+	var Roles []Role
+	db := Db.Model(&c)
+	if c.RoleName != "" {
+		db = db.Where("role_name like ?", "%"+c.RoleName+"%")
+	}
+	if c.RoleAlias != "" {
+		db = db.Where("role_alias like ?", "%"+c.RoleAlias+"%")
+	}
+	if c.TenantId != "" {
+		db = db.Where("tenant_id like ?", "%"+c.TenantId+"%")
+	}
+	err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&Roles).Error
+	return Roles, err
+}
+
+func (c *Role) Save() error {
+	return Db.Model(&c).Save(&c).Error
+}
+
+func (c *Role) Update() error {
+	return Db.Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
+}
+
+func (c *Role) Remove() error {
+	return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"is_deleted": c.IsDeleted}).Error
+}
+
+func (c *Role) IsExistChild() bool {
+	var count int
+	_ = Db.Model(&c).Where("parent_id = ? and is_deleted = 0", c.ParentId).Count(&count).Error
+	return count > 0
+}
+
+func (c *Role) UpdatePwd(pwd string) error {
+	return Db.Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"password": pwd}).Error
+}
+
+func (c *Role) GetAll() ([]Role, error) {
+	var Roles []Role
+	err := Db.Model(&c).Where("is_deleted = 0").Find(&Roles).Error
+	return Roles, err
+}
+
+func (c *Role) UpdateRoles(RoleIds []string, roleIds string) error {
+	err := Db.Model(&c).Where("id in ?", RoleIds).Updates(map[string]interface{}{"role_id": roleIds}).Error
+	return err
+}

+ 23 - 0
app/system/dao/roleMenu.go

@@ -0,0 +1,23 @@
+package dao
+
+type RoleMenu struct {
+	ID     int64 `gorm:"primary_key" json:"id"`     //编号
+	MenuId int64 `gorm:"type:bigint" json:"menuId"` //菜单id
+	RoleId int64 `gorm:"type:bigint" json:"roleId"` //角色id
+}
+
+func (RoleMenu) Table() string {
+	return "role_menu"
+}
+
+func (c *RoleMenu) Save() error {
+	return Db.Model(&c).Save(&c).Error
+}
+
+func (c *RoleMenu) BatchSave(menus []RoleMenu) error {
+	return Db.Model(&c).Save(&menus).Error
+}
+
+func (c *RoleMenu) DeleteRoleIds(roleIds []int64) error {
+	return Db.Model(&c).Where("role_id in ?", roleIds).Delete(&c).Error
+}

+ 1 - 0
app/system/model/menu.go

@@ -0,0 +1 @@
+package model

+ 25 - 0
app/system/model/role.go

@@ -0,0 +1,25 @@
+package model
+
+import "iot_manager_service/app/system/dao"
+
+type RsqRoleList struct {
+	Records []dao.Role `json:"records"` //记录列表
+	Current int        `json:"current"` //当前分页
+	Size    int        `json:"size"`    //每页数量
+	Pages   int        `json:"pages"`   //总页数
+	Total   int        `json:"total"`   //总数
+}
+
+type ReqRoleRemove struct {
+	IDs  int64  `json:"ids"`
+	Name string `json:"name"`
+}
+
+type ReqRoleGrant struct {
+	RoleIds      string `json:"roleIds"`
+	MenuIds      string `json:"menuIds"`
+	DataScopeIds string `json:"dataScopeIds"`
+	ApiScopeIds  string `json:"apiScopeIds"`
+}
+
+const ExistChild = "需要删除的记录中存在子角色,请先删除子角色!"

+ 1 - 0
app/system/service/menuService.go

@@ -0,0 +1 @@
+package service

+ 27 - 0
app/system/service/roleMenuService.go

@@ -0,0 +1,27 @@
+package service
+
+import "iot_manager_service/app/system/dao"
+
+// 角色菜单管理服务
+var RoleMenuService = new(roleMenuService)
+
+type roleMenuService struct{}
+
+func (s *roleMenuService) UpdateRoleMenus(roleIds, menuIds []int64) error {
+	roleMenu := &dao.RoleMenu{}
+	err := roleMenu.DeleteRoleIds(roleIds)
+	if err != nil {
+		return err
+	}
+
+	var roleMenus []dao.RoleMenu
+	for _, roleId := range roleIds {
+		for _, menuId := range menuIds {
+			roleMenus = append(roleMenus, dao.RoleMenu{
+				MenuId: menuId,
+				RoleId: roleId,
+			})
+		}
+	}
+	return roleMenu.BatchSave(roleMenus)
+}

+ 102 - 0
app/system/service/roleService.go

@@ -0,0 +1,102 @@
+package service
+
+import (
+	"iot_manager_service/app/system/dao"
+	"iot_manager_service/app/system/model"
+	"iot_manager_service/util"
+)
+
+// 角色管理服务
+var RoleService = new(roleService)
+
+type roleService struct{}
+
+func (s *roleService) Get(id int64) (*dao.Role, error) {
+	role := &dao.Role{
+		ID: id,
+	}
+	err := role.Get()
+	if err != nil {
+		return nil, err
+	}
+	return role, nil
+}
+
+func (s *roleService) List(roleName, tenantId, roleAlias string, current, size int) ([]dao.Role, error) {
+	role := &dao.Role{}
+	offset := (current - 1) * size
+	limit := size
+	if roleName != "" {
+		role.RoleName = roleName
+	}
+	if tenantId != "" {
+		role.TenantId = tenantId
+	}
+	if roleAlias != "" {
+		role.RoleAlias = roleAlias
+	}
+
+	roles, err := role.GetRoles(offset, limit)
+	if err != nil {
+		return nil, err
+	}
+	return roles, nil
+}
+
+func (s *roleService) Tree() error {
+	return nil
+}
+
+func (s *roleService) Submit(req dao.Role) *util.Errors {
+	role := &req
+	if role.ID == 0 {
+		err := role.Save()
+		if err != nil {
+			return util.FailResponse(err.Error(), nil)
+		}
+		return util.SuccessResponse(util.Succeeded, nil)
+	}
+	err := role.Update()
+	if err != nil {
+		return util.FailResponse(err.Error(), nil)
+	}
+	return util.SuccessResponse(util.Succeeded, nil)
+}
+
+func (s *roleService) GetList() ([]dao.Role, error) {
+	role := &dao.Role{}
+	roles, err := role.GetAll()
+	if err != nil {
+		return nil, err
+	}
+	return roles, nil
+}
+
+func (s *roleService) Remove(id int64) *util.Errors {
+	role := &dao.Role{
+		ID:        id,
+		IsDeleted: 1,
+	}
+
+	queryRole := &dao.Role{
+		ParentId:  id,
+		IsDeleted: 0,
+	}
+	if queryRole.IsExistChild() {
+		return util.FailResponse(model.ExistChild, nil)
+	}
+
+	err := role.Remove()
+	if err != nil {
+		return util.FailResponse(err.Error(), nil)
+	}
+	return nil
+}
+
+func (s *roleService) Grant(req *model.ReqRoleGrant) error {
+	roleIds := util.StringToIntArray(req.RoleIds)
+
+	menuIds := util.StringToIntArray(req.MenuIds)
+	menuErr := RoleMenuService.UpdateRoleMenus(roleIds, menuIds)
+	return menuErr
+}

+ 11 - 0
router/router.go

@@ -369,4 +369,15 @@ func InitRouter(engine *gin.Engine) {
 		tenant.POST("/remove", system.Tenant.Remove)
 		tenant.POST("/select", system.Tenant.GetList)
 	}
+
+	//角色
+	role := engine.Group("/api/blade-system/role")
+	{
+		role.GET("/detail", system.Role.GetDetail)
+		role.GET("/list", system.Role.List)
+		role.GET("/tree", system.Role.Tree)
+		role.POST("/submit", system.Role.Submit)
+		role.POST("/remove", system.Role.Remove)
+		role.POST("/grant", system.Role.Grant)
+	}
 }

+ 11 - 0
util/common.go

@@ -3,6 +3,7 @@ package util
 import (
 	"math/rand"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -35,3 +36,13 @@ func RandomString2(n int) string {
 	}
 	return string(b)
 }
+
+func StringToIntArray(str string) []int64 {
+	tmp := strings.Split(str, ",")
+	var result []int64
+	for _, t := range tmp {
+		i, _ := strconv.ParseInt(t, 10, 64)
+		result = append(result, i)
+	}
+	return result
+}