129 lines
4.3 KiB
Go
129 lines
4.3 KiB
Go
package platform
|
|
|
|
import (
|
|
"git.apinb.com/bsm-sdk/core/errcode"
|
|
"git.apinb.com/bsm-sdk/core/infra"
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ListPlatformRole 查询平台角色分页列表。
|
|
func ListPlatformRole(ctx *gin.Context) { listPage[models.PlatformRole](ctx) }
|
|
|
|
// GetPlatformRole 查询一个平台角色。
|
|
func GetPlatformRole(ctx *gin.Context) { getByIdentity[models.PlatformRole](ctx) }
|
|
|
|
// CreatePlatformRole 创建非内置平台角色。
|
|
func CreatePlatformRole(ctx *gin.Context) {
|
|
var request models.PlatformRole
|
|
if err := ctx.ShouldBindJSON(&request); err != nil || request.RoleCode == "" || request.Name == "" || request.RoleCode == "root" {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
request.Entity = newEntity("enabled")
|
|
request.IsSystem = false
|
|
if request.DataScope == "" {
|
|
request.DataScope = "global"
|
|
}
|
|
if err := impl.DBService.Create(&request).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, request)
|
|
}
|
|
|
|
// UpdatePlatformRole 更新非内置平台角色。
|
|
func UpdatePlatformRole(ctx *gin.Context) {
|
|
var request struct {
|
|
Name string `json:"name" binding:"required,max=64"`
|
|
DataScope string `json:"data_scope" binding:"required,max=32"`
|
|
}
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
var role models.PlatformRole
|
|
if err := impl.DBService.Where("identity = ?", ctx.Param("identity")).First(&role).Error; err != nil {
|
|
respondRecordError(ctx, err)
|
|
return
|
|
}
|
|
if role.IsSystem {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
if err := impl.DBService.Model(&role).Updates(gin.H{"name": request.Name, "data_scope": request.DataScope}).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, role)
|
|
}
|
|
|
|
// UpdatePlatformRoleStatus 更新非内置平台角色状态,系统角色始终受保护。
|
|
func UpdatePlatformRoleStatus(ctx *gin.Context) {
|
|
var request struct {
|
|
Status string `json:"status" binding:"required,max=32"`
|
|
}
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
var role models.PlatformRole
|
|
if err := impl.DBService.Where("identity = ?", ctx.Param("identity")).First(&role).Error; err != nil {
|
|
respondRecordError(ctx, err)
|
|
return
|
|
}
|
|
if role.IsSystem {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
if err := impl.DBService.Model(&role).Update("status", request.Status).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, gin.H{"updated": true})
|
|
}
|
|
|
|
// ArchivePlatformRole 归档非内置平台角色,系统角色始终受保护。
|
|
func ArchivePlatformRole(ctx *gin.Context) {
|
|
var role models.PlatformRole
|
|
if err := impl.DBService.Where("identity = ?", ctx.Param("identity")).First(&role).Error; err != nil {
|
|
respondRecordError(ctx, err)
|
|
return
|
|
}
|
|
if role.IsSystem {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
if err := impl.DBService.Model(&role).Update("status", "archived").Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, gin.H{"updated": true})
|
|
}
|
|
|
|
// ListPlatformMenu 返回菜单树构建所需的有序菜单列表。
|
|
func ListPlatformMenu(ctx *gin.Context) {
|
|
var list []models.PlatformMenu
|
|
if err := impl.DBService.Order("sort_no asc, id asc").Find(&list).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, gin.H{"total": len(list), "list": list})
|
|
}
|
|
|
|
// ListPlatfromAccount 查询平台账号列表,手机号在展示层脱敏。
|
|
func ListPlatfromAccount(ctx *gin.Context) {
|
|
page, size := pageSize(ctx)
|
|
list, total, err := models.ListPlatfromAccount(page, size)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
views := make([]gin.H, 0, len(list))
|
|
for _, item := range list {
|
|
views = append(views, gin.H{"identity": item.Identity, "username": item.Username, "display_name": item.DisplayName, "avatar": item.Avatar, "phone_masked": maskPhone(item.Phone), "platform_role_code": item.PlatformRoleCode, "status": item.Status})
|
|
}
|
|
infra.Response.Success(ctx, gin.H{"total": total, "list": views})
|
|
}
|