TaskTemplates.vue 18 KB

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