曹睿
2025-04-25 ec1bef3a37e8dcdf22f1bf52e7c272a18306f4b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<template>
  <view class="todo-container">
    <view class="page-header">
      <text class="page-title">待办事项</text>
      <wd-button size="small" type="primary" icon="add" @click="showAddTodoPopup = true">
        新建待办
      </wd-button>
    </view>
 
    <view class="filter-tabs">
      <wd-tabs v-model="activeTab" sticky>
        <wd-tab title="全部" name="all" />
        <wd-tab title="未完成" name="active" />
        <wd-tab title="已完成" name="completed" />
      </wd-tabs>
    </view>
 
    <TodoList :todos="filteredTodos" @update="handleUpdateTodo" @delete="handleDeleteTodo" />
 
    <!-- 新增待办弹窗 -->
    <wd-popup
      v-model="showAddTodoPopup"
      position="bottom"
      close-on-click-modal
      :style="{ height: '65%' }"
    >
      <view class="popup-header">
        <text class="popup-title">新建待办</text>
        <wd-icon name="close" @click="showAddTodoPopup = false" />
      </view>
      <view class="popup-form">
        <wd-input
          v-model="newTodo.title"
          placeholder="请输入待办标题"
          :rules="[{ required: true, message: '请输入标题' }]"
        />
        <wd-textarea
          v-model="newTodo.description"
          placeholder="请输入详细描述"
          rows="3"
          autosize
          class="mt-20"
        />
        <wd-cell title="截止日期" is-link @click="showDatePicker = true">
          <text v-if="newTodo.deadline">{{ formatDate(newTodo.deadline) }}</text>
          <text v-else class="text-placeholder">请选择</text>
        </wd-cell>
        <wd-cell title="优先级">
          <wd-radio-group v-model="newTodo.priority" shape="button">
            <wd-radio value="low">低</wd-radio>
            <wd-radio value="medium">中</wd-radio>
            <wd-radio value="high">高</wd-radio>
          </wd-radio-group>
        </wd-cell>
        <view class="form-actions">
          <wd-button
            block
            type="primary"
            :loading="loading"
            :disabled="!newTodo.title"
            @click="handleAddTodo"
          >
            保存
          </wd-button>
        </view>
      </view>
    </wd-popup>
 
    <!-- 日期选择器 -->
    <wd-datetime-picker
      v-model="showDatePicker"
      v-model:value="newTodo.deadline"
      label="截止日期"
      type="date"
      confirm-button-text="确认"
      cancel-button-text="取消"
      title="选择截止日期"
    />
 
    <wd-toast />
  </view>
</template>
 
<script lang="ts" setup>
import { ref, computed } from "vue";
import { useToast } from "wot-design-uni";
import { Todo } from "@/types/todo";
import TodoList from "@/components/todo/TodoList.vue";
 
const toast = useToast();
const loading = ref(false);
const showAddTodoPopup = ref(false);
const showDatePicker = ref(false);
const activeTab = ref("all");
 
// 待办列表
const todos = ref<Todo[]>([
  {
    id: "1",
    title: "完成个人资料填写",
    description: "包括上传头像、填写基本信息等",
    completed: false,
    priority: "high",
    createdAt: new Date().toISOString(),
  },
  {
    id: "2",
    title: "阅读使用指南",
    description: "熟悉系统功能和操作流程",
    completed: true,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  },
]);
 
// 新增待办表单
const newTodo = ref<Partial<Todo>>({
  title: "",
  description: "",
  priority: "medium",
  deadline: "",
});
 
// 根据标签筛选待办
const filteredTodos = computed(() => {
  switch (activeTab.value) {
    case "active":
      return todos.value.filter((todo) => !todo.completed);
    case "completed":
      return todos.value.filter((todo) => todo.completed);
    default:
      return todos.value;
  }
});
 
// 格式化日期
const formatDate = (date: string) => {
  const d = new Date(date);
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, "0");
  const day = String(d.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
};
 
// 生成唯一ID
const generateId = () => {
  return Date.now().toString(36) + Math.random().toString(36).substring(2);
};
 
// 添加待办
const handleAddTodo = () => {
  if (!newTodo.value.title) {
    toast.error("请输入待办标题");
    return;
  }
 
  loading.value = true;
 
  // 模拟API调用
  setTimeout(() => {
    const now = new Date().toISOString();
    const todo: Todo = {
      id: generateId(),
      title: newTodo.value.title!,
      description: newTodo.value.description,
      completed: false,
      deadline: newTodo.value.deadline,
      priority: newTodo.value.priority as "low" | "medium" | "high",
      createdAt: now,
    };
 
    todos.value.unshift(todo);
    resetForm();
    loading.value = false;
    showAddTodoPopup.value = false;
    toast.success("待办创建成功");
  }, 500);
};
 
// 更新待办
const handleUpdateTodo = (todo: Todo) => {
  const index = todos.value.findIndex((item) => item.id === todo.id);
  if (index !== -1) {
    todos.value[index] = {
      ...todo,
      updatedAt: new Date().toISOString(),
    };
    toast.success(todo.completed ? "已完成" : "已取消完成");
  }
};
 
// 删除待办
const handleDeleteTodo = (id: string) => {
  uni.showModal({
    title: "提示",
    content: "确认删除该待办事项?",
    success: (res) => {
      if (res.confirm) {
        todos.value = todos.value.filter((todo) => todo.id !== id);
        toast.success("删除成功");
      }
    },
  });
};
 
// 重置表单
const resetForm = () => {
  newTodo.value = {
    title: "",
    description: "",
    priority: "medium",
    deadline: "",
  };
};
</script>
 
<style lang="scss" scoped>
.todo-container {
  min-height: 100vh;
  padding: 30rpx;
  background-color: #f5f7fa;
 
  .page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 30rpx;
 
    .page-title {
      font-size: 36rpx;
      font-weight: 600;
      color: #333;
    }
  }
 
  .filter-tabs {
    margin-bottom: 20rpx;
    overflow: hidden;
    background-color: #fff;
    border-radius: 12rpx;
  }
 
  .popup-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 30rpx;
    border-bottom: 1rpx solid #eee;
 
    .popup-title {
      font-size: 32rpx;
      font-weight: 600;
      color: #333;
    }
  }
 
  .popup-form {
    padding: 30rpx;
 
    .form-actions {
      margin-top: 40rpx;
    }
  }
 
  .text-placeholder {
    color: #999;
  }
 
  .mt-20 {
    margin-top: 20rpx;
  }
}
</style>