mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-10 03:03:44 +08:00
添加yaml字段类型
This commit is contained in:
@@ -100,6 +100,7 @@ const (
|
||||
FormModeInputNumber = "InputNumber" // 数字输入
|
||||
FormModeInputTextarea = "InputTextarea" // 文本域
|
||||
FormModeInputEditor = "InputEditor" // 富文本
|
||||
FormModeInputYaml = "InputYaml" // YAML配置
|
||||
FormModeInputDynamic = "InputDynamic" // 动态键值对
|
||||
FormModeDate = "Date" // 日期选择(Y-M-D)
|
||||
FormModeDateRange = "DateRange" // 日期范围选择
|
||||
@@ -122,7 +123,7 @@ const (
|
||||
)
|
||||
|
||||
var FormModes = []string{
|
||||
FormModeInput, FormModeInputNumber, FormModeInputTextarea, FormModeInputEditor, FormModeInputDynamic,
|
||||
FormModeInput, FormModeInputNumber, FormModeInputTextarea, FormModeInputEditor, FormModeInputYaml, FormModeInputDynamic,
|
||||
FormModeDate, FormModeDateRange, FormModeTime, FormModeTimeRange,
|
||||
FormModeRadio, FormModeCheckbox, FormModeSelect, FormModeSelectMultiple, FormModeTreeSelect, FormModeCascader,
|
||||
FormModeUploadImage, FormModeUploadImages, FormModeUploadFile, FormModeUploadFiles,
|
||||
@@ -136,6 +137,7 @@ var FormModeMap = map[string]string{
|
||||
FormModeInputNumber: "数字输入",
|
||||
FormModeInputTextarea: "文本域",
|
||||
FormModeInputEditor: "富文本",
|
||||
FormModeInputYaml: "YAML配置",
|
||||
FormModeInputDynamic: "动态键值对",
|
||||
FormModeDate: "日期选择(Y-M-D)",
|
||||
FormModeDateRange: "日期范围选择",
|
||||
@@ -173,6 +175,7 @@ const (
|
||||
FormRoleAccount = "account"
|
||||
FormRolePassword = "password"
|
||||
FormRoleAmount = "amount"
|
||||
FormRoleYaml = "yaml"
|
||||
)
|
||||
|
||||
var FormRoleMap = map[string]string{
|
||||
@@ -191,6 +194,7 @@ var FormRoleMap = map[string]string{
|
||||
FormRoleAccount: "账号",
|
||||
FormRolePassword: "密码",
|
||||
FormRoleAmount: "金额",
|
||||
FormRoleYaml: "YAML格式",
|
||||
}
|
||||
|
||||
// 查询条件
|
||||
|
||||
@@ -30,6 +30,7 @@ const (
|
||||
InputTypeInsertFields = 6 // 编辑新增过滤字段
|
||||
InputTypeTreeOptionFields = 7 // 关系树查询字段
|
||||
EditInpValidatorGenerally = "if err := g.Validator().Rules(\"%s\").Data(in.%s).Messages(\"%s\").Run(ctx); err != nil {\n\t\treturn err.Current()\n\t}\n"
|
||||
EditInpValidatorYaml = "if err := validate.ValidateYAML(in.%s); err != nil {\n\t\treturn gerror.Newf(\"%s必须为有效的YAML格式: %%s\", err.Error())\n\t}\n"
|
||||
)
|
||||
|
||||
func (l *gCurd) inputTplData(ctx context.Context, in *CurdPreviewInput) (data g.Map, err error) {
|
||||
@@ -308,6 +309,8 @@ func makeValidatorFunc(field *sysin.GenCodesColumnListModel) (err error, rule st
|
||||
rule = fmt.Sprintf(EditInpValidatorGenerally, "regex:^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$", field.GoName, field.Dc+"必须包含6-18为字母和数字")
|
||||
} else if field.FormRole == FormRoleAmount {
|
||||
rule = fmt.Sprintf(EditInpValidatorGenerally, "regex:(^[0-9]{1,10}$)|(^[0-9]{1,10}[\\\\.]{1}[0-9]{1,2}$)", field.GoName, field.Dc+"最多允许输入10位整数及2位小数")
|
||||
} else if field.FormRole == FormRoleYaml {
|
||||
rule = fmt.Sprintf(EditInpValidatorYaml, field.GoName, field.Dc)
|
||||
} else {
|
||||
err = gerror.New("not support")
|
||||
}
|
||||
|
||||
@@ -99,10 +99,11 @@ func (l *gCurd) generateLogicSwitchFields(ctx context.Context, in *CurdPreviewIn
|
||||
|
||||
func (l *gCurd) generateLogicEdit(ctx context.Context, in *CurdPreviewInput) g.Map {
|
||||
var (
|
||||
data = make(g.Map)
|
||||
updateBuffer = bytes.NewBuffer(nil)
|
||||
insertBuffer = bytes.NewBuffer(nil)
|
||||
uniqueBuffer = bytes.NewBuffer(nil)
|
||||
data = make(g.Map)
|
||||
updateBuffer = bytes.NewBuffer(nil)
|
||||
insertBuffer = bytes.NewBuffer(nil)
|
||||
uniqueBuffer = bytes.NewBuffer(nil)
|
||||
validationBuffer = bytes.NewBuffer(nil)
|
||||
)
|
||||
|
||||
for _, field := range in.masterFields {
|
||||
@@ -117,6 +118,11 @@ func (l *gCurd) generateLogicEdit(ctx context.Context, in *CurdPreviewInput) g.M
|
||||
if field.Unique {
|
||||
uniqueBuffer.WriteString(fmt.Sprintf(LogicEditUnique, field.GoName, in.In.DaoName, in.In.DaoName, field.GoName, field.GoName, field.Dc,in.pk.GoName))
|
||||
}
|
||||
|
||||
// 添加 YAML 格式验证
|
||||
if field.IsEdit && field.FormRole == FormRoleYaml {
|
||||
validationBuffer.WriteString(fmt.Sprintf(EditInpValidatorYaml, field.GoName, field.Dc))
|
||||
}
|
||||
}
|
||||
|
||||
notFilterAuth := ""
|
||||
@@ -130,6 +136,7 @@ func (l *gCurd) generateLogicEdit(ctx context.Context, in *CurdPreviewInput) g.M
|
||||
data["update"] = updateBuffer.String()
|
||||
data["insert"] = insertBuffer.String()
|
||||
data["unique"] = uniqueBuffer.String()
|
||||
data["validation"] = validationBuffer.String()
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,9 @@ func (l *gCurd) generateWebEditFormItem(ctx context.Context, in *CurdPreviewInpu
|
||||
case FormModeInputEditor:
|
||||
component = fmt.Sprintf("<n-form-item label=\"%s\" path=\"%s\">\n <Editor style=\"height: 450px\" id=\"%s\" v-model:value=\"formValue.%s\" />\n </n-form-item>", field.Dc, field.TsName, field.TsName, field.TsName)
|
||||
|
||||
case FormModeInputYaml:
|
||||
component = fmt.Sprintf("<n-form-item label=\"%s\" path=\"%s\">\n <YamlEditor ref=\"%sYamlRef\" v-model:value=\"formValue.%s\" :height=\"400\" placeholder=\"请输入%s\" validate-on-blur show-stats prevent-invalid-submit @error=\"handleYamlError\" @valid=\"handleYamlValid\" @validation-change=\"(isValid) => handleYamlValidationChange(isValid, '%s')\" />\n </n-form-item>", field.Dc, field.TsName, field.TsName, field.TsName, field.Dc, field.TsName)
|
||||
|
||||
case FormModeInputDynamic:
|
||||
component = fmt.Sprintf("<n-form-item label=\"%s\" path=\"%s\">\n <n-dynamic-input\n v-model:value=\"formValue.%s\"\n preset=\"pair\"\n key-placeholder=\"键名\"\n value-placeholder=\"键值\"\n />\n </n-form-item>", field.Dc, field.TsName, field.TsName)
|
||||
|
||||
@@ -150,6 +153,7 @@ func (l *gCurd) generateWebEditScript(ctx context.Context, in *CurdPreviewInput)
|
||||
data = make(g.Map)
|
||||
importBuffer = bytes.NewBuffer(nil)
|
||||
setupBuffer = bytes.NewBuffer(nil)
|
||||
hasYamlField = false
|
||||
)
|
||||
|
||||
importBuffer.WriteString(" import { ref, computed } from 'vue';\n")
|
||||
@@ -190,6 +194,11 @@ func (l *gCurd) generateWebEditScript(ctx context.Context, in *CurdPreviewInput)
|
||||
if !gstr.Contains(importBuffer.String(), `import Editor`) {
|
||||
importBuffer.WriteString(" import Editor from '@/components/Editor/editor.vue';\n")
|
||||
}
|
||||
case FormModeInputYaml:
|
||||
if !gstr.Contains(importBuffer.String(), `import YamlEditor`) {
|
||||
importBuffer.WriteString(" import YamlEditor from '@/components/YamlEditor/index.vue';\n")
|
||||
}
|
||||
hasYamlField = true
|
||||
case FormModeUploadImage, FormModeUploadImages:
|
||||
if !gstr.Contains(importBuffer.String(), `import UploadImage`) {
|
||||
importBuffer.WriteString(" import UploadImage from '@/components/Upload/uploadImage.vue';\n")
|
||||
@@ -207,6 +216,13 @@ func (l *gCurd) generateWebEditScript(ctx context.Context, in *CurdPreviewInput)
|
||||
}
|
||||
}
|
||||
|
||||
// 根据是否有 YAML 字段添加相应的验证逻辑
|
||||
if hasYamlField {
|
||||
setupBuffer.WriteString(" const yamlValidationStates = ref(new Map());\n const isFormValid = computed(() => {\n for (const [fieldName, isValid] of yamlValidationStates.value) {\n if (!isValid) return false;\n }\n return true;\n });\n const handleYamlError = (error) => {\n console.error('YAML 验证错误:', error);\n };\n const handleYamlValid = (content) => {\n console.log('YAML 验证通过:', content);\n };\n const handleYamlValidationChange = (isValid, fieldName) => {\n yamlValidationStates.value.set(fieldName, isValid);\n };\n")
|
||||
} else {
|
||||
setupBuffer.WriteString(" const isFormValid = ref(true);\n")
|
||||
}
|
||||
|
||||
data["import"] = importBuffer.String()
|
||||
data["setup"] = setupBuffer.String()
|
||||
return data
|
||||
|
||||
@@ -309,7 +309,7 @@ func (l *gCurd) generateWebModelFormSchemaEach(buffer *bytes.Buffer, fields []*s
|
||||
|
||||
// 这里根据编辑表单组件来进行推断,如果没有则使用默认input,这可能会导致和查询条件所需参数不符的情况
|
||||
switch field.FormMode {
|
||||
case FormModeInput, FormModeInputTextarea, FormModeInputEditor:
|
||||
case FormModeInput, FormModeInputTextarea, FormModeInputEditor, FormModeInputYaml:
|
||||
component = defaultComponent
|
||||
|
||||
case FormModeInputNumber:
|
||||
|
||||
@@ -34,6 +34,9 @@ func (l *gCurd) generateWebViewItem(ctx context.Context, in *CurdPreviewInput) s
|
||||
case FormModeInputTextarea, FormModeInputEditor:
|
||||
component = fmt.Sprintf("<n-descriptions-item>\n <template #label>%s</template>\n <span v-html=\"formValue.%s\"></span></n-descriptions-item>", field.Dc, field.TsName)
|
||||
|
||||
case FormModeInputYaml:
|
||||
component = fmt.Sprintf("<n-descriptions-item>\n <template #label>%s</template>\n <pre style=\"white-space: pre-wrap; font-family: monospace; background: #f5f5f5; padding: 10px; border-radius: 4px;\">{{ formValue.%s }}</pre></n-descriptions-item>", field.Dc, field.TsName)
|
||||
|
||||
case FormModeInputDynamic:
|
||||
component = defaultComponent
|
||||
|
||||
|
||||
50
server/internal/library/validate/yaml.go
Normal file
50
server/internal/library/validate/yaml.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// Package validate
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2025 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package validate
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// IsValidYAML 验证字符串是否为有效的YAML格式
|
||||
func IsValidYAML(yamlStr string) bool {
|
||||
if strings.TrimSpace(yamlStr) == "" {
|
||||
return true // 空字符串被认为是有效的
|
||||
}
|
||||
|
||||
var temp interface{}
|
||||
err := yaml.Unmarshal([]byte(yamlStr), &temp)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// ValidateYAML 验证YAML格式并返回错误信息
|
||||
func ValidateYAML(yamlStr string) error {
|
||||
if strings.TrimSpace(yamlStr) == "" {
|
||||
return nil // 空字符串被认为是有效的
|
||||
}
|
||||
|
||||
var temp interface{}
|
||||
err := yaml.Unmarshal([]byte(yamlStr), &temp)
|
||||
return err
|
||||
}
|
||||
|
||||
// ParseYAML 解析YAML字符串为interface{}
|
||||
func ParseYAML(yamlStr string) (interface{}, error) {
|
||||
var result interface{}
|
||||
err := yaml.Unmarshal([]byte(yamlStr), &result)
|
||||
return result, err
|
||||
}
|
||||
|
||||
// ToYAML 将interface{}转换为YAML字符串
|
||||
func ToYAML(data interface{}) (string, error) {
|
||||
bytes, err := yaml.Marshal(data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
Reference in New Issue
Block a user