SurveyTemplates.vue 13 KB

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