diff --git a/app/router/static.go b/app/router/static.go index ae3cacb..f1e2078 100644 --- a/app/router/static.go +++ b/app/router/static.go @@ -7,6 +7,7 @@ import ( "html/template" "io/fs" "net/http" + "os" "path" "strings" @@ -22,8 +23,11 @@ func staticInit(e *gin.Engine) { template.Must(tmpl.ParseFS(static.Secure, "secure/secure.html")) template.Must(tmpl.ParseFS(static.Payment, "payment/views/*.html")) + checkoutFS, checkoutRoot, checkoutSourceDesc := checkoutSource() + log.Info("当前使用", checkoutSourceDesc) + // 注册收银台模板和静态资源 - entries, err := fs.ReadDir(static.Checkout, "checkout") + entries, err := fs.ReadDir(checkoutFS, checkoutRoot) if err != nil { panic(errors.New(fmt.Sprintf("收银台模板读取异常:%v", err))) } @@ -34,18 +38,18 @@ func staticInit(e *gin.Engine) { } name := entry.Name() - dir := path.Join("checkout", name) - checkout, err := readCheckoutInfoFromFS(static.Checkout, dir) + dir := path.Join(checkoutRoot, name) + checkout, err := readCheckoutInfoFromFS(checkoutFS, dir) if err != nil { panic(err) } - if registerTemplatesFromFS(tmpl, static.Checkout, dir, name) { + if registerTemplatesFromFS(tmpl, checkoutFS, dir, name) { model.RegisterCheckout(name, checkout) log.Info("前台收银模板注册成功:", name) } - registerAssetsFromFS(e, static.Checkout, dir, "/checkout/"+name+"/assets") + registerAssetsFromFS(e, checkoutFS, dir, "/checkout/"+name+"/assets") } e.SetHTMLTemplate(tmpl) @@ -53,6 +57,20 @@ func staticInit(e *gin.Engine) { e.StaticFS("/secure/assets", http.FS(subFS(static.Secure, "secure/assets"))) } +func checkoutSource() (fs.FS, string, string) { + const externalStaticDir = "static" + const checkoutRoot = "checkout" + const externalCheckoutDir = externalStaticDir + "/" + checkoutRoot + + if info, err := os.Stat(externalCheckoutDir); err == nil && info.IsDir() { + return os.DirFS(externalStaticDir), checkoutRoot, "外部收银台模板目录:" + externalCheckoutDir + } else if err != nil && !errors.Is(err, os.ErrNotExist) { + return static.Checkout, checkoutRoot, "内嵌收银台模板(外部目录读取失败:" + err.Error() + ")" + } + + return static.Checkout, checkoutRoot, "内嵌收银台模板" +} + func readCheckoutInfoFromFS(src fs.FS, baseDir string) (model.Checkout, error) { infoPath := path.Join(baseDir, "checkout.json") data, err := fs.ReadFile(src, infoPath)