更新网站功能
This commit is contained in:
@@ -3,6 +3,9 @@ package controllers
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -17,6 +20,31 @@ type IndexPortalController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// reNewsRow 从模板标签 {yz:newscenter row="N" ...} 中提取 row 值
|
||||
var reNewsRow = regexp.MustCompile(`\{yz:newscenter[^}]*\s+row\s*=\s*"(\d+)"`)
|
||||
|
||||
// templateNewsRow 读取当前租户首页模板中 newscenter 标签的 row 参数,
|
||||
// 作为 API 默认条数,使接口与前端模板保持一致。
|
||||
func templateNewsRow(tid uint64) int {
|
||||
code := strings.TrimSpace(cmsTenantTemplateCode(tid))
|
||||
if code == "" || code == "default" {
|
||||
code = "default"
|
||||
}
|
||||
if !cmsTemplateDirExists(code) {
|
||||
code = "default"
|
||||
}
|
||||
data, err := os.ReadFile(filepath.Join(cmsThemesRoot(), code, "index.html"))
|
||||
if err != nil {
|
||||
return 10
|
||||
}
|
||||
if m := reNewsRow.FindSubmatch(data); len(m) == 2 {
|
||||
if n, e := strconv.Atoi(string(m[1])); e == nil && n > 0 && n <= 50 {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return 10
|
||||
}
|
||||
|
||||
// getTidByHost 根据访问域名识别租户 ID,默认返回 1
|
||||
func (c *IndexPortalController) getTidByHost() uint64 {
|
||||
host := c.Ctx.Request.Host
|
||||
@@ -267,13 +295,15 @@ func (c *IndexPortalController) queryCmsArticles(categoryName string, categoryID
|
||||
tid := c.getTidByHost()
|
||||
_ = models.EnsureCmsArticleTables()
|
||||
|
||||
defaultLimit := templateNewsRow(tid)
|
||||
|
||||
page, _ := c.GetInt("page", 1)
|
||||
limit, _ := c.GetInt("limit", 10)
|
||||
limit, _ := c.GetInt("limit", defaultLimit)
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if limit < 1 {
|
||||
limit = 10
|
||||
limit = defaultLimit
|
||||
}
|
||||
|
||||
qs := models.Orm.QueryTable(new(models.CmsArticle)).
|
||||
|
||||
@@ -136,7 +136,7 @@ func (c *TenantSiteController) rewriteStorageURLs(html string) string {
|
||||
if err != nil || strings.TrimSpace(cfg.QiniuDomain) == "" {
|
||||
return html
|
||||
}
|
||||
return rewriteLegacyQiniuURLs(html, storagePublicBaseURL(cfg.QiniuDomain))
|
||||
return rewriteLegacyQiniuURLs(html, storagePublicBaseURL(cfg.QiniuDomain), cfg.QiniuDomain)
|
||||
}
|
||||
|
||||
func storagePublicBaseURL(domain string) string {
|
||||
@@ -147,13 +147,18 @@ func storagePublicBaseURL(domain string) string {
|
||||
return "https://" + base
|
||||
}
|
||||
|
||||
func rewriteLegacyQiniuURLs(html, targetBase string) string {
|
||||
func rewriteLegacyQiniuURLs(html, targetBase, qiniuDomain string) string {
|
||||
if targetBase == "" {
|
||||
return html
|
||||
}
|
||||
for _, domain := range legacyQiniuDomains {
|
||||
domains := append(legacyQiniuDomains, qiniuDomain)
|
||||
for _, domain := range domains {
|
||||
d := strings.TrimSpace(domain)
|
||||
if d == "" {
|
||||
continue
|
||||
}
|
||||
for _, scheme := range []string{"http://", "https://"} {
|
||||
html = strings.ReplaceAll(html, scheme+domain+"/", targetBase+"/")
|
||||
html = strings.ReplaceAll(html, scheme+d+"/", targetBase+"/")
|
||||
}
|
||||
}
|
||||
return html
|
||||
|
||||
Reference in New Issue
Block a user