2026-06-10 14:17:54 +08:00
|
|
|
import {
|
|
|
|
|
defineStore
|
|
|
|
|
} from 'pinia'
|
|
|
|
|
import {
|
|
|
|
|
ref,
|
|
|
|
|
computed
|
|
|
|
|
} from 'vue'
|
|
|
|
|
import {pastelColors,defaultApps} from '@/utils/constant.js'
|
|
|
|
|
import {reqMenuPowerList} from '@/api/auth.js'
|
|
|
|
|
|
|
|
|
|
export const useMenuStore = defineStore('menu', () => {
|
|
|
|
|
// 应用列表(默认配置 + 持久化)
|
|
|
|
|
const appsList = ref([...defaultApps])
|
|
|
|
|
// 编辑时的临时缓存
|
|
|
|
|
const editCache = ref([])
|
|
|
|
|
// 菜单的权限列表(后端返回的有权限的菜单 code 数组)
|
|
|
|
|
const menuProwerList = ref([])
|
|
|
|
|
|
|
|
|
|
// 快捷应用列表(已选择的)
|
|
|
|
|
const quickApps = computed(() => appsList.value.filter(app => app.selected&&hasMenuPermission(app.menuId)))
|
|
|
|
|
|
|
|
|
|
// 业务查询列表(未选择的)
|
|
|
|
|
const queryApps = computed(() => appsList.value.filter(app => !app.selected&&hasMenuPermission(app.menuId)))
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 打开编辑弹窗(备份当前状态)
|
|
|
|
|
*/
|
|
|
|
|
const openEdit = () => {
|
|
|
|
|
editCache.value = appsList.value.filter(app => hasMenuPermission(app.menuId))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 切换应用选择状态
|
|
|
|
|
*/
|
|
|
|
|
const toggleSelect = (item) => {
|
|
|
|
|
item.selected = !item.selected
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 确认编辑(保存更改)
|
|
|
|
|
*/
|
|
|
|
|
const confirmEdit = () => {
|
|
|
|
|
appsList.value = editCache.value.map(app => ({ ...app }))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 请求菜单权限列表
|
|
|
|
|
*/
|
|
|
|
|
const fetchMenuPowerList = async (userId, projectId) => {
|
|
|
|
|
if (!userId || !projectId) return
|
|
|
|
|
try {
|
|
|
|
|
const res = await reqMenuPowerList(userId, projectId)
|
|
|
|
|
menuProwerList.value = res.data || []
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('获取菜单权限失败:', err)
|
|
|
|
|
menuProwerList.value = []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断菜单是否有权限打开
|
|
|
|
|
* @param {string} menuCode - 菜单编码/标识
|
|
|
|
|
* @returns {boolean} 是否有权限
|
|
|
|
|
*/
|
|
|
|
|
const hasMenuPermission = (menuCode) => {
|
|
|
|
|
if (!menuCode) return true
|
|
|
|
|
return menuProwerList.value.includes(menuCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据权限过滤快捷应用
|
|
|
|
|
*/
|
|
|
|
|
const quickAppsWithPermission = computed(() => {
|
|
|
|
|
if (menuProwerList.value.length === 0) return quickApps.value
|
|
|
|
|
return quickApps.value.filter(app => {
|
|
|
|
|
return hasMenuPermission(app.permissionCode)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
appsList,
|
|
|
|
|
editCache,
|
|
|
|
|
quickApps,
|
|
|
|
|
quickAppsWithPermission,
|
|
|
|
|
queryApps,
|
|
|
|
|
menuProwerList,
|
|
|
|
|
pastelColors,
|
|
|
|
|
openEdit,
|
|
|
|
|
toggleSelect,
|
|
|
|
|
confirmEdit,
|
|
|
|
|
fetchMenuPowerList,
|
|
|
|
|
hasMenuPermission
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
persist: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
strategies: [{
|
|
|
|
|
key: 'menu',
|
2026-06-10 17:26:36 +08:00
|
|
|
paths: ['appsList'],
|
2026-06-10 14:17:54 +08:00
|
|
|
}]
|
|
|
|
|
},
|
|
|
|
|
})
|