go-platform/controllers/platform_role.go
2026-04-01 00:06:36 +08:00

203 lines
4.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controllers
import (
"encoding/json"
"io"
"strconv"
"strings"
"server/models"
beego "github.com/beego/beego/v2/server/web"
)
// PlatformRoleController 平台角色管理yz_admin_role
type PlatformRoleController struct {
beego.Controller
}
type rolePayload struct {
Cid *uint8 `json:"cid"`
Name string `json:"name"`
Status *uint8 `json:"status"`
Rights interface{} `json:"rights"`
}
func normalizeRights(v interface{}) *string {
if v == nil {
return nil
}
switch t := v.(type) {
case string:
s := strings.TrimSpace(t)
if s == "" {
return nil
}
return &s
default:
b, err := json.Marshal(v)
if err != nil {
return nil
}
s := string(b)
return &s
}
}
// GetAllRoles 获取角色列表
// GET /platform/allRoles
func (c *PlatformRoleController) GetAllRoles() {
var rows []models.AdminRole
_, err := models.Orm.QueryTable(new(models.AdminRole)).
OrderBy("-id").
All(&rows)
if err != nil {
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "查询失败"}
_ = c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": rows}
_ = c.ServeJSON()
}
// GetRoleByID 获取角色详情
// GET /platform/roles/:id
func (c *PlatformRoleController) GetRoleByID() {
idStr := c.Ctx.Input.Param(":id")
id, _ := strconv.ParseUint(idStr, 10, 64)
if id == 0 {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "id 不能为空"}
_ = c.ServeJSON()
return
}
role := models.AdminRole{ID: id}
if err := models.Orm.Read(&role); err != nil {
c.Data["json"] = map[string]interface{}{"code": 404, "msg": "角色不存在"}
_ = c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": role}
_ = c.ServeJSON()
}
// CreateRole 创建角色
// POST /platform/roles
func (c *PlatformRoleController) CreateRole() {
var p rolePayload
raw, _ := io.ReadAll(c.Ctx.Request.Body)
if err := json.Unmarshal(raw, &p); err != nil {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
_ = c.ServeJSON()
return
}
p.Name = strings.TrimSpace(p.Name)
if p.Name == "" {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "name 不能为空"}
_ = c.ServeJSON()
return
}
status := uint8(1)
if p.Status != nil {
status = *p.Status
}
cid := uint8(1)
if p.Cid != nil {
cid = *p.Cid
}
if cid != 1 && cid != 2 {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "cid 仅支持 1/2"}
_ = c.ServeJSON()
return
}
rights := normalizeRights(p.Rights)
role := &models.AdminRole{
Cid: cid,
Name: p.Name,
Status: status,
Rights: rights,
}
id, err := models.Orm.Insert(role)
if err != nil {
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "创建失败"}
_ = c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": map[string]interface{}{"id": id}}
_ = c.ServeJSON()
}
// UpdateRole 更新角色
// PUT /platform/roles/:id
func (c *PlatformRoleController) UpdateRole() {
idStr := c.Ctx.Input.Param(":id")
id, _ := strconv.ParseUint(idStr, 10, 64)
if id == 0 {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "id 不能为空"}
_ = c.ServeJSON()
return
}
var p rolePayload
raw, _ := io.ReadAll(c.Ctx.Request.Body)
if err := json.Unmarshal(raw, &p); err != nil {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
_ = c.ServeJSON()
return
}
update := map[string]interface{}{}
if strings.TrimSpace(p.Name) != "" {
update["name"] = strings.TrimSpace(p.Name)
}
if p.Status != nil {
update["status"] = *p.Status
}
if p.Cid != nil {
if *p.Cid != 1 && *p.Cid != 2 {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "cid 仅支持 1/2"}
_ = c.ServeJSON()
return
}
update["cid"] = *p.Cid
}
if p.Rights != nil {
update["rights"] = normalizeRights(p.Rights)
}
if len(update) == 0 {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "无更新字段"}
_ = c.ServeJSON()
return
}
_, err := models.Orm.QueryTable(new(models.AdminRole)).Filter("id", id).Update(update)
if err != nil {
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "更新失败"}
_ = c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success"}
_ = c.ServeJSON()
}
// DeleteRole 删除角色
// DELETE /platform/roles/:id
func (c *PlatformRoleController) DeleteRole() {
idStr := c.Ctx.Input.Param(":id")
id, _ := strconv.ParseUint(idStr, 10, 64)
if id == 0 {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "id 不能为空"}
_ = c.ServeJSON()
return
}
_, err := models.Orm.QueryTable(new(models.AdminRole)).Filter("id", id).Delete()
if err != nil {
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "删除失败"}
_ = c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success"}
_ = c.ServeJSON()
}