123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package role
- import (
- "authService/response"
- "authService/service/auth"
- "authService/service/permission"
- "authService/service/role"
- "authService/service/user"
- "authService/validators"
- "github.com/gin-gonic/gin"
- )
- // 修改或创建授权信息
- func UpdateOrCreate(c *gin.Context) {
- var validator validators.Role
- validateErr := c.ShouldBind(&validator)
- if validateErr != nil {
- response.FailValidator(c, validateErr)
- return
- }
- if validator.ID != 0 {
- // 更新
- errCode := role.Update(&validator)
- if errCode != nil {
- response.Fail(c, errCode)
- return
- }
- // 更新用户的凭证
- roleInfo, roleInfoErr := role.FindByRoleIdWithUser(validator.ID)
- if roleInfoErr == nil {
- for _, roleUser := range roleInfo.Users {
- auth.Refresh(user.Format(&roleUser))
- }
- }
- } else {
- errCode := role.Create(&validator)
- if errCode != nil {
- response.Fail(c, errCode)
- return
- }
- }
- response.Success(c, map[string]any{})
- }
- // 删除
- func Delete(c *gin.Context) {
- type Validator struct {
- ID int64 `json:"id" binding:"required"`
- }
- var validator Validator
- validateErr := c.ShouldBind(&validator)
- if validateErr != nil {
- response.FailValidator(c, validateErr)
- return
- }
- deleteErr := role.Delete(validator.ID)
- if deleteErr != nil {
- response.Fail(c, deleteErr)
- return
- }
- response.Success(c, map[string]any{})
- }
- // 获取列表
- func List(c *gin.Context) {
- roles := role.List()
- var list = make([]*validators.Role, 0)
- for _, item := range roles {
- list = append(list, role.FormatRole(item))
- }
- response.Success(c, map[string]any{
- "list": list,
- })
- }
- // 分页获取列表
- func Paginate(c *gin.Context) {
- type Validator struct {
- Page int `json:"page" form:"page" binding:"omitempty,min=1"`
- PageSize int `json:"pageSize" form:"pageSize" binding:"omitempty,min=1,max=50"`
- Key string `json:"key" form:"key" binding:"omitempty"`
- }
- var validator Validator
- validateErr := c.ShouldBindQuery(&validator)
- if validateErr != nil {
- response.FailValidator(c, validateErr)
- return
- }
- roles, total := role.Paginate(validator.Page, validator.PageSize, validator.Key)
- var list = make([]*validators.Role, 0)
- for _, item := range roles {
- list = append(list, role.FormatRole(item))
- }
- response.Success(c, map[string]any{
- "list": list,
- "total": total,
- })
- }
- // 获取用户权限组列表
- func ListPermissionGroup(c *gin.Context) {
- permissionGroups := permission.ListPermissionGroup()
- response.Success(c, map[string]any{
- "list": permissionGroups,
- })
- }
|