| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770 |
- <template>
- <PlayfulCard elevation="1">
- <view class="container">
- <!-- 批量操作栏 -->
- <view class="batch-bar" v-if="batchMode">
- <checkbox :checked="isAllSelected" @click="toggleSelectAll" />
- <text class="batch-label">全选</text>
- <view class="batch-actions">
- <button class="batch-btn complete" @click="batchComplete" :disabled="selectedTasks.length === 0">批量完成</button>
- <button class="batch-btn delete" @click="batchDelete" :disabled="selectedTasks.length === 0">批量删除</button>
- </view>
- <button class="batch-exit" @click="exitBatchMode">退出</button>
- </view>
- <!-- 正常模式顶部栏 -->
- <view class="header-bar" v-else>
- <text class="header-title">今日任务</text>
- <button class="btn-batch" @click="enterBatchMode" v-if="tasks.length > 0">批量操作</button>
- </view>
- <!-- 任务列表 -->
- <view class="task-list" v-if="tasks.length > 0">
- <view
- v-for="task in tasks"
- :key="task.id"
- class="task-item"
- :class="{ completed: task.status === 'completed', selected: selectedTasks.includes(task.id) }"
- >
- <view class="task-checkbox" v-if="batchMode" @click="toggleTaskSelect(task.id)">
- <checkbox :checked="selectedTasks.includes(task.id)" />
- </view>
- <view class="task-left">
- <view class="task-title">
- {{ task.title }}
- <text v-if="task.category === '小游戏类'" class="minigame-badge">🎮</text>
- </view>
- <view class="task-meta">
- <text class="points">+{{ task.points }}分</text>
- <text class="deadline">截止 {{ formatTime(task.deadline) }}</text>
- </view>
- </view>
- <view class="task-right" v-if="!batchMode">
- <button
- v-if="task.status === 'pending'"
- class="btn-complete"
- @click="isParent ? completeParentTask(task.id) : completeTask(task.id)"
- >
- 完成
- </button>
- <view v-else class="completed-badge">已完成</view>
- </view>
- </view>
- </view>
- <view class="empty" v-else>
- <text>暂无今日任务</text>
- </view>
- <!-- 家长模式:添加任务按钮 -->
- <button
- v-if="isParent && !batchMode"
- class="btn-primary add-btn"
- @click="showAddModal = true"
- >
- + 添加任务
- </button>
- <!-- 添加任务弹窗 -->
- <view class="modal-mask" v-if="showAddModal" @click="showAddModal = false">
- <view class="modal modal-large" @click.stop>
- <view class="modal-title">创建任务</view>
-
- <!-- 任务模板选择 -->
- <view class="form-item">
- <view class="form-label">从模板选择(可选)</view>
- <picker mode="selector" :range="taskTemplates" range-key="title" @change="onTemplateChange">
- <view class="picker">{{ selectedTemplate ? selectedTemplate.title : '选择任务模板' }}</view>
- </picker>
- </view>
-
- <!-- 必填:标题 -->
- <view class="form-item">
- <view class="form-label">任务标题 <text class="required">*</text></view>
- <input v-model="newTask.title" placeholder="请输入任务标题" />
- </view>
-
- <!-- 内容 -->
- <view class="form-item">
- <view class="form-label">任务内容</view>
- <textarea v-model="newTask.description" placeholder="请输入任务描述" class="textarea" />
- </view>
-
- <!-- 类型 -->
- <view class="form-item">
- <view class="form-label">类型 <text class="required">*</text></view>
- <picker mode="selector" :range="categories" @change="onCategoryChange">
- <view class="picker">{{ newTask.category || '请选择类型' }}</view>
- </picker>
- </view>
-
- <!-- 完成方式 -->
- <view class="form-item">
- <view class="form-label">完成方式 <text class="required">*</text></view>
- <checkbox-group @change="onCompleteTypeChange">
- <label v-for="type in completeTypes" :key="type.value">
- <checkbox :value="type.value" :checked="newTask.completeTypes.includes(type.value)" />
- <text>{{ type.label }}</text>
- </label>
- </checkbox-group>
- </view>
-
- <!-- 积分和时长 -->
- <view class="form-row">
- <view class="form-item half">
- <view class="form-label">积分 <text class="required">*</text></view>
- <input v-model="newTask.points" type="number" placeholder="1-10" />
- </view>
- <view class="form-item half">
- <view class="form-label">时长(分钟)</view>
- <input v-model="newTask.duration" type="number" placeholder="时长" />
- </view>
- </view>
-
- <!-- 完成孩子(多选) -->
- <view class="form-item">
- <view class="form-label">完成孩子 <text class="required">*</text></view>
- <checkbox-group @change="onChildSelectChange">
- <label v-for="child in children" :key="child.id">
- <checkbox :value="child.id" :checked="newTask.childIds.includes(child.id)" />
- <text>{{ child.nickname }}</text>
- </label>
- </checkbox-group>
- </view>
-
- <!-- 任务性质 -->
- <view class="form-item">
- <view class="form-label">任务性质 <text class="required">*</text></view>
- <radio-group @change="onTaskTypeChange">
- <label><radio value="onetime" :checked="newTask.taskType === 'onetime'" />一次性</label>
- <label><radio value="recurring" :checked="newTask.taskType === 'recurring'" />循环任务</label>
- </radio-group>
- </view>
-
- <!-- 循环任务:频率和次数 -->
- <view class="form-item" v-if="newTask.taskType === 'recurring'">
- <view class="form-label">频率</view>
- <picker mode="selector" :range="frequencies" @change="onFrequencyChange">
- <view class="picker">{{ newTask.frequency || '请选择频率' }}</view>
- </picker>
- </view>
- <view class="form-item" v-if="newTask.taskType === 'recurring'">
- <view class="form-label">周期内最多完成次数</view>
- <input v-model="newTask.maxFrequency" type="number" placeholder="超过次数不记积分" />
- </view>
-
- <!-- 审核设置 -->
- <view class="form-item">
- <view class="form-label">需要审核</view>
- <switch :checked="newTask.needReview === 1" @change="onNeedReviewChange" />
- </view>
- <view class="form-item" v-if="newTask.needReview === 1">
- <view class="form-label">审核人</view>
- <picker mode="selector" :range="reviewTypes" range-key="label" @change="onReviewTypeChange">
- <view class="picker">{{ getReviewTypeLabel(newTask.reviewType) }}</view>
- </picker>
- </view>
-
- <!-- 非必填:时间 -->
- <view class="form-item">
- <view class="form-label">最早开始时间</view>
- <picker mode="datetime" :value="newTask.earliestStart" @change="onEarliestStartChange">
- <view class="picker">{{ newTask.earliestStart || '选择时间(不填表示不限时)' }}</view>
- </picker>
- </view>
- <view class="form-item">
- <view class="form-label">最晚结束时间</view>
- <picker mode="datetime" :value="newTask.latestEnd" @change="onLatestEndChange">
- <view class="picker">{{ newTask.latestEnd || '选择时间(不填表示不限时)' }}</view>
- </picker>
- </view>
-
- <view class="modal-btns">
- <button @click="showAddModal = false">取消</button>
- <button class="btn-primary" @click="createTask">创建任务</button>
- </view>
- </view>
- </view>
- </view>
- </PlayfulCard>
- </template>
- <script>
- import { getTodayTasks, completeTask as completeTaskApi, createTask as createTaskApi, getChildren, getTodayParentTasks, completeParentTask as completeParentTaskApi, batchCompleteTasks, batchDeleteTasks } from '../../utils/api.js'
- import PlayfulCard from '@/components/PlayfulCard.vue'
- export default {
- components: { PlayfulCard },
- data() {
- return {
- tasks: [],
- isParent: false,
- currentRole: 'parent', // 当前角色
- currentChildId: '',
- children: [], // 孩子列表
- taskTemplates: [], // 任务模板
- selectedTemplate: null,
- showAddModal: false,
- batchMode: false,
- selectedTasks: [],
- newTask: {
- title: '',
- description: '',
- category: '',
- points: 2,
- duration: '',
- childIds: [],
- taskType: 'onetime',
- frequency: '',
- maxFrequency: '',
- completeTypes: ['checkin'],
- needReview: 0,
- reviewType: 'parent',
- earliestStart: '',
- latestEnd: ''
- },
- categories: ['学习类', '生活类', '运动类', '小游戏类', '其他'],
- completeTypes: [
- { value: 'checkin', label: '打卡' },
- { value: 'text', label: '文本' },
- { value: 'audio', label: '语音' },
- { value: 'image', label: '图片' },
- { value: 'video', label: '视频' }
- ],
- frequencies: ['每天', '每周', '每月'],
- reviewTypes: [
- { value: 'creator', label: '创建人' },
- { value: 'parent', label: '家长' },
- { value: 'teacher', label: '成长规划师' },
- { value: 'ai', label: 'AI自动' }
- ]
- }
- },
- onShow() {
- // 规划师角色跳转到家庭管理页
- const role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
- if (role === 'teacher') {
- uni.redirectTo({ url: '/pages/teacher/families' })
- return
- }
- // 加载任务数据
- this.loadParentTasks()
- },
- methods: {
- async loadParentTasks() {
- try {
- const currentUserId = uni.getStorageSync('userId')
- const res = await getTodayParentTasks()
- this.tasks = (res.data || []).slice(0, 5)
- } catch (e) {
- console.error('加载任务失败', e)
- }
- },
- async loadChildren() {
- try {
- const res = await getChildren()
- this.children = res.data || []
- // 默认选中第一个孩子
- if (this.children.length > 0) {
- this.newTask.childIds = [this.children[0].id]
- }
- } catch (e) {
- console.error('加载孩子失败', e)
- }
- },
- // 加载家长任务(分配给家长的任务)
- async loadParentTasks() {
- try {
- const res = await getTodayParentTasks()
- this.tasks = res.data || []
- } catch (e) {
- console.error('加载家长任务失败', e)
- }
- },
- async loadTasks() {
- try {
- const childrenRes = await getChildren()
- const children = childrenRes.data
- if (children && children.length > 0) {
- this.currentChildId = children[0].id
- const res = await getTodayTasks(this.currentChildId)
- this.tasks = res.data || []
- }
- } catch (e) {
- console.error('加载任务失败', e)
- }
- },
- // 完成任务(孩子模式)
- async completeTask(taskId) {
- try {
- const res = await completeTaskApi(taskId, this.currentChildId, '')
- uni.showToast({
- title: `获得 ${res.data.pointsEarned} 积分`,
- icon: 'success'
- })
- this.loadTasks()
- } catch (e) {
- console.error('完成任务失败', e)
- }
- },
- // 完成任务(家长模式)
- async completeParentTask(taskId) {
- try {
- const res = await completeParentTaskApi(taskId)
- uni.showToast({
- title: `获得 ${res.data.pointsEarned} 积分`,
- icon: 'success'
- })
- this.loadParentTasks()
- } catch (e) {
- console.error('完成任务失败', e)
- }
- },
- // 模板选择
- onTemplateChange(e) {
- if (e.detail.value >= 0) {
- this.selectedTemplate = this.taskTemplates[e.detail.value]
- this.applyTemplate(this.selectedTemplate)
- }
- },
- applyTemplate(template) {
- this.newTask.title = template.title || ''
- this.newTask.description = template.description || ''
- this.newTask.category = template.category || ''
- this.newTask.points = template.points || 2
- this.newTask.duration = template.duration || ''
- this.newTask.completeTypes = template.completeTypes ? template.completeTypes.split(',') : ['checkin']
- this.newTask.needReview = template.needReview || 0
- this.newTask.reviewType = template.reviewType || 'parent'
- },
- // 类型选择
- onCategoryChange(e) {
- this.newTask.category = this.categories[e.detail.value]
- },
- // 完成方式选择
- onCompleteTypeChange(e) {
- this.newTask.completeTypes = e.detail.value
- },
- // 孩子多选
- onChildSelectChange(e) {
- this.newTask.childIds = e.detail.value.map(id => parseInt(id))
- },
- // 任务性质
- onTaskTypeChange(e) {
- this.newTask.taskType = e.detail.value
- },
- // 频率选择
- onFrequencyChange(e) {
- const freq = this.frequencies[e.detail.value]
- this.newTask.frequency = freq === '每天' ? 'daily' : (freq === '每周' ? 'weekly' : 'monthly')
- },
- // 是否需要审核
- onNeedReviewChange(e) {
- this.newTask.needReview = e.detail.value ? 1 : 0
- },
- // 审核人选择
- onReviewTypeChange(e) {
- this.newTask.reviewType = this.reviewTypes[e.detail.value].value
- },
- getReviewTypeLabel(value) {
- const found = this.reviewTypes.find(r => r.value === value)
- return found ? found.label : '请选择审核人'
- },
- // 时间选择
- onEarliestStartChange(e) {
- this.newTask.earliestStart = e.detail.value
- },
- onLatestEndChange(e) {
- this.newTask.latestEnd = e.detail.value
- },
- async createTask() {
- // 验证必填
- if (!this.newTask.title) {
- uni.showToast({ title: '请输入任务标题', icon: 'none' })
- return
- }
- if (!this.newTask.category) {
- uni.showToast({ title: '请选择类型', icon: 'none' })
- return
- }
- if (!this.newTask.completeTypes || this.newTask.completeTypes.length === 0) {
- uni.showToast({ title: '请选择完成方式', icon: 'none' })
- return
- }
- if (!this.newTask.childIds || this.newTask.childIds.length === 0) {
- uni.showToast({ title: '请选择完成孩子', icon: 'none' })
- return
- }
- try {
- await createTaskApi({
- title: this.newTask.title,
- description: this.newTask.description,
- category: this.newTask.category,
- points: parseInt(this.newTask.points) || 2,
- duration: parseInt(this.newTask.duration) || null,
- childIds: this.newTask.childIds,
- taskType: this.newTask.taskType,
- frequency: this.newTask.frequency,
- maxFrequency: parseInt(this.newTask.maxFrequency) || null,
- completeTypes: this.newTask.completeTypes,
- needReview: this.newTask.needReview,
- reviewType: this.newTask.reviewType,
- earliestStart: this.newTask.earliestStart || null,
- latestEnd: this.newTask.latestEnd || null
- })
- uni.showToast({ title: '创建成功', icon: 'success' })
- this.showAddModal = false
- this.resetNewTask()
- this.loadTasks()
- } catch (e) {
- console.error('创建任务失败', e)
- uni.showToast({ title: '创建失败', icon: 'none' })
- }
- },
- resetNewTask() {
- this.newTask = {
- title: '',
- description: '',
- category: '',
- points: 2,
- duration: '',
- childIds: this.children.length > 0 ? [this.children[0].id] : [],
- taskType: 'onetime',
- frequency: '',
- maxFrequency: '',
- completeTypes: ['checkin'],
- needReview: 0,
- reviewType: 'parent',
- earliestStart: '',
- latestEnd: ''
- }
- this.selectedTemplate = null
- },
- formatTime(dateStr) {
- if (!dateStr) return ''
- const date = new Date(dateStr)
- return `${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`
- },
- enterBatchMode() {
- this.batchMode = true
- this.selectedTasks = []
- },
- exitBatchMode() {
- this.batchMode = false
- this.selectedTasks = []
- },
- toggleTaskSelect(taskId) {
- const index = this.selectedTasks.indexOf(taskId)
- if (index > -1) {
- this.selectedTasks.splice(index, 1)
- } else {
- this.selectedTasks.push(taskId)
- }
- },
- toggleSelectAll() {
- if (this.isAllSelected) {
- this.selectedTasks = []
- } else {
- this.selectedTasks = this.tasks.filter(t => t.status === 'pending').map(t => t.id)
- }
- },
- async batchComplete() {
- if (this.selectedTasks.length === 0) {
- uni.showToast({ title: '请选择任务', icon: 'none' })
- return
- }
- try {
- await batchCompleteTasks(this.selectedTasks)
- uni.showToast({ title: '批量完成成功', icon: 'success' })
- this.exitBatchMode()
- this.loadParentTasks()
- } catch (e) {
- console.error('批量完成失败', e)
- uni.showToast({ title: '批量完成失败', icon: 'none' })
- }
- },
- async batchDelete() {
- if (this.selectedTasks.length === 0) {
- uni.showToast({ title: '请选择任务', icon: 'none' })
- return
- }
- uni.showModal({
- title: '确认删除',
- content: `确定要删除选中的 ${this.selectedTasks.length} 个任务吗?`,
- success: async (res) => {
- if (res.confirm) {
- try {
- await batchDeleteTasks(this.selectedTasks)
- uni.showToast({ title: '批量删除成功', icon: 'success' })
- this.exitBatchMode()
- this.loadParentTasks()
- } catch (e) {
- console.error('批量删除失败', e)
- uni.showToast({ title: '批量删除失败', icon: 'none' })
- }
- }
- }
- })
- }
- },
- computed: {
- isAllSelected() {
- if (this.selectedTasks.length === 0) return false
- const pendingTasks = this.tasks.filter(t => t.status === 'pending')
- return pendingTasks.length > 0 && pendingTasks.every(t => this.selectedTasks.includes(t.id))
- }
- }
- }
- </script>
- <style scoped>
- .container {
- padding: 30rpx;
- padding-bottom: 120rpx;
- }
- .header-bar {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 30rpx;
- }
- .header-title {
- font-size: 32rpx;
- font-weight: bold;
- color: #333;
- }
- .btn-batch {
- background: #F97316;
- color: #fff;
- font-size: 24rpx;
- padding: 10rpx 30rpx;
- border-radius: 30rpx;
- }
- .batch-bar {
- display: flex;
- align-items: center;
- background: #f5f5f5;
- padding: 20rpx 30rpx;
- border-radius: 16rpx;
- margin-bottom: 30rpx;
- }
- .batch-label {
- font-size: 28rpx;
- color: #333;
- margin-left: 10rpx;
- margin-right: auto;
- }
- .batch-actions {
- display: flex;
- gap: 20rpx;
- }
- .batch-btn {
- font-size: 24rpx;
- padding: 10rpx 30rpx;
- border-radius: 30rpx;
- background: #F97316;
- color: #fff;
- }
- .batch-btn.complete {
- background: #52C41A;
- }
- .batch-btn.delete {
- background: #FF4D4F;
- }
- .batch-btn[disabled] {
- background: #ccc;
- }
- .batch-exit {
- font-size: 24rpx;
- color: #999;
- margin-left: 20rpx;
- }
- .task-item {
- background: #fff;
- border-radius: 16rpx;
- padding: 30rpx;
- margin-bottom: 20rpx;
- display: flex;
- justify-content: space-between;
- align-items: center;
- }
- .task-item.selected {
- background: #FFF1F0;
- border: 2rpx solid #F97316;
- }
- .task-checkbox {
- margin-right: 20rpx;
- }
- .task-title {
- background: #fff;
- border-radius: 16rpx;
- padding: 30rpx;
- margin-bottom: 20rpx;
- display: flex;
- justify-content: space-between;
- align-items: center;
- }
- .task-item.completed {
- opacity: 0.6;
- }
- .task-title {
- font-size: 30rpx;
- color: #333;
- margin-bottom: 10rpx;
- }
- .task-meta {
- display: flex;
- gap: 20rpx;
- }
- .points {
- color: #F97316;
- font-size: 24rpx;
- }
- .deadline {
- color: #999;
- font-size: 24rpx;
- }
- .btn-complete {
- background: #F97316;
- color: #fff;
- font-size: 26rpx;
- padding: 10rpx 30rpx;
- border-radius: 30rpx;
- }
- .completed-badge {
- color: #52C41A;
- font-size: 26rpx;
- }
- .minigame-badge {
- font-size: 28rpx;
- margin-left: 10rpx;
- }
- .add-btn {
- position: fixed;
- bottom: 30rpx;
- left: 30rpx;
- right: 30rpx;
- }
- .empty {
- text-align: center;
- color: #999;
- padding: 100rpx;
- }
- .modal-mask {
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background: rgba(0, 0, 0, 0.5);
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .modal {
- background: #fff;
- border-radius: 20rpx;
- padding: 40rpx;
- width: 80%;
- max-height: 80vh;
- overflow-y: auto;
- }
- .modal-large {
- width: 90%;
- }
- .modal-title {
- font-size: 32rpx;
- font-weight: bold;
- margin-bottom: 30rpx;
- text-align: center;
- }
- .form-item {
- margin-bottom: 20rpx;
- }
- .form-label {
- font-size: 28rpx;
- color: #333;
- margin-bottom: 10rpx;
- }
- .form-label .required {
- color: #F97316;
- }
- .form-row {
- display: flex;
- gap: 20rpx;
- }
- .form-item.half {
- flex: 1;
- }
- .form-item input, .picker, .textarea {
- border: 1rpx solid #ddd;
- border-radius: 10rpx;
- padding: 20rpx;
- font-size: 28rpx;
- width: 100%;
- box-sizing: border-box;
- }
- .textarea {
- min-height: 100rpx;
- }
- .form-item checkbox-group {
- display: flex;
- flex-wrap: wrap;
- gap: 20rpx;
- }
- .form-item label {
- display: flex;
- align-items: center;
- gap: 10rpx;
- font-size: 28rpx;
- }
- .modal-btns {
- display: flex;
- gap: 20rpx;
- margin-top: 30rpx;
- }
- .modal-btns button {
- flex: 1;
- }
- </style>
|