forked from GoAdminGroup/go-admin
/
beego.go
133 lines (112 loc) · 3.29 KB
/
beego.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
package beego
import (
"bytes"
"errors"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
gctx "github.com/chenhg5/go-admin/context"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/plugins"
"github.com/chenhg5/go-admin/template"
"github.com/chenhg5/go-admin/template/types"
template2 "html/template"
"net/http"
"strings"
"github.com/chenhg5/go-admin/engine"
)
type Beego struct {
}
func init() {
engine.Register(new(Beego))
}
func (bee *Beego) Use(router interface{}, plugin []plugins.Plugin) error {
var (
eng *beego.App
ok bool
)
if eng, ok = router.(*beego.App); !ok {
return errors.New("wrong parameter")
}
for _, plug := range plugin {
var plugCopy plugins.Plugin
plugCopy = plug
for _, req := range plug.GetRequest() {
eng.Handlers.AddMethod(req.Method, req.URL, func(c *context.Context) {
for key, value := range c.Input.Params() {
if c.Request.URL.RawQuery == "" {
c.Request.URL.RawQuery += strings.Replace(key, ":", "", -1) + "=" + value
} else {
c.Request.URL.RawQuery += "&" + strings.Replace(key, ":", "", -1) + "=" + value
}
}
ctx := gctx.NewContext(c.Request)
plugCopy.GetHandler(c.Request.URL.Path, strings.ToLower(c.Request.Method))(ctx)
for key, head := range ctx.Response.Header {
c.ResponseWriter.Header().Add(key, head[0])
}
c.ResponseWriter.WriteHeader(ctx.Response.StatusCode)
if ctx.Response.Body != nil {
buf := new(bytes.Buffer)
buf.ReadFrom(ctx.Response.Body)
c.WriteString(buf.String())
}
})
}
}
return nil
}
func (bee *Beego) Content(contextInterface interface{}, c types.GetPanel) {
var (
ctx *context.Context
ok bool
)
if ctx, ok = contextInterface.(*context.Context); !ok {
panic("wrong parameter")
}
globalConfig := config.Get()
sesKey := ctx.GetCookie("go_admin_session")
if sesKey == "" {
ctx.Redirect(http.StatusFound, "/"+globalConfig.PREFIX+"/login")
return
}
userId, ok := auth.Driver.Load(sesKey)["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.Request.URL.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.ResponseWriter.Header().Add("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.WriteString(buf.String())
}