老产品前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

484 lines
12 KiB

4 years ago
import Vue from "vue";
import Router from "vue-router";
import http from "@/utils/request";
import { isURL } from "@/utils/validate";
import store from "@/js/store";
4 years ago
4 years ago
Vue.use(Router);
4 years ago
// 页面路由(独立页面)
4 years ago
export const pageRoutes = [
{
path: "/404",
component: () => import("@/views/pages/404"),
name: "404",
4 years ago
meta: {
4 years ago
title: "404未找到",
4 years ago
},
beforeEnter(to, from, next) {
// 拦截处理特殊业务场景
// 如果, 重定向路由包含__双下划线, 为临时添加路由
if (/__.*/.test(to.redirectedFrom)) {
4 years ago
return next(to.redirectedFrom.replace(/__.*/, ""));
4 years ago
}
4 years ago
next();
},
4 years ago
},
{
4 years ago
path: "/login",
component: () => import("@/views/pages/login"),
name: "login",
4 years ago
meta: {
4 years ago
title: "登录",
},
4 years ago
},
{
4 years ago
path: "/",
name: "index",
4 years ago
component: () => import("@/views/pages/index"),
4 years ago
},
{
4 years ago
path: "/indexWork",
name: "indexWork",
4 years ago
redirect: {
name: "index",
4 years ago
},
},
{
path: "/caiji/:id",
props: true,
name: "caiji",
component: () => import("@/views/pages/caiji"),
meta: {
title: "居民信息填报",
},
},
{
path: "/caijisuccess",
props: true,
name: "caijisuccess",
component: () => import("@/views/pages/caijisuccess"),
meta: {
title: "居民信息填报",
},
},
3 years ago
{
path: "/staffRegister",
props: true,
name: "staffRegister",
component: () => import("@/views/pages/staffRegister"),
meta: {
title: "员工登记",
},
},
4 years ago
];
4 years ago
// 模块路由(基于主入口布局页面)
export const moduleRoutes = {
4 years ago
path: "/main",
component: () => import("@/views/main"),
name: "main",
4 years ago
redirect: {
path: "/main/shequ-index",
4 years ago
},
meta: {
4 years ago
title: "主入口布局",
4 years ago
},
4 years ago
children: [
{
path: "/home",
component: () => import("@/views/modules/home"),
name: "home",
4 years ago
meta: {
4 years ago
title: "主页",
isTab: true,
},
},
3 years ago
{
path: "unopen",
component: () => import("@/views/modules/unopen"),
name: "unopen",
meta: {
title: "未开放功能",
isTab: true,
},
},
3 years ago
{
path: "404",
props: true,
component: () => import("@/views/modules/404"),
name: "main-404",
meta: {
title: "页面未找到或无权访问",
isTab: false,
},
},
4 years ago
],
};
4 years ago
// pc端菜单的 如果没有配置 默认的 或者配置了默认的 但没有权限 默认选中第一
function cookApiDataItem(item) {
function n2one(v) {
let ret = [];
for (let i = 0; i < v.length; i++) {
// console.log("xxxxx", v[i]);
ret.push(v[i]);
if (Array.isArray(v[i].children) && v[i].children.length > 0) {
ret.push(...n2one(v[i].children));
}
}
// console.log("aaaa", ret);
return ret;
}
let allChildrenUrl = n2one(item.children)
.filter((i) => i.url != "" && i.showFlag == 1)
.map((i) => i.url);
// console.log("))))))))))))", allChildrenUrl);
if (item.children.length > 0) {
if (item.url == "" || !allChildrenUrl.find((i) => i.url == item.url)) {
item.url = allChildrenUrl[0];
}
}
// item.children.forEach((item) => {
// item.color = "#f00";
// return item;
// });
return item;
}
4 years ago
// 模块路由(基于主入口布局页面) 可视化数据
export const moduleShujuRoutes = {
4 years ago
path: "/main-shuju",
component: () => import("@/views/main-shuju/main"),
name: "main-shuju",
4 years ago
redirect: {
3 years ago
path: "/main-shuju/visual-basicinfo-basicInfoMain",
4 years ago
},
meta: {
4 years ago
title: "主入口布局",
4 years ago
},
4 years ago
children: [
4 years ago
{
4 years ago
path: "/main-shuju/visual-basicinfo-people/:uid",
props: true,
4 years ago
component: () => import("@/views/modules/visual/basicinfo/people"),
name: "visual-basicinfo-people",
meta: {
title: "个人详情",
isTab: false,
4 years ago
},
},
{
path: "/main-shuju/visual-basicinfo-people-list",
props: true,
component: () => import("@/views/modules/visual/basicinfo/peopleList"),
name: "visual-basicinfo-people-list",
meta: {
title: "人员列表",
isTab: false,
4 years ago
},
},
3 years ago
{
path: "404",
props: true,
component: () => import("@/views/modules/visual/404"),
name: "main-shuju-404",
meta: {
title: "页面未找到或无权访问",
isTab: false,
},
},
4 years ago
],
};
4 years ago
export function addDynamicRoute(routeParams, router) {
// 组装路由名称, 并判断是否已添加, 如是: 则直接跳转
4 years ago
var routeName = routeParams.routeName;
var dynamicRoute = window.SITE_CONFIG["dynamicRoutes"].filter(
4 years ago
(item) => item.name === routeName
4 years ago
)[0];
4 years ago
if (dynamicRoute) {
return router.push({
name: routeName,
4 years ago
params: routeParams.params,
});
4 years ago
}
// 否则: 添加并全局变量保存, 再跳转
dynamicRoute = {
path: routeName,
component: () => import(`@/views/modules/${routeParams.path}`),
name: routeName,
meta: {
4 years ago
...window.SITE_CONFIG["contentTabDefault"],
4 years ago
menuId: routeParams.menuId,
4 years ago
title: `${routeParams.title}`,
},
};
router.addRoutes([
{
...moduleRoutes,
name: `main-dynamic__${dynamicRoute.name}`,
children: [dynamicRoute],
},
]);
window.SITE_CONFIG["dynamicRoutes"].push(dynamicRoute);
4 years ago
router.push({
name: dynamicRoute.name,
4 years ago
params: routeParams.params,
});
4 years ago
}
const router = new Router({
4 years ago
mode: "hash",
4 years ago
scrollBehavior: () => ({
4 years ago
y: 0,
4 years ago
}),
4 years ago
routes: pageRoutes.concat(moduleRoutes, moduleShujuRoutes),
});
4 years ago
router.beforeEach((to, from, next) => {
// 添加动态(菜单)路由
// 已添加或者当前路由为页面路由, 可直接访问
3 years ago
if (to.name === "caiji") {
return next();
}
4 years ago
if (
4 years ago
window.SITE_CONFIG["dynamicMenuRoutesHasAdded"] ||
4 years ago
fnCurrentRouteIsPageRoute(to, pageRoutes)
) {
4 years ago
return next();
4 years ago
}
4 years ago
const customerId = localStorage.getItem("customerId");
console.log("customerId:", customerId);
4 years ago
http
3 years ago
.get("gov/access/menu/navDigitalCommunity?tableName=data_menu")
.then(({ data: res }) => {
if (res.code == 10005 || res.code == 10006 || res.code == 10007) {
// 10005 token为空 10006登陆超时 10007别处登录
console.log("登录失效");
Vue.prototype.$message.error(res.msg);
return next({
name: "login",
});
}
window.SITE_CONFIG["menuShujuList"] = res.data;
fnAddDynamicMenuRoutes2(window.SITE_CONFIG["menuShujuList"]);
next({
...to,
replace: true,
});
})
.catch(() => {
next({
name: "login",
});
});
4 years ago
http
3 years ago
.get("gov/access/menu/navDigitalCommunity")
4 years ago
.then(({ data: res }) => {
3 years ago
if (res.code == 10005 || res.code == 10006 || res.code == 10007) {
// 10005 token为空 10006登陆超时 10007别处登录
console.log("登录失效");
Vue.prototype.$message.error(res.msg);
return next({
name: "login",
});
4 years ago
}
window.SITE_CONFIG["menuList"] = res.data.map((item) =>
cookApiDataItem(item)
);
4 years ago
fnAddDynamicMenuRoutes(window.SITE_CONFIG["menuList"]);
// next({ ...to, replace: true })
next({
...to,
replace: true,
});
})
.catch(() => {
3 years ago
// next({
// name: "login",
// });
4 years ago
});
});
4 years ago
/**
* 判断当前路由是否为页面路由
* @param {*} route 当前路由
* @param {*} pageRoutes 页面路由
*/
function fnCurrentRouteIsPageRoute(route, pageRoutes = []) {
4 years ago
var temp = [];
4 years ago
for (var i = 0; i < pageRoutes.length; i++) {
if (route.path === pageRoutes[i].path) {
4 years ago
return true;
4 years ago
}
if (pageRoutes[i].children && pageRoutes[i].children.length >= 1) {
4 years ago
temp = temp.concat(pageRoutes[i].children);
4 years ago
}
}
4 years ago
return temp.length >= 1 ? fnCurrentRouteIsPageRoute(route, temp) : false;
4 years ago
}
/**
* 添加动态(菜单)路由
* @param {*} menuList 菜单列表
* @param {*} routes 递归创建的动态(菜单)路由
*/
function fnAddDynamicMenuRoutes(menuList = [], routes = []) {
4 years ago
var temp = [];
4 years ago
for (var i = 0; i < menuList.length; i++) {
if (menuList[i].children && menuList[i].children.length >= 1) {
4 years ago
temp = temp.concat(menuList[i].children);
// continue;
4 years ago
}
// 组装路由
var route = {
4 years ago
path: "",
4 years ago
component: null,
4 years ago
name: "",
4 years ago
meta: {
4 years ago
...window.SITE_CONFIG["contentTabDefault"],
4 years ago
menuId: menuList[i].id,
4 years ago
title: menuList[i].name,
},
};
4 years ago
// eslint-disable-next-line
4 years ago
let URL = (menuList[i].url || "").replace(/{{([^}}]+)?}}/g, (s1, s2) =>
4 years ago
eval(s2)
4 years ago
); // URL支持{{ window.xxx }}占位符变量
4 years ago
if (isURL(URL)) {
if (menuList[i].children && menuList[i].children.length >= 1) {
let item = menuList[i].children.find(
(subMenu) => subMenu.url == menuList[i].url
);
if (item) {
route["path"] = route["name"] = `i-${menuList[i].id}`;
route["redirect"] = `i-${item.id}`;
} else {
route["path"] = route["name"] = `i-${menuList[i].id}`;
route["meta"]["iframeURL"] = URL;
}
} else {
route["path"] = route["name"] = `i-${menuList[i].id}`;
route["meta"]["iframeURL"] = URL;
}
4 years ago
} else {
4 years ago
URL = URL.replace(/^\//, "").replace(/_/g, "-");
3 years ago
if (URL.startsWith("unopen")) {
route["path"] = route["name"] = URL.replace(/\//g, "-");
route["component"] = () => import(`@/views/modules/unopen`);
} else if (menuList[i].children && menuList[i].children.length >= 1) {
3 years ago
route["path"] = route["name"] = URL;
route["redirect"] = URL.replace(/\//g, "-");
} else {
route["path"] = route["name"] = URL.replace(/\//g, "-");
route["component"] = () => import(`@/views/modules/${URL}`);
}
4 years ago
}
4 years ago
routes.push(route);
4 years ago
}
if (temp.length >= 1) {
4 years ago
return fnAddDynamicMenuRoutes(temp, routes);
4 years ago
}
3 years ago
// routers.reverse();
4 years ago
// 添加路由
4 years ago
router.addRoutes([
{
4 years ago
...moduleRoutes,
4 years ago
name: "main-dynamic-menu",
children: routes,
4 years ago
},
{
3 years ago
path: "/main/*",
redirect: {
path: "/main/404",
},
4 years ago
},
]);
window.SITE_CONFIG["dynamicMenuRoutes"] = routes;
window.SITE_CONFIG["dynamicMenuRoutesHasAdded"] = true;
4 years ago
}
4 years ago
function fnAddDynamicMenuRoutes2(menuList = [], routes = []) {
4 years ago
var temp = [];
4 years ago
for (var i = 0; i < menuList.length; i++) {
if (menuList[i].children && menuList[i].children.length >= 1) {
4 years ago
temp = temp.concat(menuList[i].children);
continue;
4 years ago
}
// 组装路由
var route = {
4 years ago
path: "",
4 years ago
component: null,
4 years ago
name: "",
4 years ago
meta: {
4 years ago
// ...window.SITE_CONFIG["contentTabDefault"],
4 years ago
menuId: menuList[i].id,
4 years ago
title: menuList[i].name,
},
};
4 years ago
// eslint-disable-next-line
4 years ago
let URL = (menuList[i].url || "").replace(/{{([^}}]+)?}}/g, (s1, s2) =>
4 years ago
eval(s2)
4 years ago
); // URL支持{{ window.xxx }}占位符变量
4 years ago
if (isURL(URL)) {
3 years ago
if (menuList[i].children && menuList[i].children.length >= 1) {
let item = menuList[i].children.find(
(subMenu) => subMenu.url == menuList[i].url
);
if (item) {
route["path"] = route["name"] = `i-${menuList[i].id}`;
route["redirect"] = `i-${item.id}`;
} else {
route["path"] = route["name"] = `i-${menuList[i].id}`;
route["meta"]["iframeURL"] = URL;
}
} else {
route["path"] = route["name"] = `i-${menuList[i].id}`;
route["meta"]["iframeURL"] = URL;
}
4 years ago
} else {
4 years ago
URL = URL.replace(/^\//, "").replace(/_/g, "-");
3 years ago
if (URL.startsWith("unopen")) {
route["path"] = route["name"] = URL.replace(/\//g, "-");
route["component"] = () => import(`@/views/modules/unopen`);
} else if (menuList[i].children && menuList[i].children.length >= 1) {
route["path"] = route["name"] = URL;
route["redirect"] = URL.replace(/\//g, "-");
} else {
route["path"] = route["name"] = URL.replace(/\//g, "-");
route["component"] = () => import(`@/views/modules/${URL}`);
}
4 years ago
}
4 years ago
routes.push(route);
4 years ago
}
if (temp.length >= 1) {
4 years ago
return fnAddDynamicMenuRoutes2(temp, routes);
4 years ago
}
// 添加路由
4 years ago
router.addRoutes([
{
4 years ago
...moduleShujuRoutes,
4 years ago
name: "main-dynamic-menu-shuju",
children: routes,
4 years ago
},
{
3 years ago
path: "/main-shuju/*",
redirect: {
path: "/main-shuju/404",
},
4 years ago
},
]);
window.SITE_CONFIG["dynamicMenuRoutesShuju"] = routes;
window.SITE_CONFIG["dynamicMenuRoutesShujuHasAdded"] = true;
4 years ago
}
4 years ago
export default router;