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) {
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();

View File

@ -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(() => {

View File

@ -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();

View File

@ -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);
}
});
}
}

View File

@ -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);
},
/** *****************************************************************************************/
/** ************************************** 会话 **********************************************/
/** *****************************************************************************************/