201 lines
4.8 KiB
Go
201 lines
4.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"server/models"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
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)
|
|
isLocked := uint8(0)
|
|
if cid == 2 {
|
|
isLocked = 1
|
|
}
|
|
role := &models.AdminRole{
|
|
Cid: cid,
|
|
IsLocked: isLocked,
|
|
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()
|
|
}
|
|
|
|
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.Cid == 2 {
|
|
update["is_locked"] = uint8(1)
|
|
} else {
|
|
update["is_locked"] = uint8(0)
|
|
}
|
|
}
|
|
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()
|
|
}
|
|
|
|
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()
|
|
}
|