Refactor site configuration and color scheme management

- Replaced static config with dynamic site info context.
- Updated color scheme handling in various components to use site info.
- Removed deprecated config file and integrated site info fetching.
- Enhanced user preference page to allow color scheme selection.
- Adjusted blog and console components to reflect new site info structure.
- Improved error handling and fallback mechanisms for site info retrieval.
This commit is contained in:
2025-09-26 00:25:34 +08:00
parent 0812e334df
commit f501948f91
42 changed files with 770 additions and 199 deletions

View File

@ -1 +1,84 @@
package model
import (
"database/sql/driver"
"encoding/json"
"fmt"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
// JSONMap 是一个通用的 JSON 类型map[string]any
type JSONMap map[string]any
func (JSONMap) GormDataType() string {
return "json"
}
func (JSONMap) GormDBDataType(db *gorm.DB, _ *schema.Field) string {
switch db.Dialector.Name() {
case "mysql":
return "JSON"
case "postgres":
return "JSONB"
default: // sqlite 等
return "TEXT"
}
}
// Value 实现 driver.Valuer用于写入数据库
func (j JSONMap) Value() (driver.Value, error) {
if j == nil {
return nil, nil
}
b, err := json.Marshal(map[string]any(j))
if err != nil {
return nil, err
}
return string(b), nil
}
// Scan 实现 sql.Scanner用于从数据库读取并反序列化
func (j *JSONMap) Scan(src any) error {
if src == nil {
*j = nil
return nil
}
var data []byte
switch v := src.(type) {
case string:
data = []byte(v)
case []byte:
data = v
default:
return fmt.Errorf("cannot scan JSONMap from %T", src)
}
if len(data) == 0 {
*j = nil
return nil
}
var m map[string]any
if err := json.Unmarshal(data, &m); err != nil {
return err
}
*j = JSONMap(m)
return nil
}
func (j JSONMap) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any(j))
}
func (j *JSONMap) UnmarshalJSON(b []byte) error {
if len(b) == 0 || string(b) == "null" {
*j = nil
return nil
}
var m map[string]any
if err := json.Unmarshal(b, &m); err != nil {
return err
}
*j = JSONMap(m)
return nil
}