|
|
package web
|
|
|
|
|
|
import (
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"goskeleton/app/global/consts"
|
|
|
"goskeleton/app/global/variable"
|
|
|
"goskeleton/app/utils/response"
|
|
|
"os"
|
|
|
"encoding/json"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
type Editor struct {
|
|
|
}
|
|
|
|
|
|
func (e *Editor) UserStyleSave(context *gin.Context) {
|
|
|
// 获取用户输入
|
|
|
userId, _ := context.Get(consts.ValidatorPrefix + "user_name")
|
|
|
styleName, _ := context.Get(consts.ValidatorPrefix + "style_name")
|
|
|
elementName, _ := context.Get(consts.ValidatorPrefix + "element_name")
|
|
|
styleClasses, _ := context.Get(consts.ValidatorPrefix + "style_classes")
|
|
|
styleContent, _ := context.Get(consts.ValidatorPrefix + "style_content")
|
|
|
|
|
|
// 转换 style classes
|
|
|
var styleClassesStr []string
|
|
|
for _, v := range styleClasses.([]interface{}) {
|
|
|
styleClassesStr = append(styleClassesStr, v.(string))
|
|
|
}
|
|
|
|
|
|
// 准备新的样式数据
|
|
|
rowData := map[string]interface{}{
|
|
|
"style_name": styleName.(string),
|
|
|
"element_name": elementName.(string),
|
|
|
"style_classes": styleClassesStr,
|
|
|
"style_content": styleContent.(string),
|
|
|
}
|
|
|
|
|
|
userStyleDefinitionPath := variable.BasePath + variable.ConfigYml.GetString("Style.UserStyleSavePath") + "/" + userId.(string) + ".json"
|
|
|
|
|
|
var existingStyles []map[string]interface{}
|
|
|
|
|
|
// 检查文件是否存在
|
|
|
if _, err := os.Stat(userStyleDefinitionPath); os.IsNotExist(err) {
|
|
|
// 文件不存在,创建新的数组
|
|
|
existingStyles = []map[string]interface{}{rowData}
|
|
|
} else {
|
|
|
// 文件存在,读取现有内容
|
|
|
existingData, err := os.ReadFile(userStyleDefinitionPath)
|
|
|
if err != nil {
|
|
|
response.ErrorSystem(context, "无法读取样式文件", "")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 解析现有的 JSON 数据
|
|
|
if err := json.Unmarshal(existingData, &existingStyles); err != nil {
|
|
|
response.ErrorSystem(context, "无法解析样式文件", "")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 查找是否存在相同的 style_name
|
|
|
found := false
|
|
|
for i, style := range existingStyles {
|
|
|
if style["style_name"] == styleName.(string) {
|
|
|
// 找到相同的 style_name,更新内容
|
|
|
existingStyles[i] = rowData
|
|
|
found = true
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 如果没有找到相同的 style_name,则追加新的样式
|
|
|
if !found {
|
|
|
existingStyles = append(existingStyles, rowData)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 将更新后的数据转换为 JSON,并使用缩进格式
|
|
|
data, err := json.MarshalIndent(existingStyles, "", " ")
|
|
|
if err != nil {
|
|
|
response.ErrorSystem(context, "无法序列化样式数据", "")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 写入文件(覆盖原文件)
|
|
|
if err := os.WriteFile(userStyleDefinitionPath, data, 0644); err != nil {
|
|
|
response.ErrorSystem(context, "无法写入样式文件", "")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
response.Success(context, "UserStyleSave", "")
|
|
|
}
|
|
|
|
|
|
func (e *Editor) UserStyleGet(context *gin.Context) {
|
|
|
userId, _ := context.Get(consts.ValidatorPrefix + "user_name")
|
|
|
userStyleDefinitionPath := variable.BasePath + variable.ConfigYml.GetString("Style.UserStyleSavePath") + "/" + userId.(string) + ".json"
|
|
|
|
|
|
// 检查文件是否存在
|
|
|
if _, err := os.Stat(userStyleDefinitionPath); os.IsNotExist(err) {
|
|
|
// 如果文件不存在,返回空数组而不是错误
|
|
|
response.Success(context, "UserStyleGet", []map[string]interface{}{})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 读取文件内容
|
|
|
existingData, err := os.ReadFile(userStyleDefinitionPath)
|
|
|
if err != nil {
|
|
|
response.ErrorSystem(context, "无法读取样式文件", "")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 使用与 UserStyleSave 相同的数据类型
|
|
|
var existingStyles []map[string]interface{}
|
|
|
if err := json.Unmarshal(existingData, &existingStyles); err != nil {
|
|
|
response.ErrorSystem(context, "无法解析样式文件", "")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
response.Success(context, "UserStyleGet", existingStyles)
|
|
|
}
|
|
|
|
|
|
func (e *Editor) AiFormatConfigSave(context *gin.Context) {
|
|
|
// 获取用户输入
|
|
|
id, _ := context.Get(consts.ValidatorPrefix + "user_name")
|
|
|
titleStyle, _ := context.Get(consts.ValidatorPrefix + "title_style")
|
|
|
headingStyle, _ := context.Get(consts.ValidatorPrefix + "heading_style")
|
|
|
listStyle, _ := context.Get(consts.ValidatorPrefix + "list_style")
|
|
|
bodyStyle, _ := context.Get(consts.ValidatorPrefix + "body_style")
|
|
|
blockquoteStyle, _ := context.Get(consts.ValidatorPrefix + "blockquote_style")
|
|
|
codeblockStyle, _ := context.Get(consts.ValidatorPrefix + "codeblock_style")
|
|
|
|
|
|
// 处理 headingStyle
|
|
|
var headingStyleArray [][]string
|
|
|
if headingStr, ok := headingStyle.(string); ok {
|
|
|
// 先按逗号分割
|
|
|
headings := strings.Split(headingStr, ",")
|
|
|
// 每两个元素组成一对
|
|
|
for i := 0; i < len(headings); i += 2 {
|
|
|
if i+1 < len(headings) {
|
|
|
pair := []string{headings[i], headings[i+1]}
|
|
|
headingStyleArray = append(headingStyleArray, pair)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 处理 codeblockStyle
|
|
|
var codeblockStyleArray []string
|
|
|
if codeblockStr, ok := codeblockStyle.(string); ok {
|
|
|
codeblockStyleArray = strings.Split(codeblockStr, ",")
|
|
|
}
|
|
|
|
|
|
// 准备配置数据
|
|
|
rowData := map[string]interface{}{
|
|
|
"title_style": titleStyle.(string),
|
|
|
"heading_style": headingStyleArray, // 使用处理后的二维数组
|
|
|
"list_style": listStyle.(string),
|
|
|
"body_style": bodyStyle.(string),
|
|
|
"blockquote_style": blockquoteStyle.(string),
|
|
|
"codeblock_style": codeblockStyleArray, // 使用处理后的数组
|
|
|
}
|
|
|
|
|
|
aiFormatConfigPath := variable.BasePath + variable.ConfigYml.GetString("Style.UserFormatConfigSavePath") + "/" + id.(string) + ".json"
|
|
|
|
|
|
// 将数据转换为格式化的 JSON
|
|
|
data, err := json.MarshalIndent(rowData, "", " ")
|
|
|
if err != nil {
|
|
|
response.ErrorSystem(context, "无法序列化AI格式配置数据", "")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 写入文件(覆盖原文件)
|
|
|
if err := os.WriteFile(aiFormatConfigPath, data, 0644); err != nil {
|
|
|
response.ErrorSystem(context, "无法写入AI格式配置文件", "")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
response.Success(context, "AiFormatConfigSave", "")
|
|
|
}
|
|
|
|
|
|
func (e *Editor) AiFormatConfigGet(context *gin.Context) {
|
|
|
id, _ := context.Get(consts.ValidatorPrefix + "user_name")
|
|
|
aiFormatConfigPath := variable.BasePath + variable.ConfigYml.GetString("Style.UserFormatConfigSavePath") + "/" + id.(string) + ".json"
|
|
|
|
|
|
// 检查文件是否存在
|
|
|
if _, err := os.Stat(aiFormatConfigPath); os.IsNotExist(err) {
|
|
|
// 返回默认空配置而不是错误
|
|
|
defaultConfig := map[string]interface{}{
|
|
|
"title_style": "",
|
|
|
"heading_style": []string{},
|
|
|
"list_style": "",
|
|
|
"body_style": "",
|
|
|
"blockquote_style": "",
|
|
|
"codeblock_style": "",
|
|
|
}
|
|
|
response.Success(context, "AiFormatConfigGet", defaultConfig)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 读取文件内容
|
|
|
existingData, err := os.ReadFile(aiFormatConfigPath)
|
|
|
if err != nil {
|
|
|
response.ErrorSystem(context, "无法读取AI格式配置文件", "")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 解析 JSON 数据
|
|
|
var existingStyles map[string]interface{}
|
|
|
if err := json.Unmarshal(existingData, &existingStyles); err != nil {
|
|
|
response.ErrorSystem(context, "无法解析AI格式配置文件", "")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
response.Success(context, "AiFormatConfigGet", existingStyles)
|
|
|
} |