154 lines
3.9 KiB
Go
154 lines
3.9 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"server/models"
|
|
"server/pkg/jwtutil"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
type BackendMenuFrontController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
func (c *BackendMenuFrontController) checkAuth() (uint64, bool) {
|
|
authHeader := c.Ctx.Request.Header.Get("Authorization")
|
|
if authHeader == "" {
|
|
c.Data["json"] = map[string]interface{}{"code": 401, "msg": "未提供认证信息"}
|
|
_ = c.ServeJSON()
|
|
return 0, false
|
|
}
|
|
authParts := strings.SplitN(authHeader, " ", 2)
|
|
if len(authParts) != 2 || authParts[0] != "Bearer" {
|
|
c.Data["json"] = map[string]interface{}{"code": 401, "msg": "认证信息格式错误"}
|
|
_ = c.ServeJSON()
|
|
return 0, false
|
|
}
|
|
claims, err := jwtutil.ParseToken(authParts[1])
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 401, "msg": "token无效"}
|
|
_ = c.ServeJSON()
|
|
return 0, false
|
|
}
|
|
return uint64(claims.TenantId), true
|
|
}
|
|
|
|
func (c *BackendMenuFrontController) List() {
|
|
tid, ok := c.checkAuth()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
var menus []models.BackendMenuFront
|
|
_, err := models.Orm.QueryTable("yz_backend_menu_front").
|
|
Filter("tenant_id", tid).
|
|
Filter("delete_time__isnull", true).
|
|
OrderBy("sort").
|
|
All(&menus)
|
|
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "查询失败"}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": menus}
|
|
}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func (c *BackendMenuFrontController) Create() {
|
|
tid, ok := c.checkAuth()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
var menu models.BackendMenuFront
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &menu); err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
menu.TenantID = tid
|
|
menu.CreateTime = nil // 由orm自动处理
|
|
_, err := models.Orm.Insert(&menu)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "创建失败"}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "创建成功"}
|
|
}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func (c *BackendMenuFrontController) Update() {
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
tid, ok := c.checkAuth()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
var menu models.BackendMenuFront
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &menu); err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
num, err := models.Orm.QueryTable("yz_backend_menu_front").
|
|
Filter("id", id).
|
|
Filter("tenant_id", tid).
|
|
Update(map[string]interface{}{
|
|
"pid": menu.Pid,
|
|
"title": menu.Title,
|
|
"path": menu.Path,
|
|
"component_path": menu.ComponentPath,
|
|
"icon": menu.Icon,
|
|
"sort": menu.Sort,
|
|
"status": menu.Status,
|
|
"is_visible": menu.IsVisible,
|
|
"type": menu.Type,
|
|
"permission": menu.Permission,
|
|
"update_time": time.Now(),
|
|
})
|
|
|
|
if err != nil || num == 0 {
|
|
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "更新失败"}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "更新成功"}
|
|
}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func (c *BackendMenuFrontController) Delete() {
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
tid, ok := c.checkAuth()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
num, err := models.Orm.QueryTable("yz_backend_menu_front").
|
|
Filter("id", id).
|
|
Filter("tenant_id", tid).
|
|
Update(map[string]interface{}{"delete_time": time.Now()})
|
|
|
|
if err != nil || num == 0 {
|
|
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "删除失败"}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
|
|
}
|
|
_ = c.ServeJSON()
|
|
} |