TaskTemplates.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <template>
  2. <div class="page-container">
  3. <el-card>
  4. <div slot="header">
  5. <span>任务模板管理</span>
  6. <div style="float: right">
  7. <el-button
  8. type="primary"
  9. size="small"
  10. icon="el-icon-plus"
  11. @click="handleCreate"
  12. >
  13. 新建模板
  14. </el-button>
  15. <el-button
  16. size="small"
  17. @click="handleRefresh"
  18. :loading="loading"
  19. >
  20. 刷新
  21. </el-button>
  22. </div>
  23. </div>
  24. <!-- 搜索和筛选 -->
  25. <el-form :inline="true" :model="searchForm" style="margin-bottom: 20px">
  26. <el-form-item label="分类">
  27. <el-select v-model="searchForm.category" placeholder="全部分类" clearable @change="handleSearch">
  28. <el-option label="学习" value="学习"></el-option>
  29. <el-option label="家务" value="家务"></el-option>
  30. <el-option label="运动" value="运动"></el-option>
  31. <el-option label="阅读" value="阅读"></el-option>
  32. <el-option label="生活习惯" value="生活习惯"></el-option>
  33. <el-option label="其他" value="其他"></el-option>
  34. </el-select>
  35. </el-form-item>
  36. <el-form-item label="难度">
  37. <el-select v-model="searchForm.difficulty" placeholder="全部难度" clearable @change="handleSearch">
  38. <el-option label="简单" value="easy"></el-option>
  39. <el-option label="中等" value="medium"></el-option>
  40. <el-option label="困难" value="hard"></el-option>
  41. </el-select>
  42. </el-form-item>
  43. <el-form-item label="状态">
  44. <el-select v-model="searchForm.isActive" placeholder="全部状态" clearable @change="handleSearch">
  45. <el-option label="启用" :value="1"></el-option>
  46. <el-option label="禁用" :value="0"></el-option>
  47. </el-select>
  48. </el-form-item>
  49. <el-form-item>
  50. <el-button type="primary" @click="handleSearch">搜索</el-button>
  51. <el-button @click="handleReset">重置</el-button>
  52. </el-form-item>
  53. </el-form>
  54. <!-- 模板列表 -->
  55. <div style="overflow:auto;max-height:calc(100vh - 300px);">
  56. <el-table
  57. :data="tableData"
  58. border
  59. v-loading="loading"
  60. style="max-height:calc(100vh - 300px);width: 100%"
  61. >
  62. <el-table-column prop="id" label="ID" width="80"></el-table-column>
  63. <el-table-column prop="title" label="模板名称" min-width="200"></el-table-column>
  64. <el-table-column prop="category" label="分类" width="100">
  65. <template slot-scope="scope">
  66. <el-tag :type="getCategoryType(scope.row.category)" size="small">
  67. {{ scope.row.category }}
  68. </el-tag>
  69. </template>
  70. </el-table-column>
  71. <el-table-column prop="points" label="积分" width="80">
  72. <template slot-scope="scope">
  73. <el-tag type="success">{{ scope.row.points }}</el-tag>
  74. </template>
  75. </el-table-column>
  76. <el-table-column prop="difficulty" label="难度" width="100">
  77. <template slot-scope="scope">
  78. <el-tag :type="getDifficultyType(scope.row.difficulty)" size="small">
  79. {{ getDifficultyText(scope.row.difficulty) }}
  80. </el-tag>
  81. </template>
  82. </el-table-column>
  83. <el-table-column prop="recommendedAge" label="推荐年龄" width="100">
  84. <template slot-scope="scope">
  85. <span v-if="scope.row.recommendedAge">{{ scope.row.recommendedAge }}岁+</span>
  86. <span v-else>-</span>
  87. </template>
  88. </el-table-column>
  89. <el-table-column prop="duration" label="时长" width="80">
  90. <template slot-scope="scope">
  91. <span v-if="scope.row.duration">{{ scope.row.duration }}分钟</span>
  92. <span v-else>-</span>
  93. </template>
  94. </el-table-column>
  95. <el-table-column prop="taskType" label="性质" width="80">
  96. <template slot-scope="scope">
  97. <el-tag v-if="scope.row.taskType === 'recurring'" type="warning" size="small">循环</el-tag>
  98. <el-tag v-else type="info" size="small">一次性</el-tag>
  99. </template>
  100. </el-table-column>
  101. <el-table-column prop="needReview" label="需审核" width="80">
  102. <template slot-scope="scope">
  103. <el-tag v-if="scope.row.needReview === 1" type="warning" size="small">是</el-tag>
  104. <el-tag v-else type="info" size="small">否</el-tag>
  105. </template>
  106. </el-table-column>
  107. <el-table-column prop="isActive" label="状态" width="80">
  108. <template slot-scope="scope">
  109. <el-switch
  110. v-model="scope.row.isActive"
  111. :active-value="1"
  112. :inactive-value="0"
  113. @change="handleStatusChange(scope.row)"
  114. ></el-switch>
  115. </template>
  116. </el-table-column>
  117. <el-table-column prop="createdAt" label="创建时间" width="180">
  118. <template slot-scope="scope">
  119. {{ formatDate(scope.row.createdAt) }}
  120. </template>
  121. </el-table-column>
  122. <el-table-column label="操作" width="180" fixed="right">
  123. <template slot-scope="scope">
  124. <el-button
  125. type="text"
  126. size="small"
  127. @click="handleEdit(scope.row)"
  128. >
  129. 编辑
  130. </el-button>
  131. <el-button
  132. type="text"
  133. size="small"
  134. style="color: #F56C6C"
  135. @click="handleDelete(scope.row)"
  136. >
  137. 删除
  138. </el-button>
  139. </template>
  140. </el-table-column>
  141. </el-table>
  142. </div>
  143. <!-- 分页 -->
  144. <el-pagination
  145. style="margin-top: 20px; text-align: right"
  146. @size-change="handleSizeChange"
  147. @current-change="handleCurrentChange"
  148. :current-page="pagination.current"
  149. :page-sizes="[10, 20, 50, 100]"
  150. :page-size="pagination.size"
  151. :total="pagination.total"
  152. layout="total, sizes, prev, pager, next, jumper"
  153. ></el-pagination>
  154. </el-card>
  155. <!-- 创建/编辑模板弹窗 -->
  156. <el-dialog
  157. :title="dialogTitle"
  158. :visible.sync="editDialogVisible"
  159. width="600px"
  160. @close="handleDialogClose"
  161. >
  162. <el-form :model="editForm" :rules="editRules" ref="editForm" label-width="120px">
  163. <el-form-item label="模板名称" prop="title">
  164. <el-input v-model="editForm.title" placeholder="请输入模板名称"></el-input>
  165. </el-form-item>
  166. <el-form-item label="描述" prop="description">
  167. <el-input
  168. type="textarea"
  169. v-model="editForm.description"
  170. placeholder="请输入任务描述"
  171. :rows="3"
  172. ></el-input>
  173. </el-form-item>
  174. <el-form-item label="分类" prop="category">
  175. <el-select v-model="editForm.category" placeholder="请选择分类">
  176. <el-option label="学习" value="学习"></el-option>
  177. <el-option label="家务" value="家务"></el-option>
  178. <el-option label="运动" value="运动"></el-option>
  179. <el-option label="阅读" value="阅读"></el-option>
  180. <el-option label="生活习惯" value="生活习惯"></el-option>
  181. <el-option label="其他" value="其他"></el-option>
  182. </el-select>
  183. </el-form-item>
  184. <el-form-item label="积分" prop="points">
  185. <el-input-number v-model="editForm.points" :min="1" :max="1000" placeholder="完成奖励积分"></el-input-number>
  186. </el-form-item>
  187. <el-form-item label="难度" prop="difficulty">
  188. <el-select v-model="editForm.difficulty" placeholder="请选择难度">
  189. <el-option label="简单" value="easy"></el-option>
  190. <el-option label="中等" value="medium"></el-option>
  191. <el-option label="困难" value="hard"></el-option>
  192. </el-select>
  193. </el-form-item>
  194. <el-form-item label="推荐年龄" prop="recommendedAge">
  195. <el-input-number v-model="editForm.recommendedAge" :min="0" :max="18" placeholder="推荐年龄"></el-input-number>
  196. <span style="margin-left: 10px; color: #909399">岁及以上</span>
  197. </el-form-item>
  198. <el-form-item label="预计时长" prop="duration">
  199. <el-input-number v-model="editForm.duration" :min="1" :max="480" placeholder="预计时长"></el-input-number>
  200. <span style="margin-left: 10px; color: #909399">分钟</span>
  201. </el-form-item>
  202. <el-form-item label="完成方式" prop="completeTypes">
  203. <el-checkbox-group v-model="editForm.completeTypes">
  204. <el-checkbox label="checkin">打卡</el-checkbox>
  205. <el-checkbox label="text">文本</el-checkbox>
  206. <el-checkbox label="voice">语音</el-checkbox>
  207. <el-checkbox label="image">图片</el-checkbox>
  208. <el-checkbox label="video">视频</el-checkbox>
  209. </el-checkbox-group>
  210. </el-form-item>
  211. <el-form-item label="任务性质" prop="taskType">
  212. <el-radio-group v-model="editForm.taskType">
  213. <el-radio label="onetime">一次性任务</el-radio>
  214. <el-radio label="recurring">循环任务</el-radio>
  215. </el-radio-group>
  216. </el-form-item>
  217. <el-form-item label="频率" prop="frequency" v-if="editForm.taskType === 'recurring'">
  218. <el-select v-model="editForm.frequency" placeholder="请选择频率">
  219. <el-option label="每天" value="daily"></el-option>
  220. <el-option label="每周" value="weekly"></el-option>
  221. <el-option label="每月" value="monthly"></el-option>
  222. </el-select>
  223. </el-form-item>
  224. <el-form-item label="周期次数" prop="maxFrequency" v-if="editForm.taskType === 'recurring'">
  225. <el-input-number v-model="editForm.maxFrequency" :min="1" :max="100" placeholder="周期内最多完成次数"></el-input-number>
  226. <span style="margin-left: 10px; color: #909399; font-size: 12px">
  227. 超过此次数不计积分
  228. </span>
  229. </el-form-item>
  230. <el-form-item label="需要审核" prop="needReview">
  231. <el-switch v-model="editForm.needReview" :active-value="1" :inactive-value="0"></el-switch>
  232. </el-form-item>
  233. <el-form-item label="审核方式" prop="reviewType" v-if="editForm.needReview === 1">
  234. <el-select v-model="editForm.reviewType" placeholder="请选择审核方式">
  235. <el-option label="创建人审核" value="creator"></el-option>
  236. <el-option label="家长审核" value="parent"></el-option>
  237. <el-option label="成长规划师审核" value="teacher"></el-option>
  238. <el-option label="AI自动审核" value="ai"></el-option>
  239. </el-select>
  240. </el-form-item>
  241. <el-form-item label="按分类审核" prop="reviewByCategory" v-if="editForm.needReview === 1 && editForm.reviewType === 'parent'">
  242. <el-switch v-model="editForm.reviewByCategory" :active-value="1" :inactive-value="0"></el-switch>
  243. <span style="margin-left: 10px; color: #909399; font-size: 12px">
  244. 开启后,将按照分类自动分配审核人
  245. </span>
  246. </el-form-item>
  247. </el-form>
  248. <div slot="footer">
  249. <el-button @click="editDialogVisible = false">取消</el-button>
  250. <el-button type="primary" @click="submitEdit" :loading="submitting">确定</el-button>
  251. </div>
  252. </el-dialog>
  253. </div>
  254. </template>
  255. <script>
  256. import { getTaskTemplateList, createTaskTemplate, updateTaskTemplate, deleteTaskTemplate } from '@/api/admin'
  257. export default {
  258. name: 'TaskTemplates',
  259. data() {
  260. return {
  261. loading: false,
  262. submitting: false,
  263. tableData: [],
  264. searchForm: {
  265. category: '',
  266. difficulty: '',
  267. isActive: null
  268. },
  269. pagination: {
  270. current: 1,
  271. size: 10,
  272. total: 0
  273. },
  274. editDialogVisible: false,
  275. isCreate: false,
  276. editForm: {
  277. id: null,
  278. title: '',
  279. description: '',
  280. category: '',
  281. points: 10,
  282. difficulty: 'medium',
  283. recommendedAge: null,
  284. duration: null,
  285. completeTypes: ['checkin'],
  286. taskType: 'onetime',
  287. frequency: null,
  288. maxFrequency: null,
  289. needReview: 1,
  290. reviewType: 'creator',
  291. reviewByCategory: 0,
  292. isActive: 1
  293. },
  294. editRules: {
  295. title: [
  296. { required: true, message: '请输入模板名称', trigger: 'blur' }
  297. ],
  298. category: [
  299. { required: true, message: '请选择分类', trigger: 'change' }
  300. ],
  301. points: [
  302. { required: true, message: '请输入积分', trigger: 'blur' }
  303. ],
  304. difficulty: [
  305. { required: true, message: '请选择难度', trigger: 'change' }
  306. ]
  307. }
  308. }
  309. },
  310. computed: {
  311. dialogTitle() {
  312. return this.isCreate ? '新建任务模板' : '编辑任务模板'
  313. }
  314. },
  315. mounted() {
  316. this.loadTemplates()
  317. },
  318. methods: {
  319. async loadTemplates() {
  320. this.loading = true
  321. try {
  322. const params = {
  323. page: this.pagination.current,
  324. size: this.pagination.size,
  325. ...this.searchForm
  326. }
  327. const res = await getTaskTemplateList(params)
  328. this.tableData = res.data.records || res.data || []
  329. this.pagination.total = res.data.total || this.tableData.length
  330. } catch (error) {
  331. this.$message.error(error.message || '加载模板列表失败')
  332. } finally {
  333. this.loading = false
  334. }
  335. },
  336. handleSearch() {
  337. this.pagination.current = 1
  338. this.loadTemplates()
  339. },
  340. handleReset() {
  341. this.searchForm = {
  342. category: '',
  343. difficulty: '',
  344. isActive: null
  345. }
  346. this.handleSearch()
  347. },
  348. handleRefresh() {
  349. this.loadTemplates()
  350. },
  351. handleSizeChange(val) {
  352. this.pagination.size = val
  353. this.loadTemplates()
  354. },
  355. handleCurrentChange(val) {
  356. this.pagination.current = val
  357. this.loadTemplates()
  358. },
  359. handleCreate() {
  360. this.isCreate = true
  361. this.editForm = {
  362. id: null,
  363. title: '',
  364. description: '',
  365. category: '',
  366. points: 10,
  367. difficulty: 'medium',
  368. recommendedAge: null,
  369. duration: null,
  370. completeTypes: ['checkin'],
  371. taskType: 'onetime',
  372. frequency: null,
  373. maxFrequency: null,
  374. needReview: 1,
  375. reviewType: 'creator',
  376. reviewByCategory: 0,
  377. isActive: 1
  378. }
  379. this.editDialogVisible = true
  380. },
  381. handleEdit(row) {
  382. this.isCreate = false
  383. // 解析 completeTypes(如果是字符串则转为数组)
  384. let completeTypes = row.completeTypes
  385. if (typeof completeTypes === 'string') {
  386. try {
  387. completeTypes = JSON.parse(completeTypes)
  388. } catch (e) {
  389. completeTypes = ['checkin']
  390. }
  391. }
  392. this.editForm = {
  393. id: row.id,
  394. title: row.title,
  395. description: row.description || '',
  396. category: row.category,
  397. points: row.points,
  398. difficulty: row.difficulty,
  399. recommendedAge: row.recommendedAge,
  400. duration: row.duration,
  401. completeTypes: completeTypes || ['checkin'],
  402. taskType: row.taskType || 'onetime',
  403. frequency: row.frequency,
  404. maxFrequency: row.maxFrequency,
  405. needReview: row.needReview,
  406. reviewType: row.reviewType || 'creator',
  407. reviewByCategory: row.reviewByCategory || 0,
  408. isActive: row.isActive
  409. }
  410. this.editDialogVisible = true
  411. },
  412. handleDialogClose() {
  413. this.$refs.editForm.resetFields()
  414. },
  415. async submitEdit() {
  416. this.$refs.editForm.validate(async (valid) => {
  417. if (valid) {
  418. this.submitting = true
  419. try {
  420. // 准备提交数据,确保 completeTypes 为 JSON 字符串
  421. const submitData = {
  422. ...this.editForm,
  423. completeTypes: JSON.stringify(this.editForm.completeTypes)
  424. }
  425. if (this.isCreate) {
  426. await createTaskTemplate(submitData)
  427. this.$message.success('创建成功')
  428. } else {
  429. await updateTaskTemplate(this.editForm.id, submitData)
  430. this.$message.success('更新成功')
  431. }
  432. this.editDialogVisible = false
  433. this.loadTemplates()
  434. } catch (error) {
  435. this.$message.error(error.message || (this.isCreate ? '创建失败' : '更新失败'))
  436. } finally {
  437. this.submitting = false
  438. }
  439. }
  440. })
  441. },
  442. async handleStatusChange(row) {
  443. try {
  444. await updateTaskTemplate(row.id, { isActive: row.isActive })
  445. this.$message.success(row.isActive === 1 ? '已启用' : '已禁用')
  446. } catch (error) {
  447. this.$message.error(error.message || '状态更新失败')
  448. // 恢复原状态
  449. row.isActive = row.isActive === 1 ? 0 : 1
  450. }
  451. },
  452. handleDelete(row) {
  453. this.$confirm(`确定要删除模板 "${row.title}" 吗?`, '提示', {
  454. confirmButtonText: '确定',
  455. cancelButtonText: '取消',
  456. type: 'warning'
  457. }).then(async () => {
  458. try {
  459. await deleteTaskTemplate(row.id)
  460. this.$message.success('删除成功')
  461. this.loadTemplates()
  462. } catch (error) {
  463. this.$message.error(error.message || '删除失败')
  464. }
  465. }).catch(() => {})
  466. },
  467. getCategoryType(category) {
  468. const types = {
  469. '学习': 'primary',
  470. '家务': 'success',
  471. '运动': 'warning',
  472. '阅读': 'info',
  473. '生活习惯': 'danger'
  474. }
  475. return types[category] || ''
  476. },
  477. getDifficultyType(difficulty) {
  478. const types = {
  479. easy: 'success',
  480. medium: 'warning',
  481. hard: 'danger'
  482. }
  483. return types[difficulty] || 'info'
  484. },
  485. getDifficultyText(difficulty) {
  486. const texts = {
  487. easy: '简单',
  488. medium: '中等',
  489. hard: '困难'
  490. }
  491. return texts[difficulty] || difficulty
  492. },
  493. formatDate(dateStr) {
  494. if (!dateStr) return '-'
  495. const date = new Date(dateStr)
  496. return date.toLocaleString('zh-CN')
  497. }
  498. }
  499. }
  500. </script>
  501. <style scoped>
  502. .page-container {
  503. padding: 20px;
  504. }
  505. </style>