-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontend_route_rewrite.go
158 lines (147 loc) · 4.04 KB
/
frontend_route_rewrite.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package manager
import (
"strings"
"github.com/admpub/nging/v5/application/handler"
"github.com/admpub/nging/v5/application/library/common"
"github.com/admpub/webx/application/initialize/frontend"
"github.com/admpub/webx/application/model/official"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
)
func FrontendRouteRewrite(ctx echo.Context) error {
m := official.NewRouteRewrite(ctx)
cond := db.NewCompounds()
_, err := common.NewLister(m.OfficialCommonRouteRewrite, nil, func(r db.Result) db.Result {
return r.OrderBy(`-id`)
}, cond.And()).Paging(ctx)
if err != nil {
return err
}
list := m.Objects()
ctx.Set(`listData`, list)
return ctx.Render(`official/manager/frontend/route_rewrite`, handler.Err(ctx, err))
}
func FrontendRouteList(ctx echo.Context) error {
prefix := ctx.Form(`prefix`)
size := ctx.Formx(`size`).Int()
if size < 1 {
size = 10
}
var result []string
if len(prefix) > 0 {
routes := frontend.IRegister().Routes()
var i int
for _, route := range routes {
if strings.HasPrefix(route.Path, prefix) {
result = append(result, route.Path)
i++
if i >= size {
break
}
}
}
}
data := ctx.Data()
data.SetData(result)
return ctx.JSON(data)
}
func FrontendRouteRewriteAdd(ctx echo.Context) error {
if ctx.Form(`op`) == `routeList` {
return FrontendRouteList(ctx)
}
var err error
m := official.NewRouteRewrite(ctx)
if ctx.IsPost() {
err = ctx.MustBind(m.OfficialCommonRouteRewrite, echo.ExcludeFieldName(`updated`))
if err == nil {
_, err = m.Add()
if err != nil {
goto END
}
err = frontend.ResetRouteRewrite()
if err != nil {
goto END
}
handler.SendOk(ctx, ctx.T(`操作成功`))
return ctx.Redirect(handler.URLFor(`/manager/frontend/route_rewrite`))
}
} else {
id := ctx.Formx(`copyId`).Uint64()
if id > 0 {
err = m.Get(nil, `id`, id)
if err == nil {
echo.StructToForm(ctx, m.OfficialCommonRouteRewrite, ``, echo.LowerCaseFirstLetter)
ctx.Request().Form().Set(`id`, `0`)
}
}
}
END:
ctx.Set(`activeURL`, `/manager/frontend/route_rewrite`)
ctx.Set(`title`, ctx.T(`添加自定义网址`))
return ctx.Render(`official/manager/frontend/route_rewrite_edit`, err)
}
func FrontendRouteRewriteEdit(ctx echo.Context) error {
if ctx.Form(`op`) == `routeList` {
return FrontendRouteList(ctx)
}
var err error
id := ctx.Formx(`id`).Uint64()
m := official.NewRouteRewrite(ctx)
err = m.Get(nil, db.Cond{`id`: id})
if ctx.IsPost() {
err = ctx.MustBind(m.OfficialCommonRouteRewrite, echo.ExcludeFieldName(`created`))
if err != nil {
goto END
}
err = m.Edit(nil, db.Cond{`id`: id})
if err != nil {
goto END
}
handler.SendOk(ctx, ctx.T(`操作成功`))
return ctx.Redirect(handler.URLFor(`/manager/frontend/route_rewrite`))
} else if ctx.IsAjax() {
disabled := ctx.Query(`disabled`)
if len(disabled) > 0 {
if !common.IsBoolFlag(disabled) {
return ctx.NewError(code.InvalidParameter, ``).SetZone(`disabled`)
}
m.Disabled = disabled
data := ctx.Data()
err = m.UpdateField(nil, `disabled`, disabled, db.Cond{`id`: id})
if err != nil {
data.SetError(err)
return ctx.JSON(data)
}
data.SetInfo(ctx.T(`操作成功`))
return ctx.JSON(data)
}
} else if err == nil {
echo.StructToForm(ctx, m.OfficialCommonRouteRewrite, ``, echo.LowerCaseFirstLetter)
}
END:
ctx.Set(`activeURL`, `/manager/frontend/route_rewrite`)
ctx.Set(`title`, ctx.T(`编辑自定义网址`))
return ctx.Render(`official/manager/frontend/route_rewrite_edit`, err)
}
func FrontendRouteRewriteDelete(ctx echo.Context) error {
ids := ctx.FormxValues(`id`).Uint64(func(index int, value uint64) bool {
return value > 0
})
m := official.NewRouteRewrite(ctx)
var err error
for _, _v := range ids {
if err = m.Delete(nil, db.Cond{`id`: _v}); err != nil {
break
}
}
if err == nil {
err = frontend.ResetRouteRewrite()
}
if err == nil {
handler.SendOk(ctx, ctx.T(`操作成功`))
} else {
handler.SendFail(ctx, err.Error())
}
return ctx.Redirect(handler.URLFor(`/manager/frontend/route_rewrite`))
}