create-task.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. <checkbox-group @change="onChildrenChange">
  15. <label class="child-checkbox" v-for="child in children" :key="child.id">
  16. <checkbox :value="String(child.id)" :checked="selectedChildIds.includes(String(child.id))" />
  17. <text class="child-name">{{ child.nickname }}</text>
  18. </label>
  19. </checkbox-group>
  20. </view>
  21. <view class="form-item">
  22. <text class="label">积分</text>
  23. <input class="input" type="number" v-model="form.points" placeholder="请输入积分 (1-10)" />
  24. </view>
  25. <view class="form-item">
  26. <text class="label">分类</text>
  27. <picker :range="categories" @change="onCategoryChange">
  28. <view class="picker">
  29. {{ form.category || '请选择分类' }}
  30. </view>
  31. </picker>
  32. </view>
  33. <!-- 小游戏选择(当选择"小游戏类"时显示) -->
  34. <view class="form-item" v-if="form.category === '小游戏类'">
  35. <text class="label">选择小游戏</text>
  36. <picker :range="miniGames" range-key="gameName" @change="onMinigameChange">
  37. <view class="picker">
  38. {{ selectedMinigame ? selectedMinigame.gameName : '请选择小游戏' }}
  39. </view>
  40. </picker>
  41. </view>
  42. <!-- TASK-003: 截止时间设置 -->
  43. <view class="form-item">
  44. <text class="label">截止时间</text>
  45. <picker mode="multiSelector" :range="dateTimeRange" :value="dateTimeIndex" @change="onDateTimeChange" @columnchange="onColumnChange">
  46. <view class="picker">
  47. {{ deadlineText || '请选择截止时间' }}
  48. </view>
  49. </picker>
  50. </view>
  51. <!-- TASK-004: 重复设置 -->
  52. <view class="form-item">
  53. <text class="label">重复设置</text>
  54. <picker :range="repeatTypes" range-key="label" @change="onRepeatChange">
  55. <view class="picker">
  56. {{ repeatTypeLabel || '不重复' }}
  57. </view>
  58. </picker>
  59. </view>
  60. <view class="form-item">
  61. <text class="label">需要审核</text>
  62. <switch :checked="form.needReview" @change="onNeedReviewChange" />
  63. </view>
  64. <view class="form-item">
  65. <text class="label">预计时长(分钟)</text>
  66. <input class="input" type="number" v-model="form.duration" placeholder="请输入预计完成时长" />
  67. </view>
  68. <button class="btn-primary" @click="submit">创建任务</button>
  69. </view>
  70. </view>
  71. </template>
  72. <script>
  73. import config from '@/config.js'
  74. import { getChildren, createTask } from '../../utils/api.js'
  75. export default {
  76. data() {
  77. return {
  78. form: {
  79. title: '',
  80. description: '',
  81. childIds: [],
  82. points: 2,
  83. category: '',
  84. minigameCode: '',
  85. deadline: null,
  86. repeatType: 'none',
  87. needReview: false,
  88. duration: null
  89. },
  90. children: [],
  91. selectedChildIds: [],
  92. categories: ['学习类', '生活类', '运动类', '小游戏类', '其他'],
  93. miniGames: [],
  94. selectedMinigame: null,
  95. repeatTypes: [
  96. { value: 'none', label: '不重复' },
  97. { value: 'daily', label: '每天' },
  98. { value: 'weekly', label: '每周' }
  99. ],
  100. dateTimeRange: [[], [], [], []],
  101. dateTimeIndex: [0, 0, 0, 0],
  102. deadlineText: ''
  103. }
  104. },
  105. onLoad(options) {
  106. this.loadChildren(options.childId)
  107. this.loadMiniGames()
  108. this.initDateTimePicker()
  109. },
  110. methods: {
  111. async loadChildren(preselectedChildId) {
  112. try {
  113. const res = await getChildren()
  114. this.children = res.data || []
  115. // 如果有 preselect childId,自动选中该孩子
  116. if (preselectedChildId && this.children.length > 0) {
  117. const matched = this.children.find(c => String(c.id) === String(preselectedChildId))
  118. if (matched) {
  119. const idNum = Number(matched.id)
  120. if (!this.form.childIds.includes(idNum)) {
  121. this.form.childIds.push(idNum)
  122. this.selectedChildIds.push(String(idNum))
  123. }
  124. }
  125. }
  126. } catch (e) {
  127. console.error(e)
  128. }
  129. },
  130. async loadMiniGames() {
  131. try {
  132. const res = await uni.request({
  133. url: config.api('/api/tasks/minigame-options'),
  134. method: 'POST',
  135. header: {
  136. 'Authorization': `Bearer ${uni.getStorageSync('token')}`
  137. }
  138. })
  139. if (res.data && res.data.code === 200) {
  140. this.miniGames = res.data.data || []
  141. }
  142. } catch (e) {
  143. console.error(e)
  144. }
  145. },
  146. initDateTimePicker() {
  147. // 初始化日期时间选择器
  148. const now = new Date()
  149. const year = now.getFullYear()
  150. const month = now.getMonth() + 1
  151. const day = now.getDate()
  152. const hour = now.getHours()
  153. const minute = now.getMinutes()
  154. // 年(当前年份+未来2年)
  155. const years = []
  156. for (let i = year; i <= year + 2; i++) {
  157. years.push(i + '年')
  158. }
  159. // 月
  160. const months = []
  161. for (let i = 1; i <= 12; i++) {
  162. months.push(i + '月')
  163. }
  164. // 日
  165. const days = []
  166. const daysInMonth = new Date(year, month, 0).getDate()
  167. for (let i = 1; i <= daysInMonth; i++) {
  168. days.push(i + '日')
  169. }
  170. // 时
  171. const hours = []
  172. for (let i = 0; i < 24; i++) {
  173. hours.push(i.toString().padStart(2, '0') + '时')
  174. }
  175. // 分
  176. const minutes = []
  177. for (let i = 0; i < 60; i += 5) {
  178. minutes.push(i.toString().padStart(2, '0') + '分')
  179. }
  180. this.dateTimeRange = [years, months, days, hours, minutes]
  181. },
  182. onChildrenChange(e) {
  183. this.selectedChildIds = e.detail.value
  184. this.form.childIds = e.detail.value.map(Number)
  185. },
  186. onCategoryChange(e) {
  187. this.form.category = this.categories[e.detail.value]
  188. // 清空小游戏选择
  189. if (this.form.category !== '小游戏类') {
  190. this.form.minigameCode = ''
  191. this.selectedMinigame = null
  192. }
  193. },
  194. onMinigameChange(e) {
  195. this.selectedMinigame = this.miniGames[e.detail.value]
  196. this.form.minigameCode = this.selectedMinigame.gameCode
  197. // 自动填充任务名称
  198. if (!this.form.title) {
  199. this.form.title = this.selectedMinigame.gameName
  200. }
  201. // 自动填充积分
  202. if (this.selectedMinigame.defaultPoints) {
  203. this.form.points = this.selectedMinigame.defaultPoints
  204. }
  205. },
  206. onDateTimeChange(e) {
  207. const vals = e.detail.value
  208. this.dateTimeIndex = vals
  209. const now = new Date()
  210. const year = now.getFullYear() + vals[0]
  211. const month = vals[1] + 1
  212. const day = vals[2] + 1
  213. const hour = vals[3]
  214. const minute = vals[4] * 5
  215. this.deadlineText = `${year}年${month}月${day}日 ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`
  216. this.form.deadline = new Date(year, month - 1, day, hour, minute)
  217. },
  218. onColumnChange(e) {
  219. // 处理月份变化时更新天数
  220. if (e.detail.column === 1) {
  221. const month = e.detail.value + 1
  222. const now = new Date()
  223. const year = now.getFullYear() + this.dateTimeIndex[0]
  224. const daysInMonth = new Date(year, month, 0).getDate()
  225. const days = []
  226. for (let i = 1; i <= daysInMonth; i++) {
  227. days.push(i + '日')
  228. }
  229. this.$set(this.dateTimeRange, 2, days)
  230. }
  231. },
  232. onRepeatChange(e) {
  233. const selected = this.repeatTypes[e.detail.value]
  234. this.form.repeatType = selected.value
  235. },
  236. onNeedReviewChange(e) {
  237. this.form.needReview = e.detail.value
  238. },
  239. async submit() {
  240. if (!this.form.title) {
  241. uni.showToast({ title: '请输入任务名称', icon: 'none' })
  242. return
  243. }
  244. if (this.form.childIds.length === 0) {
  245. uni.showToast({ title: '请选择至少一个孩子', icon: 'none' })
  246. return
  247. }
  248. if (!this.form.deadline) {
  249. uni.showToast({ title: '请选择截止时间', icon: 'none' })
  250. return
  251. }
  252. // 小游戏类必须选择具体游戏
  253. if (this.form.category === '小游戏类' && !this.form.minigameCode) {
  254. uni.showToast({ title: '请选择具体小游戏', icon: 'none' })
  255. return
  256. }
  257. uni.showLoading({ title: '创建任务中...' })
  258. const taskData = {
  259. title: this.form.title,
  260. description: this.form.description,
  261. points: parseInt(this.form.points) || 2,
  262. category: this.form.category || '其他',
  263. minigameCode: this.form.minigameCode,
  264. deadline: this.form.deadline.toISOString(),
  265. repeatType: this.form.repeatType,
  266. needReview: this.form.needReview ? 1 : 0,
  267. duration: parseInt(this.form.duration) || 0
  268. }
  269. let successCount = 0
  270. let failCount = 0
  271. for (const childId of this.form.childIds) {
  272. try {
  273. await createTask({ ...taskData, childId })
  274. successCount++
  275. } catch (e) {
  276. failCount++
  277. }
  278. }
  279. uni.hideLoading()
  280. if (failCount === 0) {
  281. uni.showToast({ title: `已为${successCount}个孩子创建任务`, icon: 'success' })
  282. setTimeout(() => uni.navigateBack(), 1500)
  283. } else if (successCount > 0) {
  284. uni.showToast({ title: `${successCount}个成功,${failCount}个失败`, icon: 'none' })
  285. } else {
  286. uni.showToast({ title: '创建失败', icon: 'none' })
  287. }
  288. }
  289. }
  290. }
  291. </script>
  292. <style scoped>
  293. .container { padding: 30rpx; }
  294. .form-container { background: #fff; border-radius: 20rpx; padding: 30rpx; }
  295. .form-item { margin-bottom: 30rpx; }
  296. .label { display: block; font-size: 28rpx; color: #333; margin-bottom: 10rpx; }
  297. .input, .textarea { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; width: 100%; box-sizing: border-box; }
  298. .input { height: 80rpx; }
  299. .textarea { height: 150rpx; }
  300. .picker { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #666; }
  301. .child-checkbox { display: flex; align-items: center; padding: 16rpx 0; border-bottom: 1rpx solid #f0f0f0; }
  302. .child-checkbox:last-child { border-bottom: none; }
  303. .child-name { font-size: 28rpx; color: #333; margin-left: 10rpx; }
  304. .btn-primary { background: #F97316; color: #fff; border-radius: 20rpx; margin-top: 40rpx; }
  305. </style>