调试frontend
This commit is contained in:
+1087
-1037
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,154 @@
|
||||
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()
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -37,8 +39,54 @@ func (c *IndexPortalController) getTidByHost() uint64 {
|
||||
return 1 // 默认租户
|
||||
}
|
||||
|
||||
// GetHeadMenu GET /index/headmenu
|
||||
// 获取后端菜单数据(支持开发环境租户切换)
|
||||
func (c *IndexPortalController) GetHeadMenu() {
|
||||
// DEV 环境下支持通过 X-Tenant-ID 切换租户
|
||||
runmode, _ := beego.AppConfig.String("runmode")
|
||||
tenantName := c.Ctx.Request.Header.Get("X-Tenant-ID")
|
||||
|
||||
// 解码
|
||||
if decoded, err := url.QueryUnescape(tenantName); err == nil {
|
||||
tenantName = decoded
|
||||
}
|
||||
|
||||
// 强制打印,确保你能从终端看到真实值
|
||||
fmt.Printf("[DEBUG] runmode: %s, Decoded TenantName: %s\n", runmode, tenantName)
|
||||
|
||||
if runmode == "dev" && tenantName != "" {
|
||||
// 查找租户 ID - 支持 name 或 short_name
|
||||
var tenant models.SystemTenant
|
||||
// 使用 OR 逻辑查询 name 或 short_name
|
||||
cond := orm.NewCondition()
|
||||
cond = cond.Or("tenant_name", tenantName).Or("tenant_short_name", tenantName)
|
||||
|
||||
err := models.Orm.QueryTable(new(models.SystemTenant)).
|
||||
SetCond(cond).
|
||||
One(&tenant)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("[DEBUG] QueryTenant Error: %v, Looking for: %s\n", err, tenantName)
|
||||
} else {
|
||||
fmt.Printf("[DEBUG] Found Tenant ID: %d\n", tenant.ID)
|
||||
// 查询该租户的自定义菜单
|
||||
var menus []models.BackendMenuFront
|
||||
_, err = models.Orm.QueryTable("yz_backend_menu_front").
|
||||
Filter("tenant_id", tenant.ID).
|
||||
Filter("delete_time__isnull", true).
|
||||
OrderBy("sort").
|
||||
All(&menus)
|
||||
if err == nil && len(menus) > 0 {
|
||||
// 转换数据格式供前端使用
|
||||
menuData := buildFrontMenuTree(menus, 0)
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": menuData}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
} else {
|
||||
fmt.Printf("[DEBUG] No menus found for tenant: %d\n", tenant.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 前台的静态导航菜单结构
|
||||
menuData := []map[string]interface{}{
|
||||
{
|
||||
@@ -103,6 +151,27 @@ func (c *IndexPortalController) GetHeadMenu() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// 辅助构建树结构
|
||||
func buildFrontMenuTree(menus []models.BackendMenuFront, pid int64) []map[string]interface{} {
|
||||
var tree []map[string]interface{}
|
||||
for _, m := range menus {
|
||||
if int64(m.Pid) == pid {
|
||||
node := map[string]interface{}{
|
||||
"id": m.ID,
|
||||
"title": m.Title,
|
||||
"path": m.Path,
|
||||
"type": m.Type,
|
||||
}
|
||||
children := buildFrontMenuTree(menus, int64(m.ID))
|
||||
if len(children) > 0 {
|
||||
node["children"] = children
|
||||
}
|
||||
tree = append(tree, node)
|
||||
}
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
||||
// GetFooterData GET /index/footerdata
|
||||
func (c *IndexPortalController) GetFooterData() {
|
||||
tid := c.getTidByHost()
|
||||
@@ -296,7 +365,7 @@ func (c *IndexPortalController) GetCompanyNewsDetail() {
|
||||
c.getArticleDetail()
|
||||
}
|
||||
|
||||
// GetKingdeeNewsDetail GET /index/kingdeenews/detail/:id
|
||||
// GetKingdeeNewsDetail GET /index/kingdeeNews/detail/:id
|
||||
func (c *IndexPortalController) GetKingdeeNewsDetail() {
|
||||
c.getArticleDetail()
|
||||
}
|
||||
@@ -393,4 +462,4 @@ func (c *IndexPortalController) GetOnePageByPath() {
|
||||
"data": nil,
|
||||
}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user