实现记事本功能
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import request from '@/utils/request'
|
||||
|
||||
/**
|
||||
* 获取笔记列表
|
||||
* @param {Object} params - 查询参数
|
||||
* @param {number} params.page - 页码
|
||||
* @param {number} params.pageSize - 每页数量
|
||||
* @param {string} params.keyword - 搜索关键词
|
||||
*/
|
||||
export function getNotebookList(params) {
|
||||
return request({
|
||||
url: '/platform/notebook/list',
|
||||
method: 'get',
|
||||
params
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取笔记详情
|
||||
* @param {number} id - 笔记ID
|
||||
*/
|
||||
export function getNotebookDetail(id) {
|
||||
return request({
|
||||
url: `/platform/notebook/detail/${id}`,
|
||||
method: 'get'
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建笔记
|
||||
* @param {Object} data - 笔记数据
|
||||
* @param {string} data.title - 笔记标题
|
||||
* @param {string} data.content - 笔记内容
|
||||
*/
|
||||
export function createNotebook(data) {
|
||||
return request({
|
||||
url: '/platform/notebook/create',
|
||||
method: 'post',
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新笔记
|
||||
* @param {number} id - 笔记ID
|
||||
* @param {Object} data - 笔记数据
|
||||
* @param {string} data.title - 笔记标题
|
||||
* @param {string} data.content - 笔记内容
|
||||
*/
|
||||
export function updateNotebook(id, data) {
|
||||
return request({
|
||||
url: `/platform/notebook/update/${id}`,
|
||||
method: 'post',
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除笔记
|
||||
* @param {number} id - 笔记ID
|
||||
*/
|
||||
export function deleteNotebook(id) {
|
||||
return request({
|
||||
url: `/platform/notebook/delete/${id}`,
|
||||
method: 'delete'
|
||||
})
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
<template>
|
||||
<div class="note-editor-container">
|
||||
<div class="editor-header">
|
||||
<el-input
|
||||
v-model="noteTitle"
|
||||
placeholder="请输入标题..."
|
||||
class="title-input"
|
||||
@blur="handleTitleChange"
|
||||
/>
|
||||
<div class="editor-actions">
|
||||
<el-button type="primary" @click="handleSave">
|
||||
<el-icon><DocumentChecked /></el-icon>
|
||||
保存
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="editor-body">
|
||||
<WangEditor v-model="noteContent" />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, watch, onMounted } from 'vue';
|
||||
import { DocumentChecked } from '@element-plus/icons-vue';
|
||||
import WangEditor from './WangEditor.vue';
|
||||
|
||||
const props = defineProps({
|
||||
noteId: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
});
|
||||
|
||||
const emit = defineEmits(['save', 'update-title']);
|
||||
|
||||
const noteTitle = ref('');
|
||||
const noteContent = ref('');
|
||||
|
||||
// 模拟从父组件或 API 加载笔记数据
|
||||
const loadNote = () => {
|
||||
// 实际使用时,这里应该根据 props.noteId 从 API 加载数据
|
||||
// 现在只是演示,从父组件的 notes 数组中获取
|
||||
noteTitle.value = '新建笔记';
|
||||
noteContent.value = '';
|
||||
};
|
||||
|
||||
// 保存笔记
|
||||
const handleSave = () => {
|
||||
emit('save', {
|
||||
id: props.noteId,
|
||||
title: noteTitle.value,
|
||||
content: noteContent.value,
|
||||
});
|
||||
};
|
||||
|
||||
// 标题变更
|
||||
const handleTitleChange = () => {
|
||||
emit('update-title', {
|
||||
id: props.noteId,
|
||||
title: noteTitle.value,
|
||||
});
|
||||
};
|
||||
|
||||
// 监听笔记 ID 变化
|
||||
watch(
|
||||
() => props.noteId,
|
||||
() => {
|
||||
loadNote();
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
|
||||
// 自动保存(可选)
|
||||
let autoSaveTimer = null;
|
||||
watch([noteTitle, noteContent], () => {
|
||||
if (autoSaveTimer) {
|
||||
clearTimeout(autoSaveTimer);
|
||||
}
|
||||
|
||||
autoSaveTimer = setTimeout(() => {
|
||||
// 自动保存逻辑(可选)
|
||||
// handleSave();
|
||||
}, 3000);
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
loadNote();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.note-editor-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
background: var(--el-bg-color);
|
||||
}
|
||||
|
||||
.editor-header {
|
||||
padding: 16px 20px;
|
||||
border-bottom: 1px solid var(--el-border-color-lighter);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
background: var(--el-fill-color-light);
|
||||
|
||||
.title-input {
|
||||
flex: 1;
|
||||
|
||||
:deep(.el-input__wrapper) {
|
||||
background: var(--el-bg-color);
|
||||
box-shadow: 0 0 0 1px var(--el-border-color) inset;
|
||||
padding: 8px 12px;
|
||||
|
||||
&:hover {
|
||||
box-shadow: 0 0 0 1px var(--el-border-color-hover) inset;
|
||||
}
|
||||
|
||||
&.is-focus {
|
||||
box-shadow: 0 0 0 1px var(--el-color-primary) inset;
|
||||
}
|
||||
}
|
||||
|
||||
:deep(.el-input__inner) {
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
color: var(--el-text-color-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.editor-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
}
|
||||
|
||||
.editor-body {
|
||||
flex: 1;
|
||||
padding: 20px;
|
||||
overflow: hidden;
|
||||
|
||||
:deep(.wang-editor-wrapper) {
|
||||
height: 100%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,247 @@
|
||||
# 记事本模块使用说明
|
||||
|
||||
## 功能概述
|
||||
|
||||
这是一个完整的记事本应用模块,支持富文本编辑、笔记管理等功能。
|
||||
|
||||
## 目录结构
|
||||
|
||||
```
|
||||
notebook/
|
||||
├── index.vue # 主页面(列表+编辑器)
|
||||
├── components/
|
||||
│ ├── edit.vue # 编辑器组件
|
||||
│ └── WangEditor.vue # WangEditor富文本编辑器封装
|
||||
└── README.md # 说明文档
|
||||
```
|
||||
|
||||
## 数据库
|
||||
|
||||
### 表名
|
||||
`yz_platform_notebook`
|
||||
|
||||
### 表结构
|
||||
- `id` - 主键ID
|
||||
- `title` - 笔记标题
|
||||
- `content` - 笔记内容(HTML格式)
|
||||
- `user_id` - 创建用户ID
|
||||
- `user_name` - 创建用户名
|
||||
- `is_deleted` - 是否删除(0-否 1-是)
|
||||
- `create_time` - 创建时间
|
||||
- `update_time` - 更新时间
|
||||
- `delete_time` - 删除时间
|
||||
|
||||
### SQL文件位置
|
||||
`sql/yz_platform_notebook.sql`
|
||||
|
||||
## 后端接口
|
||||
|
||||
### 模型文件
|
||||
`go/models/platform_notebook.go`
|
||||
|
||||
### 控制器文件
|
||||
`go/controllers/platform_notebook.go`
|
||||
|
||||
### API端点
|
||||
|
||||
1. **获取笔记列表**
|
||||
- URL: `GET /platform/notebook/list`
|
||||
- 参数:
|
||||
- `page`: 页码(默认1)
|
||||
- `pageSize`: 每页数量(默认20,最大100)
|
||||
- `keyword`: 搜索关键词(可选)
|
||||
- 返回: 笔记列表和总数
|
||||
|
||||
2. **获取笔记详情**
|
||||
- URL: `GET /platform/notebook/detail/:id`
|
||||
- 参数: `id` - 笔记ID
|
||||
- 返回: 笔记详细信息
|
||||
|
||||
3. **创建笔记**
|
||||
- URL: `POST /platform/notebook/create`
|
||||
- 参数:
|
||||
```json
|
||||
{
|
||||
"title": "笔记标题",
|
||||
"content": "<p>笔记内容</p>"
|
||||
}
|
||||
```
|
||||
- 返回: 创建的笔记信息
|
||||
|
||||
4. **更新笔记**
|
||||
- URL: `POST /platform/notebook/update/:id`
|
||||
- 参数:
|
||||
```json
|
||||
{
|
||||
"title": "更新的标题",
|
||||
"content": "<p>更新的内容</p>"
|
||||
}
|
||||
```
|
||||
- 返回: 更新后的笔记信息
|
||||
|
||||
5. **删除笔记**
|
||||
- URL: `DELETE /platform/notebook/delete/:id`
|
||||
- 参数: `id` - 笔记ID
|
||||
- 返回: 删除结果
|
||||
|
||||
## 前端API
|
||||
|
||||
### API文件
|
||||
`platform/src/api/notebook.js`
|
||||
|
||||
### 方法说明
|
||||
|
||||
```javascript
|
||||
// 获取笔记列表
|
||||
getNotebookList({ page, pageSize, keyword })
|
||||
|
||||
// 获取笔记详情
|
||||
getNotebookDetail(id)
|
||||
|
||||
// 创建笔记
|
||||
createNotebook({ title, content })
|
||||
|
||||
// 更新笔记
|
||||
updateNotebook(id, { title, content })
|
||||
|
||||
// 删除笔记
|
||||
deleteNotebook(id)
|
||||
```
|
||||
|
||||
## 使用步骤
|
||||
|
||||
### 1. 初始化数据库
|
||||
```bash
|
||||
# 在MySQL中执行SQL文件
|
||||
mysql -u用户名 -p数据库名 < sql/yz_platform_notebook.sql
|
||||
```
|
||||
|
||||
### 2. 启动后端服务
|
||||
后端已自动注册模型和路由,直接启动即可:
|
||||
```bash
|
||||
cd go
|
||||
go run main.go
|
||||
```
|
||||
|
||||
### 3. 访问前端
|
||||
在浏览器中访问笔记本页面(路由需要在菜单中配置)
|
||||
|
||||
## 功能特性
|
||||
|
||||
### 列表功能
|
||||
- ✅ 显示所有笔记
|
||||
- ✅ 搜索笔记(按标题)
|
||||
- ✅ 创建新笔记
|
||||
- ✅ 删除笔记
|
||||
- ✅ 查看笔记预览
|
||||
- ✅ 显示更新时间
|
||||
|
||||
### 编辑器功能
|
||||
- ✅ 富文本编辑(基于WangEditor)
|
||||
- ✅ 标题编辑
|
||||
- ✅ 内容编辑
|
||||
- ✅ 保存笔记
|
||||
- ✅ 自动识别新建/编辑模式
|
||||
- ✅ 加载状态显示
|
||||
|
||||
### WangEditor支持的功能
|
||||
- 文本样式(加粗、斜体、下划线等)
|
||||
- 标题(H1-H6)
|
||||
- 引用
|
||||
- 代码块
|
||||
- 有序/无序列表
|
||||
- 表格
|
||||
- 链接
|
||||
- 图片上传(需配置上传接口)
|
||||
- 视频嵌入
|
||||
|
||||
## 权限说明
|
||||
|
||||
- 所有接口都需要平台用户登录(JWT Token验证)
|
||||
- 每个用户只能查看、编辑、删除自己创建的笔记
|
||||
- 删除操作为软删除,数据仍保留在数据库中
|
||||
|
||||
## 扩展功能(可选)
|
||||
|
||||
如需添加以下功能,可以扩展:
|
||||
|
||||
1. **笔记分类**
|
||||
- 添加分类表和分类字段
|
||||
- 支持笔记分类管理
|
||||
|
||||
2. **笔记标签**
|
||||
- 添加标签表和关联表
|
||||
- 支持多标签筛选
|
||||
|
||||
3. **笔记分享**
|
||||
- 添加分享链接生成功能
|
||||
- 支持公开/私密设置
|
||||
|
||||
4. **笔记导出**
|
||||
- 支持导出为PDF
|
||||
- 支持导出为Markdown
|
||||
|
||||
5. **版本历史**
|
||||
- 记录笔记的修改历史
|
||||
- 支持版本回退
|
||||
|
||||
6. **协作编辑**
|
||||
- 支持多人协作编辑
|
||||
- 实时同步功能
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 图片上传需要配置文件上传接口
|
||||
2. 富文本内容存储为HTML格式,注意XSS防护
|
||||
3. 数据库content字段使用longtext类型,支持大容量内容
|
||||
4. 建议定期清理软删除的数据
|
||||
5. 生产环境建议添加内容审核机制
|
||||
|
||||
## 技术栈
|
||||
|
||||
- **前端**: Vue 3 + Element Plus + WangEditor
|
||||
- **后端**: Go + Beego + MySQL
|
||||
- **编辑器**: WangEditor 5.x
|
||||
|
||||
## 开发调试
|
||||
|
||||
### 前端调试
|
||||
```bash
|
||||
cd platform
|
||||
npm run dev
|
||||
```
|
||||
|
||||
### 后端调试
|
||||
```bash
|
||||
cd go
|
||||
go run main.go
|
||||
```
|
||||
|
||||
### 查看API请求
|
||||
打开浏览器开发者工具 -> Network 标签页,查看API请求和响应
|
||||
|
||||
## 问题排查
|
||||
|
||||
1. **笔记列表为空**
|
||||
- 检查数据库表是否创建成功
|
||||
- 检查用户是否已登录
|
||||
- 查看浏览器Console是否有错误
|
||||
|
||||
2. **保存失败**
|
||||
- 检查JWT Token是否有效
|
||||
- 检查标题是否为空
|
||||
- 查看后端日志
|
||||
|
||||
3. **编辑器显示异常**
|
||||
- 检查WangEditor是否正确安装
|
||||
- 查看浏览器Console错误信息
|
||||
- 检查CSS样式是否正确加载
|
||||
|
||||
## 更新日志
|
||||
|
||||
### v1.0.0 (2024)
|
||||
- ✅ 完成基础功能
|
||||
- ✅ 支持笔记CRUD操作
|
||||
- ✅ 集成WangEditor富文本编辑器
|
||||
- ✅ 响应式设计支持
|
||||
- ✅ 暗色模式支持
|
||||
@@ -0,0 +1,188 @@
|
||||
<template>
|
||||
<div class="note-editor-container">
|
||||
<div class="editor-header">
|
||||
<el-input
|
||||
v-model="noteTitle"
|
||||
placeholder="请输入标题..."
|
||||
class="title-input"
|
||||
:disabled="loading"
|
||||
/>
|
||||
<div class="editor-actions">
|
||||
<el-button type="primary" :loading="loading" @click="handleSave">
|
||||
<el-icon><DocumentChecked /></el-icon>
|
||||
{{ isNew ? '创建' : '保存' }}
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="editor-body">
|
||||
<WangEditor v-model="noteContent" />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, watch, onMounted } from 'vue';
|
||||
import { ElMessage } from 'element-plus';
|
||||
import { DocumentChecked } from '@element-plus/icons-vue';
|
||||
import WangEditor from './WangEditor.vue';
|
||||
import { getNotebookDetail, createNotebook, updateNotebook } from '@/api/notebook';
|
||||
|
||||
const props = defineProps({
|
||||
noteId: {
|
||||
type: [Number, String],
|
||||
required: true,
|
||||
},
|
||||
});
|
||||
|
||||
const emit = defineEmits(['save-success', 'create-success']);
|
||||
|
||||
const noteTitle = ref('');
|
||||
const noteContent = ref('');
|
||||
const loading = ref(false);
|
||||
const isNew = ref(false);
|
||||
|
||||
// 加载笔记数据
|
||||
const loadNote = async () => {
|
||||
if (props.noteId === 'new') {
|
||||
isNew.value = true;
|
||||
noteTitle.value = '新建笔记';
|
||||
noteContent.value = '';
|
||||
return;
|
||||
}
|
||||
|
||||
isNew.value = false;
|
||||
loading.value = true;
|
||||
|
||||
try {
|
||||
const response = await getNotebookDetail(props.noteId);
|
||||
if (response.code === 200) {
|
||||
noteTitle.value = response.data.title || '无标题';
|
||||
noteContent.value = response.data.content || '';
|
||||
} else {
|
||||
ElMessage.error('加载笔记失败');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载笔记失败:', error);
|
||||
ElMessage.error('加载笔记失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 保存笔记
|
||||
const handleSave = async () => {
|
||||
if (!noteTitle.value.trim()) {
|
||||
ElMessage.warning('请输入标题');
|
||||
return;
|
||||
}
|
||||
|
||||
loading.value = true;
|
||||
|
||||
try {
|
||||
if (isNew.value) {
|
||||
// 创建新笔记
|
||||
const response = await createNotebook({
|
||||
title: noteTitle.value,
|
||||
content: noteContent.value,
|
||||
});
|
||||
|
||||
if (response.code === 200) {
|
||||
ElMessage.success('创建成功');
|
||||
isNew.value = false;
|
||||
emit('create-success', response.data.id);
|
||||
} else {
|
||||
ElMessage.error(response.msg || '创建失败');
|
||||
}
|
||||
} else {
|
||||
// 更新现有笔记
|
||||
const response = await updateNotebook(props.noteId, {
|
||||
title: noteTitle.value,
|
||||
content: noteContent.value,
|
||||
});
|
||||
|
||||
if (response.code === 200) {
|
||||
ElMessage.success('保存成功');
|
||||
emit('save-success');
|
||||
} else {
|
||||
ElMessage.error(response.msg || '保存失败');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('保存失败:', error);
|
||||
ElMessage.error('保存失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 监听笔记 ID 变化
|
||||
watch(
|
||||
() => props.noteId,
|
||||
() => {
|
||||
loadNote();
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
|
||||
onMounted(() => {
|
||||
loadNote();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.note-editor-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
background: var(--el-bg-color);
|
||||
}
|
||||
|
||||
.editor-header {
|
||||
padding: 16px 20px;
|
||||
border-bottom: 1px solid var(--el-border-color-lighter);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
background: var(--el-fill-color-light);
|
||||
|
||||
.title-input {
|
||||
flex: 1;
|
||||
|
||||
:deep(.el-input__wrapper) {
|
||||
background: var(--el-bg-color);
|
||||
box-shadow: 0 0 0 1px var(--el-border-color) inset;
|
||||
padding: 8px 12px;
|
||||
|
||||
&:hover {
|
||||
box-shadow: 0 0 0 1px var(--el-border-color-hover) inset;
|
||||
}
|
||||
|
||||
&.is-focus {
|
||||
box-shadow: 0 0 0 1px var(--el-color-primary) inset;
|
||||
}
|
||||
}
|
||||
|
||||
:deep(.el-input__inner) {
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
color: var(--el-text-color-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.editor-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
}
|
||||
|
||||
.editor-body {
|
||||
flex: 1;
|
||||
padding: 20px;
|
||||
overflow: hidden;
|
||||
|
||||
:deep(.wang-editor-wrapper) {
|
||||
height: 100%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
+64
-66
@@ -40,7 +40,7 @@
|
||||
</el-dropdown>
|
||||
</div>
|
||||
<div class="note-preview">{{ getPreviewText(note.content) }}</div>
|
||||
<div class="note-time">{{ formatTime(note.updated_at) }}</div>
|
||||
<div class="note-time">{{ formatTime(note.update_time || note.create_time) }}</div>
|
||||
</div>
|
||||
|
||||
<el-empty v-if="filteredNotes.length === 0" description="暂无笔记" />
|
||||
@@ -50,9 +50,10 @@
|
||||
<div class="notebook-editor">
|
||||
<NoteEditor
|
||||
v-if="currentNoteId"
|
||||
:key="currentNoteId"
|
||||
:note-id="currentNoteId"
|
||||
@save="handleSave"
|
||||
@update-title="handleUpdateTitle"
|
||||
@save-success="handleSaveSuccess"
|
||||
@create-success="handleCreateSuccess"
|
||||
/>
|
||||
<div v-else class="empty-editor">
|
||||
<el-empty description="请选择或创建一个笔记" />
|
||||
@@ -66,33 +67,17 @@ import { ref, computed, onMounted } from 'vue';
|
||||
import { ElMessage, ElMessageBox } from 'element-plus';
|
||||
import { Plus, Search, Delete, MoreFilled } from '@element-plus/icons-vue';
|
||||
import NoteEditor from './components/edit.vue';
|
||||
import { getNotebookList, deleteNotebook } from '@/api/notebook';
|
||||
|
||||
const searchKeyword = ref('');
|
||||
const currentNoteId = ref(null);
|
||||
const notes = ref([]);
|
||||
|
||||
// 模拟数据 - 实际使用时替换为 API 调用
|
||||
const mockNotes = [
|
||||
{
|
||||
id: 1,
|
||||
title: '欢迎使用笔记本',
|
||||
content: '<p>这是一个功能丰富的笔记应用</p><p>支持富文本编辑、图片上传等功能</p>',
|
||||
created_at: new Date().toISOString(),
|
||||
updated_at: new Date().toISOString(),
|
||||
},
|
||||
];
|
||||
const loading = ref(false);
|
||||
const total = ref(0);
|
||||
|
||||
// 过滤后的笔记列表
|
||||
const filteredNotes = computed(() => {
|
||||
if (!searchKeyword.value) return notes.value;
|
||||
|
||||
const keyword = searchKeyword.value.toLowerCase();
|
||||
return notes.value.filter(note => {
|
||||
return (
|
||||
note.title?.toLowerCase().includes(keyword) ||
|
||||
getPreviewText(note.content).toLowerCase().includes(keyword)
|
||||
);
|
||||
});
|
||||
return notes.value;
|
||||
});
|
||||
|
||||
// 获取预览文本
|
||||
@@ -131,19 +116,41 @@ const formatTime = (dateStr) => {
|
||||
}
|
||||
};
|
||||
|
||||
// 加载笔记列表
|
||||
const loadNotes = async () => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const response = await getNotebookList({
|
||||
page: 1,
|
||||
pageSize: 100,
|
||||
keyword: searchKeyword.value,
|
||||
});
|
||||
|
||||
if (response.code === 200) {
|
||||
notes.value = response.data.list || [];
|
||||
total.value = response.data.total || 0;
|
||||
|
||||
// 如果有笔记且没有选中任何笔记,默认选中第一个
|
||||
if (notes.value.length > 0 && !currentNoteId.value) {
|
||||
currentNoteId.value = notes.value[0].id;
|
||||
}
|
||||
|
||||
// 如果当前选中的笔记已被删除,选中第一个
|
||||
if (currentNoteId.value && !notes.value.find(n => n.id === currentNoteId.value)) {
|
||||
currentNoteId.value = notes.value[0]?.id || null;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载笔记失败:', error);
|
||||
ElMessage.error('加载笔记失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 创建新笔记
|
||||
const handleCreate = () => {
|
||||
const newNote = {
|
||||
id: Date.now(),
|
||||
title: '新建笔记',
|
||||
content: '',
|
||||
created_at: new Date().toISOString(),
|
||||
updated_at: new Date().toISOString(),
|
||||
};
|
||||
|
||||
notes.value.unshift(newNote);
|
||||
currentNoteId.value = newNote.id;
|
||||
ElMessage.success('创建成功');
|
||||
currentNoteId.value = 'new';
|
||||
};
|
||||
|
||||
// 选择笔记
|
||||
@@ -153,7 +160,7 @@ const handleSelectNote = (note) => {
|
||||
|
||||
// 搜索笔记
|
||||
const handleSearch = () => {
|
||||
// 搜索逻辑已在 computed 中处理
|
||||
loadNotes();
|
||||
};
|
||||
|
||||
// 笔记操作
|
||||
@@ -166,47 +173,38 @@ const handleNoteAction = async (command, note) => {
|
||||
type: 'warning',
|
||||
});
|
||||
|
||||
const index = notes.value.findIndex(n => n.id === note.id);
|
||||
if (index > -1) {
|
||||
notes.value.splice(index, 1);
|
||||
|
||||
if (currentNoteId.value === note.id) {
|
||||
currentNoteId.value = notes.value[0]?.id || null;
|
||||
}
|
||||
|
||||
ElMessage.success('删除成功');
|
||||
await deleteNotebook(note.id);
|
||||
ElMessage.success('删除成功');
|
||||
|
||||
// 重新加载列表
|
||||
await loadNotes();
|
||||
|
||||
// 如果删除的是当前笔记,切换到第一个
|
||||
if (currentNoteId.value === note.id) {
|
||||
currentNoteId.value = notes.value[0]?.id || null;
|
||||
}
|
||||
} catch (error) {
|
||||
if (error !== 'cancel') {
|
||||
console.error('删除失败:', error);
|
||||
ElMessage.error('删除失败');
|
||||
}
|
||||
} catch {
|
||||
// 用户取消删除
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 保存笔记
|
||||
const handleSave = ({ id, content }) => {
|
||||
const note = notes.value.find(n => n.id === id);
|
||||
if (note) {
|
||||
note.content = content;
|
||||
note.updated_at = new Date().toISOString();
|
||||
ElMessage.success('保存成功');
|
||||
}
|
||||
// 保存成功后刷新列表
|
||||
const handleSaveSuccess = async () => {
|
||||
await loadNotes();
|
||||
};
|
||||
|
||||
// 更新标题
|
||||
const handleUpdateTitle = ({ id, title }) => {
|
||||
const note = notes.value.find(n => n.id === id);
|
||||
if (note) {
|
||||
note.title = title;
|
||||
note.updated_at = new Date().toISOString();
|
||||
}
|
||||
// 创建成功后的回调
|
||||
const handleCreateSuccess = async (noteId) => {
|
||||
currentNoteId.value = noteId;
|
||||
await loadNotes();
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
// 初始化数据 - 实际使用时从 API 加载
|
||||
notes.value = [...mockNotes];
|
||||
if (notes.value.length > 0) {
|
||||
currentNoteId.value = notes.value[0].id;
|
||||
}
|
||||
loadNotes();
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* 记事本模块 API 测试脚本
|
||||
* 在浏览器控制台中运行此脚本测试 API
|
||||
*/
|
||||
|
||||
// 测试配置
|
||||
const API_BASE = '/platform/notebook';
|
||||
|
||||
// 辅助函数:发送请求
|
||||
async function request(url, options = {}) {
|
||||
const token = localStorage.getItem('token');
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': token ? `Bearer ${token}` : '',
|
||||
...options.headers,
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(url, { ...options, headers });
|
||||
const data = await response.json();
|
||||
console.log(`✅ ${options.method || 'GET'} ${url}`, data);
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error(`❌ ${options.method || 'GET'} ${url}`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 测试函数
|
||||
const NotebookTest = {
|
||||
// 1. 创建笔记
|
||||
async create() {
|
||||
return await request(`${API_BASE}/create`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
title: '测试笔记 - ' + new Date().toLocaleString(),
|
||||
content: '<p>这是一条测试笔记</p><p>内容包含<strong>富文本</strong>格式</p>',
|
||||
}),
|
||||
});
|
||||
},
|
||||
|
||||
// 2. 获取笔记列表
|
||||
async list(params = {}) {
|
||||
const query = new URLSearchParams({
|
||||
page: params.page || 1,
|
||||
pageSize: params.pageSize || 20,
|
||||
keyword: params.keyword || '',
|
||||
}).toString();
|
||||
return await request(`${API_BASE}/list?${query}`);
|
||||
},
|
||||
|
||||
// 3. 获取笔记详情
|
||||
async detail(id) {
|
||||
return await request(`${API_BASE}/detail/${id}`);
|
||||
},
|
||||
|
||||
// 4. 更新笔记
|
||||
async update(id, data) {
|
||||
return await request(`${API_BASE}/update/${id}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
},
|
||||
|
||||
// 5. 删除笔记
|
||||
async delete(id) {
|
||||
return await request(`${API_BASE}/delete/${id}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
},
|
||||
|
||||
// 完整测试流程
|
||||
async runFullTest() {
|
||||
console.log('🚀 开始测试记事本模块 API...\n');
|
||||
|
||||
try {
|
||||
// 1. 创建笔记
|
||||
console.log('📝 测试1: 创建笔记');
|
||||
const createResult = await this.create();
|
||||
if (createResult.code !== 200) {
|
||||
throw new Error('创建笔记失败');
|
||||
}
|
||||
const noteId = createResult.data.id;
|
||||
console.log(`✅ 创建成功,笔记ID: ${noteId}\n`);
|
||||
|
||||
// 2. 获取笔记列表
|
||||
console.log('📋 测试2: 获取笔记列表');
|
||||
const listResult = await this.list();
|
||||
if (listResult.code !== 200) {
|
||||
throw new Error('获取列表失败');
|
||||
}
|
||||
console.log(`✅ 获取成功,共 ${listResult.data.total} 条笔记\n`);
|
||||
|
||||
// 3. 获取笔记详情
|
||||
console.log('🔍 测试3: 获取笔记详情');
|
||||
const detailResult = await this.detail(noteId);
|
||||
if (detailResult.code !== 200) {
|
||||
throw new Error('获取详情失败');
|
||||
}
|
||||
console.log(`✅ 获取成功,标题: ${detailResult.data.title}\n`);
|
||||
|
||||
// 4. 更新笔记
|
||||
console.log('✏️ 测试4: 更新笔记');
|
||||
const updateResult = await this.update(noteId, {
|
||||
title: '更新后的标题 - ' + new Date().toLocaleString(),
|
||||
content: '<p>这是更新后的内容</p>',
|
||||
});
|
||||
if (updateResult.code !== 200) {
|
||||
throw new Error('更新笔记失败');
|
||||
}
|
||||
console.log(`✅ 更新成功\n`);
|
||||
|
||||
// 5. 搜索笔记
|
||||
console.log('🔎 测试5: 搜索笔记');
|
||||
const searchResult = await this.list({ keyword: '更新' });
|
||||
if (searchResult.code !== 200) {
|
||||
throw new Error('搜索失败');
|
||||
}
|
||||
console.log(`✅ 搜索成功,找到 ${searchResult.data.total} 条匹配笔记\n`);
|
||||
|
||||
// 6. 删除笔记
|
||||
console.log('🗑️ 测试6: 删除笔记');
|
||||
const deleteResult = await this.delete(noteId);
|
||||
if (deleteResult.code !== 200) {
|
||||
throw new Error('删除笔记失败');
|
||||
}
|
||||
console.log(`✅ 删除成功\n`);
|
||||
|
||||
console.log('🎉 所有测试通过!');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('❌ 测试失败:', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// 导出到全局
|
||||
window.NotebookTest = NotebookTest;
|
||||
|
||||
console.log('📚 记事本模块测试工具已加载');
|
||||
console.log('使用方法:');
|
||||
console.log(' NotebookTest.create() - 创建笔记');
|
||||
console.log(' NotebookTest.list() - 获取列表');
|
||||
console.log(' NotebookTest.detail(id) - 获取详情');
|
||||
console.log(' NotebookTest.update(id, data) - 更新笔记');
|
||||
console.log(' NotebookTest.delete(id) - 删除笔记');
|
||||
console.log(' NotebookTest.runFullTest() - 运行完整测试');
|
||||
Reference in New Issue
Block a user