优化企业网站功能

This commit is contained in:
2026-08-20 16:21:07 +08:00
parent 112d60b993
commit 3f0fcb88e7
45 changed files with 8758 additions and 5058 deletions
+41 -19
View File
@@ -120,32 +120,43 @@ func (c *TenantSiteController) resolveTemplateCode(tid uint64) string {
func (c *TenantSiteController) writeHTML(status int, html string) {
c.Ctx.Output.SetStatus(status)
c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
c.Ctx.WriteString(c.forceHTTPSStorageURLs(html))
c.Ctx.WriteString(c.rewriteStorageURLs(html))
}
// forceHTTPSStorageURLs 访客以 HTTPS 访问站点时,把页面中存储(CDN)域名的
// http:// 资源升级为 https://,避免混合内容(Mixed Content)被浏览器拦截。
// 仅替换存储配置里那个域名,不动正文里的其他外部链接。
func (c *TenantSiteController) forceHTTPSStorageURLs(html string) string {
proto := c.Ctx.Input.Header("X-Forwarded-Proto")
if proto == "" && c.Ctx.Input.IsSecure() {
proto = "https"
}
if proto != "https" {
return html
}
var legacyQiniuDomains = []string{
"7colud.yunzer.cn",
"7cloud.yunzer.cn",
}
// rewriteStorageURLs 将历史七牛域名替换为当前存储配置的访问域名。
// 这样已存入数据库的旧完整 URL 会跟随新的 CDN 或 /qiniu/ 反向代理入口,
// 不需要批量更新业务数据。
func (c *TenantSiteController) rewriteStorageURLs(html string) string {
cfg, err := models.GetStorageConfig()
if err != nil || strings.TrimSpace(cfg.QiniuDomain) == "" {
return html
}
host := strings.TrimSpace(cfg.QiniuDomain)
host = strings.TrimPrefix(host, "http://")
host = strings.TrimPrefix(host, "https://")
host = strings.TrimRight(host, "/")
if host == "" {
return rewriteLegacyQiniuURLs(html, storagePublicBaseURL(cfg.QiniuDomain))
}
func storagePublicBaseURL(domain string) string {
base := strings.TrimRight(strings.TrimSpace(domain), "/")
if base == "" || strings.HasPrefix(base, "/") || strings.HasPrefix(base, "http://") || strings.HasPrefix(base, "https://") {
return base
}
return "https://" + base
}
func rewriteLegacyQiniuURLs(html, targetBase string) string {
if targetBase == "" {
return html
}
return strings.ReplaceAll(html, "http://"+host, "https://"+host)
for _, domain := range legacyQiniuDomains {
for _, scheme := range []string{"http://", "https://"} {
html = strings.ReplaceAll(html, scheme+domain+"/", targetBase+"/")
}
}
return html
}
// rewriteThemeAssets 将模板内书写的相对资源路径(href/src/url() 等)
@@ -211,6 +222,11 @@ func rewriteThemeAssets(html, code string) string {
// render 解析并输出模板文件
func (c *TenantSiteController) render(file string, mod func(ctx *tagengine.RenderCtx)) {
c.renderFile(file, "", mod)
}
// renderFile 解析并输出模板;preferred 不存在时尝试 fallback(fallback 为空则直接 404)
func (c *TenantSiteController) renderFile(preferred, fallback string, mod func(ctx *tagengine.RenderCtx)) {
tid, ok := c.resolveTid()
if !ok {
c.writeHTML(404, siteNotFoundPage)
@@ -219,6 +235,12 @@ func (c *TenantSiteController) render(file string, mod func(ctx *tagengine.Rende
code := c.resolveTemplateCode(tid)
dir := filepath.Join(cmsThemesRoot(), code)
file := preferred
if _, err := os.Stat(filepath.Join(dir, file)); err != nil {
if fallback != "" {
file = fallback
}
}
if _, err := os.Stat(filepath.Join(dir, file)); err != nil {
c.writeHTML(404, fmt.Sprintf(renderErrorPage, "模板页面不存在"))
return
@@ -271,7 +293,7 @@ func (c *TenantSiteController) Page() {
c.writeHTML(404, fmt.Sprintf(renderErrorPage, "页面不存在"))
return
}
c.render("page.html", func(ctx *tagengine.RenderCtx) {
c.renderFile(path+".html", "page.html", func(ctx *tagengine.RenderCtx) {
ctx.PagePath = path
})
}