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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
|
<!-- views/TaskView.vue -->
<template>
<div class="task-container">
<!-- 头部 -->
<div class="task-header">
<h1>📝 任务管理系统</h1>
<p class="subtitle">使用 Vue 3 + FastAPI 构建</p>
</div>
<!-- 创建任务卡片 -->
<el-card class="create-card" shadow="hover">
<template #header>
<div class="card-header">
<span class="header-title">➕ 创建新任务</span>
</div>
</template>
<el-form
ref="taskFormRef"
:model="newTask"
:rules="formRules"
@submit.prevent="handleCreateTask"
>
<el-form-item label="任务标题" prop="title">
<el-input
v-model="newTask.title"
placeholder="请输入任务标题"
:maxlength="100"
show-word-limit
clearable
>
<template #prefix>
<el-icon><Document /></el-icon>
</template>
</el-input>
</el-form-item>
<el-form-item label="任务描述" prop="description">
<el-input
v-model="newTask.description"
type="textarea"
:rows="3"
placeholder="请输入任务描述(可选)"
:maxlength="500"
show-word-limit
resize="none"
/>
</el-form-item>
<el-form-item>
<el-button
type="primary"
@click="handleCreateTask"
:loading="loading"
:disabled="!newTask.title.trim()"
>
<el-icon class="el-icon--left"><Plus /></el-icon>
添加任务
</el-button>
<el-button @click="resetForm">重置</el-button>
</el-form-item>
</el-form>
</el-card>
<!-- 任务筛选 -->
<div class="filter-section">
<el-radio-group v-model="filterType" @change="handleFilterChange">
<el-radio-button label="all">全部任务</el-radio-button>
<el-radio-button label="active">进行中</el-radio-button>
<el-radio-button label="completed">已完成</el-radio-button>
</el-radio-group>
<div class="task-stats">
<span class="stat-item">
<el-icon><List /></el-icon>
总数: {{ filteredTasks.length }}
</span>
<span class="stat-item">
<el-icon><Check /></el-icon>
已完成: {{ completedCount }}
</span>
<span class="stat-item">
<el-icon><Clock /></el-icon>
进行中: {{ activeCount }}
</span>
</div>
</div>
<!-- 任务列表 -->
<div class="task-list">
<!-- 加载状态 -->
<div v-if="loading && tasks.length === 0" class="loading-container">
<el-icon class="loading-icon"><Loading /></el-icon>
<p>加载任务中...</p>
</div>
<!-- 空状态 -->
<el-empty
v-else-if="filteredTasks.length === 0"
:description="emptyDescription"
:image-size="200"
/>
<!-- 任务卡片 -->
<transition-group name="task-list" tag="div">
<el-card
v-for="task in filteredTasks"
:key="task.id"
class="task-card"
:class="{ 'completed-task': task.completed }"
shadow="hover"
>
<div class="task-content">
<!-- 左侧:复选框和标题 -->
<div class="task-left">
<el-checkbox
v-model="task.completed"
@change="() => handleToggleComplete(task)"
class="task-checkbox"
:label="''"
/>
<div class="task-info">
<div
class="task-title"
:class="{ 'task-title-completed': task.completed }"
@dblclick="() => startEditTask(task)"
>
<span v-if="!task.editing">{{ task.title }}</span>
<el-input
v-else
v-model="task.editTitle"
@blur="() => saveEditTask(task)"
@keyup.enter="() => saveEditTask(task)"
size="small"
autofocus
/>
</div>
<div class="task-description">
<el-icon size="14"><Document /></el-icon>
<span>{{ task.description || "暂无描述" }}</span>
</div>
<div class="task-meta">
<span class="meta-item">
<el-icon size="12"><Calendar /></el-icon>
{{ formatDate(task.created_at) }}
</span>
<span v-if="task.updated_at" class="meta-item">
<el-icon size="12"><Refresh /></el-icon>
更新: {{ formatDate(task.updated_at) }}
</span>
</div>
</div>
</div>
<!-- 右侧:操作按钮 -->
<div class="task-actions">
<el-button
v-if="!task.editing"
type="info"
size="small"
@click="() => startEditTask(task)"
circle
>
<el-icon><Edit /></el-icon>
</el-button>
<el-button
v-if="task.editing"
type="success"
size="small"
@click="() => saveEditTask(task)"
circle
>
<el-icon><Check /></el-icon>
</el-button>
<el-button
type="danger"
size="small"
@click="() => handleDeleteTask(task.id)"
:loading="deletingTaskId === task.id"
circle
>
<el-icon><Delete /></el-icon>
</el-button>
</div>
</div>
</el-card>
</transition-group>
</div>
<!-- 错误提示 -->
<el-alert
v-if="error"
:title="error"
type="error"
show-icon
closable
@close="clearError"
class="error-alert"
/>
</div>
</template>
<script>
import { mapState, mapGetters, mapActions } from "vuex";
import {
Document,
Plus,
List,
Check,
Clock,
Loading,
Calendar,
Refresh,
Edit,
Delete,
} from "@element-plus/icons-vue";
export default {
name: "TaskView",
components: {
Document,
Plus,
List,
Check,
Clock,
Loading,
Calendar,
Refresh,
Edit,
Delete,
},
data() {
return {
newTask: {
title: "",
description: "",
},
filterType: "all",
deletingTaskId: null,
formRules: {
title: [
{ required: true, message: "请输入任务标题", trigger: "blur" },
{ min: 1, max: 100, message: "长度在1到100个字符", trigger: "blur" },
],
},
};
},
computed: {
// 映射Vuex状态和getters
...mapState("task", ["tasks", "loading", "error"]),
...mapGetters("task", ["activeTasks", "completedTasks"]),
// 根据筛选类型返回任务列表
filteredTasks() {
const tasks = this.tasks.map((task) => ({
...task,
editing: false,
editTitle: task.title,
}));
switch (this.filterType) {
case "active":
return tasks.filter((task) => !task.completed);
case "completed":
return tasks.filter((task) => task.completed);
default:
return tasks;
}
},
// 任务统计
completedCount() {
return this.completedTasks.length;
},
activeCount() {
return this.activeTasks.length;
},
// 空状态描述
emptyDescription() {
switch (this.filterType) {
case "active":
return "暂无进行中的任务";
case "completed":
return "暂无已完成的任务";
default:
return "还没有任务,开始创建第一个吧!";
}
},
},
mounted() {
this.loadTasks();
},
methods: {
// 映射Vuex actions
...mapActions("task", [
"fetchTasks",
"createTask",
"updateTask",
"deleteTask",
]),
// 加载任务
async loadTasks() {
try {
await this.fetchTasks();
} catch (error) {
console.error("加载任务失败:", error);
}
},
// 创建任务
async handleCreateTask() {
if (!this.newTask.title.trim()) return;
try {
await this.createTask({
title: this.newTask.title,
description: this.newTask.description || undefined,
completed: false,
});
// 重置表单
this.resetForm();
// 显示成功消息
this.$message({
message: "任务创建成功!",
type: "success",
duration: 2000,
});
} catch (error) {
console.error("创建任务失败:", error);
}
},
// 切换任务完成状态
async handleToggleComplete(task) {
try {
await this.updateTask({
id: task.id,
taskData: {
completed: task.completed,
},
});
this.$message({
message: task.completed ? "任务已完成!" : "任务已标记为进行中",
type: "success",
duration: 1500,
});
} catch (error) {
// 如果更新失败,恢复原来的状态
task.completed = !task.completed;
console.error("更新任务状态失败:", error);
}
},
// 开始编辑任务
startEditTask(task) {
task.editing = true;
task.editTitle = task.title;
},
// 保存编辑的任务
async saveEditTask(task) {
if (!task.editTitle.trim()) {
this.$message.warning("任务标题不能为空");
task.editTitle = task.title;
task.editing = false;
return;
}
if (task.editTitle === task.title) {
task.editing = false;
return;
}
try {
await this.updateTask({
id: task.id,
taskData: {
title: task.editTitle,
description: task.description,
},
});
task.editing = false;
this.$message.success("任务更新成功!");
} catch (error) {
task.editTitle = task.title;
console.error("更新任务失败:", error);
}
},
// 删除任务
async handleDeleteTask(taskId) {
try {
// 确认对话框
await this.$confirm("确定要删除这个任务吗?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
});
this.deletingTaskId = taskId;
await this.deleteTask(taskId);
this.$message({
message: "任务删除成功!",
type: "success",
duration: 2000,
});
} catch (error) {
if (error !== "cancel") {
console.error("删除任务失败:", error);
}
} finally {
this.deletingTaskId = null;
}
},
// 筛选任务
handleFilterChange() {
// 筛选逻辑已在computed属性中处理
},
// 重置表单
resetForm() {
this.newTask = {
title: "",
description: "",
};
if (this.$refs.taskFormRef) {
this.$refs.taskFormRef.resetFields();
}
},
// 清除错误
clearError() {
this.$store.commit("task/SET_ERROR", null);
},
// 格式化日期
formatDate(dateString) {
if (!dateString) return "";
const date = new Date(dateString);
const now = new Date();
const diffTime = Math.abs(now - date);
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
if (diffDays === 0) {
// 今天
return date.toLocaleTimeString("zh-CN", {
hour: "2-digit",
minute: "2-digit",
});
} else if (diffDays === 1) {
// 昨天
return "昨天";
} else if (diffDays < 7) {
// 一周内
return `${diffDays}天前`;
} else {
// 更早
return date.toLocaleDateString("zh-CN", {
month: "short",
day: "numeric",
});
}
},
},
};
</script>
<style scoped>
.task-container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.task-header {
text-align: center;
margin-bottom: 30px;
}
.task-header h1 {
color: #2c3e50;
margin-bottom: 8px;
}
.subtitle {
color: #7f8c8d;
font-size: 16px;
}
.create-card {
margin-bottom: 30px;
}
.card-header {
display: flex;
align-items: center;
justify-content: space-between;
}
.header-title {
font-weight: bold;
color: #2c3e50;
}
.filter-section {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
padding: 15px;
background: #f8f9fa;
border-radius: 8px;
}
.task-stats {
display: flex;
gap: 20px;
}
.stat-item {
display: flex;
align-items: center;
gap: 5px;
color: #666;
font-size: 14px;
}
.task-list {
min-height: 300px;
}
.loading-container {
text-align: center;
padding: 60px 0;
color: #999;
}
.loading-icon {
font-size: 40px;
margin-bottom: 10px;
animation: rotating 2s linear infinite;
}
@keyframes rotating {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.task-card {
margin-bottom: 15px;
transition: all 0.3s ease;
border-left: 4px solid #409eff;
}
.task-card.completed-task {
border-left-color: #67c23a;
opacity: 0.8;
}
.task-card:hover {
transform: translateY(-2px);
box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
}
.task-content {
display: flex;
justify-content: space-between;
align-items: center;
}
.task-left {
display: flex;
align-items: flex-start;
gap: 15px;
flex: 1;
}
.task-checkbox {
margin-top: 3px;
}
.task-info {
flex: 1;
}
.task-title {
font-weight: 600;
color: #2c3e50;
margin-bottom: 8px;
cursor: pointer;
padding: 4px;
border-radius: 4px;
transition: background-color 0.2s;
}
.task-title:hover {
background-color: #f5f5f5;
}
.task-title-completed {
text-decoration: line-through;
color: #999;
}
.task-description {
display: flex;
align-items: center;
gap: 6px;
color: #666;
font-size: 14px;
margin-bottom: 8px;
}
.task-meta {
display: flex;
gap: 15px;
font-size: 12px;
color: #999;
}
.meta-item {
display: flex;
align-items: center;
gap: 4px;
}
.task-actions {
display: flex;
gap: 8px;
}
.error-alert {
margin-top: 20px;
}
/* 列表动画 */
.task-list-move,
.task-list-enter-active,
.task-list-leave-active {
transition: all 0.5s ease;
}
.task-list-enter-from,
.task-list-leave-to {
opacity: 0;
transform: translateX(30px);
}
.task-list-leave-active {
position: absolute;
}
@media (max-width: 768px) {
.task-container {
padding: 10px;
}
.filter-section {
flex-direction: column;
gap: 15px;
align-items: stretch;
}
.task-stats {
justify-content: space-around;
}
.task-content {
flex-direction: column;
align-items: stretch;
gap: 15px;
}
.task-actions {
justify-content: flex-end;
}
}
</style>
|