Files
audit-platform/综合管理平台/js/rules.js
T
guwangtu b91954df4a
CI / check (push) Failing after 15m48s
chore: initial open-source baseline v1.0.0
Signed-off-by: 南通一次元科技有限公司 <guwangtu@icimeta.com>
2026-12-05 15:44:53 +00:00

122 lines
7.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ==================== 规则库 ====================
function ruleColumns(){
return [
{label:'编号',render:r=>'<span style="font-size:11px;color:var(--text-muted)">'+esc(r.code)+'</span>'},
{label:'规则名称',render:r=>'<b style="color:var(--text-primary)">'+esc(r.name)+'</b>'},
{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=>'<span class="tag-chip">'+esc(t)+'</span>').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 = '<button class="btn primary" onclick="openRuleModal()">+ 新增规则</button>';
const opts = ['<option value="">全部分类</option>'].concat(cache.categories.map(c=>'<option value="'+esc(c.name)+'">'+esc(c.name)+'</option>')).join('');
qs('content').innerHTML = `
<div class="card">
<div class="toolbar" style="display:flex;gap:10px;margin-bottom:12px">
<select id="rfCategory" onchange="filterRules()">${opts}</select>
<select id="rfRisk" onchange="filterRules()"><option value="">全部等级</option><option>R1</option><option>R2</option><option>R3</option><option>R4</option></select>
<select id="rfStatus" onchange="filterRules()"><option value="">全部发布状态</option><option>草稿</option><option>待审批</option><option>已发布</option></select>
<input id="rfKeyword" placeholder="规则名称 / 编号" onkeyup="if(event.key==='Enter')filterRules()">
<button class="btn" onclick="filterRules()">查询</button>
</div>
<div id="rulesTable"></div>
</div>`;
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 '<span class="badge '+(map[s]||'b-gray')+'">'+esc(s||'草稿')+'</span>';
}
function ruleActions(r){
let b = `<button class="btn sm" onclick="openRuleModal(${r.id})">编辑</button> `;
b += `<button class="btn sm" onclick="simulateRule(${r.id})">模拟</button> `;
if(!r.publish_status || r.publish_status==='草稿' || r.publish_status==='已退回'){
b += `<button class="btn sm" onclick="submitRule(${r.id})">提交审批</button> `;
}
if(r.publish_status==='待审批'){
b += `<button class="btn sm" onclick="reviewRule(${r.id},true)">通过</button> `;
b += `<button class="btn sm danger" onclick="reviewRule(${r.id},false)">退回</button> `;
}
b += `<button class="btn sm danger" onclick="delItem('rules',${r.id})">删除</button>`;
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=>'<span style="font-size:12px">'+esc(r.description)+'</span>'}
], res.hits) + `<div style="margin-top:10px"><button class="btn" onclick="exportSimulation([${id}])">导出模拟结果 Excel</button></div>`;
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=>'<option value="'+c.id+'" '+(r&&r.category_id===c.id?'selected':'')+'>'+esc(c.name)+'</option>').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 = `
<div class="form-row"><div class="form-item"><label>规则名称</label><input id="f_name" value="${esc(r?r.name:'')}"></div>
<div class="form-item"><label>所属分类</label><select id="f_category">${cats}</select></div></div>
<div class="form-row"><div class="form-item"><label>风险等级</label><select id="f_level">${['R1','R2','R3','R4'].map(x=>'<option '+(r&&r.risk_level===x?'selected':'')+'>'+x+'</option>').join('')}</select></div>
<div class="form-item"><label>规则类型</label><select id="f_type">${types.map(t=>'<option value="'+t[0]+'" '+(r&&r.rule_type===t[0]?'selected':'')+'>'+t[1]+'</option>').join('')}</select></div>
<div class="form-item"><label>状态</label><select id="f_status"><option>启用</option><option>停用</option></select></div></div>
<div class="form-row"><div class="form-item full"><label>规则参数(JSON)</label><textarea id="f_params">${esc(r?JSON.stringify(r.params):'{"keywords":["违约责任"],"min_count":1}')}</textarea></div></div>
<div class="form-row"><div class="form-item"><label>标签(逗号分隔)</label><input id="f_tags" value="${esc(r?(r.tags||[]).join(','):'')}"></div>
<div class="form-item"><label>规则说明</label><input id="f_desc" value="${esc(r?r.description:'')}"></div></div>`;
if (r && r.status) qs('f_status').value = r.status;
qs('modalSaveBtn').textContent='保存';
qs('modalMask').classList.add('show');
}