mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-16 14:12:51 +00:00
feat: 重构收藏功能,优化状态检查与切换逻辑
- 将文件、项目和任务的收藏状态切换逻辑统一为 toggleFavorite 方法 - 添加 checkFavoriteStatus 方法以简化收藏状态检查 - 更新相关 Vue 组件以使用新的状态管理方法,提升代码可读性和维护性 - 优化上下文菜单和操作逻辑,确保收藏状态的实时更新
This commit is contained in:
parent
11b98978c1
commit
18a922b5cd
@ -353,13 +353,9 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
removeFavorite(item) {
|
removeFavorite(item) {
|
||||||
this.$store.dispatch("call", {
|
this.$store.dispatch("toggleFavorite", {
|
||||||
url: 'users/favorite/toggle',
|
type: item.type,
|
||||||
data: {
|
id: item.id
|
||||||
type: item.type,
|
|
||||||
id: item.id
|
|
||||||
},
|
|
||||||
method: 'post',
|
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
$A.messageSuccess('取消收藏成功');
|
$A.messageSuccess('取消收藏成功');
|
||||||
this.getLists();
|
this.getLists();
|
||||||
|
|||||||
@ -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',
|
type: 'project',
|
||||||
data: {
|
id: this.projectData.id
|
||||||
type: 'project',
|
|
||||||
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',
|
type: 'project',
|
||||||
data: {
|
id: this.projectData.id
|
||||||
type: 'project',
|
|
||||||
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(() => {
|
||||||
|
|||||||
@ -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',
|
type: 'task',
|
||||||
data: {
|
id: this.task.id
|
||||||
type: 'task',
|
|
||||||
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',
|
type: 'task',
|
||||||
data: {
|
id: this.task.id
|
||||||
type: 'task',
|
|
||||||
id: this.task.id
|
|
||||||
},
|
|
||||||
method: 'post',
|
|
||||||
}).then(({data, msg}) => {
|
}).then(({data, msg}) => {
|
||||||
this.isFavorited = data.favorited;
|
this.isFavorited = data.favorited;
|
||||||
this.hide();
|
this.hide();
|
||||||
|
|||||||
@ -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',
|
type: 'file',
|
||||||
data: {
|
id: item.id
|
||||||
type: 'file',
|
|
||||||
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,34 +2110,27 @@ 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;
|
type: 'file',
|
||||||
|
id: file.id
|
||||||
// 批量检查收藏状态
|
}).then(({data}) => {
|
||||||
fileIds.forEach(fileId => {
|
// 更新上下文菜单项的收藏状态
|
||||||
this.$store.dispatch("call", {
|
this.$set(this.contextMenuItem, 'favorited', data.favorited || false);
|
||||||
url: 'users/favorite/check',
|
// 同时更新文件列表中对应文件的收藏状态
|
||||||
data: {
|
const fileIndex = this.fileList.findIndex(f => f.id === file.id);
|
||||||
type: 'file',
|
if (fileIndex > -1) {
|
||||||
id: fileId
|
this.$set(this.fileList[fileIndex], 'favorited', data.favorited || false);
|
||||||
},
|
}
|
||||||
method: 'get',
|
}).catch(() => {
|
||||||
spinner: 0, // 静默调用
|
// 出错时默认为未收藏状态
|
||||||
}).then(({data}) => {
|
this.$set(this.contextMenuItem, 'favorited', false);
|
||||||
const fileIndex = this.fileList.findIndex(file => file.id === fileId);
|
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', false);
|
||||||
}
|
}
|
||||||
}).catch(() => {
|
|
||||||
// 出错时默认为未收藏状态
|
|
||||||
const fileIndex = this.fileList.findIndex(file => file.id === fileId);
|
|
||||||
if (fileIndex > -1) {
|
|
||||||
this.$set(this.fileList[fileIndex], 'favorited', false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
63
resources/assets/js/store/actions.js
vendored
63
resources/assets/js/store/actions.js
vendored
@ -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);
|
||||||
|
},
|
||||||
|
|
||||||
/** *****************************************************************************************/
|
/** *****************************************************************************************/
|
||||||
/** ************************************** 会话 **********************************************/
|
/** ************************************** 会话 **********************************************/
|
||||||
/** *****************************************************************************************/
|
/** *****************************************************************************************/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user