SurveyTemplates.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <div class="survey-templates admin-page">
  3. <el-card>
  4. <div slot="header" class="admin-page-header">
  5. <span class="admin-page-title">调研模板管理</span>
  6. <el-button type="primary" size="mini" icon="el-icon-plus" @click="openCreate">新增模板</el-button>
  7. <el-button type="success" size="mini" icon="el-icon-magic-stick" @click="openAiGenerate">AI生成调研</el-button>
  8. </div>
  9. <el-table :data="templates" v-loading="loading" border stripe>
  10. <el-table-column prop="title" label="标题" min-width="150" />
  11. <el-table-column prop="dimension" label="维度" width="80" />
  12. <el-table-column prop="cycleType" label="周期" width="100">
  13. <template slot-scope="scope">
  14. {{ cycleLabel(scope.row.cycleType) }}
  15. </template>
  16. </el-table-column>
  17. <el-table-column prop="aiGenerated" label="AI生成" width="80">
  18. <template slot-scope="scope">
  19. {{ scope.row.aiGenerated === 1 ? '是' : '否' }}
  20. </template>
  21. </el-table-column>
  22. <el-table-column label="启用" width="80" align="center">
  23. <template slot-scope="scope">
  24. <el-switch
  25. v-model="scope.row.enabled"
  26. :active-value="1"
  27. :inactive-value="0"
  28. @change="toggleEnabled(scope.row)"
  29. />
  30. </template>
  31. </el-table-column>
  32. <el-table-column prop="createdAt" label="创建时间" width="160">
  33. <template slot-scope="scope">
  34. {{ scope.row.createdAt && scope.row.createdAt.substring(0, 10) }}
  35. </template>
  36. </el-table-column>
  37. <el-table-column label="操作" width="160" align="center">
  38. <template slot-scope="scope">
  39. <el-button type="text" size="mini" @click="openEdit(scope.row)">编辑</el-button>
  40. <el-button type="text" size="mini" style="color:#f56c6c" @click="handleDelete(scope.row)">删除</el-button>
  41. </template>
  42. </el-table-column>
  43. </el-table>
  44. </el-card>
  45. <el-dialog title="新增/编辑调研模板" :visible.sync="editDialogVisible" width="600px">
  46. <el-form :model="form" label-width="80px">
  47. <el-form-item label="标题">
  48. <el-input v-model="form.title" placeholder="调研标题" />
  49. </el-form-item>
  50. <el-form-item label="维度">
  51. <el-select v-model="form.dimension" placeholder="适用维度" style="width:120px">
  52. <el-option label="身" value="body" />
  53. <el-option label="智" value="wisdom" />
  54. <el-option label="富" value="wealth" />
  55. <el-option label="行" value="action" />
  56. <el-option label="心" value="mind" />
  57. </el-select>
  58. </el-form-item>
  59. <el-form-item label="周期">
  60. <el-select v-model="form.cycleType" placeholder="周期" style="width:120px">
  61. <el-option label="每周" value="weekly" />
  62. <el-option label="两周" value="biweekly" />
  63. <el-option label="每月" value="monthly" />
  64. </el-select>
  65. </el-form-item>
  66. <el-form-item label="启用">
  67. <el-switch v-model="form.enabled" :active-value="1" :inactive-value="0" />
  68. </el-form-item>
  69. <el-form-item label="题目">
  70. <div style="width:100%">
  71. <div v-for="(q, qIdx) in form.questions" :key="qIdx" style="margin-bottom:12px;padding:10px;border:1px solid #eee;border-radius:4px;">
  72. <el-input v-model="q.text" placeholder="题目文本" style="margin-bottom:8px" />
  73. <el-tag
  74. v-for="(opt, oIdx) in q.options"
  75. :key="oIdx"
  76. closable
  77. @close="removeOption(q, oIdx)"
  78. style="margin-right:4px;margin-bottom:4px"
  79. >{{ opt }}</el-tag>
  80. <el-input
  81. v-if="addOptionText[qIdx]"
  82. v-model="addOptionText[qIdx]"
  83. placeholder="输入选项后回车添加"
  84. size="mini"
  85. style="width:200px;margin-top:4px"
  86. @keyup.enter.native="addOption(q, qIdx)"
  87. @blur="addOption(q, qIdx)"
  88. />
  89. <el-button type="text" size="mini" @click="addOptionInput(qIdx)">+ 添加选项</el-button>
  90. <el-button type="text" size="mini" style="color:#f56c6c" @click="removeQuestion(qIdx)">删除题目</el-button>
  91. </div>
  92. <el-button type="primary" size="mini" plain @click="addQuestion">+ 添加题目</el-button>
  93. </div>
  94. </el-form-item>
  95. </el-form>
  96. <span slot="footer">
  97. <el-button @click="editDialogVisible = false">取消</el-button>
  98. <el-button type="primary" @click="handleSave">保存</el-button>
  99. </span>
  100. </el-dialog>
  101. <el-dialog title="AI生成调研题目" :visible.sync="aiDialogVisible" width="500px">
  102. <el-form :model="aiForm" label-width="60px">
  103. <el-form-item label="维度">
  104. <el-select v-model="aiForm.dimension" placeholder="适用维度" style="width:120px">
  105. <el-option label="身" value="body" />
  106. <el-option label="智" value="wisdom" />
  107. <el-option label="富" value="wealth" />
  108. <el-option label="行" value="action" />
  109. <el-option label="心" value="mind" />
  110. </el-select>
  111. </el-form-item>
  112. <el-form-item label="主题">
  113. <el-input v-model="aiForm.topic" placeholder="如:饮食习惯、睡眠质量" />
  114. </el-form-item>
  115. </el-form>
  116. <div v-if="aiResult" style="margin-top:12px;padding:10px;background:#f5f5f5;border-radius:4px;max-height:300px;overflow-y:auto;">
  117. <pre>{{ aiResult }}</pre>
  118. </div>
  119. <span slot="footer">
  120. <el-button @click="aiDialogVisible = false">取消</el-button>
  121. <el-button type="primary" @click="handleAiGenerate" :loading="aiLoading">生成</el-button>
  122. <el-button type="success" @click="saveAiResult" :disabled="!aiResult">保存为模板</el-button>
  123. </span>
  124. </el-dialog>
  125. </div>
  126. </template>
  127. <script>
  128. import { getSurveyTemplates, saveSurveyTemplate, deleteSurveyTemplate, aiGenerateSurveyQuestions } from '@/api/survey'
  129. export default {
  130. name: 'SurveyTemplates',
  131. data() {
  132. return {
  133. loading: false,
  134. templates: [],
  135. editDialogVisible: false,
  136. aiDialogVisible: false,
  137. aiLoading: false,
  138. aiResult: null,
  139. form: this.emptyForm(),
  140. aiForm: { dimension: '', topic: '' },
  141. addOptionText: {}
  142. }
  143. },
  144. created() {
  145. this.loadTemplates()
  146. },
  147. methods: {
  148. cycleLabel(type) {
  149. var map = { weekly: '每周', biweekly: '两周', monthly: '每月' }
  150. return map[type] || type || '-'
  151. },
  152. emptyForm() {
  153. return { id: null, title: '', dimension: '', cycleType: 'weekly', enabled: 1, questions: [] }
  154. },
  155. async loadTemplates() {
  156. this.loading = true
  157. try {
  158. var res = await getSurveyTemplates()
  159. this.templates = res.data || []
  160. } catch (e) {
  161. this.$message.error('加载失败: ' + (e.message || '未知错误'))
  162. } finally {
  163. this.loading = false
  164. }
  165. },
  166. openCreate() {
  167. this.form = this.emptyForm()
  168. this.form.questions = [{ text: '', options: [] }]
  169. this.editDialogVisible = true
  170. },
  171. openEdit(row) {
  172. this.form = Object.assign({}, row)
  173. if (row.questionsJson) {
  174. try { this.form.questions = JSON.parse(row.questionsJson) } catch (e) { this.form.questions = [] }
  175. } else {
  176. this.form.questions = []
  177. }
  178. this.editDialogVisible = true
  179. },
  180. addQuestion() {
  181. this.form.questions.push({ text: '', options: [] })
  182. },
  183. removeQuestion(idx) {
  184. this.form.questions.splice(idx, 1)
  185. },
  186. addOptionInput(idx) {
  187. this.$set(this.addOptionText, idx, '')
  188. },
  189. addOption(q, qIdx) {
  190. var txt = this.addOptionText[qIdx]
  191. if (txt) {
  192. q.options.push(txt)
  193. this.$set(this.addOptionText, qIdx, '')
  194. }
  195. },
  196. removeOption(q, oIdx) {
  197. q.options.splice(oIdx, 1)
  198. },
  199. async handleSave() {
  200. if (!this.form.title) { this.$message.warning('请输入标题'); return }
  201. var questionsJson = JSON.stringify(this.form.questions)
  202. var data = {
  203. title: this.form.title,
  204. dimension: this.form.dimension,
  205. cycleType: this.form.cycleType,
  206. enabled: this.form.enabled,
  207. questionsJson: questionsJson,
  208. aiGenerated: 0
  209. }
  210. if (this.form.id) data.id = this.form.id
  211. try {
  212. await saveSurveyTemplate(data)
  213. this.$message.success('保存成功')
  214. this.editDialogVisible = false
  215. this.loadTemplates()
  216. } catch (e) {
  217. this.$message.error('保存失败: ' + (e.message || '未知错误'))
  218. }
  219. },
  220. async toggleEnabled(row) {
  221. try {
  222. await saveSurveyTemplate({ id: row.id, enabled: row.enabled, title: row.title, dimension: row.dimension, cycleType: row.cycleType, questionsJson: row.questionsJson, aiGenerated: row.aiGenerated })
  223. this.$message.success('状态已更新')
  224. } catch (e) {
  225. row.enabled = row.enabled === 1 ? 0 : 1
  226. this.$message.error('更新失败')
  227. }
  228. },
  229. async handleDelete(row) {
  230. try {
  231. await this.$confirm('确认删除此模板?', '提示', { type: 'warning' })
  232. await deleteSurveyTemplate({ id: row.id })
  233. this.$message.success('删除成功')
  234. this.loadTemplates()
  235. } catch (e) {
  236. if (e !== 'cancel') this.$message.error('删除失败')
  237. }
  238. },
  239. openAiGenerate() {
  240. this.aiForm = { dimension: '', topic: '' }
  241. this.aiResult = null
  242. this.aiDialogVisible = true
  243. },
  244. async handleAiGenerate() {
  245. if (!this.aiForm.dimension || !this.aiForm.topic) {
  246. this.$message.warning('请选择维度并输入主题')
  247. return
  248. }
  249. this.aiLoading = true
  250. this.aiResult = null
  251. try {
  252. var res = await aiGenerateSurveyQuestions({ dimension: this.aiForm.dimension, topic: this.aiForm.topic })
  253. this.aiResult = typeof res.data === 'string' ? res.data : JSON.stringify(res.data, null, 2)
  254. } catch (e) {
  255. this.$message.error('AI生成失败: ' + (e.message || '未知错误'))
  256. } finally {
  257. this.aiLoading = false
  258. }
  259. },
  260. saveAiResult() {
  261. if (!this.aiResult) return
  262. try {
  263. var questions = typeof this.aiResult === 'string' ? JSON.parse(this.aiResult) : this.aiResult
  264. this.form = this.emptyForm()
  265. this.form.title = this.aiForm.topic + '调研'
  266. this.form.dimension = this.aiForm.dimension
  267. this.form.questions = questions
  268. this.form.aiGenerated = 1
  269. this.aiDialogVisible = false
  270. this.editDialogVisible = true
  271. } catch (e) {
  272. this.$message.error('解析AI结果失败,请检查格式')
  273. }
  274. }
  275. }
  276. }
  277. </script>
  278. <style scoped>
  279. .admin-page-header {
  280. display: flex;
  281. align-items: center;
  282. gap: 12px;
  283. }
  284. .admin-page-title {
  285. font-size: 16px;
  286. font-weight: bold;
  287. margin-right: auto;
  288. }
  289. </style>