| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- // pages/task-create/task-create.js
- const app = getApp();
- const api = require('../../utils/api.js');
- Page({
- data: {
- childId: 0,
- childName: '',
- title: '',
- description: '',
- points: 2,
- deadline: '',
- deadlineTime: '',
- category: '学习',
- repeatType: 'none',
- needReview: false,
- reviewByCategory: false,
- categories: ['学习', '生活', '运动', '艺术', '社交'],
- repeatTypes: [
- { value: 'none', label: '不重复' },
- { value: 'daily', label: '每天' },
- { value: 'weekly', label: '每周' }
- ],
- loading: false
- },
- onLoad(options) {
- const childId = options.childId || 0;
- const childName = options.childName || '';
- this.setData({ childId, childName });
-
- // 设置默认截止时间为今天20:00
- const today = new Date();
- const deadline = today.toISOString().split('T')[0];
- this.setData({
- deadline,
- deadlineTime: '20:00'
- });
- },
- // 输入标题
- onTitleInput(e) {
- this.setData({ title: e.detail.value });
- },
- // 输入描述
- onDescriptionInput(e) {
- this.setData({ description: e.detail.value });
- },
- // 选择积分
- onPointsChange(e) {
- this.setData({ points: parseInt(e.detail.value) });
- },
- // 选择分类
- onCategoryChange(e) {
- this.setData({ category: e.detail.value });
- },
- // 选择截止日期
- onDeadlineChange(e) {
- this.setData({ deadline: e.detail.value });
- },
- // 选择截止时间
- onDeadlineTimeChange(e) {
- this.setData({ deadlineTime: e.detail.value });
- },
- // 选择重复类型
- onRepeatTypeChange(e) {
- this.setData({ repeatType: e.detail.value });
- },
- // 切换需要审核
- onNeedReviewChange(e) {
- this.setData({ needReview: e.detail.value });
- },
- // 切换按分类审核
- onReviewByCategoryChange(e) {
- this.setData({ reviewByCategory: e.detail.value });
- },
- // 表单验证
- validateForm() {
- if (!this.data.title.trim()) {
- wx.showToast({ title: '请输入任务标题', icon: 'none' });
- return false;
- }
-
- if (this.data.title.length > 20) {
- wx.showToast({ title: '标题不能超过20个字', icon: 'none' });
- return false;
- }
-
- if (this.data.description.length > 200) {
- wx.showToast({ title: '描述不能超过200个字', icon: 'none' });
- return false;
- }
-
- if (!this.data.deadline || !this.data.deadlineTime) {
- wx.showToast({ title: '请设置截止时间', icon: 'none' });
- return false;
- }
-
- // 检查截止时间是否在未来
- const deadline = new Date(`${this.data.deadline} ${this.data.deadlineTime}`);
- if (deadline <= new Date()) {
- wx.showToast({ title: '截止时间必须在未来', icon: 'none' });
- return false;
- }
-
- return true;
- },
- // 提交表单
- onSubmit() {
- if (!this.validateForm()) return;
-
- wx.showModal({
- title: '确认创建',
- content: `确定要创建任务「${this.data.title}」吗?`,
- success: (res) => {
- if (res.confirm) {
- this.doCreateTask();
- }
- }
- });
- },
- doCreateTask() {
- this.setData({ loading: true });
- wx.showLoading({ title: '创建中...' });
-
- const taskData = {
- childId: this.data.childId,
- title: this.data.title,
- description: this.data.description,
- points: this.data.points,
- deadline: `${this.data.deadline} ${this.data.deadlineTime}`,
- category: this.data.category,
- repeatType: this.data.repeatType,
- needReview: this.data.needReview,
- reviewByCategory: this.data.reviewByCategory
- };
-
- // 模拟API调用
- setTimeout(() => {
- wx.hideLoading();
- this.setData({ loading: false });
-
- wx.showToast({
- title: '创建成功',
- icon: 'success'
- });
-
- // 返回上一页
- setTimeout(() => {
- wx.navigateBack();
- }, 1500);
- }, 1000);
- },
- // 预览任务
- onPreview() {
- if (!this.validateForm()) return;
-
- const content = `
- 任务标题:${this.data.title}
- 任务描述:${this.data.description || '无'}
- 任务积分:${this.data.points}分
- 截止时间:${this.data.deadline} ${this.data.deadlineTime}
- 任务分类:${this.data.category}
- 重复类型:${this.data.repeatTypes.find(t => t.value === this.data.repeatType)?.label}
- 需要审核:${this.data.needReview ? '是' : '否'}
- `.trim();
-
- wx.showModal({
- title: '任务预览',
- content,
- showCancel: false
- });
- }
- });
|