2021-12-03 03:11:01 -08:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2021-12-06 11:53:28 -08:00
|
|
|
"time"
|
2021-12-03 03:11:01 -08:00
|
|
|
|
|
|
|
"goweb/msg"
|
2021-12-03 04:20:01 -08:00
|
|
|
"goweb/pager"
|
2021-12-03 03:11:01 -08:00
|
|
|
|
2021-12-06 18:51:21 -08:00
|
|
|
echomw "github.com/labstack/echo/v4/middleware"
|
2021-12-03 04:53:01 -08:00
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2021-12-03 04:20:01 -08:00
|
|
|
const (
|
|
|
|
DefaultItemsPerPage = 20
|
|
|
|
)
|
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
type Page struct {
|
|
|
|
AppName string
|
|
|
|
Title string
|
|
|
|
Context echo.Context
|
|
|
|
Reverse func(name string, params ...interface{}) string
|
|
|
|
Path string
|
|
|
|
Data interface{}
|
|
|
|
Layout string
|
|
|
|
Name string
|
|
|
|
IsHome bool
|
|
|
|
IsAuth bool
|
|
|
|
StatusCode int
|
|
|
|
Metatags struct {
|
|
|
|
Description string
|
|
|
|
Keywords []string
|
|
|
|
}
|
2021-12-07 18:36:57 -08:00
|
|
|
Pager pager.Pager
|
|
|
|
CSRF string
|
|
|
|
Headers map[string]string
|
|
|
|
Cache struct {
|
2021-12-11 16:32:34 -08:00
|
|
|
Enabled bool
|
|
|
|
Expiration time.Duration
|
|
|
|
Tags []string
|
2021-12-05 18:56:57 -08:00
|
|
|
}
|
2021-12-11 13:06:28 -08:00
|
|
|
RequestID string
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPage(c echo.Context) Page {
|
|
|
|
p := Page{
|
|
|
|
Context: c,
|
|
|
|
Reverse: c.Echo().Reverse,
|
|
|
|
Path: c.Request().URL.Path,
|
|
|
|
StatusCode: http.StatusOK,
|
2021-12-03 04:20:01 -08:00
|
|
|
Pager: pager.NewPager(c, DefaultItemsPerPage),
|
2021-12-07 18:36:57 -08:00
|
|
|
Headers: make(map[string]string),
|
2021-12-11 13:06:28 -08:00
|
|
|
RequestID: c.Response().Header().Get(echo.HeaderXRequestID),
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
p.IsHome = p.Path == "/"
|
|
|
|
|
2021-12-06 18:51:21 -08:00
|
|
|
if csrf := c.Get(echomw.DefaultCSRFConfig.ContextKey); csrf != nil {
|
2021-12-03 04:53:01 -08:00
|
|
|
p.CSRF = csrf.(string)
|
|
|
|
}
|
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Page) SetMessage(typ msg.Type, value string) {
|
|
|
|
msg.Set(p.Context, typ, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Page) GetMessages(typ msg.Type) []template.HTML {
|
|
|
|
strs := msg.Get(p.Context, typ)
|
|
|
|
ret := make([]template.HTML, len(strs))
|
|
|
|
for k, v := range strs {
|
|
|
|
ret[k] = template.HTML(v)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|