App.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. <script setup>
  2. import { computed, onMounted, reactive, ref, watch } from 'vue'
  3. import { TemplateAPI } from './services/api'
  4. const templates = ref([])
  5. const categories = ref([])
  6. const selectedTemplateId = ref(null)
  7. const previewResult = ref('')
  8. const toast = reactive({ type: '', message: '' })
  9. const loading = ref(false)
  10. const defaultEditor = () => ({
  11. name: '',
  12. category: '',
  13. description: '',
  14. output_description: '',
  15. call_method: '',
  16. template_body: '',
  17. input_variables: []
  18. })
  19. const editor = reactive(defaultEditor())
  20. const parameterValues = reactive({})
  21. const groupedTemplates = computed(() => {
  22. const map = {}
  23. templates.value.forEach((tpl) => {
  24. map[tpl.category] = map[tpl.category] ?? []
  25. map[tpl.category].push(tpl)
  26. })
  27. return map
  28. })
  29. const mode = computed(() => (selectedTemplateId.value ? 'update' : 'create'))
  30. const currentTemplate = computed(() =>
  31. templates.value.find((tpl) => tpl.id === selectedTemplateId.value)
  32. )
  33. const callMethodSnippet = computed(() => {
  34. const templateId = selectedTemplateId.value || '<template-id>'
  35. const parameters = editor.input_variables.reduce((acc, variable, index) => {
  36. const key = (variable.name || `variable_${index + 1}`).trim()
  37. const valueKey = variable.name?.trim()
  38. const previewValue =
  39. (valueKey ? parameterValues[valueKey] : undefined) ?? variable.default ?? ''
  40. acc[key] = previewValue
  41. return acc
  42. }, {})
  43. const body = JSON.stringify(
  44. {
  45. template_id: templateId,
  46. parameters
  47. },
  48. null,
  49. 2
  50. )
  51. return ['POST /api/templates/render', 'Content-Type: application/json', '', body].join('\n')
  52. })
  53. function setToast(message, type = 'success', timeout = 2600) {
  54. toast.type = type
  55. toast.message = message
  56. if (!message) return
  57. setTimeout(() => {
  58. toast.message = ''
  59. }, timeout)
  60. }
  61. async function loadTemplates() {
  62. loading.value = true
  63. try {
  64. const [tplList, { categories: categoryList }] = await Promise.all([
  65. TemplateAPI.listTemplates(),
  66. TemplateAPI.listCategories()
  67. ])
  68. templates.value = tplList
  69. categories.value = categoryList
  70. if (!selectedTemplateId.value && tplList.length) {
  71. selectTemplate(tplList[0])
  72. }
  73. } catch (err) {
  74. setToast(err.message ?? '加载失败', 'error', 4000)
  75. } finally {
  76. loading.value = false
  77. }
  78. }
  79. function resetEditor() {
  80. Object.assign(editor, defaultEditor())
  81. Object.keys(parameterValues).forEach((key) => delete parameterValues[key])
  82. previewResult.value = ''
  83. }
  84. function hydrateEditor(template) {
  85. Object.assign(editor, JSON.parse(JSON.stringify(template)))
  86. template.input_variables?.forEach((variable) => {
  87. parameterValues[variable.name] =
  88. parameterValues[variable.name] ?? variable.default ?? ''
  89. })
  90. }
  91. function selectTemplate(template) {
  92. selectedTemplateId.value = template.id
  93. resetEditor()
  94. hydrateEditor(template)
  95. previewResult.value = ''
  96. }
  97. function startCreate() {
  98. selectedTemplateId.value = null
  99. resetEditor()
  100. }
  101. function addVariable() {
  102. editor.input_variables.push({
  103. name: '',
  104. label: '',
  105. description: '',
  106. data_type: 'string',
  107. required: false,
  108. default: ''
  109. })
  110. }
  111. function removeVariable(index) {
  112. const [removed] = editor.input_variables.splice(index, 1)
  113. if (removed?.name) {
  114. delete parameterValues[removed.name]
  115. }
  116. }
  117. async function saveTemplate() {
  118. try {
  119. const payload = JSON.parse(JSON.stringify(editor))
  120. payload.call_method = callMethodSnippet.value
  121. if (mode.value === 'update') {
  122. await TemplateAPI.updateTemplate(selectedTemplateId.value, payload)
  123. setToast('模板已更新')
  124. } else {
  125. const created = await TemplateAPI.createTemplate(payload)
  126. selectedTemplateId.value = created.id
  127. setToast('模板已创建')
  128. }
  129. await loadTemplates()
  130. } catch (err) {
  131. setToast(err.message ?? '保存失败', 'error')
  132. }
  133. }
  134. async function removeTemplate() {
  135. if (!selectedTemplateId.value) return
  136. if (!confirm('确定要删除当前模板吗?')) return
  137. try {
  138. await TemplateAPI.deleteTemplate(selectedTemplateId.value)
  139. setToast('模板已删除', 'success')
  140. startCreate()
  141. await loadTemplates()
  142. } catch (err) {
  143. setToast(err.message ?? '删除失败', 'error')
  144. }
  145. }
  146. async function previewTemplate() {
  147. if (!editor.template_body) {
  148. setToast('请先填写模板正文', 'error')
  149. return
  150. }
  151. try {
  152. const result = await TemplateAPI.previewTemplate({
  153. template_body: editor.template_body,
  154. parameters: parameterValues
  155. })
  156. previewResult.value = result.rendered_text
  157. } catch (err) {
  158. setToast(err.message ?? '预览失败', 'error', 4000)
  159. }
  160. }
  161. async function renderTemplate() {
  162. if (!selectedTemplateId.value) {
  163. setToast('请先保存模板', 'error')
  164. return
  165. }
  166. try {
  167. const result = await TemplateAPI.renderTemplate({
  168. template_id: selectedTemplateId.value,
  169. parameters: parameterValues
  170. })
  171. previewResult.value = result.rendered_text
  172. setToast('已根据后端模板生成', 'success')
  173. } catch (err) {
  174. setToast(err.message ?? '生成失败', 'error', 4000)
  175. }
  176. }
  177. onMounted(() => {
  178. loadTemplates()
  179. startCreate()
  180. })
  181. watch(
  182. () => editor.input_variables.map((variable) => variable.name),
  183. (names) => {
  184. Object.keys(parameterValues).forEach((param) => {
  185. if (!names.includes(param)) {
  186. delete parameterValues[param]
  187. }
  188. })
  189. },
  190. { deep: true }
  191. )
  192. </script>
  193. <template>
  194. <div class="page">
  195. <header class="page-header">
  196. <div>
  197. <p class="eyebrow">洗衣机语音播报模板运营系统</p>
  198. <h1>自定义模板 · 分类管理 · 一键生成播报</h1>
  199. <p class="subtitle">
  200. 在可视化界面配置 Jinja 模板,实时预览变量效果,并把调用方法、输入输出标准化给运营或研发使用。
  201. </p>
  202. </div>
  203. <div class="header-actions">
  204. <button @click="startCreate">+ 新建模板</button>
  205. </div>
  206. </header>
  207. <section v-if="toast.message" class="toast" :class="toast.type">
  208. {{ toast.message }}
  209. </section>
  210. <div class="layout">
  211. <aside class="sidebar" :class="{ loading: loading }">
  212. <h3>模板清单</h3>
  213. <p class="muted">按分类浏览已有模板,点击可切换编辑。</p>
  214. <div v-if="!Object.keys(groupedTemplates).length" class="empty-state">
  215. 暂无模板,点击右上角「新建模板」开始配置。
  216. </div>
  217. <div v-else class="category-list">
  218. <div v-for="(items, category) in groupedTemplates" :key="category" class="category-block">
  219. <div class="category-title">
  220. <span>{{ category }}</span>
  221. <span class="tag">{{ items.length }}</span>
  222. </div>
  223. <div class="template-list">
  224. <button
  225. v-for="item in items"
  226. :key="item.id"
  227. class="template-item"
  228. :class="{ active: selectedTemplateId === item.id }"
  229. @click="selectTemplate(item)"
  230. >
  231. <div>
  232. <strong>{{ item.name }}</strong>
  233. <p>{{ item.description }}</p>
  234. </div>
  235. </button>
  236. </div>
  237. </div>
  238. </div>
  239. </aside>
  240. <main class="content">
  241. <section class="card">
  242. <div class="card-header">
  243. <div>
  244. <p class="eyebrow">模板基础信息</p>
  245. <h2>定义模板 & 调用说明</h2>
  246. </div>
  247. <div class="gap-8">
  248. <button class="ghost" v-if="selectedTemplateId" @click="removeTemplate">删除模板</button>
  249. <button @click="saveTemplate">{{ mode === 'update' ? '保存修改' : '创建模板' }}</button>
  250. </div>
  251. </div>
  252. <div class="form-grid">
  253. <label>
  254. 模板名称
  255. <input v-model="editor.name" placeholder="如:智能推荐播报" />
  256. </label>
  257. <label>
  258. 分类
  259. <input v-model="editor.category" placeholder="如:洗衣机推荐" />
  260. </label>
  261. <label>
  262. 调用方式
  263. <p class="muted">系统已根据模板 ID 和输入变量生成示例,无需手动填写。</p>
  264. <pre class="call-snippet">{{ callMethodSnippet }}</pre>
  265. </label>
  266. <label>
  267. 模板说明
  268. <textarea v-model="editor.description" placeholder="描述模板的使用场景、亮点" rows="3" />
  269. </label>
  270. <label>
  271. 输出说明
  272. <textarea v-model="editor.output_description" placeholder="向运营/研发说明生成结果结构、段落" rows="3" />
  273. </label>
  274. </div>
  275. </section>
  276. <section class="card">
  277. <div class="card-header">
  278. <div>
  279. <p class="eyebrow">输入变量</p>
  280. <h2>定义前端可视化填写项</h2>
  281. </div>
  282. <button class="ghost" @click="addVariable">+ 新增变量</button>
  283. </div>
  284. <div v-if="!editor.input_variables.length" class="empty-state">
  285. 还没有变量,点击「新增变量」来定义前端可配置项。
  286. </div>
  287. <div v-else class="variable-grid">
  288. <div v-for="(variable, index) in editor.input_variables" :key="index" class="variable-item">
  289. <div class="variable-header">
  290. <strong>{{ variable.label || '未命名变量' }}</strong>
  291. <button class="ghost" @click="removeVariable(index)">移除</button>
  292. </div>
  293. <div class="variable-fields">
  294. <label>
  295. 变量名
  296. <input v-model="variable.name" placeholder="在模板中引用的变量名" />
  297. </label>
  298. <label>
  299. 展示名称
  300. <input v-model="variable.label" placeholder="用于前端输入表单的名称" />
  301. </label>
  302. <label>
  303. 类型
  304. <select v-model="variable.data_type">
  305. <option value="string">文本</option>
  306. <option value="number">数字</option>
  307. <option value="boolean">布尔</option>
  308. <option value="options">枚举</option>
  309. </select>
  310. </label>
  311. <label class="inline-label">
  312. <input type="checkbox" v-model="variable.required" />
  313. 必填
  314. </label>
  315. <label>
  316. 默认值
  317. <input v-model="variable.default" placeholder="可选默认值,便于预览" />
  318. </label>
  319. <label>
  320. 变量描述
  321. <textarea v-model="variable.description" rows="2" placeholder="提示运营如何填写" />
  322. </label>
  323. </div>
  324. </div>
  325. </div>
  326. </section>
  327. <section class="card">
  328. <div class="card-header">
  329. <div>
  330. <p class="eyebrow">模板正文</p>
  331. <h2>编写 Jinja 播报模版</h2>
  332. </div>
  333. <div class="gap-8">
  334. <button class="ghost" @click="previewTemplate">本地预览</button>
  335. <button class="ghost" @click="saveTemplate">
  336. {{ mode === 'update' ? '保存修改' : '创建模板' }}
  337. </button>
  338. <button @click="renderTemplate">调用后端生成</button>
  339. </div>
  340. </div>
  341. <div class="editor-grid">
  342. <label>
  343. 模板正文 (支持 Jinja2)
  344. <textarea v-model="editor.template_body" placeholder="在此编写模板,可引用 {{ variable }} 等变量" />
  345. </label>
  346. <div>
  347. <h3>变量填充区</h3>
  348. <p class="muted">输入样例值,便于预览和对接。</p>
  349. <div v-if="!editor.input_variables.length" class="empty-state small">
  350. 添加变量后可在此填充预览数据。
  351. </div>
  352. <div class="parameter-grid" v-else>
  353. <label v-for="variable in editor.input_variables" :key="variable.name || variable.label">
  354. {{ variable.label || variable.name }}
  355. <span class="tag">{{ variable.data_type }}</span>
  356. <input
  357. :placeholder="variable.description"
  358. v-model="parameterValues[variable.name]"
  359. />
  360. </label>
  361. </div>
  362. </div>
  363. </div>
  364. <div class="preview-panel" v-if="previewResult">
  365. <div class="preview-header">
  366. <h3>生成结果</h3>
  367. <span class="muted">{{ editor.output_description || '播报示例' }}</span>
  368. </div>
  369. <pre>{{ previewResult }}</pre>
  370. </div>
  371. </section>
  372. </main>
  373. </div>
  374. </div>
  375. </template>
  376. <style scoped>
  377. .page {
  378. padding: 2.5rem;
  379. max-width: 1400px;
  380. margin: 0 auto;
  381. display: flex;
  382. flex-direction: column;
  383. gap: 1.5rem;
  384. }
  385. .page-header {
  386. background: #fff;
  387. border-radius: 16px;
  388. padding: 1.75rem;
  389. display: flex;
  390. justify-content: space-between;
  391. align-items: center;
  392. gap: 1.5rem;
  393. border: 1px solid #e2e8f3;
  394. }
  395. .eyebrow {
  396. font-size: 0.85rem;
  397. text-transform: uppercase;
  398. letter-spacing: 0.08rem;
  399. color: #64748b;
  400. margin-bottom: 0.25rem;
  401. }
  402. .subtitle {
  403. color: #475467;
  404. margin-top: 0.4rem;
  405. }
  406. .header-actions {
  407. display: flex;
  408. align-items: center;
  409. gap: 0.75rem;
  410. }
  411. .layout {
  412. display: grid;
  413. grid-template-columns: 320px 1fr;
  414. gap: 1.5rem;
  415. }
  416. .sidebar {
  417. background: #fff;
  418. border-radius: 16px;
  419. padding: 1.5rem;
  420. border: 1px solid #e2e8f3;
  421. min-height: 500px;
  422. }
  423. .sidebar.loading {
  424. opacity: 0.6;
  425. }
  426. .category-list {
  427. display: flex;
  428. flex-direction: column;
  429. gap: 1rem;
  430. margin-top: 1rem;
  431. }
  432. .category-title {
  433. display: flex;
  434. justify-content: space-between;
  435. align-items: center;
  436. font-weight: 600;
  437. color: #1f2937;
  438. }
  439. .template-list {
  440. display: flex;
  441. flex-direction: column;
  442. gap: 0.6rem;
  443. margin-top: 0.7rem;
  444. }
  445. .template-item {
  446. width: 100%;
  447. text-align: left;
  448. border-radius: 10px;
  449. padding: 0.75rem;
  450. border: 1px solid transparent;
  451. background: #f8fafc;
  452. }
  453. .template-item strong {
  454. display: block;
  455. color:#1f2937;
  456. margin-bottom: 0.25rem;
  457. }
  458. .template-item p {
  459. margin: 0;
  460. color: #475467;
  461. font-size: 0.85rem;
  462. }
  463. .template-item.active {
  464. background: #eef2ff;
  465. border-color: #4338ca;
  466. }
  467. .content {
  468. display: flex;
  469. flex-direction: column;
  470. gap: 1.25rem;
  471. }
  472. .card {
  473. background: #fff;
  474. border-radius: 16px;
  475. border: 1px solid #e2e8f3;
  476. padding: 1.5rem;
  477. display: flex;
  478. flex-direction: column;
  479. gap: 1rem;
  480. }
  481. .card-header {
  482. display: flex;
  483. justify-content: space-between;
  484. align-items: center;
  485. gap: 1rem;
  486. }
  487. .gap-8 {
  488. display: flex;
  489. gap: 0.75rem;
  490. }
  491. .form-grid {
  492. display: grid;
  493. gap: 1rem;
  494. }
  495. .call-snippet {
  496. background: #0f172a;
  497. color: #e2e8f0;
  498. font-family: "JetBrains Mono", Consolas, monospace;
  499. padding: 0.75rem;
  500. border-radius: 10px;
  501. font-size: 0.9rem;
  502. white-space: pre-wrap;
  503. border: 1px solid #1e293b;
  504. margin-top: 0.5rem;
  505. }
  506. .variable-grid {
  507. display: flex;
  508. flex-direction: column;
  509. gap: 1rem;
  510. }
  511. .variable-item {
  512. border: 1px solid #e2e8f3;
  513. border-radius: 12px;
  514. padding: 1rem;
  515. }
  516. .variable-header {
  517. display: flex;
  518. justify-content: space-between;
  519. align-items: center;
  520. margin-bottom: 1rem;
  521. }
  522. .variable-fields {
  523. display: grid;
  524. grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  525. gap: 0.75rem;
  526. }
  527. .inline-label {
  528. display: flex;
  529. align-items: center;
  530. gap: 0.4rem;
  531. margin-top: 1.6rem;
  532. font-size: 0.85rem;
  533. color: #1f2937;
  534. }
  535. .editor-grid {
  536. display: grid;
  537. grid-template-columns: 1fr 320px;
  538. gap: 1rem;
  539. }
  540. .parameter-grid {
  541. display: flex;
  542. flex-direction: column;
  543. gap: 0.75rem;
  544. margin-top: 0.5rem;
  545. }
  546. .preview-panel {
  547. border: 1px solid #c7d2fe;
  548. border-radius: 12px;
  549. padding: 1rem;
  550. background: #f8f9ff;
  551. }
  552. .preview-panel pre {
  553. white-space: pre-wrap;
  554. margin: 0;
  555. font-family: "JetBrains Mono", Consolas, monospace;
  556. font-size: 0.95rem;
  557. }
  558. .preview-header {
  559. display: flex;
  560. justify-content: space-between;
  561. align-items: baseline;
  562. margin-bottom: 0.8rem;
  563. }
  564. .muted {
  565. color: #6b7280;
  566. font-size: 0.9rem;
  567. }
  568. .toast {
  569. padding: 0.85rem 1rem;
  570. border-radius: 10px;
  571. font-weight: 600;
  572. }
  573. .toast.success {
  574. background: #ecfdf3;
  575. color: #027a48;
  576. border: 1px solid #a7f3d0;
  577. }
  578. .toast.error {
  579. background: #fef3f2;
  580. color: #b42318;
  581. border: 1px solid #fecdcf;
  582. }
  583. .empty-state {
  584. padding: 1rem;
  585. background: #f8fafc;
  586. border: 1px dashed #cbd5f5;
  587. border-radius: 10px;
  588. color: #475467;
  589. }
  590. .empty-state.small {
  591. padding: 0.8rem;
  592. }
  593. @media (max-width: 1100px) {
  594. .layout {
  595. grid-template-columns: 1fr;
  596. }
  597. .editor-grid {
  598. grid-template-columns: 1fr;
  599. }
  600. }
  601. </style>