diff --git a/pages/notes/detail.vue b/pages/notes/detail.vue index 254444a..5c845b5 100644 --- a/pages/notes/detail.vue +++ b/pages/notes/detail.vue @@ -1,5 +1,24 @@ @@ -97,6 +99,7 @@ export default { return { selectedImages: [], noteForm: { + id: "", // 笔记ID,编辑模式下有值 title: "", content: "", tags: [], // 存储选中的标签对象 {id, name} @@ -107,6 +110,7 @@ export default { height: "200rpx", // 边框高度 width: "200rpx", // 边框宽度 }, + isEditMode: false, // 是否是编辑模式 }; }, computed: { @@ -118,28 +122,39 @@ export default { ); }, }, - mounted() { - this.getTagList(); + onLoad(options) { + // 先获取标签列表 + this.getTagList().then(() => { + // 检查是否有noteId参数,如果有则是编辑模式 + if (options && options.id) { + this.noteForm.id = options.id; + this.isEditMode = true; + this.loadNoteDetail(); + } + }); }, methods: { // 获取标签列表 async getTagList() { - try { - const res = await this.Post({}, "/framework/tag/list", "DES"); - if (res && res.data) { - this.quickTags = res.data; + return new Promise(async (resolve) => { + try { + const res = await this.Post({}, "/framework/tag/list", "DES"); + if (res && res.data) { + this.quickTags = res.data; + } + } catch (error) { + console.error("获取标签列表失败:", error); + // 如果接口失败,可以设置默认标签 + this.quickTags = [ + { id: "1", name: "DES" }, + { id: "2", name: "AGENT" }, + { id: "3", name: "时间银行" }, + { id: "4", name: "阅读体验" }, + { id: "5", name: "时间力" }, + ]; } - } catch (error) { - console.error("获取标签列表失败:", error); - // 如果接口失败,可以设置默认标签 - this.quickTags = [ - { id: "1", name: "DES" }, - { id: "2", name: "AGENT" }, - { id: "3", name: "时间银行" }, - { id: "4", name: "阅读体验" }, - { id: "5", name: "时间力" }, - ]; - } + resolve(); + }); }, // 返回上一页 goBack() { @@ -270,7 +285,7 @@ export default { } }, - // 发布笔记 + // 发布或更新笔记 async publishNote() { console.log(this.noteForm, "0000"); if (!this.noteForm.images || this.noteForm.images.length === 0) { @@ -289,13 +304,14 @@ export default { } try { - uni.showLoading({ title: "发布中..." }); + // 根据模式显示不同的加载提示 + uni.showLoading({ title: this.isEditMode ? "保存中..." : "发布中..." }); // 图片已经在选择时上传,直接提交表单 await this.submitNote(this.noteForm); uni.hideLoading(); uni.showToast({ - title: "发布成功", + title: this.isEditMode ? "修改成功" : "发布成功", icon: "none", duration: 2000, }); @@ -307,9 +323,98 @@ export default { } catch (error) { uni.hideLoading(); uni.showToast({ - title: error.message || "发布失败,请重试", + title: + error.message || + (this.isEditMode ? "修改失败,请重试" : "发布失败,请重试"), + icon: "none", + }); + } + }, + + // 加载笔记详情 + async loadNoteDetail() { + try { + uni.showLoading({ title: "加载中..." }); + const res = await this.Post( + { noteId: this.noteForm.id }, + "/framework/note/getInfo/" + this.noteForm.id, + "DES" + ); + + if (res.code === 200 && res.data) { + const noteData = res.data; + + // 填充表单数据 + this.noteForm.title = noteData.title || ""; + this.noteForm.content = noteData.content || ""; + + // 处理图片 + if (noteData.coverImage) { + const imageUrls = noteData.coverImage.split(","); + this.noteForm.images = imageUrls; + + // 为uni-file-picker准备数据 + this.selectedImages = imageUrls.map((url) => ({ + url: url, + extname: url.split(".").pop(), + name: url.split("/").pop(), + })); + } + + // 处理标签 + if (noteData.tagNames && noteData.tagIds) { + // 处理同时有tagNames和tagIds的情况 + const tagNames = noteData.tagNames.split(","); + const tagIds = noteData.tagIds.split(","); + + this.noteForm.tags = tagIds.map((id, index) => ({ + id: id, + name: tagNames[index] || "", + })); + } else if (noteData.tags) { + // 处理只有tags字段的情况(格式为"3,4"这样的ids集合) + const tagIds = noteData.tags.split(","); + + // 从quickTags中查找匹配的标签名称 + this.noteForm.tags = tagIds.map((id) => { + const matchedTag = this.quickTags.find((tag) => tag.id == id); + if (matchedTag) { + return { + id: id, + name: matchedTag.name, + }; + } else { + console.warn(`未找到ID为${id}的标签,请确保标签列表已正确加载`); + return { + id: id, + name: `标签${id}`, // 提供更友好的默认名称 + }; + } + }); + + // 如果没有找到任何匹配的标签,记录日志 + if ( + this.noteForm.tags.some( + (tag) => !this.quickTags.find((qt) => qt.id === tag.id) + ) + ) { + console.warn("部分标签未在标签列表中找到,可能需要刷新标签列表"); + } + } + } else { + uni.showToast({ + title: res.msg || "获取笔记详情失败", + icon: "none", + }); + } + } catch (error) { + console.error("加载笔记详情失败:", error); + uni.showToast({ + title: "获取笔记详情失败", icon: "none", }); + } finally { + uni.hideLoading(); } }, @@ -324,7 +429,15 @@ export default { method: "POST", }; - return this.Post(formData, "/framework/note/addNote", "DES"); + // 如果是编辑模式,添加笔记ID + if (this.isEditMode && this.noteForm.id) { + formData.id = this.noteForm.id; + formData.method = "PUT"; + + return this.Post(formData, "/framework/note/editNote", "DES"); + } else { + return this.Post(formData, "/framework/note/addNote", "DES"); + } }, }, };