ReportTemplateManage.vue 19 KB

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