create-task.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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="请输入积分" />
  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. <view class="form-item">
  33. <text class="label">需要审核</text>
  34. <switch :checked="form.needReview" @change="form.needReview = !form.needReview" />
  35. </view>
  36. <button class="btn-primary" @click="submit">创建任务</button>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import { getChildren, createTask } from '../../utils/api.js'
  42. export default {
  43. data() {
  44. return {
  45. form: {
  46. title: '',
  47. description: '',
  48. childId: '',
  49. points: 2,
  50. category: '',
  51. needReview: false
  52. },
  53. children: [],
  54. selectedChild: null,
  55. categories: ['学习', '生活', '运动', '其他']
  56. }
  57. },
  58. onLoad() {
  59. this.loadChildren()
  60. },
  61. methods: {
  62. async loadChildren() {
  63. try {
  64. const res = await getChildren()
  65. this.children = res.data || []
  66. } catch (e) {
  67. console.error(e)
  68. }
  69. },
  70. onChildChange(e) {
  71. this.selectedChild = this.children[e.detail.value]
  72. this.form.childId = this.selectedChild.id
  73. },
  74. onCategoryChange(e) {
  75. this.form.category = this.categories[e.detail.value]
  76. },
  77. async submit() {
  78. if (!this.form.title) {
  79. uni.showToast({ title: '请输入任务名称', icon: 'none' })
  80. return
  81. }
  82. if (!this.form.childId) {
  83. uni.showToast({ title: '请选择孩子', icon: 'none' })
  84. return
  85. }
  86. try {
  87. await createTask({
  88. title: this.form.title,
  89. description: this.form.description,
  90. childId: this.form.childId,
  91. points: parseInt(this.form.points) || 2,
  92. category: this.form.category || '其他',
  93. needReview: this.form.needReview ? 1 : 0
  94. })
  95. uni.showToast({ title: '创建成功', icon: 'success' })
  96. setTimeout(() => uni.navigateBack(), 1500)
  97. } catch (e) {
  98. uni.showToast({ title: '创建失败', icon: 'none' })
  99. }
  100. }
  101. }
  102. }
  103. </script>
  104. <style scoped>
  105. .container { padding: 30rpx; }
  106. .form-container { background: #fff; border-radius: 20rpx; padding: 30rpx; }
  107. .form-item { margin-bottom: 30rpx; }
  108. .label { display: block; font-size: 28rpx; color: #333; margin-bottom: 10rpx; }
  109. .input, .textarea { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; }
  110. .textarea { height: 150rpx; }
  111. .picker { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #666; }
  112. .btn-primary { background: #FF6B6B; color: #fff; border-radius: 20rpx; margin-top: 40rpx; }
  113. </style>