Skip to content

Commit d8dc2bd

Browse files
committed
chore: 优化Template启动函数
1 parent d3eac61 commit d8dc2bd

40 files changed

+349
-211
lines changed

app/admin/uploads/image.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,31 @@ import (
1818

1919
type Image struct {
2020
upload.Template
21+
GetListPath string // 获取文件列表路由路径
22+
DeletePath string // 文件删除路由路径
23+
CropPath string // 图片裁剪路由路径
24+
}
25+
26+
// 启动模版
27+
func (p *Image) Bootstrap() interface{} {
28+
p.GetListPath = "/api/admin/upload/:resource/getList" // 获取文件列表路由路径
29+
p.DeletePath = "/api/admin/upload/:resource/delete" // 文件删除路由路径
30+
p.CropPath = "/api/admin/upload/:resource/crop" // 图片裁剪路由路径
31+
p.HandlePath = "/api/admin/upload/:resource/handle" // 文件上传路由路径
32+
p.Base64HandlePath = "/api/admin/upload/:resource/base64Handle" // Base64文件上传路由路径
33+
34+
return p
35+
}
36+
37+
// 加载初始化路由
38+
func (p *Image) LoadInitRoute() interface{} {
39+
p.GET(p.GetListPath, p.GetList)
40+
p.Any(p.DeletePath, p.Delete)
41+
p.POST(p.CropPath, p.Crop)
42+
p.POST(p.HandlePath, p.Handle)
43+
p.POST(p.Base64HandlePath, p.HandleFromBase64)
44+
45+
return p
2146
}
2247

2348
// 初始化
@@ -40,17 +65,6 @@ func (p *Image) Init(ctx *quark.Context) interface{} {
4065
return p
4166
}
4267

43-
// 初始化路由映射
44-
func (p *Image) RouteInit() interface{} {
45-
p.GET("/api/admin/upload/:resource/getList", p.GetList)
46-
p.Any("/api/admin/upload/:resource/delete", p.Delete)
47-
p.POST("/api/admin/upload/:resource/crop", p.Crop)
48-
p.POST("/api/admin/upload/:resource/handle", p.Handle)
49-
p.POST("/api/admin/upload/:resource/base64Handle", p.HandleFromBase64)
50-
51-
return p
52-
}
53-
5468
// 获取文件列表
5569
func (p *Image) GetList(ctx *quark.Context) error {
5670
imageListReq := request.ImageListReq{}

context.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,8 @@ func (p *Context) getTemplate(ctx *Context) (interface{}, error) {
635635

636636
// 模版参数初始化
637637
provider.(interface {
638-
TemplateInit(ctx *Context) interface{}
639-
}).TemplateInit(ctx)
638+
LoadInitData(ctx *Context) interface{}
639+
}).LoadInitData(ctx)
640640

641641
// 实例初始化
642642
provider.(interface {
@@ -645,8 +645,8 @@ func (p *Context) getTemplate(ctx *Context) (interface{}, error) {
645645

646646
// 初始化路由
647647
provider.(interface {
648-
RouteInit() interface{}
649-
}).RouteInit()
648+
LoadInitRoute() interface{}
649+
}).LoadInitRoute()
650650

651651
// 加载自定义路由
652652
provider.(interface {

engine.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,15 @@ func (p *Engine) initPaths() {
203203
}
204204
for _, provider := range p.providers {
205205

206-
// 初始化路由
206+
// 启动模版
207207
provider.(interface {
208-
RouteInit() interface{}
209-
}).RouteInit()
208+
Bootstrap() interface{}
209+
}).Bootstrap()
210+
211+
// 加载初始化路由
212+
provider.(interface {
213+
LoadInitRoute() interface{}
214+
}).LoadInitRoute()
210215

211216
// 加载自定义路由
212217
provider.(interface {
@@ -235,10 +240,10 @@ func (p *Engine) initPaths() {
235240
// 解析行为
236241
for _, av := range actions {
237242

238-
// 模版初始化
243+
// 加载初始化数据
239244
av.(interface {
240-
TemplateInit(ctx *Context) interface{}
241-
}).TemplateInit(&Context{})
245+
LoadInitData(ctx *Context) interface{}
246+
}).LoadInitData(&Context{})
242247

243248
// uri唯一标识
244249
uriKey := av.(interface {

examples/hertzadmin/biz/handler/resources/demo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Demo struct {
1313
func (p *Demo) Init(ctx *quark.Context) interface{} {
1414

1515
// 初始化模板
16-
p.TemplateInit(ctx)
16+
p.LoadInitData(ctx)
1717

1818
return p
1919
}

template.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ import (
99
// 模版接口
1010
type Templater interface {
1111

12-
// 初始化
13-
Init(ctx *Context) interface{}
12+
// 启动模版
13+
Bootstrap() interface{}
14+
15+
// 加载初始化路由
16+
LoadInitRoute() interface{}
1417

15-
// 初始化模板
16-
TemplateInit(ctx *Context) interface{}
18+
// 加载初始化数据
19+
LoadInitData(ctx *Context) interface{}
1720

18-
// 初始化路由
19-
RouteInit() interface{}
21+
// 模版初始化
22+
Init(ctx *Context) interface{}
2023

2124
// 自定义路由
2225
Route() interface{}

template/admin/dashboard/dashboard.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,27 @@ import (
1515
// 后台登录模板
1616
type Template struct {
1717
quark.Template
18-
Title string // 页面标题
19-
SubTitle string // 页面子标题
20-
BackIcon bool // 页面是否携带返回Icon
18+
IndexPath string // 路由路径
19+
Title string // 页面标题
20+
SubTitle string // 页面子标题
21+
BackIcon bool // 页面是否携带返回Icon
2122
}
2223

23-
// 初始化
24-
func (p *Template) Init(ctx *quark.Context) interface{} {
25-
p.TemplateInit(ctx)
24+
// 启动模版
25+
func (p *Template) Bootstrap() interface{} {
26+
p.IndexPath = "/api/admin/dashboard/:resource/index" // 路由路径
27+
return p
28+
}
29+
30+
// 加载初始化路由
31+
func (p *Template) LoadInitRoute() interface{} {
32+
p.GET(p.IndexPath, p.Render) // 后台仪表盘路由
2633

2734
return p
2835
}
2936

30-
// 初始化模板
31-
func (p *Template) TemplateInit(ctx *quark.Context) interface{} {
37+
// 加载初始化数据
38+
func (p *Template) LoadInitData(ctx *quark.Context) interface{} {
3239

3340
// 初始化数据对象
3441
p.DB = db.Client
@@ -42,10 +49,8 @@ func (p *Template) TemplateInit(ctx *quark.Context) interface{} {
4249
return p
4350
}
4451

45-
// 初始化路由映射
46-
func (p *Template) RouteInit() interface{} {
47-
p.GET("/api/admin/dashboard/:resource/index", p.Render) // 后台仪表盘路由
48-
52+
// 初始化
53+
func (p *Template) Init(ctx *quark.Context) interface{} {
4954
return p
5055
}
5156

template/admin/layout/layout.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
// 后台登录模板
1515
type Template struct {
1616
quark.Template
17+
IndexPath string // 路由路径
1718
Title string // layout 的左上角 的 title
1819
Logo interface{} // layout 的左上角 的 logo
1920
Actions interface{} // layout 的头部行为
@@ -31,13 +32,21 @@ type Template struct {
3132
RightMenus []interface{} // 右上角菜单
3233
}
3334

34-
// 初始化
35-
func (p *Template) Init(ctx *quark.Context) interface{} {
35+
// 启动模版
36+
func (p *Template) Bootstrap() interface{} {
37+
p.IndexPath = "/api/admin/layout/:resource/index" // 路由路径
3638
return p
3739
}
3840

39-
// 初始化模板
40-
func (p *Template) TemplateInit(ctx *quark.Context) interface{} {
41+
// 加载初始化路由
42+
func (p *Template) LoadInitRoute() interface{} {
43+
p.GET(p.IndexPath, p.Render) // 获取布局配置
44+
45+
return p
46+
}
47+
48+
// 加载初始化数据
49+
func (p *Template) LoadInitData(ctx *quark.Context) interface{} {
4150

4251
// 初始化数据对象
4352
p.DB = db.Client
@@ -130,10 +139,8 @@ func (p *Template) TemplateInit(ctx *quark.Context) interface{} {
130139
return p
131140
}
132141

133-
// 初始化路由映射
134-
func (p *Template) RouteInit() interface{} {
135-
p.GET("/api/admin/layout/:resource/index", p.Render) // 获取布局配置
136-
142+
// 初始化
143+
func (p *Template) Init(ctx *quark.Context) interface{} {
137144
return p
138145
}
139146

template/admin/login/login.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,42 @@ import (
1818
// 后台登录模板
1919
type Template struct {
2020
quark.Template
21-
Api string // 登录接口
22-
Redirect string // 登录后跳转地址
23-
Logo interface{} // 登录页面Logo
24-
Title string // 标题
25-
SubTitle string // 子标题
26-
Body interface{} `json:"body,omitempty"` // 表单内容
21+
IndexPath string // 登录页面路由
22+
HandlePath string // 登录执行路由
23+
CaptchaIdPath string // 登录获取验证码ID路由
24+
CaptchaPath string // 登录验证码路由
25+
LogoutPath string // 退出执行路由
26+
Api string // 登录接口
27+
Redirect string // 登录后跳转地址
28+
Logo interface{} // 登录页面Logo
29+
Title string // 标题
30+
SubTitle string // 子标题
31+
Body interface{} `json:"body,omitempty"` // 表单内容
2732
}
2833

29-
// 初始化
30-
func (p *Template) Init(ctx *quark.Context) interface{} {
34+
// 启动模版
35+
func (p *Template) Bootstrap() interface{} {
36+
p.IndexPath = "/api/admin/login/:resource/index" // 登录页面路由
37+
p.HandlePath = "/api/admin/login/:resource/handle" // 登录执行路由
38+
p.CaptchaIdPath = "/api/admin/login/:resource/captchaId" // 登录获取验证码ID路由
39+
p.CaptchaPath = "/api/admin/login/:resource/captcha/:id" // 登录验证码路由
40+
p.LogoutPath = "/api/admin/logout/:resource/handle" // 退出执行路由
41+
return p
42+
}
43+
44+
// 初始化路由映射
45+
func (p *Template) LoadInitRoute() interface{} {
46+
p.GET(p.IndexPath, p.Render) // 登录页面路由
47+
p.POST(p.HandlePath, p.Handle) // 登录执行路由
48+
p.GET(p.CaptchaIdPath, p.CaptchaId) // 登录获取验证码ID路由
49+
p.GET(p.CaptchaPath, p.Captcha) // 登录验证码路由
50+
p.GET(p.LogoutPath, p.Logout) // 退出执行路由
51+
3152
return p
3253
}
3354

3455
// 初始化模板
35-
func (p *Template) TemplateInit(ctx *quark.Context) interface{} {
56+
func (p *Template) LoadInitData(ctx *quark.Context) interface{} {
3657

3758
// 初始化数据对象
3859
p.DB = db.Client
@@ -60,14 +81,8 @@ func (p *Template) TemplateInit(ctx *quark.Context) interface{} {
6081
return p
6182
}
6283

63-
// 初始化路由映射
64-
func (p *Template) RouteInit() interface{} {
65-
p.GET("/api/admin/login/:resource/index", p.Render) // 渲染登录页面路由
66-
p.POST("/api/admin/login/:resource/handle", p.Handle) // 后台登录执行路由
67-
p.GET("/api/admin/login/:resource/captchaId", p.CaptchaId) // 后台登录获取验证码ID路由
68-
p.GET("/api/admin/login/:resource/captcha/:id", p.Captcha) // 后台登录验证码路由
69-
p.GET("/api/admin/logout/:resource/handle", p.Logout) // 后台退出执行路由
70-
84+
// 初始化
85+
func (p *Template) Init(ctx *quark.Context) interface{} {
7186
return p
7287
}
7388

template/admin/module.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ func Middleware(ctx *quark.Context) error {
6363
// 获取登录实例
6464
loginInstance := &logins.Index{}
6565

66+
// 启动模版
67+
loginInstance.Bootstrap()
68+
6669
// 初始化路由
67-
loginInstance.RouteInit()
70+
loginInstance.LoadInitRoute()
6871

6972
// 加载自定义路由
7073
loginInstance.Route()

template/admin/resource/actions/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (p *Action) Init(ctx *quark.Context) interface{} {
9999
}
100100

101101
// 初始化模板
102-
func (p *Action) TemplateInit(ctx *quark.Context) interface{} {
102+
func (p *Action) LoadInitData(ctx *quark.Context) interface{} {
103103
p.ActionType = "ajax"
104104

105105
return p

0 commit comments

Comments
 (0)