From 9c8fe21d1c3146f7ec000373a2e97be763a5c620 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 6 Aug 2018 22:38:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84gpage=E9=9D=99=E6=80=81?= =?UTF-8?q?=E5=88=86=E9=A1=B5=E7=A4=BA=E4=BE=8B=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/util/gpage/gpage.go | 51 ++++++++++++++++++++------------- geg/other/test.go | 16 +++++------ geg/util/gpage/gpage_static2.go | 2 +- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/g/util/gpage/gpage.go b/g/util/gpage/gpage.go index a8ecbb113..c05715dd5 100644 --- a/g/util/gpage/gpage.go +++ b/g/util/gpage/gpage.go @@ -15,6 +15,7 @@ import ( "gitee.com/johng/gf/g/net/ghttp" "gitee.com/johng/gf/g/util/gregex" "gitee.com/johng/gf/g/util/gstr" + "strings" ) // 分页对象 @@ -246,38 +247,48 @@ func (page *Page) GetContent(mode int) string { // 为指定的页面返回地址值 func (page *Page) GetUrl(pageNo int) string { + // 复制一个URL对象 url := *page.Url + if len(page.UrlTemplate) == 0 && page.Router != nil { + page.UrlTemplate = page.makeUrlTemplate(url.Path, page.Router) + } if len(page.UrlTemplate) > 0 { // 指定URL生成模板 url.Path = gstr.Replace(page.UrlTemplate, "{.page}", gconv.String(pageNo)) return url.String() } - if page.Router != nil { - // Router的规则与ghttp高度耦合 - if page.Router != nil { - match1, _ := gregex.MatchString(page.Router.RegRule, page.Router.Uri) - match2, _ := gregex.MatchString(page.Router.RegRule, url.Path) - if len(match1) > 1 && len(match1) == len(match2) { - path := page.Router.Uri - rule := fmt.Sprintf(`^[:\*]%s|\{%s\}`, page.PageName, page.PageName) - for i := 1; i < len(match1); i++ { - replace := match2[i] - if gregex.IsMatchString(rule, match1[i]) { - replace = gconv.String(pageNo) - } - path = gstr.Replace(path, match1[i], replace) - } - url.Path = path - return url.String() - } - } - } + values := page.Url.Query() values.Set(page.PageName, gconv.String(pageNo)) url.RawQuery = values.Encode() return url.String() } +// 根据当前URL以及注册路由信息计算出对应的URL模板 +func (page *Page) makeUrlTemplate(url string, router *ghttp.Router) (tpl string) { + if page.Router != nil && len(router.RegNames) > 0 { + if match, err := gregex.MatchString(router.RegRule, url); err == nil && len(match) > 0 { + if len(match) > len(router.RegNames) { + tpl = router.Uri + hasPageName := false + for i, name := range router.RegNames { + rule := fmt.Sprintf(`[:\*]%s|\{%s\}`, name, name) + if !hasPageName && strings.Compare(name, page.PageName) == 0 { + hasPageName = true + tpl, _ = gregex.ReplaceString(rule, `{.page}`, tpl) + } else { + tpl, _ = gregex.ReplaceString(rule, match[i + 1], tpl) + } + } + if !hasPageName { + tpl = "" + } + } + } + } + return +} + // 获取链接地址 func (page *Page) GetLink(url, text, title, style string) string { if len(style) > 0 { diff --git a/geg/other/test.go b/geg/other/test.go index cb980fd94..b54b9da63 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,15 +1,15 @@ package main import ( - "gitee.com/johng/gf/g/os/gfile" + "fmt" + "gitee.com/johng/gf/g/util/gregex" ) func main() { - path := "/home/john/Documents/temp" - flags1 := gfile.IsFile(path) - if flags1 == true { - println("有") - } else { - println("无") - } + name := "page" + path := "/page/template/{page}.html" + rule := fmt.Sprintf(`{%s}`, name, name) + tpl, err := gregex.ReplaceString(rule, `{.page}`, path) + fmt.Println(err) + fmt.Println(tpl) } diff --git a/geg/util/gpage/gpage_static2.go b/geg/util/gpage/gpage_static2.go index 90b9d695b..f67372999 100644 --- a/geg/util/gpage/gpage_static2.go +++ b/geg/util/gpage/gpage_static2.go @@ -9,7 +9,7 @@ import ( func main() { s := g.Server() - s.BindHandler("/page/template/{page}.html", func(r *ghttp.Request){ + s.BindHandler("/:obj/*action/{page}.html", func(r *ghttp.Request){ page := gpage.New(100, 10, r.Get("page"), r.URL.String(), r.Router) buffer, _ := gview.ParseContent(`