新增权限认证。
This commit is contained in:
parent
4a9c5b9c4d
commit
91e3d72873
@ -8,10 +8,13 @@ import (
|
||||
"hospital-admin-api/api/service"
|
||||
"hospital-admin-api/global"
|
||||
"hospital-admin-api/utils"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Role struct{}
|
||||
|
||||
var RoleRequest requests.RoleRequest
|
||||
|
||||
// GetRoleMenuList 获取角色菜单列表
|
||||
func (r *Role) GetRoleMenuList(c *gin.Context) {
|
||||
roleId := c.GetInt64("RoleId")
|
||||
@ -36,10 +39,8 @@ func (r *Role) GetRoleMenuList(c *gin.Context) {
|
||||
responses.OkWithData(roleMenuList, c)
|
||||
}
|
||||
|
||||
// GetRoleList 获取角色列表
|
||||
// GetRoleList 搜索角色列表
|
||||
func (r *Role) GetRoleList(c *gin.Context) {
|
||||
var RoleRequest requests.RoleRequest
|
||||
|
||||
if err := c.ShouldBind(&RoleRequest.GetRoleList); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
@ -69,3 +70,46 @@ func (r *Role) GetRoleList(c *gin.Context) {
|
||||
|
||||
responses.OkWithData(adminRole, c)
|
||||
}
|
||||
|
||||
// PutRoleStatus 角色禁用/启用
|
||||
// 此处应该允许修改下级角色
|
||||
func (r *Role) PutRoleStatus(c *gin.Context) {
|
||||
if err := c.ShouldBind(&RoleRequest.PutRoleStatus); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := global.Validate.Struct(RoleRequest.PutRoleStatus); err != nil {
|
||||
responses.FailWithMessage(utils.Translate(err), c)
|
||||
return
|
||||
}
|
||||
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 将 id 转换为 int64 类型
|
||||
roleId, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
return
|
||||
}
|
||||
|
||||
// 不可修改自己
|
||||
if roleId == c.GetInt64("RoleId") {
|
||||
responses.FailWithMessage("无法操作当前账号所属角色", c)
|
||||
return
|
||||
}
|
||||
|
||||
RoleService := service.RoleService{}
|
||||
_, err = RoleService.PutRoleStatus(roleId, RoleRequest.PutRoleStatus.RoleStatus)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
@ -36,3 +36,12 @@ func (r *AdminRoleDao) GetAdminRolePageSearch(roleName string, page, pageSize in
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// EditAdminRoleStatusById 角色启用/禁用-角色id
|
||||
func (r *AdminRoleDao) EditAdminRoleStatusById(roleId int64, roleStatus int) error {
|
||||
err := global.Db.Model(&model.AdminRole{}).Where("role_id = ?", roleId).Update("role_status", roleStatus).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"hospital-admin-api/api/responses"
|
||||
"hospital-admin-api/consts"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -64,6 +65,13 @@ func Auth() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// 检测角色是否已被禁用
|
||||
if adminRole.RoleStatus == 2 {
|
||||
responses.FailWithMessage("角色已被禁用", c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取角色菜单id
|
||||
AdminRoleMenuDao := dao.AdminRoleMenuDao{}
|
||||
adminRoleMenu, _ := AdminRoleMenuDao.GetAdminRoleMenuListByRoleId(roleId)
|
||||
@ -95,22 +103,31 @@ func Auth() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
path := ""
|
||||
// 找到最后一个数字的索引
|
||||
lastSlashIndex := strings.LastIndex(c.Request.RequestURI, "/")
|
||||
if lastSlashIndex != -1 {
|
||||
// 替换最后一个数字部分为 :id
|
||||
path = c.Request.RequestURI[:lastSlashIndex] + "/:id" + c.Request.Method
|
||||
path := c.Request.URL.Path
|
||||
|
||||
// 编译正则表达式
|
||||
reg := regexp.MustCompile("/(\\d+)$")
|
||||
|
||||
// 进行匹配
|
||||
match := reg.MatchString(c.Request.RequestURI)
|
||||
if match {
|
||||
// 找到最后一个数字的索引
|
||||
lastSlashIndex := strings.LastIndex(c.Request.RequestURI, "/")
|
||||
if lastSlashIndex != -1 {
|
||||
// 替换最后一个数字部分为 :id
|
||||
path = path[:lastSlashIndex] + "/:id" + c.Request.Method
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "请求路径错误",
|
||||
"code": consts.SERVER_ERROR,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "请求路径错误",
|
||||
"code": consts.SERVER_ERROR,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
path = path + c.Request.Method
|
||||
}
|
||||
|
||||
// 在apiPermissions中查找对应的API权限
|
||||
|
||||
@ -1,14 +1,28 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
// AdminRole 后台-角色表
|
||||
type AdminRole struct {
|
||||
Model
|
||||
RoleId int64 `gorm:"column:role_id;type:bigint(19);primary_key;comment:主键id" json:"role_id"`
|
||||
RoleName string `gorm:"column:role_name;type:varchar(100);comment:角色名称" json:"role_name"`
|
||||
RoleStatus int `gorm:"column:role_status;type:tinyint(1);default:1;comment:角色状态(1:正常 2:禁用)" json:"role_status"`
|
||||
IsAdmin int `gorm:"column:is_admin;type:tinyint(1);default:0;comment:是否管理员(0:否 1:是)" json:"is_admin"`
|
||||
// Model
|
||||
RoleId int64 `gorm:"column:role_id;type:bigint(19);primary_key;comment:主键id" json:"role_id"`
|
||||
RoleName string `gorm:"column:role_name;type:varchar(100);comment:角色名称" json:"role_name"`
|
||||
RoleStatus int `gorm:"column:role_status;type:tinyint(1);default:1;comment:角色状态(1:正常 2:禁用)" json:"role_status"`
|
||||
IsAdmin int `gorm:"column:is_admin;type:tinyint(1);default:0;comment:是否管理员(0:否 1:是)" json:"is_admin"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:datetime;comment:创建时间" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;comment:修改时间" json:"updated_at"`
|
||||
}
|
||||
|
||||
func (m *AdminRole) TableName() string {
|
||||
return "gdxz_admin_role"
|
||||
}
|
||||
|
||||
func (m *AdminRole) BeforeUpdate(tx *gorm.DB) (err error) {
|
||||
fmt.Println(111111)
|
||||
m.UpdatedAt = time.Now()
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -1,12 +1,18 @@
|
||||
package requests
|
||||
|
||||
type RoleRequest struct {
|
||||
GetRoleList // 获取角色列表
|
||||
GetRoleList // 获取角色列表
|
||||
PutRoleStatus // 角色禁用/启用
|
||||
}
|
||||
|
||||
// GetRoleListRequest 获取角色列表
|
||||
// GetRoleList 获取角色列表
|
||||
type GetRoleList struct {
|
||||
RoleName string `json:"role_name" form:"role_name" label:"角色名称"`
|
||||
Page int `json:"page" form:"page" label:"页码"`
|
||||
PageSize int `json:"page_size" form:"page_size" label:"每页个数"`
|
||||
}
|
||||
|
||||
// PutRoleStatus 角色禁用/启用
|
||||
type PutRoleStatus struct {
|
||||
RoleStatus int `json:"role_status" form:"role_status" validate:"oneof=1 2" label:"角色状态"` // (1:正常 2:禁用)
|
||||
}
|
||||
|
||||
@ -75,8 +75,11 @@ func privateRouter(r *gin.Engine, api controller.Api) {
|
||||
// 获取角色菜单列表
|
||||
base1Group.GET("menu", api.Role.GetRoleMenuList)
|
||||
|
||||
// 获取角色列表
|
||||
// 搜索角色列表
|
||||
base1Group.GET("list", api.Role.GetRoleList)
|
||||
|
||||
// 角色禁用/启用
|
||||
base1Group.PUT("status/:id", api.Role.PutRoleStatus)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"hospital-admin-api/api/dao"
|
||||
"hospital-admin-api/api/model"
|
||||
"hospital-admin-api/api/responses/roleResponse"
|
||||
@ -76,3 +77,25 @@ func buildMenuTree(menuIds []int64, menuData []*model.AdminMenu) []*roleResponse
|
||||
|
||||
return rootNodes
|
||||
}
|
||||
|
||||
// PutRoleStatus 角色禁用/启用
|
||||
func (r *RoleService) PutRoleStatus(roleId int64, roleStatus int) (bool, error) {
|
||||
// 获取请求角色数据
|
||||
AdminRoleDao := dao.AdminRoleDao{}
|
||||
adminRole, err := AdminRoleDao.GetAdminRoleFirstById(roleId)
|
||||
if err != nil || adminRole.RoleId == 0 {
|
||||
return false, errors.New("非法操作")
|
||||
}
|
||||
|
||||
// 检测是否为超级管理员
|
||||
if adminRole.IsAdmin == 1 {
|
||||
return false, errors.New("请勿修改超级管理员数据")
|
||||
}
|
||||
|
||||
// 修改角色状态
|
||||
err = AdminRoleDao.EditAdminRoleStatusById(roleId, roleStatus)
|
||||
if err != nil {
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user