feat(home): 新增全部功能页与常用功能编辑同步
- 新增 pages/home/edit-quick-actions 页面与路由配置 - 全部功能页支持搜索、首字母导航、父子菜单展示、编辑常用(最多24个) - 首页常用功能改为读取 fixed_home_menus 并与最新权限菜单对齐 - user 模块统一菜单归一化逻辑并打印接口菜单列表 - 引入 pinyin-pro 处理中文首字母分组
This commit is contained in:
parent
2a05daba98
commit
fb1573b2a5
@ -9,6 +9,22 @@ import {
|
||||
} from '@/core/utils/auth'
|
||||
import { router } from '../../router'
|
||||
import { goLogin } from '@/core/utils/auth-guard'
|
||||
|
||||
const normalizeMenus = (user = {}) => {
|
||||
const sourceMenus = Array.isArray(user.menus)
|
||||
? user.menus
|
||||
: (Array.isArray(user.permissions) ? user.permissions : []);
|
||||
|
||||
const allChildren = sourceMenus.flatMap(item => item.children || []);
|
||||
return allChildren.filter(menu =>
|
||||
menu &&
|
||||
menu.component &&
|
||||
menu.component !== 'Layout' &&
|
||||
menu.meta &&
|
||||
menu.meta.title
|
||||
);
|
||||
}
|
||||
|
||||
const state = {
|
||||
token: uni.getStorageSync("token") || "",
|
||||
isLogin: uni.getStorageSync("isLogin") || false, // 是否登陆
|
||||
@ -74,6 +90,8 @@ const actions = {
|
||||
}
|
||||
|
||||
const user = data
|
||||
const menuList = normalizeMenus(user);
|
||||
console.log('用户信息接口菜单列表:', menuList);
|
||||
commit('SET_PERMISSIONS', user.permissions)
|
||||
commit('SET_USER', user)
|
||||
|
||||
@ -266,19 +284,7 @@ const mutations = {
|
||||
},
|
||||
SET_USER: (state, user) => {
|
||||
state.user = user;
|
||||
const allChildren = user.menus.flatMap(item =>
|
||||
item.children || [] // 确保 children 存在,否则返回空数组
|
||||
);
|
||||
|
||||
// 2. 过滤有效菜单并限制数量
|
||||
const validMenus = allChildren.filter(menu =>
|
||||
menu &&
|
||||
menu.component &&
|
||||
menu.component !== 'Layout' &&
|
||||
menu.meta &&
|
||||
menu.meta.title
|
||||
)
|
||||
state.menus = validMenus;
|
||||
state.menus = normalizeMenus(user);
|
||||
},
|
||||
SET_ROLEID: (state, roleId) => {
|
||||
state.roleId = roleId
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"html2canvas": "^1.4.1",
|
||||
"pinyin-pro": "^3.28.0",
|
||||
"qrcode": "^1.5.4",
|
||||
"uni-read-pages": "^1.0.5",
|
||||
"uqrcodejs": "^4.0.7"
|
||||
|
||||
@ -122,6 +122,13 @@
|
||||
{
|
||||
"titleNView": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"path" : "pages/home/edit-quick-actions",
|
||||
"style" :
|
||||
{
|
||||
"titleNView": false
|
||||
}
|
||||
}
|
||||
|
||||
],
|
||||
|
||||
609
pages/home/edit-quick-actions.vue
Normal file
609
pages/home/edit-quick-actions.vue
Normal file
@ -0,0 +1,609 @@
|
||||
<template>
|
||||
<view class="all-functions-page">
|
||||
<view class="status_bar"></view>
|
||||
<view class="page-header">
|
||||
<view class="back-btn" @click="goBack">
|
||||
<u-icon name="arrow-left" size="18" color="#333"></u-icon>
|
||||
</view>
|
||||
<text class="header-title">全部功能</text>
|
||||
<view class="edit-common-btn" :class="{ active: isEditMode }" @click="onEditCommon">
|
||||
<u-icon :name="isEditMode ? 'checkmark-circle-fill' : 'edit-pen'" size="14" :color="isEditMode ? '#19be6b' : '#2979ff'"></u-icon>
|
||||
<text class="edit-common-text">{{ isEditMode ? '完成' : '编辑常用' }}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="search-bar">
|
||||
<view class="search-input-wrap">
|
||||
<u-icon name="search" size="18" color="#999"></u-icon>
|
||||
<input
|
||||
class="search-input"
|
||||
v-model="searchKeyword"
|
||||
placeholder="输入关键字搜索功能"
|
||||
placeholder-class="search-placeholder"
|
||||
/>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="content-area">
|
||||
<scroll-view v-if="allActions.length > 0" class="left-nav" scroll-y :scroll-into-view="leftNavTargetId" scroll-with-animation>
|
||||
<view class="nav-item" :class="{ active: activeParent === '全部' }" @click="onClickAllParent">
|
||||
全部
|
||||
</view>
|
||||
<view
|
||||
v-for="(group, index) in leftParentList"
|
||||
:key="group.key"
|
||||
:id="`parent-${index}`"
|
||||
class="nav-item parent-nav-item"
|
||||
:class="{ active: activeParent === group.key }"
|
||||
@click="onClickParent(group)"
|
||||
>
|
||||
{{ group.key }}
|
||||
</view>
|
||||
</scroll-view>
|
||||
|
||||
<scroll-view class="page-scroll" scroll-y>
|
||||
<view v-if="allActions.length === 0" class="empty-state">
|
||||
<text class="empty-text">暂无可用功能</text>
|
||||
</view>
|
||||
|
||||
<view v-else-if="displayGroups.length > 0" class="group-list">
|
||||
<view v-for="group in displayGroups" :key="group.key" class="group-section">
|
||||
<view v-if="activeParent === '全部'" class="group-title">{{ group.key }}</view>
|
||||
<view class="function-grid">
|
||||
<view
|
||||
v-for="(item, index) in group.items"
|
||||
:key="item.id || index"
|
||||
class="function-item"
|
||||
:class="{ selected: isEditMode && isSelected(item) }"
|
||||
@click="onFunctionClick(item)"
|
||||
>
|
||||
<view v-if="isEditMode" class="select-mark">
|
||||
<u-icon :name="isSelected(item) ? 'checkmark-circle-fill' : 'checkmark-circle'" size="18" :color="isSelected(item) ? '#2979ff' : '#c0c4cc'"></u-icon>
|
||||
</view>
|
||||
<view class="item-icon-box" :style="{ background: item.color + '15' }">
|
||||
<u-icon :name="item.icon || 'grid'" size="24" :color="item.color"></u-icon>
|
||||
</view>
|
||||
<text class="item-name">{{ item.name }}</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view v-else class="empty-state">
|
||||
<text class="empty-text">未找到相关功能</text>
|
||||
</view>
|
||||
</scroll-view>
|
||||
|
||||
<view v-if="allActions.length > 0" class="right-initial-nav">
|
||||
<view
|
||||
v-for="nav in initialNavList"
|
||||
:key="nav"
|
||||
class="initial-item"
|
||||
:class="{ active: nav === activeNav }"
|
||||
@click="handleInitialNavClick(nav)"
|
||||
>
|
||||
{{ nav }}
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { pinyin } from 'pinyin-pro'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
allGroups: [],
|
||||
allActions: [],
|
||||
searchKeyword: '',
|
||||
activeNav: '全部',
|
||||
activeParent: '全部',
|
||||
leftNavTargetId: '',
|
||||
isEditMode: false,
|
||||
selectedKeys: [],
|
||||
FIXED_MENU_KEY: 'fixed_home_menus'
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
filteredGroups() {
|
||||
const keyword = (this.searchKeyword || '').trim().toLowerCase();
|
||||
if (!keyword) {
|
||||
return this.allGroups;
|
||||
}
|
||||
|
||||
return this.allGroups
|
||||
.map(group => {
|
||||
const groupMatch = (group.key || '').toLowerCase().includes(keyword);
|
||||
const items = groupMatch
|
||||
? group.items
|
||||
: group.items.filter(item => (item.name || '').toLowerCase().includes(keyword));
|
||||
|
||||
return {
|
||||
...group,
|
||||
items
|
||||
};
|
||||
})
|
||||
.filter(group => group.items.length > 0);
|
||||
},
|
||||
groupedActions() {
|
||||
return [...this.filteredGroups]
|
||||
.map(group => ({
|
||||
...group,
|
||||
items: [...group.items].sort((first, second) =>
|
||||
(first.name || '').localeCompare(second.name || '', 'zh-Hans-CN-u-co-pinyin')
|
||||
)
|
||||
}))
|
||||
.sort((first, second) => {
|
||||
if (first.initial === '#') return 1;
|
||||
if (second.initial === '#') return -1;
|
||||
if (first.initial !== second.initial) {
|
||||
return first.initial.localeCompare(second.initial);
|
||||
}
|
||||
return (first.key || '').localeCompare(second.key || '', 'zh-Hans-CN-u-co-pinyin');
|
||||
});
|
||||
},
|
||||
initialNavList() {
|
||||
return [...new Set(this.groupedActions.map(group => group.initial))];
|
||||
},
|
||||
leftParentList() {
|
||||
return this.groupedActions;
|
||||
},
|
||||
displayGroups() {
|
||||
if (this.activeParent !== '全部') {
|
||||
return this.groupedActions.filter(group => group.key === this.activeParent);
|
||||
}
|
||||
|
||||
if (this.activeNav !== '全部') {
|
||||
return this.groupedActions.filter(group => group.initial === this.activeNav);
|
||||
}
|
||||
|
||||
return this.groupedActions;
|
||||
}
|
||||
},
|
||||
async onLoad() {
|
||||
await this.loadAllActions();
|
||||
},
|
||||
methods: {
|
||||
async loadAllActions() {
|
||||
let user = null;
|
||||
try {
|
||||
user = await this.$store.dispatch('getUserInfo');
|
||||
} catch (error) {
|
||||
console.error('获取用户信息失败,使用本地缓存菜单:', error);
|
||||
user = this.$store.getters.user || {};
|
||||
}
|
||||
|
||||
const menus = (user && (user.menus || user.permissions)) || [];
|
||||
console.log('用户信息接口菜单列表:', menus);
|
||||
|
||||
const colorPool = [
|
||||
'#FF6B6B', '#4ECDC4', '#FFD166', '#06D6A0', '#118AB2',
|
||||
'#EF476F', '#26547C', '#7209B7', '#3A86FF', '#FB5607', '#8338EC'
|
||||
];
|
||||
|
||||
const validGroups = menus
|
||||
.map(parent => {
|
||||
const parentName = parent?.meta?.title || parent?.name || parent?.title || '未分类';
|
||||
const items = (parent?.children || [])
|
||||
.filter(menu =>
|
||||
menu &&
|
||||
menu.component &&
|
||||
menu.component !== 'Layout' &&
|
||||
menu.meta &&
|
||||
menu.meta.title
|
||||
)
|
||||
.map(menu => ({
|
||||
id: menu.id || menu.menuId || menu.component || menu.meta?.title || '',
|
||||
name: menu.meta?.title || '未命名菜单',
|
||||
component: menu.component || '',
|
||||
icon: menu.meta?.icon || '',
|
||||
color: colorPool[Math.floor(Math.random() * colorPool.length)],
|
||||
parentName
|
||||
}));
|
||||
|
||||
return {
|
||||
key: parentName,
|
||||
initial: this.getInitial(parentName),
|
||||
items
|
||||
};
|
||||
})
|
||||
.filter(group => group.items.length > 0);
|
||||
|
||||
this.allGroups = validGroups;
|
||||
this.allActions = validGroups.flatMap(group => group.items);
|
||||
if (!validGroups.some(group => group.key === this.activeParent)) {
|
||||
this.activeParent = '全部';
|
||||
}
|
||||
|
||||
await this.loadSelectedCommonMenus();
|
||||
},
|
||||
async handleInitialNavClick(nav) {
|
||||
this.activeNav = nav;
|
||||
if (nav === '全部') {
|
||||
this.activeParent = '全部';
|
||||
this.leftNavTargetId = '';
|
||||
return;
|
||||
}
|
||||
|
||||
this.activeParent = '全部';
|
||||
await this.$nextTick();
|
||||
|
||||
const targetIndex = this.leftParentList.findIndex(group => group.initial === nav);
|
||||
if (targetIndex > -1) {
|
||||
this.activeParent = this.leftParentList[targetIndex].key;
|
||||
this.leftNavTargetId = `parent-${targetIndex}`;
|
||||
setTimeout(() => {
|
||||
this.leftNavTargetId = '';
|
||||
}, 350);
|
||||
} else {
|
||||
this.activeParent = '全部';
|
||||
this.leftNavTargetId = '';
|
||||
}
|
||||
},
|
||||
getMenuKey(item) {
|
||||
return String(item?.id || item?.component || item?.name || '');
|
||||
},
|
||||
async loadSelectedCommonMenus() {
|
||||
const saved = await this.$store.dispatch('setting/getValue', this.FIXED_MENU_KEY) || [];
|
||||
const savedKeys = new Set((saved || []).map(item => this.getMenuKey(item)));
|
||||
this.selectedKeys = this.allActions
|
||||
.filter(item => savedKeys.has(this.getMenuKey(item)))
|
||||
.map(item => this.getMenuKey(item));
|
||||
},
|
||||
isSelected(item) {
|
||||
return this.selectedKeys.includes(this.getMenuKey(item));
|
||||
},
|
||||
onFunctionClick(item) {
|
||||
if (!this.isEditMode) {
|
||||
this.toPath(item);
|
||||
return;
|
||||
}
|
||||
|
||||
const key = this.getMenuKey(item);
|
||||
const index = this.selectedKeys.indexOf(key);
|
||||
if (index > -1) {
|
||||
this.selectedKeys.splice(index, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.selectedKeys.length >= 24) {
|
||||
uni.showToast({
|
||||
title: '最多选择24个常用功能',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this.selectedKeys.push(key);
|
||||
},
|
||||
async onEditCommon() {
|
||||
if (!this.isEditMode) {
|
||||
await this.loadSelectedCommonMenus();
|
||||
this.isEditMode = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const selectedMenus = this.allActions.filter(item => this.selectedKeys.includes(this.getMenuKey(item)));
|
||||
await this.$store.dispatch('setting/setValue', {
|
||||
key: this.FIXED_MENU_KEY,
|
||||
value: selectedMenus
|
||||
});
|
||||
|
||||
this.isEditMode = false;
|
||||
uni.showToast({
|
||||
title: '常用功能已保存',
|
||||
icon: 'success'
|
||||
});
|
||||
setTimeout(() => {
|
||||
uni.navigateBack({
|
||||
delta: 1
|
||||
});
|
||||
}, 300);
|
||||
},
|
||||
onClickAllParent() {
|
||||
this.activeParent = '全部';
|
||||
this.activeNav = '全部';
|
||||
},
|
||||
onClickParent(group) {
|
||||
this.activeParent = group.key;
|
||||
this.activeNav = group.initial || '全部';
|
||||
},
|
||||
toPath(item) {
|
||||
if (!item || !item.component) {
|
||||
uni.showToast({
|
||||
title: '功能暂未开放',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
uni.navigateTo({
|
||||
url: item.component.startsWith('/') ? item.component : `/${item.component}`,
|
||||
animationType: 'slide-in-right'
|
||||
});
|
||||
},
|
||||
goBack() {
|
||||
uni.navigateBack({
|
||||
delta: 1
|
||||
});
|
||||
},
|
||||
getInitial(name) {
|
||||
const firstChar = ((name || '').trim()[0] || '').toUpperCase();
|
||||
if (!firstChar) {
|
||||
return '#';
|
||||
}
|
||||
|
||||
if (/^[A-Z]$/.test(firstChar)) {
|
||||
return firstChar;
|
||||
}
|
||||
|
||||
const initial = pinyin(firstChar, {
|
||||
pattern: 'first',
|
||||
toneType: 'none',
|
||||
type: 'array'
|
||||
})?.[0]?.toUpperCase() || '#';
|
||||
|
||||
return /^[A-Z]$/.test(initial) ? initial : '#';
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.all-functions-page {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
background: #f8f9fa;
|
||||
}
|
||||
|
||||
.page-header {
|
||||
position: relative;
|
||||
padding: 20rpx;
|
||||
background: #fff;
|
||||
border-bottom: 1rpx solid #f0f0f0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
.back-btn {
|
||||
position: absolute;
|
||||
left: 20rpx;
|
||||
width: 56rpx;
|
||||
height: 56rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.header-title {
|
||||
font-size: 32rpx;
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.edit-common-btn {
|
||||
position: absolute;
|
||||
right: 20rpx;
|
||||
height: 56rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 24rpx;
|
||||
color: #2979ff;
|
||||
padding: 0 8rpx;
|
||||
|
||||
.edit-common-text {
|
||||
margin-left: 6rpx;
|
||||
font-size: 24rpx;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
&.active {
|
||||
color: #19be6b;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.content-area {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
min-height: 0;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.left-nav {
|
||||
width: 132rpx;
|
||||
background: #fff;
|
||||
border-right: 1rpx solid #f0f0f0;
|
||||
padding: 12rpx 8rpx;
|
||||
|
||||
.nav-item {
|
||||
position: relative;
|
||||
min-height: 56rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-bottom: 10rpx;
|
||||
font-size: 22rpx;
|
||||
color: #666;
|
||||
background: #f4f5f7;
|
||||
border-radius: 10rpx;
|
||||
padding: 8rpx;
|
||||
text-align: center;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&.active {
|
||||
color: #2979ff;
|
||||
background: #eaf2ff;
|
||||
font-weight: 600;
|
||||
box-shadow: 0 4rpx 12rpx rgba(41, 121, 255, 0.12);
|
||||
|
||||
&::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 10rpx;
|
||||
bottom: 10rpx;
|
||||
width: 6rpx;
|
||||
background: #2979ff;
|
||||
border-radius: 0 6rpx 6rpx 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.parent-nav-item {
|
||||
font-size: 20rpx;
|
||||
line-height: 1.3;
|
||||
word-break: break-all;
|
||||
}
|
||||
}
|
||||
|
||||
.page-scroll {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.right-initial-nav {
|
||||
position: absolute;
|
||||
right: 8rpx;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
z-index: 2;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
background: transparent;
|
||||
border-radius: 16rpx;
|
||||
padding: 4rpx 3rpx;
|
||||
|
||||
.initial-item {
|
||||
position: relative;
|
||||
min-width: 26rpx;
|
||||
height: 26rpx;
|
||||
line-height: 26rpx;
|
||||
text-align: center;
|
||||
font-size: 16rpx;
|
||||
color: #666;
|
||||
border-radius: 6rpx;
|
||||
|
||||
&::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
left: -8rpx;
|
||||
right: -8rpx;
|
||||
top: -6rpx;
|
||||
bottom: -6rpx;
|
||||
}
|
||||
|
||||
&.active {
|
||||
color: #2979ff;
|
||||
background: #eaf2ff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.search-bar {
|
||||
padding: 16rpx 20rpx;
|
||||
background: #fff;
|
||||
border-bottom: 1rpx solid #f0f0f0;
|
||||
|
||||
.search-input-wrap {
|
||||
height: 72rpx;
|
||||
background: #f4f5f7;
|
||||
border-radius: 10rpx;
|
||||
padding: 0 20rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.search-input {
|
||||
flex: 1;
|
||||
height: 72rpx;
|
||||
padding-left: 12rpx;
|
||||
font-size: 26rpx;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.search-placeholder {
|
||||
font-size: 26rpx;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
|
||||
.group-list {
|
||||
padding: 0 52rpx 20rpx 20rpx;
|
||||
|
||||
.group-section {
|
||||
.group-title {
|
||||
font-size: 26rpx;
|
||||
font-weight: 600;
|
||||
color: #666;
|
||||
padding: 18rpx 4rpx 10rpx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.function-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(1, 1fr);
|
||||
gap: 16rpx;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.function-item {
|
||||
height: 128rpx;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: #fff;
|
||||
border-radius: 12rpx;
|
||||
padding: 0 12rpx;
|
||||
position: relative;
|
||||
|
||||
&.selected {
|
||||
border: 2rpx solid #2979ff;
|
||||
}
|
||||
|
||||
.select-mark {
|
||||
position: absolute;
|
||||
top: 8rpx;
|
||||
right: 8rpx;
|
||||
}
|
||||
|
||||
.item-icon-box {
|
||||
width: 68rpx;
|
||||
height: 68rpx;
|
||||
border-radius: 14rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-right: 12rpx;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.item-name {
|
||||
font-size: 22rpx;
|
||||
color: #333;
|
||||
text-align: left;
|
||||
padding: 0;
|
||||
line-height: 1.3;
|
||||
flex: 1;
|
||||
word-break: break-all;
|
||||
}
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
padding: 120rpx 0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
|
||||
.empty-text {
|
||||
font-size: 28rpx;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@ -161,40 +161,36 @@
|
||||
if (userData) {
|
||||
this.isLoading = false;
|
||||
this.userInfo = userData;
|
||||
this.refreshQuickActions();
|
||||
await this.refreshQuickActions();
|
||||
}
|
||||
},
|
||||
|
||||
// 刷新快捷入口
|
||||
refreshQuickActions() {
|
||||
async refreshQuickActions() {
|
||||
this.isLoading = true;
|
||||
|
||||
try {
|
||||
// 1. 获取用户当前可用的菜单
|
||||
const userMenus = this.getUserMenus();
|
||||
const latestActions = this.processMenus(userMenus, null);
|
||||
|
||||
// 2. 从Vuex获取已固定的菜单
|
||||
const savedFixedMenus = this.$store.getters[`setting/${this.FIXED_MENU_KEY}`] || [];
|
||||
const savedFixedMenus = await this.$store.dispatch('setting/getValue', this.FIXED_MENU_KEY) || [];
|
||||
const hasSavedConfig = Array.isArray(savedFixedMenus);
|
||||
|
||||
// 3. 如果有保存的固定菜单
|
||||
if (savedFixedMenus && Array.isArray(savedFixedMenus) && savedFixedMenus.length > 0) {
|
||||
// 验证用户是否还有这些菜单的权限
|
||||
const validFixedMenus = this.validateAndFilterMenus(savedFixedMenus, userMenus);
|
||||
|
||||
if (validFixedMenus.length > 0) {
|
||||
// 使用有效的固定菜单(不更新存储)
|
||||
console.log('使用有效的固定菜单:', validFixedMenus);
|
||||
this.quickActions = validFixedMenus;
|
||||
return;
|
||||
}
|
||||
// 如果固定菜单全部失效,继续执行下面的逻辑
|
||||
// 3. 如果存在已保存的常用配置(包括空数组),优先按配置展示
|
||||
if (hasSavedConfig) {
|
||||
const validFixedMenus = this.validateAndFilterMenus(savedFixedMenus, latestActions);
|
||||
console.log('按已保存常用菜单展示:', validFixedMenus);
|
||||
this.quickActions = validFixedMenus;
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 如果没有有效的固定菜单,检查用户菜单
|
||||
if (userMenus && userMenus.length > 0) {
|
||||
console.log('从用户菜单生成快捷入口:', userMenus);
|
||||
// 根据用户菜单生成快捷入口
|
||||
this.quickActions = this.processMenus(userMenus);
|
||||
this.quickActions = this.processMenus(userMenus, 24);
|
||||
} else {
|
||||
// 5. 如果还是没有菜单,返回空数组
|
||||
console.log('用户没有可用菜单');
|
||||
@ -210,30 +206,40 @@
|
||||
},
|
||||
|
||||
// 辅助方法:验证和过滤菜单权限
|
||||
validateAndFilterMenus(fixedMenus, userMenus) {
|
||||
if (!fixedMenus || !userMenus) return [];
|
||||
validateAndFilterMenus(fixedMenus, latestActions) {
|
||||
if (!Array.isArray(fixedMenus) || !Array.isArray(latestActions)) return [];
|
||||
|
||||
// 根据你的数据结构调整验证逻辑
|
||||
// 假设菜单项有 id 或 code 作为唯一标识
|
||||
return fixedMenus.filter(fixedMenu => {
|
||||
// 检查用户菜单中是否包含这个固定菜单
|
||||
return userMenus.some(userMenu => {
|
||||
// 根据你的实际数据结构调整比对逻辑
|
||||
// 示例1:如果有 id
|
||||
if (fixedMenu.id && userMenu.id) {
|
||||
return fixedMenu.id === userMenu.id;
|
||||
}
|
||||
// 示例2:如果有 menuCode
|
||||
if (fixedMenu.menuCode && userMenu.menuCode) {
|
||||
return fixedMenu.menuCode === userMenu.menuCode;
|
||||
}
|
||||
// 示例3:如果有 path
|
||||
if (fixedMenu.path && userMenu.path) {
|
||||
return fixedMenu.path === userMenu.path;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
const actionMap = new Map();
|
||||
latestActions.forEach(action => {
|
||||
actionMap.set(this.getMenuKey(action), action);
|
||||
});
|
||||
|
||||
return fixedMenus
|
||||
.map(fixedMenu => {
|
||||
const matched = actionMap.get(this.getMenuKey(fixedMenu));
|
||||
if (!matched) return null;
|
||||
return {
|
||||
...matched,
|
||||
...fixedMenu,
|
||||
name: matched.name,
|
||||
path: matched.path,
|
||||
component: matched.component,
|
||||
icon: matched.icon || fixedMenu.icon,
|
||||
id: matched.id || fixedMenu.id
|
||||
};
|
||||
})
|
||||
.filter(Boolean);
|
||||
},
|
||||
|
||||
getMenuKey(menu) {
|
||||
return String(
|
||||
menu?.component ||
|
||||
menu?.path ||
|
||||
menu?.id ||
|
||||
menu?.menuId ||
|
||||
menu?.name ||
|
||||
''
|
||||
);
|
||||
},
|
||||
|
||||
// 获取用户菜单
|
||||
@ -246,20 +252,24 @@
|
||||
},
|
||||
|
||||
// 处理菜单数据
|
||||
processMenus(menus) {
|
||||
processMenus(menus, limit = 24) {
|
||||
// 1. 展平所有子菜单(使用 flatMap 替代 map + filter)
|
||||
const allChildren = menus.flatMap(item =>
|
||||
item.children || [] // 确保 children 存在,否则返回空数组
|
||||
);
|
||||
|
||||
// 2. 过滤有效菜单并限制数量
|
||||
const validMenus = allChildren.filter(menu =>
|
||||
let validMenus = allChildren.filter(menu =>
|
||||
menu &&
|
||||
menu.component &&
|
||||
menu.component !== 'Layout' &&
|
||||
menu.meta &&
|
||||
menu.meta.title
|
||||
).slice(0, 24); // 最多显示24个
|
||||
);
|
||||
|
||||
if (typeof limit === 'number' && limit > 0) {
|
||||
validMenus = validMenus.slice(0, limit); // 默认最多显示24个
|
||||
}
|
||||
|
||||
// 3. 生成随机颜色的函数
|
||||
const generateRandomColor = () => {
|
||||
@ -274,7 +284,7 @@
|
||||
// 4. 转换为目标格式
|
||||
return validMenus.map(menu => {
|
||||
return {
|
||||
id: menu.id || menu.menuId || Math.random().toString(36).substr(2, 9),
|
||||
id: menu.id || menu.menuId || menu.component || menu.path || menu.meta?.title || '',
|
||||
name: menu.meta?.title || '未命名菜单',
|
||||
path: menu.path || '',
|
||||
component: menu.component || '',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user