feat: 可配置站点 URL 风格
- 新增 urlStyle 工具与测试:postPath/userPath/sitePagePath 统一生成链接 - 管理端新增 UrlStylePanel 设置面板,settings 服务与 handler 适配 - 全站路由链接与 JSON-LD/sitemap 切换为统一 URL 生成 - middleware 与相关组件适配
This commit is contained in:
@@ -80,6 +80,9 @@ const (
|
||||
|
||||
// SettingKeyLevelsFx 等级特效动画总开关(流光/渐变动画);缺行视为开启(默认开)
|
||||
SettingKeyLevelsFx = "levels_fx"
|
||||
|
||||
// SettingKeyUrlStyle 详情页伪静态风格;缺行视为 default(标准动态路由风格)
|
||||
SettingKeyUrlStyle = "url_style"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -112,6 +115,9 @@ const (
|
||||
DefaultPointsCheckinBase = 5
|
||||
DefaultPointsStreakEveryDays = 7
|
||||
MaxPointsRule = 100 // 各积分规则值/上限的统一上界
|
||||
|
||||
// DefaultUrlStyle 标准动态路由风格(/post/123)
|
||||
DefaultUrlStyle = "default"
|
||||
)
|
||||
|
||||
// DefaultAttachmentExts 论坛向默认允许扩展名(含安装包/脚本/网页)
|
||||
@@ -127,6 +133,9 @@ var DefaultAttachmentExts = []string{
|
||||
|
||||
var attachmentExtRe = regexp.MustCompile(`^[a-z0-9]{1,16}$`)
|
||||
|
||||
// UrlStyles 伪静态风格白名单,与前端 lib/urlStyle.ts 保持一致
|
||||
var UrlStyles = []string{"default", "html", "discuz"}
|
||||
|
||||
// ErrInvalidAccent 主题色格式非法
|
||||
var ErrInvalidAccent = errors.New("主题色格式不正确,应为 #RRGGBB")
|
||||
|
||||
@@ -194,6 +203,9 @@ type PublicSiteSettings struct {
|
||||
Levels []model.LevelDef `json:"levels"`
|
||||
// LevelsFx 等级特效动画总开关(流光/渐变动画;关闭后徽章退化为静态配色)
|
||||
LevelsFx bool `json:"levels_fx"`
|
||||
|
||||
// UrlStyle 详情页伪静态风格:default(/post/123)、html(/post-123.html)、discuz(/thread-123.html)
|
||||
UrlStyle string `json:"url_style"`
|
||||
}
|
||||
|
||||
// SettingService 站点级键值设置
|
||||
@@ -297,6 +309,7 @@ func (s *SettingService) Public() (PublicSiteSettings, error) {
|
||||
BrandLogoSize: BrandLogoSizeSQ,
|
||||
BrandLogoFit: BrandLogoFitContain,
|
||||
FooterLinks: []FooterLink{},
|
||||
UrlStyle: DefaultUrlStyle,
|
||||
}
|
||||
accent, err := s.AccentColor()
|
||||
if err != nil {
|
||||
@@ -494,12 +507,50 @@ func (s *SettingService) Public() (PublicSiteSettings, error) {
|
||||
return out, err
|
||||
}
|
||||
out.BgAdminMode = adminMode
|
||||
urlStyle, err := s.UrlStyle()
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
out.UrlStyle = urlStyle
|
||||
|
||||
if err := s.fillBrand(&out); err != nil {
|
||||
return out, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// UrlStyle 返回详情页伪静态风格;缺行或值非法时回落 default
|
||||
func (s *SettingService) UrlStyle() (string, error) {
|
||||
v, found, err := s.getValue(SettingKeyUrlStyle)
|
||||
if err != nil {
|
||||
return DefaultUrlStyle, err
|
||||
}
|
||||
if !found {
|
||||
return DefaultUrlStyle, nil
|
||||
}
|
||||
style := strings.TrimSpace(strings.ToLower(v))
|
||||
for _, valid := range UrlStyles {
|
||||
if style == valid {
|
||||
return style, nil
|
||||
}
|
||||
}
|
||||
return DefaultUrlStyle, nil
|
||||
}
|
||||
|
||||
// SetUrlStyle 校验并保存伪静态风格;传 default 表示恢复标准风格(删除该键)
|
||||
func (s *SettingService) SetUrlStyle(style string) error {
|
||||
style = strings.TrimSpace(strings.ToLower(style))
|
||||
if style == DefaultUrlStyle {
|
||||
return s.deleteKey(SettingKeyUrlStyle)
|
||||
}
|
||||
for _, valid := range UrlStyles {
|
||||
if style == valid {
|
||||
return s.putValue(SettingKeyUrlStyle, style)
|
||||
}
|
||||
}
|
||||
return ErrInvalidSiteSetting
|
||||
}
|
||||
|
||||
// AccentColor 返回配置的主题色(小写 #rrggbb);未配置时返回空串,表示走前端默认色
|
||||
func (s *SettingService) AccentColor() (string, error) {
|
||||
v, found, err := s.getValue(SettingKeyAccent)
|
||||
|
||||
Reference in New Issue
Block a user