Skip to content

Commit

Permalink
feat(menu): support three level menus
Browse files Browse the repository at this point in the history
  • Loading branch information
cg33 committed Apr 22, 2020
1 parent 631247f commit 6dee16a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 29 deletions.
71 changes: 55 additions & 16 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/json"
errors2 "errors"
"fmt"
"github.com/GoAdminGroup/go-admin/modules/system"
template2 "html/template"
"net/http"
"runtime/debug"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/GoAdminGroup/go-admin/modules/logger"
"github.com/GoAdminGroup/go-admin/modules/menu"
"github.com/GoAdminGroup/go-admin/modules/service"
"github.com/GoAdminGroup/go-admin/modules/system"
"github.com/GoAdminGroup/go-admin/modules/ui"
"github.com/GoAdminGroup/go-admin/plugins"
"github.com/GoAdminGroup/go-admin/plugins/admin"
Expand Down Expand Up @@ -288,10 +288,8 @@ func (eng *Engine) ClonedBySetter(setter Setter) *Engine {
return eng
}

// wrapWithAuthMiddleware wrap a auth middleware to the given handler.
func (eng *Engine) wrapWithAuthMiddleware(handler context.Handler) context.Handlers {
conn := db.GetConnection(eng.Services)
return []context.Handler{func(ctx *context.Context) {
func (eng *Engine) deferHandler(conn db.Connection) context.Handler {
return func(ctx *context.Context) {
defer func(ctx *context.Context) {
if user, ok := ctx.UserValue["user"].(models.UserModel); ok {
var input []byte
Expand Down Expand Up @@ -332,7 +330,19 @@ func (eng *Engine) wrapWithAuthMiddleware(handler context.Handler) context.Handl
}
}(ctx)
ctx.Next()
}, response.OffLineHandler, auth.Middleware(conn), handler}
}
}

// wrapWithAuthMiddleware wrap a auth middleware to the given handler.
func (eng *Engine) wrapWithAuthMiddleware(handler context.Handler) context.Handlers {
conn := db.GetConnection(eng.Services)
return []context.Handler{eng.deferHandler(conn), response.OffLineHandler, auth.Middleware(conn), handler}
}

// wrapWithAuthMiddleware wrap a auth middleware to the given handler.
func (eng *Engine) wrap(handler context.Handler) context.Handlers {
conn := db.GetConnection(eng.Services)
return []context.Handler{eng.deferHandler(conn), response.OffLineHandler, handler}
}

// ============================
Expand Down Expand Up @@ -366,14 +376,18 @@ func Content(ctx interface{}, panel types.GetPanelFn) {
}

// Data inject the route and corresponding handler to the web framework.
func (eng *Engine) Data(method, url string, handler context.Handler) {
eng.Adapter.AddHandler(method, url, eng.wrapWithAuthMiddleware(handler))
func (eng *Engine) Data(method, url string, handler context.Handler, noAuth ...bool) {
if len(noAuth) > 0 && noAuth[0] {
eng.Adapter.AddHandler(method, url, eng.wrap(handler))
} else {
eng.Adapter.AddHandler(method, url, eng.wrapWithAuthMiddleware(handler))
}
}

// HTML inject the route and corresponding handler wrapped by the given function to the web framework.
func (eng *Engine) HTML(method, url string, fn types.GetPanelInfoFn) {
func (eng *Engine) HTML(method, url string, fn types.GetPanelInfoFn, noAuth ...bool) {

eng.Adapter.AddHandler(method, url, eng.wrapWithAuthMiddleware(func(ctx *context.Context) {
var handler = func(ctx *context.Context) {
panel, err := fn(ctx)
if err != nil {
panel = template.WarningPanel(err.Error())
Expand All @@ -397,13 +411,20 @@ func (eng *Engine) HTML(method, url string, fn types.GetPanelInfoFn) {
}

ctx.HTMLByte(http.StatusOK, buf.Bytes())
}))
}

if len(noAuth) > 0 && noAuth[0] {
eng.Adapter.AddHandler(method, url, eng.wrap(handler))
} else {
eng.Adapter.AddHandler(method, url, eng.wrapWithAuthMiddleware(handler))
}
}

// HTMLFile inject the route and corresponding handler which returns the panel content of given html file path
// to the web framework.
func (eng *Engine) HTMLFile(method, url, path string, data map[string]interface{}) {
eng.Adapter.AddHandler(method, url, eng.wrapWithAuthMiddleware(func(ctx *context.Context) {
func (eng *Engine) HTMLFile(method, url, path string, data map[string]interface{}, noAuth ...bool) {

var handler = func(ctx *context.Context) {

cbuf := new(bytes.Buffer)

Expand Down Expand Up @@ -438,13 +459,31 @@ func (eng *Engine) HTMLFile(method, url, path string, data map[string]interface{
}

ctx.HTMLByte(http.StatusOK, buf.Bytes())
}))
}

if len(noAuth) > 0 && noAuth[0] {
eng.Adapter.AddHandler(method, url, eng.wrap(handler))
} else {
eng.Adapter.AddHandler(method, url, eng.wrapWithAuthMiddleware(handler))
}
}

// HTMLFiles inject the route and corresponding handler which returns the panel content of given html files path
// to the web framework.
func (eng *Engine) HTMLFiles(method, url string, data map[string]interface{}, files ...string) {
eng.Adapter.AddHandler(method, url, eng.wrapWithAuthMiddleware(func(ctx *context.Context) {
eng.Adapter.AddHandler(method, url, eng.wrapWithAuthMiddleware(eng.htmlFilesHandler(data, files...)))
}

// HTMLFilesNoAuth inject the route and corresponding handler which returns the panel content of given html files path
// to the web framework without auth check.
func (eng *Engine) HTMLFilesNoAuth(method, url string, data map[string]interface{}, files ...string) {
eng.Adapter.AddHandler(method, url, eng.wrap(eng.htmlFilesHandler(data, files...)))
}

// HTMLFiles inject the route and corresponding handler which returns the panel content of given html files path
// to the web framework.
func (eng *Engine) htmlFilesHandler(data map[string]interface{}, files ...string) context.Handler {
return func(ctx *context.Context) {

cbuf := new(bytes.Buffer)

Expand Down Expand Up @@ -479,7 +518,7 @@ func (eng *Engine) HTMLFiles(method, url string, data map[string]interface{}, fi
}

ctx.HTMLByte(http.StatusOK, buf.Bytes())
}))
}
}

// errorPanelHTML add an error panel html to context response.
Expand Down
2 changes: 1 addition & 1 deletion plugins/admin/controller/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (h *Handler) MenuOrder(ctx *context.Context) {
var data []map[string]interface{}
_ = json.Unmarshal([]byte(ctx.FormValue("_order")), &data)

models.Menu().SetConn(h.conn).ResetOrder(data)
models.Menu().SetConn(h.conn).ResetOrder([]byte(ctx.FormValue("_order")))

response.Ok(ctx)
}
Expand Down
54 changes: 42 additions & 12 deletions plugins/admin/models/menu.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"encoding/json"
"github.com/GoAdminGroup/go-admin/modules/db"
"github.com/GoAdminGroup/go-admin/modules/db/dialect"
"strconv"
Expand Down Expand Up @@ -96,28 +97,57 @@ func (t MenuModel) Update(title, icon, uri, header string, parentId int64) (int6
})
}

type OrderItems []OrderItem

type OrderItem struct {
ID uint `json:"id"`
Children OrderItems `json:"children"`
}

// ResetOrder update the order of menu models.
func (t MenuModel) ResetOrder(data []map[string]interface{}) {
func (t MenuModel) ResetOrder(data []byte) {

var items OrderItems
_ = json.Unmarshal(data, &items)

count := 1
for _, v := range data {
if child, ok := v["children"]; ok {
for _, v := range items {
if len(v.Children) > 0 {
_, _ = t.Table(t.TableName).
Where("id", "=", v["id"]).Update(dialect.H{
Where("id", "=", v.ID).Update(dialect.H{
"order": count,
"parent_id": 0,
})

for _, v2 := range child.([]interface{}) {
_, _ = t.Table(t.TableName).
Where("id", "=", v2.(map[string]interface{})["id"]).Update(dialect.H{
"order": count,
"parent_id": v["id"],
})
count++
for _, v2 := range v.Children {
if len(v2.Children) > 0 {

_, _ = t.Table(t.TableName).
Where("id", "=", v2.ID).Update(dialect.H{
"order": count,
"parent_id": v.ID,
})

for _, v3 := range v2.Children {
_, _ = t.Table(t.TableName).
Where("id", "=", v3.ID).Update(dialect.H{
"order": count,
"parent_id": v2.ID,
})
count++
}
} else {
_, _ = t.Table(t.TableName).
Where("id", "=", v2.ID).Update(dialect.H{
"order": count,
"parent_id": v.ID,
})
count++
}
}
} else {
_, _ = t.Table(t.TableName).
Where("id", "=", v["id"]).Update(dialect.H{
Where("id", "=", v.ID).Update(dialect.H{
"order": count,
"parent_id": 0,
})
Expand Down

0 comments on commit 6dee16a

Please sign in to comment.