feat: 重构收藏功能,优化状态检查与切换逻辑

- 将文件、项目和任务的收藏状态切换逻辑统一为 toggleFavorite 方法
- 添加 checkFavoriteStatus 方法以简化收藏状态检查
- 更新相关 Vue 组件以使用新的状态管理方法,提升代码可读性和维护性
- 优化上下文菜单和操作逻辑,确保收藏状态的实时更新
This commit is contained in:
kuaifan 2025-09-23 08:59:15 +08:00
parent 11b98978c1
commit 18a922b5cd
5 changed files with 104 additions and 70 deletions

View File

@ -353,13 +353,9 @@ export default {
}, },
removeFavorite(item) { removeFavorite(item) {
this.$store.dispatch("call", { this.$store.dispatch("toggleFavorite", {
url: 'users/favorite/toggle',
data: {
type: item.type, type: item.type,
id: item.id id: item.id
},
method: 'post',
}).then(() => { }).then(() => {
$A.messageSuccess('取消收藏成功'); $A.messageSuccess('取消收藏成功');
this.getLists(); this.getLists();

View File

@ -1894,13 +1894,9 @@ export default {
toggleProjectFavorite() { toggleProjectFavorite() {
if (!this.projectData.id) return; if (!this.projectData.id) return;
this.$store.dispatch("call", { this.$store.dispatch("toggleFavorite", {
url: 'users/favorite/toggle',
data: {
type: 'project', type: 'project',
id: this.projectData.id id: this.projectData.id
},
method: 'post',
}).then(({data, msg}) => { }).then(({data, msg}) => {
// //
this.$set(this.projectData, 'favorited', data.favorited); this.$set(this.projectData, 'favorited', data.favorited);
@ -1916,14 +1912,9 @@ export default {
checkProjectFavoriteStatus() { checkProjectFavoriteStatus() {
if (!this.projectData.id) return; if (!this.projectData.id) return;
this.$store.dispatch("call", { this.$store.dispatch("checkFavoriteStatus", {
url: 'users/favorite/check',
data: {
type: 'project', type: 'project',
id: this.projectData.id id: this.projectData.id
},
method: 'get',
spinner: 0, //
}).then(({data}) => { }).then(({data}) => {
this.$set(this.projectData, 'favorited', data.favorited || false); this.$set(this.projectData, 'favorited', data.favorited || false);
}).catch(() => { }).catch(() => {

View File

@ -506,12 +506,9 @@ export default {
checkFavoriteStatus() { checkFavoriteStatus() {
if (!this.task.id) return; if (!this.task.id) return;
this.$store.dispatch("call", { this.$store.dispatch("checkFavoriteStatus", {
url: 'users/favorite/check',
data: {
type: 'task', type: 'task',
id: this.task.id id: this.task.id
},
}).then(({data}) => { }).then(({data}) => {
this.isFavorited = data.favorited || false; this.isFavorited = data.favorited || false;
}).catch(() => { }).catch(() => {
@ -525,13 +522,9 @@ export default {
toggleFavorite() { toggleFavorite() {
if (!this.task.id) return; if (!this.task.id) return;
this.$store.dispatch("call", { this.$store.dispatch("toggleFavorite", {
url: 'users/favorite/toggle',
data: {
type: 'task', type: 'task',
id: this.task.id id: this.task.id
},
method: 'post',
}).then(({data, msg}) => { }).then(({data, msg}) => {
this.isFavorited = data.favorited; this.isFavorited = data.favorited;
this.hide(); this.hide();

View File

@ -1012,7 +1012,6 @@ export default {
this.loadIng--; this.loadIng--;
this.openFileJudge() this.openFileJudge()
this.shakeFile(this.$route.params.shakeId); this.shakeFile(this.$route.params.shakeId);
this.checkFileFavoriteStatus(this.fileList);
await $A.IDBSet("fileFolderId", this.pid) await $A.IDBSet("fileFolderId", this.pid)
}).catch(({msg}) => { }).catch(({msg}) => {
this.loadIng--; this.loadIng--;
@ -1082,6 +1081,9 @@ export default {
handleRightClick(event, item, isAddButton) { handleRightClick(event, item, isAddButton) {
this.contextMenuItem = $A.isJson(item) ? item : {}; this.contextMenuItem = $A.isJson(item) ? item : {};
if (this.contextMenuItem.id && this.contextMenuItem.type !== 'folder') {
this.checkSingleFileFavoriteStatus(this.contextMenuItem);
}
if (this.contextMenuVisible) { if (this.contextMenuVisible) {
this.handleClickContextMenuOutside(); this.handleClickContextMenuOutside();
} }
@ -2086,13 +2088,9 @@ export default {
toggleFileFavorite(item) { toggleFileFavorite(item) {
if (!item.id || item.type === 'folder') return; if (!item.id || item.type === 'folder') return;
this.$store.dispatch("call", { this.$store.dispatch("toggleFavorite", {
url: 'users/favorite/toggle',
data: {
type: 'file', type: 'file',
id: item.id id: item.id
},
method: 'post',
}).then(({data, msg}) => { }).then(({data, msg}) => {
// //
const fileIndex = this.fileList.findIndex(file => file.id === item.id); const fileIndex = this.fileList.findIndex(file => file.id === item.id);
@ -2112,35 +2110,28 @@ export default {
/** /**
* 检查文件收藏状态 * 检查文件收藏状态
*/ */
checkFileFavoriteStatus(files) { checkSingleFileFavoriteStatus(file) {
if (!Array.isArray(files) || files.length === 0) return; if (!file.id || file.type === 'folder') return;
const fileIds = files.filter(file => file.type !== 'folder').map(file => file.id); this.$store.dispatch("checkFavoriteStatus", {
if (fileIds.length === 0) return;
//
fileIds.forEach(fileId => {
this.$store.dispatch("call", {
url: 'users/favorite/check',
data: {
type: 'file', type: 'file',
id: fileId id: file.id
},
method: 'get',
spinner: 0, //
}).then(({data}) => { }).then(({data}) => {
const fileIndex = this.fileList.findIndex(file => file.id === fileId); //
this.$set(this.contextMenuItem, 'favorited', data.favorited || false);
//
const fileIndex = this.fileList.findIndex(f => f.id === file.id);
if (fileIndex > -1) { if (fileIndex > -1) {
this.$set(this.fileList[fileIndex], 'favorited', data.favorited || false); this.$set(this.fileList[fileIndex], 'favorited', data.favorited || false);
} }
}).catch(() => { }).catch(() => {
// //
const fileIndex = this.fileList.findIndex(file => file.id === fileId); this.$set(this.contextMenuItem, 'favorited', false);
const fileIndex = this.fileList.findIndex(f => f.id === file.id);
if (fileIndex > -1) { if (fileIndex > -1) {
this.$set(this.fileList[fileIndex], 'favorited', false); this.$set(this.fileList[fileIndex], 'favorited', false);
} }
}); });
});
} }
} }
} }

View File

@ -2844,6 +2844,69 @@ export default {
state.taskTemplates = state.taskTemplates.filter(template => template.project_id !== projectId).concat(data || []) 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);
},
/** *****************************************************************************************/ /** *****************************************************************************************/
/** ************************************** 会话 **********************************************/ /** ************************************** 会话 **********************************************/
/** *****************************************************************************************/ /** *****************************************************************************************/