tasks.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="container">
  3. <!-- 任务列表 -->
  4. <view class="task-list" v-if="tasks.length > 0">
  5. <view
  6. v-for="task in tasks"
  7. :key="task.id"
  8. class="task-item"
  9. :class="{ completed: task.status === 'completed' }"
  10. >
  11. <view class="task-left">
  12. <view class="task-title">{{ task.title }}</view>
  13. <view class="task-meta">
  14. <text class="points">+{{ task.points }}分</text>
  15. <text class="deadline">截止 {{ formatTime(task.deadline) }}</text>
  16. </view>
  17. </view>
  18. <view class="task-right">
  19. <button
  20. v-if="task.status === 'pending'"
  21. class="btn-complete"
  22. @click="completeTask(task.id)"
  23. >
  24. 完成
  25. </button>
  26. <view v-else class="completed-badge">已完成</view>
  27. </view>
  28. </view>
  29. </view>
  30. <view class="empty" v-else>
  31. <text>暂无今日任务</text>
  32. </view>
  33. <!-- 家长模式:添加任务按钮 -->
  34. <button
  35. v-if="isParent"
  36. class="btn-primary add-btn"
  37. @click="showAddModal = true"
  38. >
  39. + 添加任务
  40. </button>
  41. <!-- 添加任务弹窗 -->
  42. <view class="modal-mask" v-if="showAddModal" @click="showAddModal = false">
  43. <view class="modal" @click.stop>
  44. <view class="modal-title">添加任务</view>
  45. <view class="form-item">
  46. <input v-model="newTask.title" placeholder="任务名称" />
  47. </view>
  48. <view class="form-item">
  49. <input v-model="newTask.points" type="number" placeholder="积分(1-10)" />
  50. </view>
  51. <view class="form-item">
  52. <picker mode="time" @change="onTimeChange">
  53. <view class="picker">
  54. {{ newTask.deadlineTime || '选择截止时间' }}
  55. </view>
  56. </picker>
  57. </view>
  58. <view class="form-item">
  59. <picker mode="selector" :range="categories" @change="onCategoryChange">
  60. <view class="picker">
  61. {{ newTask.category || '选择分类' }}
  62. </view>
  63. </picker>
  64. </view>
  65. <view class="modal-btns">
  66. <button @click="showAddModal = false">取消</button>
  67. <button class="btn-primary" @click="createTask">确定</button>
  68. </view>
  69. </view>
  70. </view>
  71. </view>
  72. </template>
  73. <script>
  74. import { getTodayTasks, completeTask, createTask, getChildren } from '../../utils/api.js'
  75. export default {
  76. data() {
  77. return {
  78. tasks: [],
  79. isParent: false,
  80. currentChildId: '',
  81. showAddModal: false,
  82. newTask: {
  83. title: '',
  84. points: 2,
  85. deadlineTime: '',
  86. category: ''
  87. },
  88. categories: ['学习类', '生活类', '运动类', '其他']
  89. }
  90. },
  91. onShow() {
  92. this.loadTasks()
  93. this.isParent = this.$store.state.role === 'parent'
  94. },
  95. methods: {
  96. async loadTasks() {
  97. try {
  98. const childrenRes = await getChildren()
  99. const children = childrenRes.data
  100. if (children && children.length > 0) {
  101. this.currentChildId = children[0].id
  102. const res = await getTodayTasks(this.currentChildId)
  103. this.tasks = res.data || []
  104. }
  105. } catch (e) {
  106. console.error('加载任务失败', e)
  107. }
  108. },
  109. async completeTask(taskId) {
  110. try {
  111. const res = await completeTask(taskId, this.currentChildId, '')
  112. uni.showToast({
  113. title: `获得 ${res.data.pointsEarned} 积分`,
  114. icon: 'success'
  115. })
  116. this.loadTasks()
  117. } catch (e) {
  118. console.error('完成任务失败', e)
  119. }
  120. },
  121. onTimeChange(e) {
  122. const now = new Date()
  123. const deadline = new Date(now.getFullYear(), now.getMonth(), now.getDate(), e.detail.value)
  124. this.newTask.deadlineTime = e.detail.value
  125. },
  126. onCategoryChange(e) {
  127. this.newTask.category = this.categories[e.detail.value]
  128. },
  129. async createTask() {
  130. if (!this.newTask.title) {
  131. uni.showToast({ title: '请输入任务名称', icon: 'none' })
  132. return
  133. }
  134. const now = new Date()
  135. const deadline = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1)
  136. const [hours, minutes] = (this.newTask.deadlineTime || '20:00').split(':')
  137. deadline.setHours(parseInt(hours), parseInt(minutes), 0, 0)
  138. try {
  139. await createTask({
  140. title: this.newTask.title,
  141. points: parseInt(this.newTask.points),
  142. deadline: deadline.toISOString(),
  143. category: this.newTask.category,
  144. childId: this.currentChildId
  145. })
  146. uni.showToast({ title: '创建成功', icon: 'success' })
  147. this.showAddModal = false
  148. this.loadTasks()
  149. } catch (e) {
  150. console.error('创建任务失败', e)
  151. }
  152. },
  153. formatTime(dateStr) {
  154. if (!dateStr) return ''
  155. const date = new Date(dateStr)
  156. return `${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`
  157. }
  158. }
  159. }
  160. </script>
  161. <style scoped>
  162. .container {
  163. padding: 30rpx;
  164. padding-bottom: 120rpx;
  165. }
  166. .task-item {
  167. background: #fff;
  168. border-radius: 16rpx;
  169. padding: 30rpx;
  170. margin-bottom: 20rpx;
  171. display: flex;
  172. justify-content: space-between;
  173. align-items: center;
  174. }
  175. .task-item.completed {
  176. opacity: 0.6;
  177. }
  178. .task-title {
  179. font-size: 30rpx;
  180. color: #333;
  181. margin-bottom: 10rpx;
  182. }
  183. .task-meta {
  184. display: flex;
  185. gap: 20rpx;
  186. }
  187. .points {
  188. color: #FF6B6B;
  189. font-size: 24rpx;
  190. }
  191. .deadline {
  192. color: #999;
  193. font-size: 24rpx;
  194. }
  195. .btn-complete {
  196. background: #FF6B6B;
  197. color: #fff;
  198. font-size: 26rpx;
  199. padding: 10rpx 30rpx;
  200. border-radius: 30rpx;
  201. }
  202. .completed-badge {
  203. color: #52C41A;
  204. font-size: 26rpx;
  205. }
  206. .add-btn {
  207. position: fixed;
  208. bottom: 30rpx;
  209. left: 30rpx;
  210. right: 30rpx;
  211. }
  212. .empty {
  213. text-align: center;
  214. color: #999;
  215. padding: 100rpx;
  216. }
  217. .modal-mask {
  218. position: fixed;
  219. top: 0;
  220. left: 0;
  221. right: 0;
  222. bottom: 0;
  223. background: rgba(0, 0, 0, 0.5);
  224. display: flex;
  225. align-items: center;
  226. justify-content: center;
  227. }
  228. .modal {
  229. background: #fff;
  230. border-radius: 20rpx;
  231. padding: 40rpx;
  232. width: 80%;
  233. }
  234. .modal-title {
  235. font-size: 32rpx;
  236. font-weight: bold;
  237. margin-bottom: 30rpx;
  238. text-align: center;
  239. }
  240. .form-item {
  241. margin-bottom: 20rpx;
  242. }
  243. .form-item input, .picker {
  244. border: 1rpx solid #ddd;
  245. border-radius: 10rpx;
  246. padding: 20rpx;
  247. font-size: 28rpx;
  248. }
  249. .modal-btns {
  250. display: flex;
  251. gap: 20rpx;
  252. margin-top: 30rpx;
  253. }
  254. .modal-btns button {
  255. flex: 1;
  256. }
  257. </style>