From bd23e4f4d293975a19df50844e6899d42e66e5ae Mon Sep 17 00:00:00 2001 From: LCJ-MinYa <1049468118@qq.com> Date: Thu, 10 Jul 2025 16:06:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=AE=9A=E4=B9=89=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E4=BA=8C=E7=BA=A7=E7=9B=AE=E5=BD=95=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/modules/demo.ts | 18 ++++++--- src/router/modules/python.ts | 60 ++++++++++++++++++++++++++++ src/utils/tools.ts | 6 ++- src/views/python/init/init.vue | 17 ++++++++ src/views/python/init/python-init.md | 0 5 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 src/router/modules/python.ts create mode 100644 src/views/python/init/init.vue create mode 100644 src/views/python/init/python-init.md diff --git a/src/router/modules/demo.ts b/src/router/modules/demo.ts index 3bc2bd3..adf546f 100644 --- a/src/router/modules/demo.ts +++ b/src/router/modules/demo.ts @@ -1,5 +1,10 @@ import { route } from '@/utils/tools'; const titleArr = [ + { + key: 'code', + title: '面试手写题', + rank: 1, + }, { key: 'jsx', title: '使用jsx', @@ -43,6 +48,7 @@ const titleArr = [ { key: 'autoLoadData', title: '首页高度足够,自动触发下拉加载数据', + rank: 2, }, { key: 'dynamicNodeEvent', @@ -80,10 +86,6 @@ const titleArr = [ key: 'sendBeacon', title: 'sendBeacon离开页面埋点保证送达方案', }, - { - key: 'code', - title: '面试手写题', - }, { key: 'getSymbolObject', title: '获取对象中属性为Symbol类型的方法', @@ -128,16 +130,20 @@ const demoRoutes = Object.keys(components).map((path) => { component: components[path], meta: { title: route.getTitleFromPathStr(titleArr, cptPath, '/demo/'), + rank: route.getRankFromPathStr(titleArr, cptPath, '/demo/'), }, }; }); +// 子菜单自己实现排序,根据rank字段,默认值99 +demoRoutes.sort((a, b) => Number(a.meta.rank) - Number(b.meta.rank)); + export default { path: '/demo', - redirect: '/demo/jsx', + redirect: '/demo/code', meta: { icon: 'ep:data-analysis', - title: '示例', + title: '前端示例', rank: 1, }, children: [...demoRoutes], diff --git a/src/router/modules/python.ts b/src/router/modules/python.ts new file mode 100644 index 0000000..5faaa76 --- /dev/null +++ b/src/router/modules/python.ts @@ -0,0 +1,60 @@ +import { route } from '@/utils/tools'; +const titleArr = [ + { + key: 'init', + title: '初始化环境和工程', + }, +]; + +// @/views/demo/**/*.vue +/** + * (使用 ** 表示递归查找) + * 递归匹配views/demo目录下所有.vue文件 + * 也包含/demo/jsx/components/index.vue 这种某个页面的子组件 + */ + +// @/views/demo/*.vue 第一层目录 +// @/views/demo/*/*.vue 第二层目录 +/** + * 第一层目录 => 匹配views/demo目录下所有.vue文件(第一层) + * 例如 /views/demo/index.vue + * + * 第二层目录 => 匹配views/demo目录下所有文件夹下的.vue文件(第二层) + * 例如 /views/demo/jsx/index.vue + * + * 不会匹配/views/demo/jsx/components/index.vue 这种某个页面的子组件 + */ + +const firstLevelComponents = import.meta.glob('@/views/python/*.vue'); +const secondLevelComponents = import.meta.glob('@/views/python/*/*.vue'); +const components = { ...firstLevelComponents, ...secondLevelComponents }; + +const pythonRoutes = Object.keys(components).map((path) => { + const isHasIndex = path.includes('/index.vue'); + let cptPath = path.replace(route.FILE_NAME_PREFIX, ''); + cptPath = isHasIndex ? cptPath.replace('/index.vue', '') : cptPath.replace('.vue', ''); + cptPath = route.mergeDuplicatePathSegments(cptPath); + return { + path: cptPath, + name: route.convertPathToName(cptPath), + component: components[path], + meta: { + title: route.getTitleFromPathStr(titleArr, cptPath, '/python/'), + rank: route.getRankFromPathStr(titleArr, cptPath, '/python/'), + }, + }; +}); + +// 子菜单自己实现排序,根据rank字段,默认值99 +pythonRoutes.sort((a, b) => Number(a.meta.rank) - Number(b.meta.rank)); + +export default { + path: '/python', + redirect: '/python/init', + meta: { + icon: 'ep:data-analysis', + title: 'python示例', + rank: 1, + }, + children: [...pythonRoutes], +} satisfies RouteConfigsTable; diff --git a/src/utils/tools.ts b/src/utils/tools.ts index b90dfc5..55d247d 100644 --- a/src/utils/tools.ts +++ b/src/utils/tools.ts @@ -41,7 +41,11 @@ export const route = { }, getTitleFromPathStr: (arr: any, path: string, ignoreFields: string = ''): string => { const item = arr.find(({ key }) => path.includes(key)); - return item ? item.title : path.replace(ignoreFields, ''); // 如果找到,返回 title;否则返回 null + return item ? item.title : path.replace(ignoreFields, ''); // 如果找到,返回 title;否则返回文件名 + }, + getRankFromPathStr: (arr: any, path: string, ignoreFields: string = ''): string => { + const item = arr.find(({ key }) => path.includes(key)); + return item && item.rank ? item.rank : 99; // 如果找到,返回 title;否则返回99 }, mergeDuplicatePathSegments(path: string) { // 移除开头和结尾的斜杠,然后按斜杠分割 diff --git a/src/views/python/init/init.vue b/src/views/python/init/init.vue new file mode 100644 index 0000000..b3bbf05 --- /dev/null +++ b/src/views/python/init/init.vue @@ -0,0 +1,17 @@ + + + diff --git a/src/views/python/init/python-init.md b/src/views/python/init/python-init.md new file mode 100644 index 0000000..e69de29