Explorar el Código

修改菜单逻辑,及接口返回数据接口变化进行菜单数据结构转化,适配系统使用

yanglzh hace 3 años
padre
commit
b0e73df34e
Se han modificado 3 ficheros con 32 adiciones y 21 borrados
  1. 2 2
      src/layout/navMenu/horizontal.vue
  2. 16 5
      src/router/backEnd.ts
  3. 14 14
      src/views/login/component/account.vue

+ 2 - 2
src/layout/navMenu/horizontal.vue

@@ -71,7 +71,7 @@ export default defineComponent({
 		// 路由过滤递归函数
 		const filterRoutesFun = (arr: Array<object>) => {
 			return arr
-				.filter((item: any) => !item.meta?.isHide)
+				.filter((item: any) => !item.meta.isHide)
 				.map((item: any) => {
 					item = Object.assign({}, item);
 					if (item.children) item.children = filterRoutesFun(item.children);
@@ -99,7 +99,7 @@ export default defineComponent({
 				(<any>state.defaultActive) = `/${path.split('/')[1]}`;
 			} else {
 				const pathSplit = meta.isDynamic ? meta.isDynamicPath.split('/') : path.split('/');
-				if (pathSplit.length >= 4 && meta?.isHide) state.defaultActive = pathSplit.splice(0, 3).join('/');
+				if (pathSplit.length >= 4 && meta.isHide) state.defaultActive = pathSplit.splice(0, 3).join('/');
 				else state.defaultActive = path;
 			}
 		};

+ 16 - 5
src/router/backEnd.ts

@@ -2,7 +2,7 @@ import { store } from '/@/store/index.ts';
 import { Session } from '/@/utils/storage';
 import { NextLoading } from '/@/utils/loading';
 import { setAddRoute, setFilterMenuAndCacheTagsViewRoutes } from '/@/router/index';
-import {demoRoutes, dynamicRoutes} from '/@/router/route';
+import { demoRoutes, dynamicRoutes } from '/@/router/route';
 import { currentUser } from '/@/api/login';
 
 
@@ -43,7 +43,7 @@ export async function initBackEndControlRoutes() {
 	// 存储接口原始路由(未处理component),根据需求选择使用
 	store.dispatch('requestOldRoutes/setBackEndControlRoutes', JSON.parse(JSON.stringify(menuRoute)));
 	// 处理路由(component),替换 dynamicRoutes(/@/router/route)第一个顶级 children 的路由
-	dynamicRoutes[0].children?.push(...await backEndComponent(menuRoute),...demoRoutes) ;
+	dynamicRoutes[0].children?.push(...await backEndComponent(menuRoute), ...demoRoutes);
 	// 添加动态路由
 	await setAddRoute();
 	// 设置递归过滤有权限的路由到 vuex routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
@@ -57,8 +57,7 @@ export async function initBackEndControlRoutes() {
  */
 export async function getBackEndControlRoutes() {
 	return currentUser().then((res: any) => {
-		console.log(res)
-		// Session.set('userMenu',res.data.menuList)
+		Session.set('userMenu', res)
 		// Session.set('permissions',res.data.permissions)
 		// store.dispatch('userInfos/setPermissions',res.data.permissions)
 	})
@@ -82,7 +81,19 @@ export function backEndComponent(routes: any) {
 	if (!routes) return;
 	return routes.map((item: any) => {
 		if (item.component) item.component = dynamicImport(dynamicViewsModules, item.component as string);
-		item.children && backEndComponent(item.children);
+		// 将 大写Children 转换为系统里用的小写的 children
+		item.children = item.Children
+		item.children?.length && backEndComponent(item.children);
+		// 将 meta 信息进行整理
+		item.meta = {
+			icon: item.icon,
+			isAffix: item.isAffix,
+			isHide: item.isHide,
+			isIframe: item.isIframe,
+			isKeepAlive: item.isKeepAlive,
+			isLink: item.isLink,
+			title: item.title,
+		}
 		return item;
 	});
 }

+ 14 - 14
src/views/login/component/account.vue

@@ -62,7 +62,7 @@ import * as api from '/@/api/login';
 export default defineComponent({
 	name: 'loginAccount',
 	setup() {
-			console.log('setup')
+		console.log('setup');
 		const { t } = useI18n();
 		const store = useStore();
 		const route = useRoute();
@@ -87,7 +87,7 @@ export default defineComponent({
 			captchaSrc: '',
 		});
 		onMounted(() => {
-			console.log('onMounted')
+			console.log('onMounted');
 			getCaptcha();
 			currentUser();
 		});
@@ -132,26 +132,26 @@ export default defineComponent({
 		};
 		// 获取登录用户信息
 		const currentUser = async () => {
-			api.currentUser().then((res) => {
+			api.currentUser().then(async (res) => {
 				// 设置用户菜单
 				Session.set('userMenu', res);
 				store.dispatch('requestOldRoutes/setBackEndControlRoutes', res);
+				if (!store.state.themeConfig.themeConfig.isRequestRoutes) {
+					// 前端控制路由,2、请注意执行顺序
+					await initFrontEndControlRoutes();
+					signInSuccess();
+				} else {
+					// 模拟后端控制路由,isRequestRoutes 为 true,则开启后端控制路由
+					// 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
+					await initBackEndControlRoutes();
+					// 执行完 initBackEndControlRoutes,再执行 signInSuccess
+					signInSuccess();
+				}
 			});
 			// // 设置按钮权限
 			// Session.set('permissions', res.data.permissions);
 			// // 1、请注意执行顺序(存储用户信息到vuex)
 			// await store.dispatch('userInfos/setPermissions', res.data.permissions);
-			// if (!store.state.themeConfig.themeConfig.isRequestRoutes) {
-			// 	// 前端控制路由,2、请注意执行顺序
-			// 	await initFrontEndControlRoutes();
-			// 	signInSuccess();
-			// } else {
-			// 	// 模拟后端控制路由,isRequestRoutes 为 true,则开启后端控制路由
-			// 	// 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
-			// 	await initBackEndControlRoutes();
-			// 	// 执行完 initBackEndControlRoutes,再执行 signInSuccess
-			// 	signInSuccess();
-			// }
 		};
 		// 登录成功后的跳转
 		const signInSuccess = () => {