forked from GoAdminGroup/go-admin
/
echo.go
135 lines (111 loc) · 3.26 KB
/
echo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package echo
import (
"bytes"
"errors"
"github.com/labstack/echo"
"github.com/chenhg5/go-admin/context"
"github.com/chenhg5/go-admin/plugins"
"strings"
"github.com/chenhg5/go-admin/engine"
"net/http"
"github.com/chenhg5/go-admin/template/types"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/template"
template2 "html/template"
"github.com/chenhg5/go-admin/modules/menu"
)
type Echo struct {
}
func init() {
engine.Register(new(Echo))
}
func (e *Echo) Use(router interface{}, plugin []plugins.Plugin) error {
var (
eng *echo.Echo
ok bool
)
if eng, ok = router.(*echo.Echo); !ok {
return errors.New("wrong parameter")
}
for _, plug := range plugin {
var plugCopy plugins.Plugin
plugCopy = plug
for _, req := range plug.GetRequest() {
eng.Add(strings.ToUpper(req.Method), req.URL, func(c echo.Context) error {
ctx := context.NewContext(c.Request())
for _, key := range c.ParamNames() {
if c.Request().URL.RawQuery == "" {
c.Request().URL.RawQuery += strings.Replace(key, ":", "", -1) + "=" + c.Param(key)
} else {
c.Request().URL.RawQuery += "&" + strings.Replace(key, ":", "", -1) + "=" + c.Param(key)
}
}
plugCopy.GetHandler(c.Request().URL.Path, strings.ToLower(c.Request().Method))(ctx)
for key, head := range ctx.Response.Header {
c.Response().Header().Set(key, head[0])
}
if ctx.Response.Body != nil {
buf := new(bytes.Buffer)
buf.ReadFrom(ctx.Response.Body)
c.String(ctx.Response.StatusCode, buf.String())
}
return nil
})
}
}
return nil
}
func (e *Echo) Content(contextInterface interface{}, c types.GetPanel) {
var (
ctx echo.Context
ok bool
)
if ctx, ok = contextInterface.(echo.Context); !ok {
panic("wrong parameter")
}
globalConfig := config.Get()
sesKey, err := ctx.Cookie("go_admin_session")
if err != nil || sesKey == nil {
ctx.Redirect(http.StatusFound, "/"+globalConfig.PREFIX+"/login")
return
}
userId, ok := auth.Driver.Load(sesKey.Value)["user_id"]
if !ok {
ctx.Redirect(http.StatusFound, "/"+globalConfig.PREFIX+"/login")
return
}
user, ok := auth.GetCurUserById(userId.(string))
if !ok {
ctx.Redirect(http.StatusFound, "/"+globalConfig.PREFIX+"/login")
return
}
var panel types.Panel
if !auth.CheckPermissions(user, ctx.Path(), ctx.Request().Method) {
alert := template.Get(globalConfig.THEME).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
SetTheme("warning").SetContent(template2.HTML("没有权限")).GetContent()
panel = types.Panel{
Content: alert,
Description: "Error",
Title: "Error",
}
} else {
panel = c()
}
tmpl, tmplName := template.Get(globalConfig.THEME).GetTemplate(ctx.Request().Header.Get("X-PJAX") == "true")
ctx.Request().Header.Set("Content-Type", "text/html; charset=utf-8")
buf := new(bytes.Buffer)
tmpl.ExecuteTemplate(buf, tmplName, types.Page{
User: user,
Menu: menu.GetGlobalMenu(user),
System: types.SystemInfo{
"0.0.1",
},
Panel: panel,
AssertRootUrl: "/" + globalConfig.PREFIX,
Title: globalConfig.TITLE,
Logo: globalConfig.LOGO,
MiniLogo: globalConfig.MINILOGO,
})
ctx.String(http.StatusOK, buf.String())
}