ReportTemplateManage.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <template>
  2. <div class="report-template-page admin-page">
  3. <!-- Tab 1: 模板列表 -->
  4. <el-card v-show="activeTab === 'list'">
  5. <div slot="header">
  6. <span>报告模板管理</span>
  7. <el-button size="small" type="primary" @click="openEditor(null)">+ 新增模板</el-button>
  8. </div>
  9. <div class="adm-table-wrap">
  10. <el-table :data="templates" v-loading="loading" border stripe height="100%">
  11. <el-table-column prop="id" label="ID" width="60">
  12. <template slot="header">
  13. <span class="sort-header" @click.stop="onSortClick('id', $event)">ID{{ sortIcon('id') }}</span>
  14. </template>
  15. </el-table-column>
  16. <el-table-column prop="name" label="模板名称" min-width="160">
  17. <template slot="header">
  18. <span class="sort-header" @click.stop="onSortClick('name', $event)">模板名称{{ sortIcon('name') }}</span>
  19. </template>
  20. </el-table-column>
  21. <el-table-column prop="reportType" label="报告类型" width="140">
  22. <template slot-scope="{ row }">{{ typeLabel(row.reportType) }}</template>
  23. </el-table-column>
  24. <el-table-column label="状态" width="80">
  25. <template slot-scope="{ row }">
  26. <el-tag :type="row.isActive === 1 ? 'success' : 'info'" size="small">
  27. {{ row.isActive === 1 ? '启用' : '停用' }}
  28. </el-tag>
  29. </template>
  30. </el-table-column>
  31. <el-table-column prop="updatedAt" label="更新时间" width="170">
  32. <template slot-scope="{ row }">{{ formatTime(row.updatedAt) }}</template>
  33. </el-table-column>
  34. <el-table-column label="操作" width="200" fixed="right">
  35. <template slot-scope="{ row }">
  36. <el-button size="mini" @click="openEditor(row)">编辑</el-button>
  37. <el-button size="mini" :type="row.isActive === 1 ? 'warning' : 'success'" @click="handleToggle(row)">
  38. {{ row.isActive === 1 ? '停用' : '启用' }}
  39. </el-button>
  40. <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
  41. </template>
  42. </el-table-column>
  43. </el-table>
  44. </div>
  45. </el-card>
  46. <!-- Tab 2: 模板编辑器 -->
  47. <el-card v-show="activeTab === 'editor'">
  48. <div slot="header">
  49. <span>{{ isEdit ? '编辑模板' : '新增模板' }}</span>
  50. <el-button size="small" @click="activeTab = 'list'">返回列表</el-button>
  51. </div>
  52. <div class="report-template-form-wrap">
  53. <el-form ref="form" :model="form" label-width="110px" style="max-width:900px">
  54. <el-row :gutter="16">
  55. <el-col :span="12">
  56. <el-form-item label="模板名称" required>
  57. <el-input v-model="form.name" placeholder="如:肠道菌群标准报告" />
  58. </el-form-item>
  59. </el-col>
  60. <el-col :span="12">
  61. <el-form-item label="报告类型" required>
  62. <el-select v-model="form.reportType" placeholder="选择报告类型" style="width:100%">
  63. <el-option label="肠道菌群 (gut_flora)" value="gut_flora" />
  64. <el-option label="DAN测评 (dan)" value="dan" />
  65. <el-option label="体检报告 (physical_exam)" value="physical_exam" />
  66. <el-option label="舌诊报告 (tongue)" value="tongue" />
  67. </el-select>
  68. </el-form-item>
  69. </el-col>
  70. </el-row>
  71. <!-- 固定块编辑区 -->
  72. <el-divider content-position="left">固定展示块(按顺序渲染)</el-divider>
  73. <div class="block-list">
  74. <div v-for="(block, bIdx) in form.fixedBlocks" :key="bIdx" class="block-item">
  75. <div class="block-header">
  76. <span class="block-type-tag">{{ blockTypeLabel(block.type) }}</span>
  77. <span class="block-title-display">{{ block.title || '(无标题)' }}</span>
  78. <el-button size="mini" type="text" class="block-move-up" @click="moveBlock(bIdx, -1)" :disabled="bIdx === 0">↑</el-button>
  79. <el-button size="mini" type="text" class="block-move-dn" @click="moveBlock(bIdx, 1)" :disabled="bIdx === form.fixedBlocks.length - 1">↓</el-button>
  80. <el-button size="mini" type="text" class="block-del" @click="removeBlock(bIdx)">删除</el-button>
  81. </div>
  82. <div class="block-body">
  83. <!-- score 块配置 -->
  84. <template v-if="block.type === 'score'">
  85. <el-form-item label="分数字段">
  86. <el-checkbox-group v-model="block.scoreFields">
  87. <el-checkbox label="overallScore">综合评分</el-checkbox>
  88. <el-checkbox label="gutHealthScore">菌群健康</el-checkbox>
  89. <el-checkbox label="chronicDiseaseScore">慢病控制</el-checkbox>
  90. <el-checkbox label="nutritionScore">营养均衡</el-checkbox>
  91. </el-checkbox-group>
  92. </el-form-item>
  93. </template>
  94. <!-- indicator 块配置 -->
  95. <template v-if="block.type === 'indicator'">
  96. <el-form-item label="分组标签">
  97. <el-input v-model="block.groupLabel" placeholder="如:血脂指标" style="width:200px" />
  98. </el-form-item>
  99. <el-form-item label="指标名称">
  100. <el-select v-model="block.indicatorNames" multiple filterable placeholder="搜索指标名称" style="width:100%">
  101. <el-option v-for="ind in indicatorList" :key="ind.name" :label="ind.name" :value="ind.name" />
  102. </el-select>
  103. </el-form-item>
  104. </template>
  105. <!-- list 块配置 -->
  106. <template v-if="block.type === 'list'">
  107. <el-form-item label="列表来源">
  108. <el-select v-model="block.listKey" style="width:200px">
  109. <el-option label="菌种详情" value="flora" />
  110. <el-option label="益生菌种" value="probiotic" />
  111. <el-option label="分类学" value="taxonomy" />
  112. <el-option label="食物推荐" value="food" />
  113. </el-select>
  114. </el-form-item>
  115. <el-form-item label="块标题">
  116. <el-input v-model="block.title" placeholder="块显示标题" style="width:200px" />
  117. </el-form-item>
  118. </template>
  119. <!-- text 块配置 -->
  120. <template v-if="block.type === 'text'">
  121. <el-form-item label="标题">
  122. <el-input v-model="block.title" style="width:200px" />
  123. </el-form-item>
  124. <el-form-item label="内容">
  125. <el-input v-model="block.content" type="textarea" :rows="3" />
  126. </el-form-item>
  127. </template>
  128. </div>
  129. </div>
  130. </div>
  131. <el-button size="small" type="dashed" @click="addBlock" style="margin-top:8px">+ 添加固定块</el-button>
  132. <!-- 自由显示配置 -->
  133. <el-divider content-position="left">自由显示配置</el-divider>
  134. <el-form-item label="启用自由显示">
  135. <el-switch v-model="form.freeDisplay.enabled" :active-value="true" :inactive-value="false" />
  136. <span class="hint-text">开启后,未配置在固定块的指标将追加到末尾</span>
  137. </el-form-item>
  138. <el-form-item label="追加分组名" v-if="form.freeDisplay.enabled">
  139. <el-input v-model="form.freeDisplay.groupName" placeholder="如:其他指标" style="width:200px" />
  140. </el-form-item>
  141. <el-form-item>
  142. <el-button type="primary" :loading="saving" @click="handleSave">保存</el-button>
  143. <el-button @click="handlePreview">预览效果</el-button>
  144. </el-form-item>
  145. </el-form>
  146. </div>
  147. </el-card>
  148. <!-- 预览对话框 -->
  149. <el-dialog title="模板预览" :visible.sync="previewVisible" width="700px">
  150. <div v-loading="previewLoading">
  151. <div v-for="(block, bi) in previewBlocks" :key="bi" class="preview-block">
  152. <div class="preview-block-title">{{ block.title }}</div>
  153. <!-- score -->
  154. <div v-if="block.type === 'score'" class="preview-score">
  155. <div v-for="item in block.items" :key="item.label" class="preview-score-item">
  156. <span>{{ item.label }}: {{ item.value }}</span>
  157. </div>
  158. </div>
  159. <!-- indicator -->
  160. <div v-else-if="block.type === 'indicator'" class="preview-indicator">
  161. <div v-for="(item, ii) in block.items" :key="ii" class="preview-ind-item">
  162. <span class="ind-name">{{ item.name }}</span>
  163. <span class="ind-value">{{ item.value }} {{ item.unit }}</span>
  164. <span class="ind-status" :class="'status-' + item.status">{{ item.status }}</span>
  165. </div>
  166. </div>
  167. <!-- list -->
  168. <div v-else-if="block.type === 'list'" class="preview-list">
  169. <table class="preview-table">
  170. <thead>
  171. <tr v-for="(col, ci) in block.columns" :key="ci">
  172. <th>{{ col.label }}</th>
  173. </tr>
  174. </thead>
  175. <tbody>
  176. <tr v-for="(item, li) in block.items" :key="li">
  177. <td v-for="(col, ci) in block.columns" :key="ci">{{ item[col.key] || '--' }}</td>
  178. </tr>
  179. </tbody>
  180. </table>
  181. </div>
  182. <!-- text -->
  183. <div v-else class="preview-text">{{ block.content }}</div>
  184. </div>
  185. <div v-if="!previewBlocks.length" class="empty-hint">暂无数据,请先保存模板</div>
  186. </div>
  187. <div slot="footer">
  188. <el-button @click="previewVisible = false">关闭</el-button>
  189. </div>
  190. </el-dialog>
  191. </div>
  192. </template>
  193. <script>
  194. import { getReportTemplates, saveReportTemplate, deleteReportTemplate, toggleReportTemplate, getAvailableIndicators } from '@/api/reportTemplate'
  195. import SortMixin from '@/mixins/SortMixin'
  196. export default {
  197. name: 'ReportTemplateManage',
  198. components: {},
  199. mixins: [SortMixin],
  200. data() {
  201. return {
  202. activeTab: 'list',
  203. loading: false,
  204. saving: false,
  205. templates: [],
  206. sortableColumns: { id: 'ID', name: '模板名称' },
  207. indicatorList: [],
  208. isEdit: false,
  209. previewVisible: false,
  210. previewLoading: false,
  211. previewBlocks: [],
  212. form: this.emptyForm()
  213. }
  214. },
  215. computed: {
  216. tableHeight() {
  217. return window.innerHeight - 280
  218. }
  219. },
  220. mounted() { this.fetchTemplates() },
  221. methods: {
  222. loadList() {
  223. this.fetchTemplates()
  224. },
  225. emptyForm() {
  226. return {
  227. id: null,
  228. name: '',
  229. reportType: 'gut_flora',
  230. fixedBlocks: [
  231. { type: 'score', title: '健康评分', scoreFields: ['overallScore', 'gutHealthScore', 'chronicDiseaseScore'] },
  232. { type: 'indicator', title: '指标', groupLabel: '血脂指标', indicatorNames: [] }
  233. ],
  234. freeDisplay: { enabled: true, groupName: '其他指标' }
  235. }
  236. },
  237. fetchTemplates() {
  238. this.loading = true
  239. var params = {}
  240. if (this.sortState.length > 0) params.sort = this.sortState
  241. getReportTemplates(params).then(res => {
  242. this.templates = res.data || []
  243. }).finally(() => { this.loading = false })
  244. },
  245. fetchIndicators() {
  246. getAvailableIndicators({ domain: '' }).then(res => {
  247. this.indicatorList = res.data || []
  248. }).catch(() => {})
  249. },
  250. typeLabel(rt) {
  251. const map = { gut_flora: '肠道菌群', dan: 'DAN测评', physical_exam: '体检报告', tongue: '舌诊' }
  252. return map[rt] || rt
  253. },
  254. blockTypeLabel(t) {
  255. const map = { score: '评分', indicator: '指标', list: '列表', text: '文本' }
  256. return map[t] || t
  257. },
  258. openEditor(row) {
  259. this.fetchIndicators()
  260. if (row) {
  261. this.isEdit = true
  262. try {
  263. var cfg = JSON.parse(row.config || '{}')
  264. this.form = {
  265. id: row.id,
  266. name: row.name,
  267. reportType: row.reportType,
  268. fixedBlocks: cfg.fixedBlocks || [],
  269. freeDisplay: cfg.freeDisplay || { enabled: true, groupName: '其他指标' }
  270. }
  271. } catch (e) {
  272. this.form = this.emptyForm()
  273. }
  274. } else {
  275. this.isEdit = false
  276. this.form = this.emptyForm()
  277. }
  278. this.activeTab = 'editor'
  279. },
  280. addBlock() {
  281. this.form.fixedBlocks.push({ type: 'indicator', title: '新块', groupLabel: '', indicatorNames: [] })
  282. },
  283. removeBlock(idx) {
  284. this.form.fixedBlocks.splice(idx, 1)
  285. },
  286. moveBlock(idx, dir) {
  287. var list = this.form.fixedBlocks
  288. var newIdx = idx + dir
  289. if (newIdx < 0 || newIdx >= list.length) return
  290. var tmp = list[idx]
  291. list[idx] = list[newIdx]
  292. list[newIdx] = tmp
  293. },
  294. handleSave() {
  295. if (!this.form.name || !this.form.reportType) {
  296. this.$message.error('请填写名称和报告类型')
  297. return
  298. }
  299. this.saving = true
  300. var config = {
  301. fixedBlocks: this.form.fixedBlocks.map(function(b) {
  302. var block = { type: b.type, title: b.title || '' }
  303. if (b.type === 'score') block.items = b.scoreFields || []
  304. else if (b.type === 'indicator') { block.indicatorNames = b.indicatorNames || []; block.groupLabel = b.groupLabel || '' }
  305. else if (b.type === 'list') { block.listKey = b.listKey || ''; block.title = b.title || '' }
  306. else if (b.type === 'text') { block.content = b.content || '' }
  307. return block
  308. }),
  309. freeDisplay: this.form.freeDisplay
  310. }
  311. var payload = Object.assign({}, this.form, { config: JSON.stringify(config) })
  312. delete payload.fixedBlocks
  313. delete payload.freeDisplay
  314. saveReportTemplate(payload).then(res => {
  315. this.$message.success('保存成功')
  316. this.activeTab = 'list'
  317. this.fetchTemplates()
  318. }).catch(() => { this.$message.error('保存失败') }).finally(() => { this.saving = false })
  319. },
  320. handleDelete(row) {
  321. this.$confirm('确定删除该模板?', '提示', { type: 'warning' }).then(() => {
  322. deleteReportTemplate(row.id).then(() => {
  323. this.$message.success('删除成功')
  324. this.fetchTemplates()
  325. }).catch(() => { this.$message.error('删除失败') })
  326. }).catch(function() {})
  327. },
  328. handleToggle(row) {
  329. toggleReportTemplate({ id: row.id, isActive: row.isActive === 1 ? 0 : 1 }).then(() => {
  330. this.fetchTemplates()
  331. }).catch(() => { this.$message.error('操作失败') })
  332. },
  333. handlePreview() {
  334. this.previewLoading = true
  335. this.previewVisible = true
  336. // 模拟预览:用默认 gut_flora payload 渲染
  337. var mockPayload = {
  338. summary: { overallScore: 85, gutHealthScore: 78, chronicDiseaseScore: 90, nutritionScore: 72 },
  339. indicators: [
  340. { indicatorName: '总胆固醇', value: '5.2', unit: 'mmol/L', refRange: '3.1-5.2', status: 'normal', category: '血脂' },
  341. { indicatorName: '甘油三酯', value: '1.8', unit: 'mmol/L', refRange: '0.56-1.7', status: 'high', category: '血脂' },
  342. { indicatorName: '血红蛋白', value: '135', unit: 'g/L', refRange: '115-150', status: 'normal', category: '血常规' }
  343. ],
  344. gutFlora: [{ bacteriaName: '双歧杆菌', bacteriaValue: '4.2', normalRange: '3-6', populationLevel: 'normal' }],
  345. diseaseRisks: [{ diseaseName: '心血管疾病', riskValue: '12%', riskLevel: 'low' }]
  346. }
  347. setTimeout(() => {
  348. this.previewBlocks = this.renderPreview(this.form.fixedBlocks, mockPayload)
  349. this.previewLoading = false
  350. }, 300)
  351. },
  352. renderPreview(blocks, payload) {
  353. var result = []
  354. for (var i = 0; i < blocks.length; i++) {
  355. var b = blocks[i]
  356. if (b.type === 'score') {
  357. var items = []
  358. var s = payload.summary || {}
  359. var fieldMap = { overallScore: '综合', gutHealthScore: '菌群健康', chronicDiseaseScore: '慢病控制', nutritionScore: '营养均衡' }
  360. if (b.items) {
  361. for (var j = 0; j < b.items.length; j++) {
  362. var f = b.items[j]
  363. if (s[f] != null) items.push({ label: fieldMap[f] || f, value: s[f] })
  364. }
  365. }
  366. if (items.length > 0) result.push({ type: 'score', title: b.title, items: items })
  367. } else if (b.type === 'indicator') {
  368. var filtered = (payload.indicators || []).filter(function(ind) {
  369. return b.indicatorNames && b.indicatorNames.indexOf(ind.indicatorName) >= 0
  370. })
  371. if (filtered.length > 0) result.push({ type: 'indicator', title: b.groupLabel || b.title, items: filtered })
  372. } else if (b.type === 'list') {
  373. if (b.listKey === 'flora' && payload.gutFlora && payload.gutFlora.length) {
  374. result.push({ type: 'list', title: b.title, columns: [{ key: 'bacteriaName', label: '菌种' }, { key: 'bacteriaValue', label: '数值' }, { key: 'normalRange', label: '正常范围' }, { key: 'populationLevel', label: '状态' }], items: payload.gutFlora })
  375. }
  376. } else if (b.type === 'text') {
  377. result.push({ type: 'text', title: b.title, content: b.content })
  378. }
  379. }
  380. // 自由显示
  381. if (this.form.freeDisplay && this.form.freeDisplay.enabled) {
  382. var remaining = (payload.indicators || []).filter(function(ind) {
  383. for (var j = 0; j < blocks.length; j++) {
  384. if (blocks[j].type === 'indicator' && blocks[j].indicatorNames && blocks[j].indicatorNames.indexOf(ind.indicatorName) >= 0) return false
  385. }
  386. return true
  387. })
  388. if (remaining.length > 0) {
  389. result.push({ type: 'indicator', title: this.form.freeDisplay.groupName || '其他指标', items: remaining })
  390. }
  391. }
  392. return result
  393. },
  394. }
  395. }
  396. </script>
  397. <style scoped>
  398. .report-template-page { padding: 20px; }
  399. .block-list { border: 1px solid #ebeef5; border-radius: 4px; margin-bottom: 12px; }
  400. .block-item { border-bottom: 1px solid #f0f0f0; padding: 12px 16px; }
  401. .block-item:last-child { border-bottom: none; }
  402. .block-header { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
  403. .drag-handle { cursor: move; color: #bbb; font-size: 16px; }
  404. .block-type-tag { background: #f0f9ff; color: #2c7be5; padding: 2px 8px; border-radius: 4px; font-size: 12px; }
  405. .block-title-display { color: #333; font-size: 14px; }
  406. .block-del { margin-left: auto; }
  407. .hint-text { margin-left: 12px; color: #999; font-size: 12px; }
  408. .block-list { border: 1px solid #ebeef5; border-radius: 4px; margin-bottom: 12px; }
  409. .block-item { border-bottom: 1px solid #f0f0f0; padding: 12px 16px; }
  410. .block-item:last-child { border-bottom: none; }
  411. .block-header { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
  412. .block-type-tag { background: #f0f9ff; color: #2c7be5; padding: 2px 8px; border-radius: 4px; font-size: 12px; }
  413. .block-title-display { color: #333; font-size: 14px; }
  414. .block-del { margin-left: auto; }
  415. .block-move-up, .block-move-dn { color: #909399; }
  416. .preview-block { background: #fff; border: 1px solid #eee; border-radius: 8px; padding: 16px; margin-bottom: 12px; }
  417. .preview-block-title { font-weight: bold; font-size: 15px; margin-bottom: 10px; color: #303133; }
  418. .preview-score-item { display: inline-block; background: #f5f7fa; padding: 4px 12px; margin: 4px; border-radius: 4px; }
  419. .preview-ind-item { display: flex; align-items: center; padding: 6px 0; border-bottom: 1px solid #f0f0f0; }
  420. .ind-name { flex: 1; font-size: 13px; }
  421. .ind-value { font-size: 13px; color: #666; margin: 0 12px; }
  422. .ind-status { font-size: 12px; padding: 2px 8px; border-radius: 10px; }
  423. .status-normal { background: #e8f5e9; color: #2e7d32; }
  424. .status-high { background: #ffebee; color: #c62828; }
  425. .status-low { background: #fff3e0; color: #e65100; }
  426. .preview-table { width: 100%; border-collapse: collapse; font-size: 13px; }
  427. .preview-table th, .preview-table td { border: 1px solid #eee; padding: 6px 10px; text-align: left; }
  428. .preview-table th { background: #f5f7fa; font-weight: 600; }
  429. .preview-text { font-size: 14px; color: #555; line-height: 1.7; white-space: pre-wrap; }
  430. .empty-hint { color: #bbb; text-align: center; padding: 40px; }
  431. .sort-header {
  432. cursor: pointer;
  433. user-select: none;
  434. font-weight: 500;
  435. font-size: 12px;
  436. }
  437. .sort-header:hover {
  438. color: #409eff;
  439. }
  440. </style>