feat(recycle-bin): 实现回收站功能

- 新增回收站管理相关接口和页面
- 实现笔记和分组的软删除、恢复和永久删除功能
- 添加回收站数据展示和操作界面
- 设计回收站功能的数据库表结构和API接口
This commit is contained in:
2025-08-01 08:36:05 +08:00
parent 0f989d1d6b
commit 7008b3c02d
6 changed files with 324 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
package com.test.bijihoudaun.controller;
import com.test.bijihoudaun.common.response.R;
import com.test.bijihoudaun.entity.TrashItemVo;
import com.test.bijihoudaun.service.TrashService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/trash")
@Tag(name = "回收站管理")
public class TrashController {
@Autowired
private TrashService trashService;
@GetMapping
@Operation(summary = "获取回收站列表")
public R<List<TrashItemVo>> getTrashItems() {
return R.success(trashService.getTrashItems());
}
@PostMapping("/restore/{type}/{id}")
@Operation(summary = "恢复项目")
public R<Void> restoreItem(@PathVariable String type, @PathVariable String id) {
trashService.restoreItem(id, type);
return R.success();
}
@DeleteMapping("/permanently/{type}/{id}")
@Operation(summary = "永久删除项目")
public R<Void> permanentlyDeleteItem(@PathVariable String type, @PathVariable String id) {
trashService.permanentlyDeleteItem(id, type);
return R.success();
}
@DeleteMapping("/clean")
@Operation(summary = "清空回收站")
public R<Void> cleanTrash() {
trashService.cleanTrash();
return R.success();
}
}

View File

@@ -0,0 +1,28 @@
package com.test.bijihoudaun.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.Date;
@Data
@Schema(name = "回收站项目视图对象")
public class TrashItemVo {
@Schema(description = "项目ID")
private String id;
@Schema(description = "项目名称(笔记标题或分组名称)")
private String title;
@Schema(description = "项目类型note 或 group")
private String type;
@Schema(description = "删除时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date deletedAt;
@Schema(description = "删除者ID")
private String deletedBy;
}

View File

@@ -0,0 +1,33 @@
package com.test.bijihoudaun.service;
import com.test.bijihoudaun.entity.TrashItemVo;
import java.util.List;
public interface TrashService {
/**
* 获取回收站中的所有项目
* @return 回收站项目列表
*/
List<TrashItemVo> getTrashItems();
/**
* 恢复指定的回收站项目
* @param id 项目ID
* @param type 项目类型 ("note" 或 "group")
*/
void restoreItem(String id, String type);
/**
* 永久删除指定的回收站项目
* @param id 项目ID
* @param type 项目类型 ("note" 或 "group")
*/
void permanentlyDeleteItem(String id, String type);
/**
* 清空回收站
*/
void cleanTrash();
}

View File

@@ -0,0 +1,90 @@
package com.test.bijihoudaun.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.test.bijihoudaun.entity.Grouping;
import com.test.bijihoudaun.entity.MarkdownFile;
import com.test.bijihoudaun.entity.TrashItemVo;
import com.test.bijihoudaun.mapper.GroupingMapper;
import com.test.bijihoudaun.mapper.MarkdownFileMapper;
import com.test.bijihoudaun.service.TrashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class TrashServiceImpl implements TrashService {
@Autowired
private MarkdownFileMapper markdownFileMapper;
@Autowired
private GroupingMapper groupingMapper;
@Override
public List<TrashItemVo> getTrashItems() {
// 调用自定义的Mapper方法查询已删除的笔记绕过全局逻辑删除过滤器
List<TrashItemVo> deletedNotes = markdownFileMapper.selectDeleted()
.stream()
.map(file -> {
TrashItemVo vo = new TrashItemVo();
vo.setId(String.valueOf(file.getId()));
vo.setTitle(file.getTitle());
vo.setType("note");
vo.setDeletedAt(file.getDeletedAt());
vo.setDeletedBy(String.valueOf(file.getDeletedBy()));
return vo;
})
.collect(Collectors.toList());
// 调用自定义的Mapper方法查询已删除的分组
List<TrashItemVo> deletedGroups = groupingMapper.selectDeleted()
.stream()
.map(group -> {
TrashItemVo vo = new TrashItemVo();
vo.setId(String.valueOf(group.getId()));
vo.setTitle(group.getGrouping());
vo.setType("group");
vo.setDeletedAt(group.getDeletedAt());
vo.setDeletedBy(String.valueOf(group.getDeletedBy()));
return vo;
})
.collect(Collectors.toList());
// 合并并返回
return Stream.concat(deletedNotes.stream(), deletedGroups.stream()).collect(Collectors.toList());
}
@Override
@Transactional
public void restoreItem(String id, String type) {
if ("note".equals(type)) {
markdownFileMapper.restoreById(Long.parseLong(id));
} else if ("group".equals(type)) {
groupingMapper.restoreById(Long.parseLong(id));
}
}
@Override
@Transactional
public void permanentlyDeleteItem(String id, String type) {
if ("note".equals(type)) {
markdownFileMapper.physicalDeleteById(Long.parseLong(id));
} else if ("group".equals(type)) {
// 永久删除分组时,也永久删除其下的所有笔记
groupingMapper.physicalDeleteById(Long.parseLong(id));
markdownFileMapper.physicalDeleteByGroupingId(Long.parseLong(id));
}
}
@Override
@Transactional
public void cleanTrash() {
markdownFileMapper.delete(new QueryWrapper<MarkdownFile>().eq("is_deleted", 1));
groupingMapper.delete(new QueryWrapper<Grouping>().eq("is_deleted", 1));
}
}

View File

@@ -0,0 +1,104 @@
<template>
<el-container>
<el-header>
<h1>回收站</h1>
<el-button type="danger" @click="handleCleanTrash" :disabled="trashItems.length === 0">清空回收站</el-button>
<el-button @click="goBack">返回首页</el-button>
</el-header>
<el-main>
<el-table :data="trashItems" style="width: 100%">
<el-table-column prop="title" label="名称"></el-table-column>
<el-table-column prop="type" label="类型">
<template #default="scope">
<el-tag :type="scope.row.type === 'group' ? 'success' : 'primary'">
{{ scope.row.type === 'group' ? '分类' : '笔记' }}
</el-tag>
</template>
</el-table-column>
<el-table-column prop="deletedAt" label="删除时间"></el-table-column>
<el-table-column label="操作">
<template #default="scope">
<el-button size="small" type="primary" @click="handleRestore(scope.row)">恢复</el-button>
<el-button size="small" type="danger" @click="handleDeletePermanently(scope.row)">永久删除</el-button>
</template>
</el-table-column>
</el-table>
<el-empty v-if="trashItems.length === 0" description="回收站是空的"></el-empty>
</el-main>
</el-container>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getTrash, restoreTrashItem, permanentlyDeleteItem, cleanTrash } from '@/api/CommonApi.js';
const router = useRouter();
const trashItems = ref([]);
const fetchTrashItems = async () => {
try {
const response = await getTrash();
trashItems.value = response.data || [];
} catch (error) {
ElMessage.error('获取回收站内容失败');
}
};
const handleRestore = async (item) => {
try {
await restoreTrashItem(item.id, item.type);
ElMessage.success('恢复成功');
fetchTrashItems();
} catch (error) {
ElMessage.error('恢复失败');
}
};
const handleDeletePermanently = async (item) => {
await ElMessageBox.confirm('确定要永久删除此项目吗?此操作不可恢复。', '警告', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
});
try {
await permanentlyDeleteItem(item.id, item.type);
ElMessage.success('已永久删除');
fetchTrashItems();
} catch (error) {
ElMessage.error('删除失败');
}
};
const handleCleanTrash = async () => {
await ElMessageBox.confirm('确定要清空回收站吗?此操作不可恢复。', '警告', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
});
try {
await cleanTrash();
ElMessage.success('回收站已清空');
fetchTrashItems();
} catch (error) {
ElMessage.error('清空失败');
}
};
const goBack = () => {
router.push('/home');
};
onMounted(() => {
fetchTrashItems();
});
</script>
<style scoped>
.el-header {
display: flex;
justify-content: space-between;
align-items: center;
}
</style>

22
回收站功能设计.md Normal file
View File

@@ -0,0 +1,22 @@
# 回收站功能设计方案
## 功能需求
- 实现笔记和分类的软删除功能
- 提供30天数据保留期
- 支持恢复和永久删除操作
## 前端修改
1. 侧边栏添加回收站入口
2. 删除操作改为"移至回收站"
3. 新建TrashPage.vue组件
## 后端修改
```sql
ALTER TABLE markdown ADD COLUMN is_deleted BOOLEAN DEFAULT false;
ALTER TABLE markdown ADD COLUMN deleted_at TIMESTAMP;
```
## API接口
- GET /api/trash - 获取回收站内容
- POST /api/trash/restore - 恢复项目
- DELETE /api/trash/clean - 清空回收站