// ==================== 规则库 ==================== function ruleColumns(){ return [ {label:'编号',render:r=>''+esc(r.code)+''}, {label:'规则名称',render:r=>''+esc(r.name)+''}, {label:'分类',render:r=>fmt(r.category_name)},{label:'等级',render:r=>statusBadge(r.risk_level)}, {label:'类型',render:r=>esc(ruleTypeName(r.rule_type))}, {label:'标签',render:r=>(r.tags||[]).map(t=>''+esc(t)+'').join('')}, {label:'版本',render:r=>'v'+r.version},{label:'状态',render:r=>statusBadge(r.status)}, {label:'发布状态',render:r=>publishBadge(r.publish_status)} ]; } async function renderRules(){ cache.categories = await api('/rule-categories'); window._allRules = await api('/rules'); cache.rules = window._allRules; qs('pageActions').innerHTML = ''; const opts = [''].concat(cache.categories.map(c=>'')).join(''); qs('content').innerHTML = `
`; renderRulesTable(); } function filteredRules(){ const c=(qs('rfCategory')||{}).value||'', r=(qs('rfRisk')||{}).value||'', st=(qs('rfStatus')||{}).value||'', k=((qs('rfKeyword')||{}).value||'').trim().toLowerCase(); return (window._allRules||[]).filter(x=>{ if(c && x.category_name!==c) return false; if(r && x.risk_level!==r) return false; if(st && (x.publish_status||'')!==st) return false; if(k && !((x.name||'').toLowerCase().includes(k)||(x.code||'').toLowerCase().includes(k))) return false; return true; }); } function filterRules(){ window._rulePage = 1; renderRulesTable(); } function renderRulesTable(){ const rows = filteredRules(); cache.rules = rows; const paged = pageRows(rows, window._rulePage || 1); window._rulePage = paged.page; qs('rulesTable').innerHTML = tableHtml(ruleColumns(), paged.items, x=>ruleActions(x)) + pagerHtml(paged.page, paged.total, paged.pages, 'ruleGoPage'); } function ruleGoPage(n){ window._rulePage = n; renderRulesTable(); } function ruleTypeName(t){ return {keyword_exists:'关键词存在性',keyword_forbidden:'禁止词',regex_match:'正则校验',similarity:'文本相似度',time_logic:'时间逻辑',calc_check:'计算校验',numeric_range:'数值范围',cross_doc:'跨文档勾稽'}[t]||t; } function publishBadge(s){ const map = {'草稿':'b-gray','待审批':'b-amber','已发布':'b-green'}; return ''+esc(s||'草稿')+''; } function ruleActions(r){ let b = ` `; b += ` `; if(!r.publish_status || r.publish_status==='草稿' || r.publish_status==='已退回'){ b += ` `; } if(r.publish_status==='待审批'){ b += ` `; b += ` `; } b += ``; return b; } async function submitRule(id){ await api('/rules/'+id+'/submit',{method:'POST'}); toast('已提交审批'); renderRules(); } async function reviewRule(id, approve){ if(!approve && !confirm('确认退回该规则?')) return; const res = await api('/rules/'+id+'/review',{method:'POST',body:JSON.stringify({approve})}); toast('审批结果:'+(res.publish_status||'')); renderRules(); } async function simulateRule(id){ toast('规则模拟运行中...'); const res = await api('/rules/simulate',{method:'POST',body:JSON.stringify({rule_ids:[id]})}); state.modalType='view'; qs('modalTitle').textContent = '规则模拟结果(命中 '+res.hit_count+' 条 / 扫描 '+res.doc_count+' 份资料)'; qs('modalBody').innerHTML = tableHtml([ {label:'规则',render:r=>esc(r.rule_name)},{label:'等级',render:r=>statusBadge(r.risk_level)}, {label:'资料',render:r=>esc(r.doc_title)},{label:'疑点描述',render:r=>''+esc(r.description)+''} ], res.hits) + `
`; qs('modalSaveBtn').textContent='关闭'; qs('modalMask').classList.add('show'); } async function exportSimulation(ids){ const res = await fetch(API+'/rules/simulate/export',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({rule_ids:ids})}); if(!res.ok){ toast('导出失败'); return; } const blob = await res.blob(); const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = '规则模拟结果.xlsx'; a.click(); URL.revokeObjectURL(a.href); } function openRuleModal(id){ const r = id ? cache.rules.find(x=>x.id===id) : null; state.modalType='rule'; state.modalId=id||null; const cats = cache.categories.map(c=>'').join(''); const types = [['keyword_exists','关键词存在性'],['keyword_forbidden','禁止词校验'],['regex_match','正则格式校验'],['similarity','文本相似度'],['time_logic','时间逻辑'],['numeric_range','数值范围'],['cross_doc','跨文档勾稽'],['calc_check','计算校验']]; qs('modalTitle').textContent = id?'编辑规则':'新增规则'; qs('modalBody').innerHTML = `
`; if (r && r.status) qs('f_status').value = r.status; qs('modalSaveBtn').textContent='保存'; qs('modalMask').classList.add('show'); }