支持data_filter显式声明op(LIKE),不自动对所有字符串字段模糊匹配

This commit is contained in:
Hermes Agent 2026-06-22 13:53:30 +08:00
parent 36267a18cc
commit db5f579464

View File

@ -171,17 +171,21 @@ ori_fields = json.loads(fields_str)
if not filterjson: if not filterjson:
fields = [ f['name'] for f in ori_fields ] fields = [ f['name'] for f in ori_fields ]
filterjson = default_filterjson(fields, ns) filterjson = default_filterjson(fields, ns)
# 字符串字段使用LIKE模糊匹配 {% if data_filter and data_filter.fields %}
str_fields = {f['name'] for f in ori_fields if f.get('type') in ('str', 'text')} # 应用 data_filter 中显式声明的 op如 LIKE
if filterjson and str_fields: __df_fields = {{json.dumps(data_filter.fields, ensure_ascii=False)}}
__df_ops = {f['field']: f.get('op') for f in __df_fields if f.get('op')}
if filterjson and __df_ops:
if not isinstance(filterjson, dict) or 'AND' not in filterjson: if not isinstance(filterjson, dict) or 'AND' not in filterjson:
filterjson = {'AND': [filterjson] if filterjson else []} filterjson = {'AND': [filterjson] if filterjson else []}
for fj in filterjson['AND']: for fj in filterjson['AND']:
if fj.get('field') in str_fields and fj.get('op') == '=': if fj.get('field') in __df_ops:
fj['op'] = __df_ops[fj['field']]
if fj['op'] == 'LIKE':
var = fj.get('var') var = fj.get('var')
if var and var in ns and ns[var]: if var and var in ns and ns[var]:
ns[var] = f'%{ns[var]}%' ns[var] = f'%{ns[var]}%'
fj['op'] = 'LIKE' {% endif %}
{% if logined_userorgid or logined_userid %} {% if logined_userorgid or logined_userid %}
# 确保 logined 过滤条件始终生效 # 确保 logined 过滤条件始终生效
if filterjson: if filterjson: