diff --git a/resources/assets/js/pages/manage/components/FavoriteManagement.vue b/resources/assets/js/pages/manage/components/FavoriteManagement.vue index 9a0e10145..d03f5d21f 100644 --- a/resources/assets/js/pages/manage/components/FavoriteManagement.vue +++ b/resources/assets/js/pages/manage/components/FavoriteManagement.vue @@ -353,13 +353,9 @@ export default { }, removeFavorite(item) { - this.$store.dispatch("call", { - url: 'users/favorite/toggle', - data: { - type: item.type, - id: item.id - }, - method: 'post', + this.$store.dispatch("toggleFavorite", { + type: item.type, + id: item.id }).then(() => { $A.messageSuccess('取消收藏成功'); this.getLists(); diff --git a/resources/assets/js/pages/manage/components/ProjectPanel.vue b/resources/assets/js/pages/manage/components/ProjectPanel.vue index a30260f33..4fa1a7266 100644 --- a/resources/assets/js/pages/manage/components/ProjectPanel.vue +++ b/resources/assets/js/pages/manage/components/ProjectPanel.vue @@ -1894,13 +1894,9 @@ export default { toggleProjectFavorite() { if (!this.projectData.id) return; - this.$store.dispatch("call", { - url: 'users/favorite/toggle', - data: { - type: 'project', - id: this.projectData.id - }, - method: 'post', + this.$store.dispatch("toggleFavorite", { + type: 'project', + id: this.projectData.id }).then(({data, msg}) => { // 更新项目的收藏状态 this.$set(this.projectData, 'favorited', data.favorited); @@ -1916,14 +1912,9 @@ export default { checkProjectFavoriteStatus() { if (!this.projectData.id) return; - this.$store.dispatch("call", { - url: 'users/favorite/check', - data: { - type: 'project', - id: this.projectData.id - }, - method: 'get', - spinner: 0, // 静默调用 + this.$store.dispatch("checkFavoriteStatus", { + type: 'project', + id: this.projectData.id }).then(({data}) => { this.$set(this.projectData, 'favorited', data.favorited || false); }).catch(() => { diff --git a/resources/assets/js/pages/manage/components/TaskOperation.vue b/resources/assets/js/pages/manage/components/TaskOperation.vue index 138ac3c0f..2e159fa11 100644 --- a/resources/assets/js/pages/manage/components/TaskOperation.vue +++ b/resources/assets/js/pages/manage/components/TaskOperation.vue @@ -506,12 +506,9 @@ export default { checkFavoriteStatus() { if (!this.task.id) return; - this.$store.dispatch("call", { - url: 'users/favorite/check', - data: { - type: 'task', - id: this.task.id - }, + this.$store.dispatch("checkFavoriteStatus", { + type: 'task', + id: this.task.id }).then(({data}) => { this.isFavorited = data.favorited || false; }).catch(() => { @@ -525,13 +522,9 @@ export default { toggleFavorite() { if (!this.task.id) return; - this.$store.dispatch("call", { - url: 'users/favorite/toggle', - data: { - type: 'task', - id: this.task.id - }, - method: 'post', + this.$store.dispatch("toggleFavorite", { + type: 'task', + id: this.task.id }).then(({data, msg}) => { this.isFavorited = data.favorited; this.hide(); diff --git a/resources/assets/js/pages/manage/file.vue b/resources/assets/js/pages/manage/file.vue index 349c948e7..12d0b063e 100644 --- a/resources/assets/js/pages/manage/file.vue +++ b/resources/assets/js/pages/manage/file.vue @@ -1012,7 +1012,6 @@ export default { this.loadIng--; this.openFileJudge() this.shakeFile(this.$route.params.shakeId); - this.checkFileFavoriteStatus(this.fileList); await $A.IDBSet("fileFolderId", this.pid) }).catch(({msg}) => { this.loadIng--; @@ -1082,6 +1081,9 @@ export default { handleRightClick(event, item, isAddButton) { this.contextMenuItem = $A.isJson(item) ? item : {}; + if (this.contextMenuItem.id && this.contextMenuItem.type !== 'folder') { + this.checkSingleFileFavoriteStatus(this.contextMenuItem); + } if (this.contextMenuVisible) { this.handleClickContextMenuOutside(); } @@ -2086,13 +2088,9 @@ export default { toggleFileFavorite(item) { if (!item.id || item.type === 'folder') return; - this.$store.dispatch("call", { - url: 'users/favorite/toggle', - data: { - type: 'file', - id: item.id - }, - method: 'post', + this.$store.dispatch("toggleFavorite", { + type: 'file', + id: item.id }).then(({data, msg}) => { // 更新文件的收藏状态 const fileIndex = this.fileList.findIndex(file => file.id === item.id); @@ -2112,34 +2110,27 @@ export default { /** * 检查文件收藏状态 */ - checkFileFavoriteStatus(files) { - if (!Array.isArray(files) || files.length === 0) return; + checkSingleFileFavoriteStatus(file) { + if (!file.id || file.type === 'folder') return; - const fileIds = files.filter(file => file.type !== 'folder').map(file => file.id); - if (fileIds.length === 0) return; - - // 批量检查收藏状态 - fileIds.forEach(fileId => { - this.$store.dispatch("call", { - url: 'users/favorite/check', - data: { - type: 'file', - id: fileId - }, - method: 'get', - spinner: 0, // 静默调用 - }).then(({data}) => { - const fileIndex = this.fileList.findIndex(file => file.id === fileId); - if (fileIndex > -1) { - this.$set(this.fileList[fileIndex], 'favorited', data.favorited || false); - } - }).catch(() => { - // 出错时默认为未收藏状态 - const fileIndex = this.fileList.findIndex(file => file.id === fileId); - if (fileIndex > -1) { - this.$set(this.fileList[fileIndex], 'favorited', false); - } - }); + this.$store.dispatch("checkFavoriteStatus", { + type: 'file', + id: file.id + }).then(({data}) => { + // 更新上下文菜单项的收藏状态 + this.$set(this.contextMenuItem, 'favorited', data.favorited || false); + // 同时更新文件列表中对应文件的收藏状态 + const fileIndex = this.fileList.findIndex(f => f.id === file.id); + if (fileIndex > -1) { + this.$set(this.fileList[fileIndex], 'favorited', data.favorited || false); + } + }).catch(() => { + // 出错时默认为未收藏状态 + this.$set(this.contextMenuItem, 'favorited', false); + const fileIndex = this.fileList.findIndex(f => f.id === file.id); + if (fileIndex > -1) { + this.$set(this.fileList[fileIndex], 'favorited', false); + } }); } } diff --git a/resources/assets/js/store/actions.js b/resources/assets/js/store/actions.js index a2dc8cd57..7bf76565d 100644 --- a/resources/assets/js/store/actions.js +++ b/resources/assets/js/store/actions.js @@ -2844,6 +2844,69 @@ export default { state.taskTemplates = state.taskTemplates.filter(template => template.project_id !== projectId).concat(data || []) }, + /** *****************************************************************************************/ + /** ************************************** 收藏 **********************************************/ + /** *****************************************************************************************/ + + /** + * 检查收藏状态 + * @param dispatch + * @param {object} params {type: 'task|project|file', id: number} + */ + checkFavoriteStatus({dispatch}, {type, id}) { + return dispatch('call', { + url: 'users/favorite/check', + data: { + type: type, + id: id + }, + method: 'get', + spinner: 0, // 静默调用 + }); + }, + + /** + * 切换收藏状态 + * @param dispatch + * @param {object} params {type: 'task|project|file', id: number} + */ + toggleFavorite({dispatch}, {type, id}) { + return dispatch('call', { + url: 'users/favorite/toggle', + data: { + type: type, + id: id + }, + method: 'post', + }); + }, + + /** + * 批量检查收藏状态 + * @param dispatch + * @param {object} params {type: 'task|project|file', items: array} + */ + checkFavoritesStatus({dispatch}, {type, items}) { + if (!Array.isArray(items) || items.length === 0) { + return Promise.resolve([]); + } + + // 批量检查收藏状态 + const promises = items.map(item => { + return dispatch('checkFavoriteStatus', {type, id: item.id}) + .then(({data}) => ({ + id: item.id, + favorited: data.favorited || false + })) + .catch(() => ({ + id: item.id, + favorited: false + })); + }); + + return Promise.all(promises); + }, + /** *****************************************************************************************/ /** ************************************** 会话 **********************************************/ /** *****************************************************************************************/