/** * browse.js — 法规浏览页 + 原文详情 */ const BrowsePage = { state: { view: 'list', laws: [], total: 0, page: 1, pageSize: 50, category: '', province: '', keyword: '', loading: false, error: null, currentLaw: null }, render() { if (this.state.view === 'detail' && this.state.currentLaw) { return this.renderDetail(); } return this.renderList(); }, renderList() { return `

法规浏览

${UI.emptyState('输入条件浏览法规', '可按类别、省份、关键词筛选')}
`; }, renderDetail() { const law = this.state.currentLaw; const badge = UI.categoryBadge(law.category); const province = law.province ? `[${law.province}${law.city ? ' · ' + law.city : ''}]` : ''; const date = law.publish_date ? `发布: ${law.publish_date}` : ''; // 目录树(章节 + 条号) const toc = (law.clauses || []).map(c => { const chapter = c.chapter ? c.chapter : ''; return `
${chapter} ${c.clause_no || ''}
`; }).join(''); return `

${law.name}

${badge} ${province} ${date}
${toc ? `` : ''}
${marked.parse(law.content)}
`; }, init() { this.loadProvinces(); if (this.state.category) document.getElementById('browse-category').value = this.state.category; if (this.state.province) document.getElementById('browse-province').value = this.state.province; if (this.state.laws.length === 0) this.doBrowse(1); }, provinces: ['北京市','天津市','河北省','山西省','内蒙古自治区','辽宁省','吉林省','黑龙江省','上海市','江苏省','浙江省','安徽省','福建省','江西省','山东省','河南省','湖北省','湖南省','广东省','广西壮族自治区','海南省','重庆市','四川省','贵州省','云南省','西藏自治区','陕西省','甘肃省','青海省','宁夏回族自治区','新疆维吾尔自治区'], loadProvinces() { const sel = document.getElementById('browse-province'); if (!sel) return; this.provinces.forEach(p => { const opt = document.createElement('option'); opt.value = p; opt.textContent = p; sel.appendChild(opt); }); }, onFilterChange() { this.state.category = document.getElementById('browse-category').value; this.state.province = document.getElementById('browse-province').value; }, async doBrowse(page) { const kwInput = document.getElementById('browse-keyword'); if (kwInput) this.state.keyword = kwInput.value.trim(); this.state.page = page || 1; this.state.view = 'list'; this.state.loading = true; this.state.error = null; this.renderListState(); try { const params = { page: this.state.page, page_size: this.state.pageSize }; if (this.state.category) params.category = this.state.category; if (this.state.province) params.province = this.state.province; if (this.state.keyword) params.keyword = this.state.keyword; const resp = await API.listLaws(params); this.state.laws = resp.data.results || []; this.state.total = resp.data.total || 0; this.state.loading = false; this.renderListState(); } catch (e) { this.state.loading = false; this.state.error = e.message; this.renderListState(); } }, renderListState() { const el = document.getElementById('browse-list'); if (!el) return; if (this.state.loading) { el.innerHTML = `
${UI.spinner(20)}加载中...
`; return; } if (this.state.error) { el.innerHTML = UI.errorState(this.state.error, 'BrowsePage.doBrowse()'); return; } if (!this.state.laws.length) { el.innerHTML = UI.emptyState('无法规', '试试调整筛选条件'); return; } const rows = this.state.laws.map(l => `
${l.name} ${UI.categoryBadge(l.category)} ${l.province ? `[${l.province}${l.city ? ' · ' + l.city : ''}]` : ''} ${l.publish_date ? `${l.publish_date}` : ''} ${l.clause_count} 条
`).join(''); const pager = UI.pagination(this.state.page, this.state.pageSize, this.state.total, 'BrowsePage.doBrowse'); el.innerHTML = `
共 ${this.state.total} 篇法规
${rows}${pager}`; }, async viewLaw(lawId) { this.state.view = 'detail'; this.state.currentLaw = null; const content = document.getElementById('content'); content.innerHTML = `
${UI.spinner(20)}加载原文...
`; try { const resp = await API.getLaw(lawId); this.state.currentLaw = resp.data; content.innerHTML = this.renderDetail(); } catch (e) { content.innerHTML = UI.errorState(e.message, `BrowsePage.viewLaw(${lawId})`); } }, backToList() { this.state.view = 'list'; this.state.currentLaw = null; const content = document.getElementById('content'); content.innerHTML = this.renderList(); this.init(); this.renderListState(); }, scrollToClause(clauseId) { // 简单实现:滚动到条文内容(原文中条文已渲染) // TODO: 更精确的定位需要解析原文中的条号 const el = document.getElementById(`clause-${clauseId}`); if (el) { el.scrollIntoView({ behavior: 'smooth', block: 'center' }); el.classList.add('clause-highlight'); setTimeout(() => el.classList.remove('clause-highlight'), 2000); } }, };