create-task.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <view class="container">
  3. <view class="form-container">
  4. <view class="form-item">
  5. <text class="label">任务名称</text>
  6. <input class="input" v-model="form.title" placeholder="请输入任务名称" />
  7. </view>
  8. <view class="form-item">
  9. <text class="label">任务描述</text>
  10. <textarea class="textarea" v-model="form.description" placeholder="请输入任务描述" />
  11. </view>
  12. <view class="form-item">
  13. <text class="label">选择孩子</text>
  14. <picker :range="children" range-key="nickname" @change="onChildChange">
  15. <view class="picker">
  16. {{ selectedChild ? selectedChild.nickname : '请选择孩子' }}
  17. </view>
  18. </picker>
  19. </view>
  20. <view class="form-item">
  21. <text class="label">积分</text>
  22. <input class="input" type="number" v-model="form.points" placeholder="请输入积分 (1-10)" />
  23. </view>
  24. <view class="form-item">
  25. <text class="label">分类</text>
  26. <picker :range="categories" @change="onCategoryChange">
  27. <view class="picker">
  28. {{ form.category || '请选择分类' }}
  29. </view>
  30. </picker>
  31. </view>
  32. <!-- TASK-003: 截止时间设置 -->
  33. <view class="form-item">
  34. <text class="label">截止时间</text>
  35. <picker mode="multiSelector" :range="dateTimeRange" :value="dateTimeIndex" @change="onDateTimeChange" @columnchange="onColumnChange">
  36. <view class="picker">
  37. {{ deadlineText || '请选择截止时间' }}
  38. </view>
  39. </picker>
  40. </view>
  41. <!-- TASK-004: 重复设置 -->
  42. <view class="form-item">
  43. <text class="label">重复设置</text>
  44. <picker :range="repeatTypes" range-key="label" @change="onRepeatChange">
  45. <view class="picker">
  46. {{ repeatTypeLabel || '不重复' }}
  47. </view>
  48. </picker>
  49. </view>
  50. <view class="form-item">
  51. <text class="label">需要审核</text>
  52. <switch :checked="form.needReview" @change="onNeedReviewChange" />
  53. </view>
  54. <button class="btn-primary" @click="submit">创建任务</button>
  55. </view>
  56. </view>
  57. </template>
  58. <script>
  59. import { getChildren, createTask } from '../../utils/api.js'
  60. export default {
  61. data() {
  62. return {
  63. form: {
  64. title: '',
  65. description: '',
  66. childId: '',
  67. points: 2,
  68. category: '',
  69. deadline: null,
  70. repeatType: 'none',
  71. needReview: false
  72. },
  73. children: [],
  74. selectedChild: null,
  75. categories: ['学习类', '生活类', '运动类', '其他'],
  76. repeatTypes: [
  77. { value: 'none', label: '不重复' },
  78. { value: 'daily', label: '每天' },
  79. { value: 'weekly', label: '每周' }
  80. ],
  81. dateTimeRange: [[], [], [], []],
  82. dateTimeIndex: [0, 0, 0, 0],
  83. deadlineText: ''
  84. }
  85. },
  86. onLoad() {
  87. this.loadChildren()
  88. this.initDateTimePicker()
  89. },
  90. methods: {
  91. async loadChildren() {
  92. try {
  93. const res = await getChildren()
  94. this.children = res.data || []
  95. } catch (e) {
  96. console.error(e)
  97. }
  98. },
  99. initDateTimePicker() {
  100. // 初始化日期时间选择器
  101. const now = new Date()
  102. const year = now.getFullYear()
  103. const month = now.getMonth() + 1
  104. const day = now.getDate()
  105. const hour = now.getHours()
  106. const minute = now.getMinutes()
  107. // 年(当前年份+未来2年)
  108. const years = []
  109. for (let i = year; i <= year + 2; i++) {
  110. years.push(i + '年')
  111. }
  112. // 月
  113. const months = []
  114. for (let i = 1; i <= 12; i++) {
  115. months.push(i + '月')
  116. }
  117. // 日
  118. const days = []
  119. const daysInMonth = new Date(year, month, 0).getDate()
  120. for (let i = 1; i <= daysInMonth; i++) {
  121. days.push(i + '日')
  122. }
  123. // 时
  124. const hours = []
  125. for (let i = 0; i < 24; i++) {
  126. hours.push(i.toString().padStart(2, '0') + '时')
  127. }
  128. // 分
  129. const minutes = []
  130. for (let i = 0; i < 60; i += 5) {
  131. minutes.push(i.toString().padStart(2, '0') + '分')
  132. }
  133. this.dateTimeRange = [years, months, days, hours, minutes]
  134. },
  135. onChildChange(e) {
  136. this.selectedChild = this.children[e.detail.value]
  137. this.form.childId = this.selectedChild.id
  138. },
  139. onCategoryChange(e) {
  140. this.form.category = this.categories[e.detail.value]
  141. },
  142. onDateTimeChange(e) {
  143. const vals = e.detail.value
  144. this.dateTimeIndex = vals
  145. const now = new Date()
  146. const year = now.getFullYear() + vals[0]
  147. const month = vals[1] + 1
  148. const day = vals[2] + 1
  149. const hour = vals[3]
  150. const minute = vals[4] * 5
  151. this.deadlineText = `${year}年${month}月${day}日 ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`
  152. this.form.deadline = new Date(year, month - 1, day, hour, minute)
  153. },
  154. onColumnChange(e) {
  155. // 处理月份变化时更新天数
  156. if (e.detail.column === 1) {
  157. const month = e.detail.value + 1
  158. const now = new Date()
  159. const year = now.getFullYear() + this.dateTimeIndex[0]
  160. const daysInMonth = new Date(year, month, 0).getDate()
  161. const days = []
  162. for (let i = 1; i <= daysInMonth; i++) {
  163. days.push(i + '日')
  164. }
  165. this.$set(this.dateTimeRange, 2, days)
  166. }
  167. },
  168. onRepeatChange(e) {
  169. const selected = this.repeatTypes[e.detail.value]
  170. this.form.repeatType = selected.value
  171. },
  172. onNeedReviewChange(e) {
  173. this.form.needReview = e.detail.value
  174. },
  175. async submit() {
  176. if (!this.form.title) {
  177. uni.showToast({ title: '请输入任务名称', icon: 'none' })
  178. return
  179. }
  180. if (!this.form.childId) {
  181. uni.showToast({ title: '请选择孩子', icon: 'none' })
  182. return
  183. }
  184. if (!this.form.deadline) {
  185. uni.showToast({ title: '请选择截止时间', icon: 'none' })
  186. return
  187. }
  188. try {
  189. await createTask({
  190. title: this.form.title,
  191. description: this.form.description,
  192. childId: this.form.childId,
  193. points: parseInt(this.form.points) || 2,
  194. category: this.form.category || '其他',
  195. deadline: this.form.deadline.toISOString(),
  196. repeatType: this.form.repeatType,
  197. needReview: this.form.needReview ? 1 : 0
  198. })
  199. uni.showToast({ title: '创建成功', icon: 'success' })
  200. setTimeout(() => uni.navigateBack(), 1500)
  201. } catch (e) {
  202. uni.showToast({ title: '创建失败', icon: 'none' })
  203. }
  204. }
  205. }
  206. }
  207. </script>
  208. <style scoped>
  209. .container { padding: 30rpx; }
  210. .form-container { background: #fff; border-radius: 20rpx; padding: 30rpx; }
  211. .form-item { margin-bottom: 30rpx; }
  212. .label { display: block; font-size: 28rpx; color: #333; margin-bottom: 10rpx; }
  213. .input, .textarea { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; width: 100%; box-sizing: border-box; }
  214. .textarea { height: 150rpx; }
  215. .picker { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #666; }
  216. .btn-primary { background: #FF6B6B; color: #fff; border-radius: 20rpx; margin-top: 40rpx; }
  217. </style>