Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions server/internal/library/hggen/views/curd_generate_web_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,17 @@ func (l *gCurd) generateWebModelStateItems(ctx context.Context, in *CurdPreviewI
if value == nil {
value = "null"
}
//注释为空判断 避免模版生成的model.ts重复加引号
//if value == "" {
// value = `''`
//}
if value == "" {
// 修复字符串字段为空时的处理
if isStringType(field.TsType, dataType) {
value = "" // 模板会自动加引号变成 ''
} else {
value = "null"
}
} else if valueStr, ok := value.(string); ok && isStringType(field.TsType, dataType) {
// 对于字符串类型,直接使用原值,模板会自动加引号
value = valueStr
}

// 选项组件默认值调整
if gconv.Int(value) == 0 && IsSelectFormMode(field.FormMode) {
Expand All @@ -146,6 +153,7 @@ func (l *gCurd) generateWebModelStateItems(ctx context.Context, in *CurdPreviewI
items = append(items, &StateItem{
Name: field.TsName,
DefaultValue: value,
DataType: dataType,
Dc: field.Dc,
})

Expand Down Expand Up @@ -452,3 +460,26 @@ func (l *gCurd) generateWebModelColumnsEach(buffer *bytes.Buffer, in *CurdPrevie
}
return
}

// isStringType 判断字段是否为字符串类型
func isStringType(tsType, dataType string) bool {
// 根据 TypeScript 类型判断
if tsType == "string" {
return true
}

// 根据数据库数据类型判断
stringTypes := []string{
"varchar", "char", "text", "longtext", "mediumtext", "tinytext",
"nvarchar", "nchar", "ntext",
"string", "enum", "set",
}

for _, t := range stringTypes {
if strings.EqualFold(dataType, t) {
return true
}
}

return false
}