| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <template>
- <view class="container">
- <view class="form-container">
- <view class="form-item">
- <text class="label">任务名称</text>
- <input class="input" v-model="form.title" placeholder="请输入任务名称" />
- </view>
- <view class="form-item">
- <text class="label">任务描述</text>
- <textarea class="textarea" v-model="form.description" placeholder="请输入任务描述" />
- </view>
- <view class="form-item">
- <text class="label">选择孩子(可多选)</text>
- <checkbox-group @change="onChildrenChange">
- <label class="child-checkbox" v-for="child in children" :key="child.id">
- <checkbox :value="String(child.id)" :checked="selectedChildIds.includes(String(child.id))" />
- <text class="child-name">{{ child.nickname }}</text>
- </label>
- </checkbox-group>
- </view>
- <view class="form-item">
- <text class="label">积分</text>
- <input class="input" type="number" v-model="form.points" placeholder="请输入积分 (1-10)" />
- </view>
- <view class="form-item">
- <text class="label">分类</text>
- <picker :range="categories" @change="onCategoryChange">
- <view class="picker">
- {{ form.category || '请选择分类' }}
- </view>
- </picker>
- </view>
- <!-- 小游戏选择(当选择"小游戏类"时显示) -->
- <view class="form-item" v-if="form.category === '小游戏类'">
- <text class="label">选择小游戏</text>
- <picker :range="miniGames" range-key="gameName" @change="onMinigameChange">
- <view class="picker">
- {{ selectedMinigame ? selectedMinigame.gameName : '请选择小游戏' }}
- </view>
- </picker>
- </view>
- <!-- TASK-003: 截止时间设置 -->
- <view class="form-item">
- <text class="label">截止时间</text>
- <picker mode="multiSelector" :range="dateTimeRange" :value="dateTimeIndex" @change="onDateTimeChange" @columnchange="onColumnChange">
- <view class="picker">
- {{ deadlineText || '请选择截止时间' }}
- </view>
- </picker>
- </view>
- <!-- TASK-004: 重复设置 -->
- <view class="form-item">
- <text class="label">重复设置</text>
- <picker :range="repeatTypes" range-key="label" @change="onRepeatChange">
- <view class="picker">
- {{ repeatTypeLabel || '不重复' }}
- </view>
- </picker>
- </view>
- <view class="form-item">
- <text class="label">需要审核</text>
- <switch :checked="form.needReview" @change="onNeedReviewChange" />
- </view>
- <view class="form-item">
- <text class="label">预计时长(分钟)</text>
- <input class="input" type="number" v-model="form.duration" placeholder="请输入预计完成时长" />
- </view>
- <button class="btn-primary" @click="submit">创建任务</button>
- </view>
- </view>
- </template>
- <script>
- import config from '@/config.js'
- import { getChildren, createTask } from '../../utils/api.js'
- export default {
- data() {
- return {
- form: {
- title: '',
- description: '',
- childIds: [],
- points: 2,
- category: '',
- minigameCode: '',
- deadline: null,
- repeatType: 'none',
- needReview: false,
- duration: null
- },
- children: [],
- selectedChildIds: [],
- categories: ['学习类', '生活类', '运动类', '小游戏类', '其他'],
- miniGames: [],
- selectedMinigame: null,
- repeatTypes: [
- { value: 'none', label: '不重复' },
- { value: 'daily', label: '每天' },
- { value: 'weekly', label: '每周' }
- ],
- dateTimeRange: [[], [], [], []],
- dateTimeIndex: [0, 0, 0, 0],
- deadlineText: ''
- }
- },
- onLoad(options) {
- this.loadChildren(options.childId)
- this.loadMiniGames()
- this.initDateTimePicker()
- },
- methods: {
- async loadChildren(preselectedChildId) {
- try {
- const res = await getChildren()
- this.children = res.data || []
- // 如果有 preselect childId,自动选中该孩子
- if (preselectedChildId && this.children.length > 0) {
- const matched = this.children.find(c => String(c.id) === String(preselectedChildId))
- if (matched) {
- const idNum = Number(matched.id)
- if (!this.form.childIds.includes(idNum)) {
- this.form.childIds.push(idNum)
- this.selectedChildIds.push(String(idNum))
- }
- }
- }
- } catch (e) {
- console.error(e)
- }
- },
- async loadMiniGames() {
- try {
- const res = await uni.request({
- url: config.api('/api/tasks/minigame-options'),
- method: 'POST',
- header: {
- 'Authorization': `Bearer ${uni.getStorageSync('token')}`
- }
- })
- if (res.data && res.data.code === 200) {
- this.miniGames = res.data.data || []
- }
- } catch (e) {
- console.error(e)
- }
- },
- initDateTimePicker() {
- // 初始化日期时间选择器
- const now = new Date()
- const year = now.getFullYear()
- const month = now.getMonth() + 1
- const day = now.getDate()
- const hour = now.getHours()
- const minute = now.getMinutes()
- // 年(当前年份+未来2年)
- const years = []
- for (let i = year; i <= year + 2; i++) {
- years.push(i + '年')
- }
- // 月
- const months = []
- for (let i = 1; i <= 12; i++) {
- months.push(i + '月')
- }
- // 日
- const days = []
- const daysInMonth = new Date(year, month, 0).getDate()
- for (let i = 1; i <= daysInMonth; i++) {
- days.push(i + '日')
- }
- // 时
- const hours = []
- for (let i = 0; i < 24; i++) {
- hours.push(i.toString().padStart(2, '0') + '时')
- }
- // 分
- const minutes = []
- for (let i = 0; i < 60; i += 5) {
- minutes.push(i.toString().padStart(2, '0') + '分')
- }
- this.dateTimeRange = [years, months, days, hours, minutes]
- },
- onChildrenChange(e) {
- this.selectedChildIds = e.detail.value
- this.form.childIds = e.detail.value.map(Number)
- },
- onCategoryChange(e) {
- this.form.category = this.categories[e.detail.value]
- // 清空小游戏选择
- if (this.form.category !== '小游戏类') {
- this.form.minigameCode = ''
- this.selectedMinigame = null
- }
- },
- onMinigameChange(e) {
- this.selectedMinigame = this.miniGames[e.detail.value]
- this.form.minigameCode = this.selectedMinigame.gameCode
- // 自动填充任务名称
- if (!this.form.title) {
- this.form.title = this.selectedMinigame.gameName
- }
- // 自动填充积分
- if (this.selectedMinigame.defaultPoints) {
- this.form.points = this.selectedMinigame.defaultPoints
- }
- },
- onDateTimeChange(e) {
- const vals = e.detail.value
- this.dateTimeIndex = vals
- const now = new Date()
- const year = now.getFullYear() + vals[0]
- const month = vals[1] + 1
- const day = vals[2] + 1
- const hour = vals[3]
- const minute = vals[4] * 5
- this.deadlineText = `${year}年${month}月${day}日 ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`
- this.form.deadline = new Date(year, month - 1, day, hour, minute)
- },
- onColumnChange(e) {
- // 处理月份变化时更新天数
- if (e.detail.column === 1) {
- const month = e.detail.value + 1
- const now = new Date()
- const year = now.getFullYear() + this.dateTimeIndex[0]
- const daysInMonth = new Date(year, month, 0).getDate()
- const days = []
- for (let i = 1; i <= daysInMonth; i++) {
- days.push(i + '日')
- }
- this.$set(this.dateTimeRange, 2, days)
- }
- },
- onRepeatChange(e) {
- const selected = this.repeatTypes[e.detail.value]
- this.form.repeatType = selected.value
- },
- onNeedReviewChange(e) {
- this.form.needReview = e.detail.value
- },
- async submit() {
- if (!this.form.title) {
- uni.showToast({ title: '请输入任务名称', icon: 'none' })
- return
- }
- if (this.form.childIds.length === 0) {
- uni.showToast({ title: '请选择至少一个孩子', icon: 'none' })
- return
- }
- if (!this.form.deadline) {
- uni.showToast({ title: '请选择截止时间', icon: 'none' })
- return
- }
- // 小游戏类必须选择具体游戏
- if (this.form.category === '小游戏类' && !this.form.minigameCode) {
- uni.showToast({ title: '请选择具体小游戏', icon: 'none' })
- return
- }
- uni.showLoading({ title: '创建任务中...' })
- const taskData = {
- title: this.form.title,
- description: this.form.description,
- points: parseInt(this.form.points) || 2,
- category: this.form.category || '其他',
- minigameCode: this.form.minigameCode,
- deadline: this.form.deadline.toISOString(),
- repeatType: this.form.repeatType,
- needReview: this.form.needReview ? 1 : 0,
- duration: parseInt(this.form.duration) || 0
- }
- let successCount = 0
- let failCount = 0
- for (const childId of this.form.childIds) {
- try {
- await createTask({ ...taskData, childId })
- successCount++
- } catch (e) {
- failCount++
- }
- }
- uni.hideLoading()
- if (failCount === 0) {
- uni.showToast({ title: `已为${successCount}个孩子创建任务`, icon: 'success' })
- setTimeout(() => uni.navigateBack(), 1500)
- } else if (successCount > 0) {
- uni.showToast({ title: `${successCount}个成功,${failCount}个失败`, icon: 'none' })
- } else {
- uni.showToast({ title: '创建失败', icon: 'none' })
- }
- }
- }
- }
- </script>
- <style scoped>
- .container { padding: 30rpx; }
- .form-container { background: #fff; border-radius: 20rpx; padding: 30rpx; }
- .form-item { margin-bottom: 30rpx; }
- .label { display: block; font-size: 28rpx; color: #333; margin-bottom: 10rpx; }
- .input, .textarea { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; width: 100%; box-sizing: border-box; }
- .input { height: 80rpx; }
- .textarea { height: 150rpx; }
- .picker { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #666; }
- .child-checkbox { display: flex; align-items: center; padding: 16rpx 0; border-bottom: 1rpx solid #f0f0f0; }
- .child-checkbox:last-child { border-bottom: none; }
- .child-name { font-size: 28rpx; color: #333; margin-left: 10rpx; }
- .btn-primary { background: #F97316; color: #fff; border-radius: 20rpx; margin-top: 40rpx; }
- </style>
|