tasks.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. <template>
  2. <PlayfulCard elevation="1">
  3. <view class="container">
  4. <!-- 任务列表 -->
  5. <view class="task-list" v-if="tasks.length > 0">
  6. <view
  7. v-for="task in tasks"
  8. :key="task.id"
  9. class="task-item"
  10. :class="{ completed: task.status === 'completed' }"
  11. >
  12. <view class="task-left">
  13. <view class="task-title">{{ task.title }}</view>
  14. <view class="task-meta">
  15. <text class="points">+{{ task.points }}分</text>
  16. <text class="deadline">截止 {{ formatTime(task.deadline) }}</text>
  17. </view>
  18. </view>
  19. <view class="task-right">
  20. <button
  21. v-if="task.status === 'pending'"
  22. class="btn-complete"
  23. @click="completeTask(task.id)"
  24. >
  25. 完成
  26. </button>
  27. <view v-else class="completed-badge">已完成</view>
  28. </view>
  29. </view>
  30. </view>
  31. <view class="empty" v-else>
  32. <text>暂无今日任务</text>
  33. </view>
  34. <!-- 家长模式:添加任务按钮 -->
  35. <button
  36. v-if="isParent"
  37. class="btn-primary add-btn"
  38. @click="showAddModal = true"
  39. >
  40. + 添加任务
  41. </button>
  42. <!-- 添加任务弹窗 -->
  43. <view class="modal-mask" v-if="showAddModal" @click="showAddModal = false">
  44. <view class="modal modal-large" @click.stop>
  45. <view class="modal-title">创建任务</view>
  46. <!-- 任务模板选择 -->
  47. <view class="form-item">
  48. <view class="form-label">从模板选择(可选)</view>
  49. <picker mode="selector" :range="taskTemplates" range-key="title" @change="onTemplateChange">
  50. <view class="picker">{{ selectedTemplate ? selectedTemplate.title : '选择任务模板' }}</view>
  51. </picker>
  52. </view>
  53. <!-- 必填:标题 -->
  54. <view class="form-item">
  55. <view class="form-label">任务标题 <text class="required">*</text></view>
  56. <input v-model="newTask.title" placeholder="请输入任务标题" />
  57. </view>
  58. <!-- 内容 -->
  59. <view class="form-item">
  60. <view class="form-label">任务内容</view>
  61. <textarea v-model="newTask.description" placeholder="请输入任务描述" class="textarea" />
  62. </view>
  63. <!-- 类型 -->
  64. <view class="form-item">
  65. <view class="form-label">类型 <text class="required">*</text></view>
  66. <picker mode="selector" :range="categories" @change="onCategoryChange">
  67. <view class="picker">{{ newTask.category || '请选择类型' }}</view>
  68. </picker>
  69. </view>
  70. <!-- 完成方式 -->
  71. <view class="form-item">
  72. <view class="form-label">完成方式 <text class="required">*</text></view>
  73. <checkbox-group @change="onCompleteTypeChange">
  74. <label v-for="type in completeTypes" :key="type.value">
  75. <checkbox :value="type.value" :checked="newTask.completeTypes.includes(type.value)" />
  76. <text>{{ type.label }}</text>
  77. </label>
  78. </checkbox-group>
  79. </view>
  80. <!-- 积分和时长 -->
  81. <view class="form-row">
  82. <view class="form-item half">
  83. <view class="form-label">积分 <text class="required">*</text></view>
  84. <input v-model="newTask.points" type="number" placeholder="1-10" />
  85. </view>
  86. <view class="form-item half">
  87. <view class="form-label">时长(分钟)</view>
  88. <input v-model="newTask.duration" type="number" placeholder="时长" />
  89. </view>
  90. </view>
  91. <!-- 完成孩子(多选) -->
  92. <view class="form-item">
  93. <view class="form-label">完成孩子 <text class="required">*</text></view>
  94. <checkbox-group @change="onChildSelectChange">
  95. <label v-for="child in children" :key="child.id">
  96. <checkbox :value="child.id" :checked="newTask.childIds.includes(child.id)" />
  97. <text>{{ child.nickname }}</text>
  98. </label>
  99. </checkbox-group>
  100. </view>
  101. <!-- 任务性质 -->
  102. <view class="form-item">
  103. <view class="form-label">任务性质 <text class="required">*</text></view>
  104. <radio-group @change="onTaskTypeChange">
  105. <label><radio value="onetime" :checked="newTask.taskType === 'onetime'" />一次性</label>
  106. <label><radio value="recurring" :checked="newTask.taskType === 'recurring'" />循环任务</label>
  107. </radio-group>
  108. </view>
  109. <!-- 循环任务:频率和次数 -->
  110. <view class="form-item" v-if="newTask.taskType === 'recurring'">
  111. <view class="form-label">频率</view>
  112. <picker mode="selector" :range="frequencies" @change="onFrequencyChange">
  113. <view class="picker">{{ newTask.frequency || '请选择频率' }}</view>
  114. </picker>
  115. </view>
  116. <view class="form-item" v-if="newTask.taskType === 'recurring'">
  117. <view class="form-label">周期内最多完成次数</view>
  118. <input v-model="newTask.maxFrequency" type="number" placeholder="超过次数不记积分" />
  119. </view>
  120. <!-- 审核设置 -->
  121. <view class="form-item">
  122. <view class="form-label">需要审核</view>
  123. <switch :checked="newTask.needReview === 1" @change="onNeedReviewChange" />
  124. </view>
  125. <view class="form-item" v-if="newTask.needReview === 1">
  126. <view class="form-label">审核人</view>
  127. <picker mode="selector" :range="reviewTypes" range-key="label" @change="onReviewTypeChange">
  128. <view class="picker">{{ getReviewTypeLabel(newTask.reviewType) }}</view>
  129. </picker>
  130. </view>
  131. <!-- 非必填:时间 -->
  132. <view class="form-item">
  133. <view class="form-label">最早开始时间</view>
  134. <picker mode="datetime" :value="newTask.earliestStart" @change="onEarliestStartChange">
  135. <view class="picker">{{ newTask.earliestStart || '选择时间(不填表示不限时)' }}</view>
  136. </picker>
  137. </view>
  138. <view class="form-item">
  139. <view class="form-label">最晚结束时间</view>
  140. <picker mode="datetime" :value="newTask.latestEnd" @change="onLatestEndChange">
  141. <view class="picker">{{ newTask.latestEnd || '选择时间(不填表示不限时)' }}</view>
  142. </picker>
  143. </view>
  144. <view class="modal-btns">
  145. <button @click="showAddModal = false">取消</button>
  146. <button class="btn-primary" @click="createTask">创建任务</button>
  147. </view>
  148. </view>
  149. </view>
  150. </view>
  151. </PlayfulCard>
  152. </template>
  153. <script>
  154. import { getTodayTasks, completeTask, createTask, getChildren } from '../../utils/api.js'
  155. import PlayfulCard from '@/components/PlayfulCard.vue'
  156. export default {
  157. components: { PlayfulCard },
  158. data() {
  159. return {
  160. tasks: [],
  161. isParent: false,
  162. currentChildId: '',
  163. children: [], // 孩子列表
  164. taskTemplates: [], // 任务模板
  165. selectedTemplate: null,
  166. showAddModal: false,
  167. newTask: {
  168. title: '',
  169. description: '',
  170. category: '',
  171. points: 2,
  172. duration: '',
  173. childIds: [],
  174. taskType: 'onetime',
  175. frequency: '',
  176. maxFrequency: '',
  177. completeTypes: ['checkin'],
  178. needReview: 0,
  179. reviewType: 'parent',
  180. earliestStart: '',
  181. latestEnd: ''
  182. },
  183. categories: ['学习类', '生活类', '运动类', '其他'],
  184. completeTypes: [
  185. { value: 'checkin', label: '打卡' },
  186. { value: 'text', label: '文本' },
  187. { value: 'audio', label: '语音' },
  188. { value: 'image', label: '图片' },
  189. { value: 'video', label: '视频' }
  190. ],
  191. frequencies: ['每天', '每周', '每月'],
  192. reviewTypes: [
  193. { value: 'creator', label: '创建人' },
  194. { value: 'parent', label: '家长' },
  195. { value: 'teacher', label: '指导师' },
  196. { value: 'ai', label: 'AI自动' }
  197. ]
  198. }
  199. },
  200. onShow() {
  201. this.loadTasks()
  202. this.loadChildren()
  203. this.isParent = this.$store.state.role === 'parent'
  204. },
  205. methods: {
  206. async loadChildren() {
  207. try {
  208. const res = await getChildren()
  209. this.children = res.data || []
  210. // 默认选中第一个孩子
  211. if (this.children.length > 0) {
  212. this.newTask.childIds = [this.children[0].id]
  213. }
  214. } catch (e) {
  215. console.error('加载孩子失败', e)
  216. }
  217. },
  218. async loadTasks() {
  219. try {
  220. const childrenRes = await getChildren()
  221. const children = childrenRes.data
  222. if (children && children.length > 0) {
  223. this.currentChildId = children[0].id
  224. const res = await getTodayTasks(this.currentChildId)
  225. this.tasks = res.data || []
  226. }
  227. } catch (e) {
  228. console.error('加载任务失败', e)
  229. }
  230. },
  231. async completeTask(taskId) {
  232. try {
  233. const res = await completeTask(taskId, this.currentChildId, '')
  234. uni.showToast({
  235. title: `获得 ${res.data.pointsEarned} 积分`,
  236. icon: 'success'
  237. })
  238. this.loadTasks()
  239. } catch (e) {
  240. console.error('完成任务失败', e)
  241. }
  242. },
  243. // 模板选择
  244. onTemplateChange(e) {
  245. if (e.detail.value >= 0) {
  246. this.selectedTemplate = this.taskTemplates[e.detail.value]
  247. this.applyTemplate(this.selectedTemplate)
  248. }
  249. },
  250. applyTemplate(template) {
  251. this.newTask.title = template.title || ''
  252. this.newTask.description = template.description || ''
  253. this.newTask.category = template.category || ''
  254. this.newTask.points = template.points || 2
  255. this.newTask.duration = template.duration || ''
  256. this.newTask.completeTypes = template.completeTypes ? template.completeTypes.split(',') : ['checkin']
  257. this.newTask.needReview = template.needReview || 0
  258. this.newTask.reviewType = template.reviewType || 'parent'
  259. },
  260. // 类型选择
  261. onCategoryChange(e) {
  262. this.newTask.category = this.categories[e.detail.value]
  263. },
  264. // 完成方式选择
  265. onCompleteTypeChange(e) {
  266. this.newTask.completeTypes = e.detail.value
  267. },
  268. // 孩子多选
  269. onChildSelectChange(e) {
  270. this.newTask.childIds = e.detail.value.map(id => parseInt(id))
  271. },
  272. // 任务性质
  273. onTaskTypeChange(e) {
  274. this.newTask.taskType = e.detail.value
  275. },
  276. // 频率选择
  277. onFrequencyChange(e) {
  278. const freq = this.frequencies[e.detail.value]
  279. this.newTask.frequency = freq === '每天' ? 'daily' : (freq === '每周' ? 'weekly' : 'monthly')
  280. },
  281. // 是否需要审核
  282. onNeedReviewChange(e) {
  283. this.newTask.needReview = e.detail.value ? 1 : 0
  284. },
  285. // 审核人选择
  286. onReviewTypeChange(e) {
  287. this.newTask.reviewType = this.reviewTypes[e.detail.value].value
  288. },
  289. getReviewTypeLabel(value) {
  290. const found = this.reviewTypes.find(r => r.value === value)
  291. return found ? found.label : '请选择审核人'
  292. },
  293. // 时间选择
  294. onEarliestStartChange(e) {
  295. this.newTask.earliestStart = e.detail.value
  296. },
  297. onLatestEndChange(e) {
  298. this.newTask.latestEnd = e.detail.value
  299. },
  300. async createTask() {
  301. // 验证必填
  302. if (!this.newTask.title) {
  303. uni.showToast({ title: '请输入任务标题', icon: 'none' })
  304. return
  305. }
  306. if (!this.newTask.category) {
  307. uni.showToast({ title: '请选择类型', icon: 'none' })
  308. return
  309. }
  310. if (!this.newTask.completeTypes || this.newTask.completeTypes.length === 0) {
  311. uni.showToast({ title: '请选择完成方式', icon: 'none' })
  312. return
  313. }
  314. if (!this.newTask.childIds || this.newTask.childIds.length === 0) {
  315. uni.showToast({ title: '请选择完成孩子', icon: 'none' })
  316. return
  317. }
  318. try {
  319. await createTask({
  320. title: this.newTask.title,
  321. description: this.newTask.description,
  322. category: this.newTask.category,
  323. points: parseInt(this.newTask.points) || 2,
  324. duration: parseInt(this.newTask.duration) || null,
  325. childIds: this.newTask.childIds,
  326. taskType: this.newTask.taskType,
  327. frequency: this.newTask.frequency,
  328. maxFrequency: parseInt(this.newTask.maxFrequency) || null,
  329. completeTypes: this.newTask.completeTypes,
  330. needReview: this.newTask.needReview,
  331. reviewType: this.newTask.reviewType,
  332. earliestStart: this.newTask.earliestStart || null,
  333. latestEnd: this.newTask.latestEnd || null
  334. })
  335. uni.showToast({ title: '创建成功', icon: 'success' })
  336. this.showAddModal = false
  337. this.resetNewTask()
  338. this.loadTasks()
  339. } catch (e) {
  340. console.error('创建任务失败', e)
  341. uni.showToast({ title: '创建失败', icon: 'none' })
  342. }
  343. },
  344. resetNewTask() {
  345. this.newTask = {
  346. title: '',
  347. description: '',
  348. category: '',
  349. points: 2,
  350. duration: '',
  351. childIds: this.children.length > 0 ? [this.children[0].id] : [],
  352. taskType: 'onetime',
  353. frequency: '',
  354. maxFrequency: '',
  355. completeTypes: ['checkin'],
  356. needReview: 0,
  357. reviewType: 'parent',
  358. earliestStart: '',
  359. latestEnd: ''
  360. }
  361. this.selectedTemplate = null
  362. },
  363. formatTime(dateStr) {
  364. if (!dateStr) return ''
  365. const date = new Date(dateStr)
  366. return `${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`
  367. }
  368. }
  369. }
  370. </script>
  371. <style scoped>
  372. .container {
  373. padding: 30rpx;
  374. padding-bottom: 120rpx;
  375. }
  376. .task-item {
  377. background: #fff;
  378. border-radius: 16rpx;
  379. padding: 30rpx;
  380. margin-bottom: 20rpx;
  381. display: flex;
  382. justify-content: space-between;
  383. align-items: center;
  384. }
  385. .task-item.completed {
  386. opacity: 0.6;
  387. }
  388. .task-title {
  389. font-size: 30rpx;
  390. color: #333;
  391. margin-bottom: 10rpx;
  392. }
  393. .task-meta {
  394. display: flex;
  395. gap: 20rpx;
  396. }
  397. .points {
  398. color: #FF6B6B;
  399. font-size: 24rpx;
  400. }
  401. .deadline {
  402. color: #999;
  403. font-size: 24rpx;
  404. }
  405. .btn-complete {
  406. background: #FF6B6B;
  407. color: #fff;
  408. font-size: 26rpx;
  409. padding: 10rpx 30rpx;
  410. border-radius: 30rpx;
  411. }
  412. .completed-badge {
  413. color: #52C41A;
  414. font-size: 26rpx;
  415. }
  416. .add-btn {
  417. position: fixed;
  418. bottom: 30rpx;
  419. left: 30rpx;
  420. right: 30rpx;
  421. }
  422. .empty {
  423. text-align: center;
  424. color: #999;
  425. padding: 100rpx;
  426. }
  427. .modal-mask {
  428. position: fixed;
  429. top: 0;
  430. left: 0;
  431. right: 0;
  432. bottom: 0;
  433. background: rgba(0, 0, 0, 0.5);
  434. display: flex;
  435. align-items: center;
  436. justify-content: center;
  437. }
  438. .modal {
  439. background: #fff;
  440. border-radius: 20rpx;
  441. padding: 40rpx;
  442. width: 80%;
  443. max-height: 80vh;
  444. overflow-y: auto;
  445. }
  446. .modal-large {
  447. width: 90%;
  448. }
  449. .modal-title {
  450. font-size: 32rpx;
  451. font-weight: bold;
  452. margin-bottom: 30rpx;
  453. text-align: center;
  454. }
  455. .form-item {
  456. margin-bottom: 20rpx;
  457. }
  458. .form-label {
  459. font-size: 28rpx;
  460. color: #333;
  461. margin-bottom: 10rpx;
  462. }
  463. .form-label .required {
  464. color: #FF6B6B;
  465. }
  466. .form-row {
  467. display: flex;
  468. gap: 20rpx;
  469. }
  470. .form-item.half {
  471. flex: 1;
  472. }
  473. .form-item input, .picker, .textarea {
  474. border: 1rpx solid #ddd;
  475. border-radius: 10rpx;
  476. padding: 20rpx;
  477. font-size: 28rpx;
  478. width: 100%;
  479. box-sizing: border-box;
  480. }
  481. .textarea {
  482. min-height: 100rpx;
  483. }
  484. .form-item checkbox-group {
  485. display: flex;
  486. flex-wrap: wrap;
  487. gap: 20rpx;
  488. }
  489. .form-item label {
  490. display: flex;
  491. align-items: center;
  492. gap: 10rpx;
  493. font-size: 28rpx;
  494. }
  495. .modal-btns {
  496. display: flex;
  497. gap: 20rpx;
  498. margin-top: 30rpx;
  499. }
  500. .modal-btns button {
  501. flex: 1;
  502. }
  503. </style>