181 lines
3.8 KiB
Go
181 lines
3.8 KiB
Go
package tpl
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"html/template"
|
|
"io/fs"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
|
|
"management/internal/erpserver/model/dto"
|
|
"management/internal/pkg/know"
|
|
templates "management/web/templates/manage"
|
|
|
|
"github.com/justinas/nosurf"
|
|
)
|
|
|
|
func (r *render) setDefaultData(req *http.Request, data map[string]any) map[string]any {
|
|
if data == nil {
|
|
data = make(map[string]any)
|
|
}
|
|
|
|
ctx := req.Context()
|
|
isAuth := r.session.Exists(ctx, know.StoreName)
|
|
data["IsAuthenticated"] = isAuth
|
|
if isAuth {
|
|
var authUser dto.AuthorizeUser
|
|
u := r.session.GetBytes(ctx, know.StoreName)
|
|
_ = json.Unmarshal(u, &authUser)
|
|
|
|
data["AuthorizeMenus"] = r.getCurrentPathBtns(ctx, authUser.RoleID, req.URL.Path)
|
|
}
|
|
token := nosurf.Token(req)
|
|
data["CsrfToken"] = token
|
|
data["CsrfTokenField"] = template.HTML(fmt.Sprintf(`<input type="hidden" name="csrf_token" value="%s" />`, token))
|
|
|
|
return data
|
|
}
|
|
|
|
func (r *render) getCurrentPathBtns(ctx context.Context, roleID int32, path string) []*dto.OwnerMenuDto {
|
|
var res []*dto.OwnerMenuDto
|
|
|
|
// 获取当前登陆角色的权限
|
|
menus, err := r.menusvc.ListByRoleIDToMap(ctx, roleID)
|
|
if err != nil {
|
|
return res
|
|
}
|
|
|
|
menu, ok := menus[path]
|
|
if !ok {
|
|
return res
|
|
}
|
|
|
|
for _, item := range menus {
|
|
if menu.IsList {
|
|
if item.ParentID == menu.ID || item.ID == menu.ID {
|
|
res = append(res, item)
|
|
}
|
|
} else {
|
|
if item.ParentID == menu.ParentID {
|
|
res = append(res, item)
|
|
}
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func (r *render) createTemplateCache() (map[string]*template.Template, error) {
|
|
cache := make(map[string]*template.Template)
|
|
pages, err := getFiles(r.config.Root, r.config.Extension)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
layoutAndPartial, err := r.getLayoutAndPartials()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, page := range pages {
|
|
if strings.HasPrefix(page, "base") || strings.HasSuffix(page, "partial") {
|
|
continue
|
|
}
|
|
|
|
name := filepath.Base(page)
|
|
pathArr := strings.Split(page, "/")
|
|
dir := pathArr[len(pathArr)-2 : len(pathArr)-1]
|
|
templateName := fmt.Sprintf("%s_%s", dir[0], name)
|
|
ts := template.Must(template.New(templateName).Funcs(r.btnFuncs()).Funcs(r.Methods()).ParseFS(templates.TemplateFS, page))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ts, err = ts.ParseFS(templates.TemplateFS, layoutAndPartial...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cache[templateName] = ts
|
|
}
|
|
|
|
return cache, nil
|
|
}
|
|
|
|
func (r *render) getLayoutAndPartials() ([]string, error) {
|
|
layouts, err := getFiles(r.config.Layout, r.config.Extension)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
partials, err := getFiles(r.config.Partial, r.config.Extension)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return slices.Concat(layouts, partials), nil
|
|
}
|
|
|
|
func getFiles(path string, stuffix string) ([]string, error) {
|
|
files := make([]string, 0)
|
|
b, err := pathExists(templates.TemplateFS, path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !b {
|
|
return files, nil
|
|
}
|
|
|
|
err = fs.WalkDir(templates.TemplateFS, path, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.IsDir() {
|
|
return nil
|
|
}
|
|
if strings.HasSuffix(path, stuffix) {
|
|
files = append(files, path)
|
|
}
|
|
return nil
|
|
})
|
|
// err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
|
|
// if info == nil {
|
|
// return err
|
|
// }
|
|
// if info.IsDir() {
|
|
// return nil
|
|
// }
|
|
// // 将模板后缀的文件放到列表
|
|
// if strings.HasSuffix(path, stuffix) {
|
|
// files = append(files, path)
|
|
// }
|
|
// return nil
|
|
// })
|
|
return files, err
|
|
}
|
|
|
|
func pathExists(fs fs.FS, path string) (bool, error) {
|
|
_, err := fs.Open(path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
return true, err
|
|
}
|
|
|
|
func firstLower(s string) string {
|
|
if len(s) == 0 {
|
|
return s
|
|
}
|
|
|
|
return strings.ToLower(s[:1]) + s[1:]
|
|
}
|