diff --git a/pages/message/common-phrases.vue b/pages/message/common-phrases.vue index cae8a9a..f917d00 100644 --- a/pages/message/common-phrases.vue +++ b/pages/message/common-phrases.vue @@ -220,59 +220,63 @@ const editPhrase = (phrase) => { const savePhrase = async () => { if (!phraseForm.value.content.trim()) { uni.showToast({ - title: "请输入内容", - icon: "none", + title: '请输入内容', + icon: 'none' }); return; } try { - if (editingPhrase.value) { - // 更新 - await api("saveCommonPhrase", { - id: editingPhrase.value.id, - categoryId: editingPhrase.value.categoryId, - content: phraseForm.value.content, - corpId: uni.getStorageSync("corpId"), + const corpId = uni.getStorageSync('corpId'); + const userId = uni.getStorageSync('userId'); + + if (!corpId || !userId) { + uni.showToast({ + title: '请先登录', + icon: 'none' }); - - const index = phrases.value.findIndex( - (p) => p.id === editingPhrase.value.id - ); - if (index !== -1) { - phrases.value[index].content = phraseForm.value.content; - } - } else { - // 新增 - const result = await api("saveCommonPhrase", { - categoryId: currentCategory.value, - content: phraseForm.value.content, - corpId: uni.getStorageSync("corpId"), - }); - - if (result.code === 200 && result.data) { - phrases.value.push(result.data); - } else { - // 如果API失败,使用本地ID - phrases.value.push({ - id: Date.now(), - categoryId: currentCategory.value, - content: phraseForm.value.content, - }); - } + return; } - uni.showToast({ - title: editingPhrase.value ? "保存成功" : "添加成功", - icon: "success", + const result = await api('saveCommonPhrase', { + id: editingPhrase.value?.id, + categoryId: editingPhrase.value?.categoryId || currentCategory.value, + content: phraseForm.value.content, + corpId, + userId }); - closePopup(); + if (result.code === 200) { + if (editingPhrase.value) { + // 更新 + const index = phrases.value.findIndex(p => p.id === editingPhrase.value.id); + if (index !== -1) { + phrases.value[index].content = phraseForm.value.content; + } + } else { + // 新增 + if (result.data) { + phrases.value.push(result.data); + } + } + + uni.showToast({ + title: editingPhrase.value ? '保存成功' : '添加成功', + icon: 'success' + }); + + closePopup(); + } else { + uni.showToast({ + title: result.message || '操作失败', + icon: 'none' + }); + } } catch (error) { - console.error("保存常用语失败:", error); + console.error('保存常用语失败:', error); uni.showToast({ - title: "操作失败", - icon: "none", + title: '操作失败', + icon: 'none' }); } }; @@ -398,20 +402,52 @@ const closeCategoryPopup = () => { // 加载常用语数据 const loadPhrases = async () => { try { - const result = await api("getCommonPhrases", { - corpId: uni.getStorageSync("corpId"), + const corpId = uni.getStorageSync('corpId'); + const userId = uni.getStorageSync('userId'); + + if (!corpId) { + uni.showToast({ + title: '请先登录', + icon: 'none' + }); + return; + } + + const result = await api('getCommonPhrases', { + corpId, + userId }); - - if (result.code === 200 && result.data) { - phrases.value = result.data; - } else { - // 如果API返回失败,使用模拟数据 - phrases.value = getMockPhrases(); + + if (result.code === 200) { + // 更新常用语列表 + if (Array.isArray(result.data)) { + phrases.value = result.data; + } + + // 更新分类列表(合并默认分类和后台分类) + if (Array.isArray(result.categories)) { + const backendCategories = result.categories.map(cat => ({ + id: cat.id, + name: cat.name, + deletable: true // 后台分类都可以删除 + })); + + // 如果后台有分类,使用后台分类,否则使用默认分类 + if (backendCategories.length > 0) { + categories.value = backendCategories; + // 设置当前分类为第一个 + if (!currentCategory.value || !categories.value.find(c => c.id === currentCategory.value)) { + currentCategory.value = categories.value[0].id; + } + } + } } } catch (error) { - console.error("加载常用语失败:", error); - // 加载失败时使用模拟数据 - phrases.value = getMockPhrases(); + console.error('加载常用语失败:', error); + uni.showToast({ + title: '加载失败', + icon: 'none' + }); } };