完善配送端资源搜索与资质导航
This commit is contained in:
@@ -119,7 +119,7 @@ func ListPageFiltered[T any](ctx *gin.Context, filter func(*gorm.DB) *gorm.DB) {
|
||||
var keywordSafeColumns = map[string]bool{
|
||||
"code": true, "name": true, "username": true, "display_name": true,
|
||||
"role_code": true, "delivery_code": true, "work_status": true,
|
||||
"credential_type": true, "device_no": true, "model": true,
|
||||
"credential_type": true, "credential_no": true, "device_no": true, "model": true,
|
||||
"online_status": true, "rule_code": true, "action": true,
|
||||
"event_code": true, "title": true, "result": true,
|
||||
"product_code": true, "value": true, "order_no": true,
|
||||
|
||||
@@ -18,7 +18,7 @@ type KeywordSearchKind string
|
||||
const (
|
||||
// KeywordSearchText 按数据库原始文本执行不区分大小写的包含匹配。
|
||||
KeywordSearchText KeywordSearchKind = "text"
|
||||
// KeywordSearchEnum 仅按页面展示的中文枚举名称匹配,不暴露内部英文编码。
|
||||
// KeywordSearchEnum 同时按页面中文名称和内部稳定编码匹配。
|
||||
KeywordSearchEnum KeywordSearchKind = "enum"
|
||||
)
|
||||
|
||||
@@ -85,7 +85,7 @@ func useConfiguredKeywordSearch(ctx *gin.Context) bool {
|
||||
}
|
||||
|
||||
// configuredKeywordConditions 将用户关键字编译为参数化 SQL 条件。
|
||||
// 枚举字段只接受中文展示名称,普通文本字段保持原有包含匹配行为。
|
||||
// 枚举字段接受中文展示名称和内部编码,普通文本字段保持原有包含匹配行为。
|
||||
func configuredKeywordConditions(model any, keyword string) ([]string, []any) {
|
||||
fields := ConfiguredKeywordSearchFields(model)
|
||||
conditions := make([]string, 0, len(fields))
|
||||
@@ -113,7 +113,8 @@ func configuredKeywordConditions(model any, keyword string) ([]string, []any) {
|
||||
func matchingKeywordEnumValues(values []KeywordSearchValue, keyword string) []string {
|
||||
matched := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
if strings.Contains(strings.ToLower(value.Label), keyword) {
|
||||
if strings.Contains(strings.ToLower(value.Label), keyword) ||
|
||||
strings.Contains(strings.ToLower(value.Value), keyword) {
|
||||
matched = append(matched, value.Value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// keywordSearchEnumModel 用于验证中文枚举别名不会退化为英文编码搜索。
|
||||
// keywordSearchEnumModel 用于验证中文枚举别名和稳定编码均可搜索。
|
||||
type keywordSearchEnumModel struct {
|
||||
RoleCode string `gorm:"column:role_code"`
|
||||
}
|
||||
@@ -48,8 +48,9 @@ func TestConfiguredKeywordConditionsMatchChineseEnumLabels(t *testing.T) {
|
||||
}
|
||||
|
||||
conditions, arguments = configuredKeywordConditions(&keywordSearchEnumModel{}, "delivery")
|
||||
if len(conditions) != 0 || len(arguments) != 0 {
|
||||
t.Fatalf("英文枚举编码不应继续可搜:conditions=%v arguments=%v", conditions, arguments)
|
||||
if !reflect.DeepEqual(conditions, []string{`"role_code" IN (?)`}) ||
|
||||
!reflect.DeepEqual(arguments, []any{"delivery"}) {
|
||||
t.Fatalf("英文枚举编码应保持可搜:conditions=%v arguments=%v", conditions, arguments)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user