init
This commit is contained in:
213
src/router/avue-router.js
Executable file
213
src/router/avue-router.js
Executable file
@@ -0,0 +1,213 @@
|
||||
import website from '@/config/website';
|
||||
import { getToken } from '@/utils/auth';
|
||||
import store from '@/store';
|
||||
import { generateIframePath, processUrlForQuery, isURL } from './router';
|
||||
const modules = import.meta.glob('../**/**/*.vue');
|
||||
|
||||
let RouterPlugin = function () {
|
||||
this.$router = null;
|
||||
this.$store = null;
|
||||
};
|
||||
RouterPlugin.install = function (option = {}) {
|
||||
this.$router = option.router;
|
||||
this.$store = option.store;
|
||||
let i18n = option.i18n.global;
|
||||
this.$router.$avueRouter = {
|
||||
safe: this,
|
||||
// 设置标题
|
||||
setTitle: title => {
|
||||
const defaultTitle = i18n.t('title');
|
||||
title = title ? `${title} | ${defaultTitle}` : defaultTitle;
|
||||
document.title = title;
|
||||
},
|
||||
closeTag: value => {
|
||||
let tag = value || this.$store.getters.tag;
|
||||
if (typeof value === 'string') {
|
||||
tag = this.$store.getters.tagList.find(ele => ele.fullPath === value);
|
||||
}
|
||||
this.$store.commit('DEL_TAG', tag);
|
||||
},
|
||||
generateTitle: (item, props = {}) => {
|
||||
let query = item[props.query || 'query'] || {};
|
||||
let title = query.name || item[props.label || 'label'];
|
||||
let meta = item[props.meta || 'meta'] || {};
|
||||
let key = meta.i18n;
|
||||
if (key) {
|
||||
const hasKey = i18n.te('route.' + key);
|
||||
if (hasKey) return i18n.t('route.' + key);
|
||||
}
|
||||
return title ? title.split(',')[0] : title;
|
||||
},
|
||||
//动态路由
|
||||
formatRoutes: function (aMenu = [], first) {
|
||||
const aRouter = [];
|
||||
const propsDefault = website.menu;
|
||||
|
||||
if (aMenu && aMenu.length === 0) return;
|
||||
for (let i = 0; i < aMenu.length; i++) {
|
||||
const oMenu = aMenu[i];
|
||||
let path = oMenu[propsDefault.path],
|
||||
isComponent = true,
|
||||
component = oMenu.component,
|
||||
name = oMenu[propsDefault.label] + ',' + oMenu.id,
|
||||
icon = oMenu[propsDefault.icon],
|
||||
children = oMenu[propsDefault.children],
|
||||
query = oMenu[propsDefault.query],
|
||||
meta = oMenu[propsDefault.meta];
|
||||
|
||||
// 处理iframe场景,如果path是URL且是一级路由,需要生成一个内部路径
|
||||
const isUrlPath = isURL(path);
|
||||
if (isUrlPath && first && !children?.length) {
|
||||
// 保存原始URL到query中(如果formatPath还没处理)
|
||||
if (!query || !query.url) {
|
||||
query = { url: processUrlForQuery(path) };
|
||||
}
|
||||
// 生成一个内部路径用于路由
|
||||
const generatedPath = generateIframePath(oMenu, propsDefault.path);
|
||||
if (generatedPath !== path) {
|
||||
path = generatedPath;
|
||||
oMenu[propsDefault.path] = path;
|
||||
}
|
||||
// 确保使用iframe组件
|
||||
if (!component || component.indexOf('iframe') === -1) {
|
||||
component = 'components/iframe/main';
|
||||
oMenu.component = component;
|
||||
}
|
||||
}
|
||||
if (option.keepAlive) {
|
||||
meta.keepAlive = option.keepAlive;
|
||||
}
|
||||
const isChild = !!(children && children.length !== 0);
|
||||
const oRouter = {
|
||||
path: path,
|
||||
component: (() => {
|
||||
// 判断是否为首路由
|
||||
if (first) {
|
||||
return modules[
|
||||
option.store.getters.isMacOs || !website.setting.menu
|
||||
? '../page/index/layout.vue'
|
||||
: '../page/index/index.vue'
|
||||
];
|
||||
// 判断是否为多层路由
|
||||
} else if (isChild && !first) {
|
||||
return modules['../page/index/layout.vue'];
|
||||
// 判断是否为最终的页面视图
|
||||
} else {
|
||||
let result = modules[`../${component}.vue`];
|
||||
if (!result) {
|
||||
isComponent = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
})(),
|
||||
name,
|
||||
icon,
|
||||
meta,
|
||||
query,
|
||||
redirect: (() => {
|
||||
if (!isChild && first) return `${path}`;
|
||||
else return '';
|
||||
})(),
|
||||
// 处理是否为一级路由
|
||||
children: !isChild
|
||||
? (() => {
|
||||
if (first) {
|
||||
oMenu[propsDefault.path] = `${path}`;
|
||||
let componentPath = oMenu.component || component;
|
||||
let result = modules[`../${componentPath}.vue`];
|
||||
if (!result) {
|
||||
isComponent = false;
|
||||
}
|
||||
let childName = name + '_index';
|
||||
let childQuery = oMenu[propsDefault.query] || query;
|
||||
return [
|
||||
{
|
||||
component: result,
|
||||
icon: icon,
|
||||
name: childName,
|
||||
meta: meta,
|
||||
query: childQuery,
|
||||
path: '',
|
||||
},
|
||||
];
|
||||
}
|
||||
return [];
|
||||
})()
|
||||
: (() => {
|
||||
return this.formatRoutes(children, false);
|
||||
})(),
|
||||
};
|
||||
const isIframeRoute =
|
||||
component === 'components/iframe/main' || oMenu.component === 'components/iframe/main';
|
||||
if ((!isURL(path) || isIframeRoute) && isComponent) aRouter.push(oRouter);
|
||||
}
|
||||
if (first) {
|
||||
aRouter.forEach(ele => this.safe.$router.addRoute(ele));
|
||||
} else {
|
||||
return aRouter;
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
export const formatPath = (ele, first) => {
|
||||
const propsDefault = website.menu;
|
||||
const icon = ele[propsDefault.icon];
|
||||
ele[propsDefault.icon] = !icon ? propsDefault.iconDefault : icon;
|
||||
ele.meta = {
|
||||
keepAlive: ele.isOpen === 2,
|
||||
};
|
||||
const iframeComponent = 'components/iframe/main';
|
||||
const iframeSrc = href => {
|
||||
// 替换&为#
|
||||
let processedHref = href.replace(/&/g, '#');
|
||||
|
||||
// 获取用户信息
|
||||
const userInfo = store.getters.userInfo || {};
|
||||
const userToken = getToken() || '';
|
||||
|
||||
// 定义替换参数映射
|
||||
const replacements = {
|
||||
token: userToken,
|
||||
userId: userInfo.userId || '',
|
||||
userName: userInfo.userName || '',
|
||||
roleName: userInfo.roleName || '',
|
||||
};
|
||||
|
||||
// 统一替换所有参数
|
||||
Object.entries(replacements).forEach(([key, value]) => {
|
||||
const pattern = new RegExp(`\\$\\{${key}\\}`, 'g');
|
||||
processedHref = processedHref.replace(pattern, value);
|
||||
});
|
||||
|
||||
return processedHref;
|
||||
};
|
||||
const isChild = !!(ele[propsDefault.children] && ele[propsDefault.children].length !== 0);
|
||||
if (!isChild && first) {
|
||||
ele.component = 'views' + ele[propsDefault.path];
|
||||
if (isURL(ele[propsDefault.href])) {
|
||||
let href = ele[propsDefault.href];
|
||||
ele.component = iframeComponent;
|
||||
ele[propsDefault.query] = {
|
||||
url: iframeSrc(href),
|
||||
};
|
||||
}
|
||||
} else {
|
||||
ele[propsDefault.children] &&
|
||||
ele[propsDefault.children].forEach(child => {
|
||||
child.component = 'views' + child[propsDefault.path];
|
||||
child.meta = {
|
||||
keepAlive: child.isOpen === 2,
|
||||
};
|
||||
if (isURL(child[propsDefault.href])) {
|
||||
let href = child[propsDefault.href];
|
||||
child[propsDefault.path] = ele[propsDefault.path] + '/' + child.code;
|
||||
child.component = iframeComponent;
|
||||
child[propsDefault.query] = {
|
||||
url: iframeSrc(href),
|
||||
};
|
||||
}
|
||||
formatPath(child);
|
||||
});
|
||||
}
|
||||
};
|
||||
export default RouterPlugin;
|
||||
1
src/router/ext/index.js
Executable file
1
src/router/ext/index.js
Executable file
@@ -0,0 +1 @@
|
||||
export default [];
|
||||
33
src/router/index.js
Executable file
33
src/router/index.js
Executable file
@@ -0,0 +1,33 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router';
|
||||
import ExtRouter from './ext/';
|
||||
import PageRouter from './page/';
|
||||
import ViewsRouter from './views/';
|
||||
import AvueRouter from './avue-router';
|
||||
import i18n from '@/lang';
|
||||
import Store from '@/store/';
|
||||
//创建路由
|
||||
const Router = createRouter({
|
||||
base: import.meta.env.VITE_APP_BASE,
|
||||
history: createWebHistory(import.meta.env.VITE_APP_BASE),
|
||||
routes: [...ExtRouter, ...PageRouter, ...ViewsRouter],
|
||||
});
|
||||
AvueRouter.install({
|
||||
store: Store,
|
||||
router: Router,
|
||||
i18n: i18n,
|
||||
});
|
||||
|
||||
Router.$avueRouter.formatRoutes(Store.getters.menuAll, true);
|
||||
|
||||
export function resetRouter() {
|
||||
// 重置路由 比如用于身份验证失败,需要重新登录时 先清空当前的路有权限
|
||||
const newRouter = createRouter();
|
||||
Router.matcher = newRouter.matcher; // reset router
|
||||
AvueRouter.install(Vue, {
|
||||
router: Router,
|
||||
store: Store,
|
||||
i18n: i18n,
|
||||
});
|
||||
}
|
||||
|
||||
export default Router;
|
||||
72
src/router/page/index.js
Executable file
72
src/router/page/index.js
Executable file
@@ -0,0 +1,72 @@
|
||||
import Store from '@/store/';
|
||||
|
||||
export default [
|
||||
{
|
||||
path: '/login',
|
||||
name: '登录页',
|
||||
component: () =>
|
||||
Store.getters.isMacOs ? import('@/mac/login.vue') : import('@/page/login/index.vue'),
|
||||
meta: {
|
||||
keepAlive: true,
|
||||
isTab: false,
|
||||
isAuth: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/oauth/redirect/:source',
|
||||
name: '第三方登录',
|
||||
component: () =>
|
||||
Store.getters.isMacOs ? import('@/mac/login.vue') : import('@/page/login/index.vue'),
|
||||
meta: {
|
||||
keepAlive: true,
|
||||
isTab: false,
|
||||
isAuth: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/lock',
|
||||
name: '锁屏页',
|
||||
component: () =>
|
||||
Store.getters.isMacOs ? import('@/mac/lock.vue') : import('@/page/lock/index.vue'),
|
||||
meta: {
|
||||
keepAlive: true,
|
||||
isTab: false,
|
||||
isAuth: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/404',
|
||||
component: () => import(/* webpackChunkName: "page" */ '@/components/error-page/404.vue'),
|
||||
name: '404',
|
||||
meta: {
|
||||
keepAlive: true,
|
||||
isTab: false,
|
||||
isAuth: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/403',
|
||||
component: () => import(/* webpackChunkName: "page" */ '@/components/error-page/403.vue'),
|
||||
name: '403',
|
||||
meta: {
|
||||
keepAlive: true,
|
||||
isTab: false,
|
||||
isAuth: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/500',
|
||||
component: () => import(/* webpackChunkName: "page" */ '@/components/error-page/500.vue'),
|
||||
name: '500',
|
||||
meta: {
|
||||
keepAlive: true,
|
||||
isTab: false,
|
||||
isAuth: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
name: '主页',
|
||||
redirect: '/wel',
|
||||
},
|
||||
];
|
||||
78
src/router/router.js
Normal file
78
src/router/router.js
Normal file
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* 路由工具函数
|
||||
*/
|
||||
|
||||
// 统一的URL正则表达式
|
||||
export const URL_REGEX = /^https?:\/\//;
|
||||
|
||||
/**
|
||||
* 生成iframe路径
|
||||
* @param {Object} item - 菜单项
|
||||
* @param {String} pathKey - 路径字段名
|
||||
* @returns {String} 生成的路径
|
||||
*/
|
||||
export function generateIframePath(item, pathKey = 'path') {
|
||||
const path = item[pathKey];
|
||||
if (!path) return path;
|
||||
|
||||
// 如果是URL且有ID,生成iframe路径
|
||||
if (URL_REGEX.test(path) && item.id) {
|
||||
return `/iframe/${item.id}`;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理URL参数,将&替换为#以避免路由参数问题
|
||||
* @param {String} url - 原始URL
|
||||
* @returns {String} 处理后的URL
|
||||
*/
|
||||
export function processUrlForQuery(url) {
|
||||
if (!url) return url;
|
||||
return url.replace(/&/g, '#');
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成iframe的query参数
|
||||
* @param {Object} item - 菜单项
|
||||
* @param {String} pathKey - 路径字段名
|
||||
* @param {String} queryKey - query字段名
|
||||
* @returns {Object} query参数
|
||||
*/
|
||||
export function generateIframeQuery(item, pathKey = 'path', queryKey = 'query') {
|
||||
const originalPath = item[pathKey];
|
||||
const existingQuery = item[queryKey] || {};
|
||||
|
||||
// 如果是URL且没有url参数,添加处理后的URL
|
||||
if (URL_REGEX.test(originalPath)) {
|
||||
// 保留现有query,只在没有url时添加
|
||||
if (!existingQuery.url) {
|
||||
return {
|
||||
...existingQuery,
|
||||
url: processUrlForQuery(originalPath)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return existingQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为iframe路由
|
||||
* @param {String} path - 当前路径
|
||||
* @param {String} originalPath - 原始路径
|
||||
* @returns {Boolean} 是否为iframe路由
|
||||
*/
|
||||
export function isIframeRoute(path, originalPath) {
|
||||
// 路径以/iframe/开头且原始路径是URL
|
||||
return path && path.startsWith('/iframe/') && URL_REGEX.test(originalPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断路径是否为URL
|
||||
* @param {String} path - 路径
|
||||
* @returns {Boolean} 是否为URL
|
||||
*/
|
||||
export function isURL(path) {
|
||||
return URL_REGEX.test(path);
|
||||
}
|
||||
126
src/router/views/index.js
Executable file
126
src/router/views/index.js
Executable file
@@ -0,0 +1,126 @@
|
||||
import Layout from '@/page/index/index.vue';
|
||||
import Store from '@/store/';
|
||||
|
||||
export default [
|
||||
{
|
||||
path: '/wel',
|
||||
component: () =>
|
||||
Store.getters.isMacOs ? import('@/mac/index.vue') : import('@/page/index/index.vue'),
|
||||
redirect: '/wel/index',
|
||||
children: [
|
||||
{
|
||||
path: 'index',
|
||||
name: '首页',
|
||||
meta: {
|
||||
i18n: 'dashboard',
|
||||
},
|
||||
component: () => import(/* webpackChunkName: "views" */ '@/views/wel/index.vue'),
|
||||
},
|
||||
{
|
||||
path: 'dashboard',
|
||||
name: '控制台',
|
||||
meta: {
|
||||
i18n: 'dashboard',
|
||||
menu: false,
|
||||
},
|
||||
component: () => import(/* webpackChunkName: "views" */ '@/views/wel/dashboard.vue'),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '/test',
|
||||
component: Layout,
|
||||
redirect: '/test/index',
|
||||
children: [
|
||||
{
|
||||
path: 'index',
|
||||
name: '测试页',
|
||||
meta: {
|
||||
i18n: 'test',
|
||||
},
|
||||
component: () => import(/* webpackChunkName: "views" */ '@/views/util/test.vue'),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '/dict-horizontal',
|
||||
component: Layout,
|
||||
redirect: '/dict-horizontal/index',
|
||||
children: [
|
||||
{
|
||||
path: 'index',
|
||||
name: '字典管理',
|
||||
meta: {
|
||||
i18n: 'dict',
|
||||
},
|
||||
component: () =>
|
||||
import(/* webpackChunkName: "views" */ '@/views/util/demo/dict-horizontal.vue'),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '/dict-vertical',
|
||||
component: Layout,
|
||||
redirect: '/dict-vertical/index',
|
||||
children: [
|
||||
{
|
||||
path: 'index',
|
||||
name: '字典管理',
|
||||
meta: {
|
||||
i18n: 'dict',
|
||||
},
|
||||
component: () =>
|
||||
import(/* webpackChunkName: "views" */ '@/views/util/demo/dict-vertical.vue'),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '/info',
|
||||
component: Layout,
|
||||
redirect: '/info/index',
|
||||
children: [
|
||||
{
|
||||
path: 'index',
|
||||
name: '个人信息',
|
||||
meta: {
|
||||
i18n: 'info',
|
||||
},
|
||||
component: () => import(/* webpackChunkName: "views" */ '@/views/system/userinfo.vue'),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '/work/process/leave',
|
||||
component: Layout,
|
||||
redirect: '/work/process/leave/form',
|
||||
children: [
|
||||
{
|
||||
path: 'form/:processDefinitionId',
|
||||
name: '请假流程',
|
||||
meta: {
|
||||
i18n: 'work',
|
||||
},
|
||||
component: () =>
|
||||
import(/* webpackChunkName: "views" */ '@/views/work/process/leave/form.vue'),
|
||||
},
|
||||
{
|
||||
path: 'handle/:taskId/:processInstanceId/:businessId',
|
||||
name: '处理请假流程',
|
||||
meta: {
|
||||
i18n: 'work',
|
||||
},
|
||||
component: () =>
|
||||
import(/* webpackChunkName: "views" */ '@/views/work/process/leave/handle.vue'),
|
||||
},
|
||||
{
|
||||
path: 'detail/:processInstanceId/:businessId',
|
||||
name: '请假流程详情',
|
||||
meta: {
|
||||
i18n: 'work',
|
||||
},
|
||||
component: () =>
|
||||
import(/* webpackChunkName: "views" */ '@/views/work/process/leave/detail.vue'),
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
Reference in New Issue
Block a user