task-create.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // pages/task-create/task-create.js
  2. const app = getApp();
  3. const api = require('../../utils/api.js');
  4. Page({
  5. data: {
  6. childId: 0,
  7. childName: '',
  8. title: '',
  9. description: '',
  10. points: 2,
  11. deadline: '',
  12. deadlineTime: '',
  13. category: '学习',
  14. repeatType: 'none',
  15. needReview: false,
  16. reviewByCategory: false,
  17. categories: ['学习', '生活', '运动', '艺术', '社交'],
  18. repeatTypes: [
  19. { value: 'none', label: '不重复' },
  20. { value: 'daily', label: '每天' },
  21. { value: 'weekly', label: '每周' }
  22. ],
  23. loading: false
  24. },
  25. onLoad(options) {
  26. const childId = options.childId || 0;
  27. const childName = options.childName || '';
  28. this.setData({ childId, childName });
  29. // 设置默认截止时间为今天20:00
  30. const today = new Date();
  31. const deadline = today.toISOString().split('T')[0];
  32. this.setData({
  33. deadline,
  34. deadlineTime: '20:00'
  35. });
  36. },
  37. // 输入标题
  38. onTitleInput(e) {
  39. this.setData({ title: e.detail.value });
  40. },
  41. // 输入描述
  42. onDescriptionInput(e) {
  43. this.setData({ description: e.detail.value });
  44. },
  45. // 选择积分
  46. onPointsChange(e) {
  47. this.setData({ points: parseInt(e.detail.value) });
  48. },
  49. // 选择分类
  50. onCategoryChange(e) {
  51. this.setData({ category: e.detail.value });
  52. },
  53. // 选择截止日期
  54. onDeadlineChange(e) {
  55. this.setData({ deadline: e.detail.value });
  56. },
  57. // 选择截止时间
  58. onDeadlineTimeChange(e) {
  59. this.setData({ deadlineTime: e.detail.value });
  60. },
  61. // 选择重复类型
  62. onRepeatTypeChange(e) {
  63. this.setData({ repeatType: e.detail.value });
  64. },
  65. // 切换需要审核
  66. onNeedReviewChange(e) {
  67. this.setData({ needReview: e.detail.value });
  68. },
  69. // 切换按分类审核
  70. onReviewByCategoryChange(e) {
  71. this.setData({ reviewByCategory: e.detail.value });
  72. },
  73. // 表单验证
  74. validateForm() {
  75. if (!this.data.title.trim()) {
  76. wx.showToast({ title: '请输入任务标题', icon: 'none' });
  77. return false;
  78. }
  79. if (this.data.title.length > 20) {
  80. wx.showToast({ title: '标题不能超过20个字', icon: 'none' });
  81. return false;
  82. }
  83. if (this.data.description.length > 200) {
  84. wx.showToast({ title: '描述不能超过200个字', icon: 'none' });
  85. return false;
  86. }
  87. if (!this.data.deadline || !this.data.deadlineTime) {
  88. wx.showToast({ title: '请设置截止时间', icon: 'none' });
  89. return false;
  90. }
  91. // 检查截止时间是否在未来
  92. const deadline = new Date(`${this.data.deadline} ${this.data.deadlineTime}`);
  93. if (deadline <= new Date()) {
  94. wx.showToast({ title: '截止时间必须在未来', icon: 'none' });
  95. return false;
  96. }
  97. return true;
  98. },
  99. // 提交表单
  100. onSubmit() {
  101. if (!this.validateForm()) return;
  102. wx.showModal({
  103. title: '确认创建',
  104. content: `确定要创建任务「${this.data.title}」吗?`,
  105. success: (res) => {
  106. if (res.confirm) {
  107. this.doCreateTask();
  108. }
  109. }
  110. });
  111. },
  112. doCreateTask() {
  113. this.setData({ loading: true });
  114. wx.showLoading({ title: '创建中...' });
  115. const taskData = {
  116. childId: this.data.childId,
  117. title: this.data.title,
  118. description: this.data.description,
  119. points: this.data.points,
  120. deadline: `${this.data.deadline} ${this.data.deadlineTime}`,
  121. category: this.data.category,
  122. repeatType: this.data.repeatType,
  123. needReview: this.data.needReview,
  124. reviewByCategory: this.data.reviewByCategory
  125. };
  126. // 模拟API调用
  127. setTimeout(() => {
  128. wx.hideLoading();
  129. this.setData({ loading: false });
  130. wx.showToast({
  131. title: '创建成功',
  132. icon: 'success'
  133. });
  134. // 返回上一页
  135. setTimeout(() => {
  136. wx.navigateBack();
  137. }, 1500);
  138. }, 1000);
  139. },
  140. // 预览任务
  141. onPreview() {
  142. if (!this.validateForm()) return;
  143. const content = `
  144. 任务标题:${this.data.title}
  145. 任务描述:${this.data.description || '无'}
  146. 任务积分:${this.data.points}分
  147. 截止时间:${this.data.deadline} ${this.data.deadlineTime}
  148. 任务分类:${this.data.category}
  149. 重复类型:${this.data.repeatTypes.find(t => t.value === this.data.repeatType)?.label}
  150. 需要审核:${this.data.needReview ? '是' : '否'}
  151. `.trim();
  152. wx.showModal({
  153. title: '任务预览',
  154. content,
  155. showCancel: false
  156. });
  157. }
  158. });