This commit is contained in:
2025-03-28 17:51:34 +08:00
parent da612380e0
commit 5c8802d2f0
68 changed files with 3422 additions and 630 deletions

View File

@@ -23,11 +23,11 @@ type HtmlData struct {
Data any
}
func HTML(w http.ResponseWriter, req *http.Request, tpl string, data map[string]any) {
rndr.html(w, req, tpl, data)
func HTML(w http.ResponseWriter, r *http.Request, tpl string, data map[string]any) {
rndr.HTML(w, r, tpl, data)
}
func (r *render) html(w http.ResponseWriter, req *http.Request, tpl string, data map[string]any) {
func (r *render) HTML(w http.ResponseWriter, req *http.Request, tpl string, data map[string]any) {
name := strings.ReplaceAll(tpl, "/", "_")
t, ok := r.templates[name]
if !ok {

View File

@@ -29,22 +29,34 @@ type ResponseList struct {
}
func JSON(w http.ResponseWriter, data any) {
rndr.json(w, data)
rndr.JSON(w, data)
}
func JSONF(w http.ResponseWriter, success bool, message string) {
rndr.json(w, Response{Success: success, Message: message})
rndr.JSONF(w, success, message)
}
func JSONOK(w http.ResponseWriter, message string) {
rndr.json(w, Response{Success: true, Message: message})
rndr.JSONOK(w, message)
}
func JSONERR(w http.ResponseWriter, message string) {
rndr.json(w, Response{Success: false, Message: message})
rndr.JSONERR(w, message)
}
func (r *render) json(w http.ResponseWriter, data any) {
func (r *render) JSONF(w http.ResponseWriter, success bool, message string) {
r.JSON(w, Response{Success: success, Message: message})
}
func (r *render) JSONOK(w http.ResponseWriter, message string) {
r.JSON(w, Response{Success: true, Message: message})
}
func (r *render) JSONERR(w http.ResponseWriter, message string) {
r.JSON(w, Response{Success: false, Message: message})
}
func (r *render) JSON(w http.ResponseWriter, data any) {
v, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@@ -3,26 +3,29 @@ package tpl
import (
"html/template"
"net/http"
"management/internal/pkg/session"
)
var rndr Renderer
func Render() Renderer {
return rndr
}
type Renderer interface {
html(w http.ResponseWriter, req *http.Request, name string, data map[string]any)
json(w http.ResponseWriter, data any)
HTML(w http.ResponseWriter, req *http.Request, name string, data map[string]any)
JSON(w http.ResponseWriter, data any)
JSONF(w http.ResponseWriter, success bool, message string)
JSONOK(w http.ResponseWriter, message string)
JSONERR(w http.ResponseWriter, message string)
}
type render struct {
session session.ISession
config *TemplateConfig
templates map[string]*template.Template
}
func Init() error {
func New(session session.ISession) (Renderer, error) {
render := &render{
session: session,
config: &TemplateConfig{
Root: ".",
Extension: ".tmpl",
@@ -33,15 +36,9 @@ func Init() error {
templates, err := render.createTemplateCache()
if err != nil {
return err
return nil, err
}
render.templates = templates
rndr = render
return nil
}
func InitJson() error {
rndr = &render{}
return nil
return render, nil
}

View File

@@ -14,7 +14,6 @@ import (
"management/internal/db/model/dto"
"management/internal/global/auth"
"management/internal/pkg/session"
systemservice "management/internal/service/system"
templates "management/web/templates/manage"
@@ -27,11 +26,11 @@ func (r *render) setDefaultData(req *http.Request, data map[string]any) map[stri
}
ctx := req.Context()
isAuth := session.Exists(ctx, auth.StoreName)
isAuth := r.session.Exists(ctx, auth.StoreName)
data["IsAuthenticated"] = isAuth
if isAuth {
var authUser dto.AuthorizeUser
u := session.GetBytes(ctx, auth.StoreName)
u := r.session.GetBytes(ctx, auth.StoreName)
_ = json.Unmarshal(u, &authUser)
data["AuthorizeMenus"] = r.getCurrentPathBtns(ctx, authUser.RoleID, req.URL.Path)