| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- <template>
- <div class="page-container">
- <el-card>
- <div slot="header">
- <span>任务模板管理</span>
- <div style="float: right">
- <el-button
- type="primary"
- size="small"
- icon="el-icon-plus"
- @click="handleCreate"
- >
- 新建模板
- </el-button>
- <el-button
- size="small"
- @click="handleRefresh"
- :loading="loading"
- >
- 刷新
- </el-button>
- </div>
- </div>
- <!-- 搜索和筛选 -->
- <el-form :inline="true" :model="searchForm" style="margin-bottom: 20px">
- <el-form-item label="分类">
- <el-select v-model="searchForm.category" placeholder="全部分类" clearable @change="handleSearch">
- <el-option label="学习" value="学习"></el-option>
- <el-option label="家务" value="家务"></el-option>
- <el-option label="运动" value="运动"></el-option>
- <el-option label="阅读" value="阅读"></el-option>
- <el-option label="生活习惯" value="生活习惯"></el-option>
- <el-option label="其他" value="其他"></el-option>
- </el-select>
- </el-form-item>
- <el-form-item label="难度">
- <el-select v-model="searchForm.difficulty" placeholder="全部难度" clearable @change="handleSearch">
- <el-option label="简单" value="easy"></el-option>
- <el-option label="中等" value="medium"></el-option>
- <el-option label="困难" value="hard"></el-option>
- </el-select>
- </el-form-item>
- <el-form-item label="状态">
- <el-select v-model="searchForm.isActive" placeholder="全部状态" clearable @change="handleSearch">
- <el-option label="启用" :value="1"></el-option>
- <el-option label="禁用" :value="0"></el-option>
- </el-select>
- </el-form-item>
- <el-form-item>
- <el-button type="primary" @click="handleSearch">搜索</el-button>
- <el-button @click="handleReset">重置</el-button>
- </el-form-item>
- </el-form>
- <!-- 模板列表 -->
- <el-table
- :data="tableData"
- border
- v-loading="loading"
- style="width: 100%"
- :max-height="tableHeight"
- >
- <el-table-column prop="id" label="ID" width="80"></el-table-column>
- <el-table-column prop="title" label="模板名称" min-width="200"></el-table-column>
- <el-table-column prop="category" label="分类" width="100">
- <template slot-scope="scope">
- <el-tag :type="getCategoryType(scope.row.category)" size="small">
- {{ scope.row.category }}
- </el-tag>
- </template>
- </el-table-column>
- <el-table-column prop="points" label="积分" width="80">
- <template slot-scope="scope">
- <el-tag type="success">{{ scope.row.points }}</el-tag>
- </template>
- </el-table-column>
- <el-table-column prop="difficulty" label="难度" width="100">
- <template slot-scope="scope">
- <el-tag :type="getDifficultyType(scope.row.difficulty)" size="small">
- {{ getDifficultyText(scope.row.difficulty) }}
- </el-tag>
- </template>
- </el-table-column>
- <el-table-column prop="recommendedAge" label="推荐年龄" width="100">
- <template slot-scope="scope">
- <span v-if="scope.row.recommendedAge">{{ scope.row.recommendedAge }}岁+</span>
- <span v-else>-</span>
- </template>
- </el-table-column>
- <el-table-column prop="duration" label="时长" width="80">
- <template slot-scope="scope">
- <span v-if="scope.row.duration">{{ scope.row.duration }}分钟</span>
- <span v-else>-</span>
- </template>
- </el-table-column>
- <el-table-column prop="taskType" label="性质" width="80">
- <template slot-scope="scope">
- <el-tag v-if="scope.row.taskType === 'recurring'" type="warning" size="small">循环</el-tag>
- <el-tag v-else type="info" size="small">一次性</el-tag>
- </template>
- </el-table-column>
- <el-table-column prop="needReview" label="需审核" width="80">
- <template slot-scope="scope">
- <el-tag v-if="scope.row.needReview === 1" type="warning" size="small">是</el-tag>
- <el-tag v-else type="info" size="small">否</el-tag>
- </template>
- </el-table-column>
- <el-table-column prop="isActive" label="状态" width="80">
- <template slot-scope="scope">
- <el-switch
- v-model="scope.row.isActive"
- :active-value="1"
- :inactive-value="0"
- @change="handleStatusChange(scope.row)"
- ></el-switch>
- </template>
- </el-table-column>
- <el-table-column prop="createdAt" label="创建时间" width="180">
- <template slot-scope="scope">
- {{ formatDate(scope.row.createdAt) }}
- </template>
- </el-table-column>
- <el-table-column label="操作" width="180" fixed="right">
- <template slot-scope="scope">
- <el-button
- type="text"
- size="small"
- @click="handleEdit(scope.row)"
- >
- 编辑
- </el-button>
- <el-button
- type="text"
- size="small"
- style="color: #F56C6C"
- @click="handleDelete(scope.row)"
- >
- 删除
- </el-button>
- </template>
- </el-table-column>
- </el-table>
- <!-- 分页 -->
- <el-pagination
- style="margin-top: 20px; text-align: right"
- @size-change="handleSizeChange"
- @current-change="handleCurrentChange"
- :current-page="pagination.current"
- :page-sizes="[10, 20, 50, 100]"
- :page-size="pagination.size"
- :total="pagination.total"
- layout="total, sizes, prev, pager, next, jumper"
- ></el-pagination>
- </el-card>
- <!-- 创建/编辑模板弹窗 -->
- <el-dialog
- :title="dialogTitle"
- :visible.sync="editDialogVisible"
- width="600px"
- @close="handleDialogClose"
- >
- <el-form :model="editForm" :rules="editRules" ref="editForm" label-width="120px">
- <el-form-item label="模板名称" prop="title">
- <el-input v-model="editForm.title" placeholder="请输入模板名称"></el-input>
- </el-form-item>
- <el-form-item label="描述" prop="description">
- <el-input
- type="textarea"
- v-model="editForm.description"
- placeholder="请输入任务描述"
- :rows="3"
- ></el-input>
- </el-form-item>
- <el-form-item label="分类" prop="category">
- <el-select v-model="editForm.category" placeholder="请选择分类">
- <el-option label="学习" value="学习"></el-option>
- <el-option label="家务" value="家务"></el-option>
- <el-option label="运动" value="运动"></el-option>
- <el-option label="阅读" value="阅读"></el-option>
- <el-option label="生活习惯" value="生活习惯"></el-option>
- <el-option label="其他" value="其他"></el-option>
- </el-select>
- </el-form-item>
- <el-form-item label="积分" prop="points">
- <el-input-number v-model="editForm.points" :min="1" :max="1000" placeholder="完成奖励积分"></el-input-number>
- </el-form-item>
- <el-form-item label="难度" prop="difficulty">
- <el-select v-model="editForm.difficulty" placeholder="请选择难度">
- <el-option label="简单" value="easy"></el-option>
- <el-option label="中等" value="medium"></el-option>
- <el-option label="困难" value="hard"></el-option>
- </el-select>
- </el-form-item>
- <el-form-item label="推荐年龄" prop="recommendedAge">
- <el-input-number v-model="editForm.recommendedAge" :min="0" :max="18" placeholder="推荐年龄"></el-input-number>
- <span style="margin-left: 10px; color: #909399">岁及以上</span>
- </el-form-item>
- <el-form-item label="预计时长" prop="duration">
- <el-input-number v-model="editForm.duration" :min="1" :max="480" placeholder="预计时长"></el-input-number>
- <span style="margin-left: 10px; color: #909399">分钟</span>
- </el-form-item>
- <el-form-item label="完成方式" prop="completeTypes">
- <el-checkbox-group v-model="editForm.completeTypes">
- <el-checkbox label="checkin">打卡</el-checkbox>
- <el-checkbox label="text">文本</el-checkbox>
- <el-checkbox label="voice">语音</el-checkbox>
- <el-checkbox label="image">图片</el-checkbox>
- <el-checkbox label="video">视频</el-checkbox>
- </el-checkbox-group>
- </el-form-item>
- <el-form-item label="任务性质" prop="taskType">
- <el-radio-group v-model="editForm.taskType">
- <el-radio label="onetime">一次性任务</el-radio>
- <el-radio label="recurring">循环任务</el-radio>
- </el-radio-group>
- </el-form-item>
- <el-form-item label="频率" prop="frequency" v-if="editForm.taskType === 'recurring'">
- <el-select v-model="editForm.frequency" placeholder="请选择频率">
- <el-option label="每天" value="daily"></el-option>
- <el-option label="每周" value="weekly"></el-option>
- <el-option label="每月" value="monthly"></el-option>
- </el-select>
- </el-form-item>
- <el-form-item label="周期次数" prop="maxFrequency" v-if="editForm.taskType === 'recurring'">
- <el-input-number v-model="editForm.maxFrequency" :min="1" :max="100" placeholder="周期内最多完成次数"></el-input-number>
- <span style="margin-left: 10px; color: #909399; font-size: 12px">
- 超过此次数不计积分
- </span>
- </el-form-item>
- <el-form-item label="需要审核" prop="needReview">
- <el-switch v-model="editForm.needReview" :active-value="1" :inactive-value="0"></el-switch>
- </el-form-item>
- <el-form-item label="审核方式" prop="reviewType" v-if="editForm.needReview === 1">
- <el-select v-model="editForm.reviewType" placeholder="请选择审核方式">
- <el-option label="创建人审核" value="creator"></el-option>
- <el-option label="家长审核" value="parent"></el-option>
- <el-option label="成长规划师审核" value="teacher"></el-option>
- <el-option label="AI自动审核" value="ai"></el-option>
- </el-select>
- </el-form-item>
- <el-form-item label="按分类审核" prop="reviewByCategory" v-if="editForm.needReview === 1 && editForm.reviewType === 'parent'">
- <el-switch v-model="editForm.reviewByCategory" :active-value="1" :inactive-value="0"></el-switch>
- <span style="margin-left: 10px; color: #909399; font-size: 12px">
- 开启后,将按照分类自动分配审核人
- </span>
- </el-form-item>
- </el-form>
- <div slot="footer">
- <el-button @click="editDialogVisible = false">取消</el-button>
- <el-button type="primary" @click="submitEdit" :loading="submitting">确定</el-button>
- </div>
- </el-dialog>
- </div>
- </template>
- <script>
- import { getTaskTemplateList, createTaskTemplate, updateTaskTemplate, deleteTaskTemplate } from '@/api/admin'
- export default {
- name: 'TaskTemplates',
- computed: {
- tableHeight() {
- return window.innerHeight - 340
- }
- },
- data() {
- return {
- loading: false,
- submitting: false,
- tableData: [],
- searchForm: {
- category: '',
- difficulty: '',
- isActive: null
- },
- pagination: {
- current: 1,
- size: 10,
- total: 0
- },
- editDialogVisible: false,
- isCreate: false,
- editForm: {
- id: null,
- title: '',
- description: '',
- category: '',
- points: 10,
- difficulty: 'medium',
- recommendedAge: null,
- duration: null,
- completeTypes: ['checkin'],
- taskType: 'onetime',
- frequency: null,
- maxFrequency: null,
- needReview: 1,
- reviewType: 'creator',
- reviewByCategory: 0,
- isActive: 1
- },
- editRules: {
- title: [
- { required: true, message: '请输入模板名称', trigger: 'blur' }
- ],
- category: [
- { required: true, message: '请选择分类', trigger: 'change' }
- ],
- points: [
- { required: true, message: '请输入积分', trigger: 'blur' }
- ],
- difficulty: [
- { required: true, message: '请选择难度', trigger: 'change' }
- ]
- }
- }
- },
- computed: {
- dialogTitle() {
- return this.isCreate ? '新建任务模板' : '编辑任务模板'
- }
- },
- mounted() {
- this.loadTemplates()
- },
- methods: {
- async loadTemplates() {
- this.loading = true
- try {
- const params = {
- page: this.pagination.current,
- size: this.pagination.size,
- ...this.searchForm
- }
- const res = await getTaskTemplateList(params)
- this.tableData = res.data.records || res.data || []
- this.pagination.total = res.data.total || this.tableData.length
- } catch (error) {
- this.$message.error(error.message || '加载模板列表失败')
- } finally {
- this.loading = false
- }
- },
- handleSearch() {
- this.pagination.current = 1
- this.loadTemplates()
- },
- handleReset() {
- this.searchForm = {
- category: '',
- difficulty: '',
- isActive: null
- }
- this.handleSearch()
- },
- handleRefresh() {
- this.loadTemplates()
- },
- handleSizeChange(val) {
- this.pagination.size = val
- this.pagination.current = 1
- this.loadTemplates()
- },
- handleCurrentChange(val) {
- this.pagination.current = val
- this.loadTemplates()
- },
- handleCreate() {
- this.isCreate = true
- this.editForm = {
- id: null,
- title: '',
- description: '',
- category: '',
- points: 10,
- difficulty: 'medium',
- recommendedAge: null,
- duration: null,
- completeTypes: ['checkin'],
- taskType: 'onetime',
- frequency: null,
- maxFrequency: null,
- needReview: 1,
- reviewType: 'creator',
- reviewByCategory: 0,
- isActive: 1
- }
- this.editDialogVisible = true
- },
- handleEdit(row) {
- this.isCreate = false
- // 解析 completeTypes(如果是字符串则转为数组)
- let completeTypes = row.completeTypes
- if (typeof completeTypes === 'string') {
- try {
- completeTypes = JSON.parse(completeTypes)
- } catch (e) {
- completeTypes = ['checkin']
- }
- }
- this.editForm = {
- id: row.id,
- title: row.title,
- description: row.description || '',
- category: row.category,
- points: row.points,
- difficulty: row.difficulty,
- recommendedAge: row.recommendedAge,
- duration: row.duration,
- completeTypes: completeTypes || ['checkin'],
- taskType: row.taskType || 'onetime',
- frequency: row.frequency,
- maxFrequency: row.maxFrequency,
- needReview: row.needReview,
- reviewType: row.reviewType || 'creator',
- reviewByCategory: row.reviewByCategory || 0,
- isActive: row.isActive
- }
- this.editDialogVisible = true
- },
- handleDialogClose() {
- this.$refs.editForm.resetFields()
- },
- async submitEdit() {
- this.$refs.editForm.validate(async (valid) => {
- if (valid) {
- this.submitting = true
- try {
- // 准备提交数据,确保 completeTypes 为 JSON 字符串
- const submitData = {
- ...this.editForm,
- completeTypes: JSON.stringify(this.editForm.completeTypes)
- }
- if (this.isCreate) {
- await createTaskTemplate(submitData)
- this.$message.success('创建成功')
- } else {
- await updateTaskTemplate(this.editForm.id, submitData)
- this.$message.success('更新成功')
- }
- this.editDialogVisible = false
- this.loadTemplates()
- } catch (error) {
- this.$message.error(error.message || (this.isCreate ? '创建失败' : '更新失败'))
- } finally {
- this.submitting = false
- }
- }
- })
- },
- async handleStatusChange(row) {
- try {
- await updateTaskTemplate(row.id, { isActive: row.isActive })
- this.$message.success(row.isActive === 1 ? '已启用' : '已禁用')
- } catch (error) {
- this.$message.error(error.message || '状态更新失败')
- // 恢复原状态
- row.isActive = row.isActive === 1 ? 0 : 1
- }
- },
- handleDelete(row) {
- this.$confirm(`确定要删除模板 "${row.title}" 吗?`, '提示', {
- confirmButtonText: '确定',
- cancelButtonText: '取消',
- type: 'warning'
- }).then(async () => {
- try {
- await deleteTaskTemplate(row.id)
- this.$message.success('删除成功')
- this.loadTemplates()
- } catch (error) {
- this.$message.error(error.message || '删除失败')
- }
- }).catch(() => {})
- },
- getCategoryType(category) {
- const types = {
- '学习': 'primary',
- '家务': 'success',
- '运动': 'warning',
- '阅读': 'info',
- '生活习惯': 'danger'
- }
- return types[category] || ''
- },
- getDifficultyType(difficulty) {
- const types = {
- easy: 'success',
- medium: 'warning',
- hard: 'danger'
- }
- return types[difficulty] || 'info'
- },
- getDifficultyText(difficulty) {
- const texts = {
- easy: '简单',
- medium: '中等',
- hard: '困难'
- }
- return texts[difficulty] || difficulty
- },
- formatDate(dateStr) {
- if (!dateStr) return '-'
- const date = new Date(dateStr)
- return date.toLocaleString('zh-CN')
- }
- }
- }
- </script>
- <style scoped>
- </style>
|