2021-09-22 15:08:34 +08:00
|
|
|
|
import { getCurrentPermissionList } from "@/api/index";
|
|
|
|
|
import lazyLoading from "./lazyLoading.js";
|
2021-05-13 10:56:04 +08:00
|
|
|
|
|
2021-09-22 15:08:34 +08:00
|
|
|
|
import Cookies from "js-cookie";
|
2021-05-13 10:56:04 +08:00
|
|
|
|
|
2021-09-22 15:08:34 +08:00
|
|
|
|
let util = {};
|
2021-05-13 10:56:04 +08:00
|
|
|
|
|
2021-09-22 15:08:34 +08:00
|
|
|
|
util.title = function(title) {
|
|
|
|
|
title = title || "运营后台";
|
2021-08-11 18:47:59 +08:00
|
|
|
|
window.document.title = title;
|
2021-05-13 10:56:04 +08:00
|
|
|
|
};
|
2021-08-11 18:47:59 +08:00
|
|
|
|
// 判断元素是否存在于数组中
|
2021-09-22 15:08:34 +08:00
|
|
|
|
util.oneOf = function(ele, targetArr) {
|
2021-08-11 18:47:59 +08:00
|
|
|
|
if (targetArr.indexOf(ele) >= 0) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-05-13 10:56:04 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-08-11 18:47:59 +08:00
|
|
|
|
// 打开新的页面
|
2021-09-22 15:08:34 +08:00
|
|
|
|
util.openNewPage = function(vm, name, argu, query) {
|
2021-05-13 10:56:04 +08:00
|
|
|
|
if (!vm.$store) {
|
2021-08-11 18:47:59 +08:00
|
|
|
|
return;
|
2021-05-13 10:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
let pageOpenedList = vm.$store.state.app.pageOpenedList;
|
|
|
|
|
let openedPageLen = pageOpenedList.length;
|
|
|
|
|
let i = 0;
|
|
|
|
|
let tagHasOpened = false;
|
|
|
|
|
while (i < openedPageLen) {
|
2021-09-22 15:08:34 +08:00
|
|
|
|
if (name == pageOpenedList[i].name) {
|
|
|
|
|
// 页面已经打开
|
|
|
|
|
vm.$store.commit("pageOpenedList", {
|
2021-08-11 18:47:59 +08:00
|
|
|
|
index: i,
|
|
|
|
|
argu: argu,
|
|
|
|
|
query: query
|
|
|
|
|
});
|
|
|
|
|
tagHasOpened = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
2021-05-13 10:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
if (!tagHasOpened) {
|
2021-09-22 15:08:34 +08:00
|
|
|
|
let tag = vm.$store.state.app.tagsList.filter(item => {
|
2021-08-11 18:47:59 +08:00
|
|
|
|
if (item.children) {
|
|
|
|
|
return name == item.children[0].name;
|
|
|
|
|
} else {
|
|
|
|
|
return name == item.name;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
tag = tag[0];
|
|
|
|
|
if (tag) {
|
|
|
|
|
tag = tag.children ? tag.children[0] : tag;
|
|
|
|
|
if (argu) {
|
|
|
|
|
tag.argu = argu;
|
|
|
|
|
}
|
|
|
|
|
if (query) {
|
|
|
|
|
tag.query = query;
|
2021-05-13 10:56:04 +08:00
|
|
|
|
}
|
2021-09-22 15:08:34 +08:00
|
|
|
|
vm.$store.commit("increateTag", tag);
|
2021-08-11 18:47:59 +08:00
|
|
|
|
}
|
2021-05-13 10:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-22 15:08:34 +08:00
|
|
|
|
util.toDefaultPage = function(routers, name, route, next) {
|
2021-08-11 18:47:59 +08:00
|
|
|
|
let len = routers.length;
|
|
|
|
|
let i = 0;
|
|
|
|
|
let notHandle = true;
|
|
|
|
|
while (i < len) {
|
2021-09-22 15:08:34 +08:00
|
|
|
|
if (
|
|
|
|
|
routers[i].name == name &&
|
|
|
|
|
routers[i].children &&
|
|
|
|
|
routers[i].redirect == undefined
|
|
|
|
|
) {
|
2021-08-11 18:47:59 +08:00
|
|
|
|
route.replace({
|
|
|
|
|
name: routers[i].children[0].name
|
|
|
|
|
});
|
|
|
|
|
notHandle = false;
|
|
|
|
|
next();
|
|
|
|
|
break;
|
2021-05-13 10:56:04 +08:00
|
|
|
|
}
|
2021-08-11 18:47:59 +08:00
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
if (notHandle) {
|
|
|
|
|
next();
|
|
|
|
|
}
|
2021-05-13 10:56:04 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-09-22 15:08:34 +08:00
|
|
|
|
util.initRouter = function(vm) {
|
|
|
|
|
// 初始化路由
|
2021-08-11 18:47:59 +08:00
|
|
|
|
const constRoutes = [];
|
|
|
|
|
const otherRoutes = [];
|
|
|
|
|
|
|
|
|
|
// 404路由需要和动态路由一起加载
|
2021-09-22 15:08:34 +08:00
|
|
|
|
const otherRouter = [
|
|
|
|
|
{
|
|
|
|
|
path: "/*",
|
|
|
|
|
name: "error-404",
|
|
|
|
|
meta: {
|
|
|
|
|
title: "404-页面不存在"
|
|
|
|
|
},
|
|
|
|
|
frontRoute: "error-page/404"
|
|
|
|
|
}
|
|
|
|
|
];
|
2021-08-11 18:47:59 +08:00
|
|
|
|
// 判断用户是否登录
|
2021-09-22 15:08:34 +08:00
|
|
|
|
let userInfo = Cookies.get("userInfoManager");
|
2021-08-11 18:47:59 +08:00
|
|
|
|
if (!userInfo) {
|
|
|
|
|
// 未登录
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!vm.$store.state.app.added) {
|
|
|
|
|
// 第一次加载 读取数据
|
|
|
|
|
// 加载菜单
|
|
|
|
|
getCurrentPermissionList().then(res => {
|
|
|
|
|
if (!res.success) return false;
|
|
|
|
|
let menuData = res.result;
|
|
|
|
|
|
|
|
|
|
// 格式化数据,设置 空children 为 null
|
|
|
|
|
for (let i = 0; i < menuData.length; i++) {
|
2021-09-22 15:08:34 +08:00
|
|
|
|
let t = menuData[i].children;
|
2021-08-11 18:47:59 +08:00
|
|
|
|
for (let k = 0; k < t.length; k++) {
|
|
|
|
|
let tt = t[k].children;
|
|
|
|
|
for (let z = 0; z < tt.length; z++) {
|
2021-09-22 15:08:34 +08:00
|
|
|
|
tt[z].children = null;
|
2021-08-11 18:47:59 +08:00
|
|
|
|
// 给所有三级路由添加字段,显示一级菜单name,方便点击页签时的选中筛选
|
2021-09-22 15:08:34 +08:00
|
|
|
|
tt[z].firstRouterName = menuData[i].name;
|
2021-08-11 18:47:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-13 10:56:04 +08:00
|
|
|
|
|
2021-08-11 18:47:59 +08:00
|
|
|
|
if (!menuData) {
|
2021-05-13 10:56:04 +08:00
|
|
|
|
return;
|
2021-08-11 18:47:59 +08:00
|
|
|
|
}
|
|
|
|
|
util.initAllMenuData(constRoutes, menuData);
|
|
|
|
|
util.initRouterNode(otherRoutes, otherRouter);
|
|
|
|
|
// 添加所有主界面路由
|
2021-09-22 15:08:34 +08:00
|
|
|
|
vm.$store.commit(
|
|
|
|
|
"updateAppRouter",
|
|
|
|
|
constRoutes.filter(item => item.children.length > 0)
|
|
|
|
|
);
|
2021-08-11 18:47:59 +08:00
|
|
|
|
// 添加全局路由
|
2021-09-22 15:08:34 +08:00
|
|
|
|
vm.$store.commit("updateDefaultRouter", otherRoutes);
|
2021-08-11 18:47:59 +08:00
|
|
|
|
// 添加菜单路由
|
|
|
|
|
util.initMenuData(vm, menuData);
|
|
|
|
|
// 缓存数据 修改加载标识
|
2021-09-22 15:08:34 +08:00
|
|
|
|
window.localStorage.setItem("menuData", JSON.stringify(menuData));
|
|
|
|
|
vm.$store.commit("setAdded", true);
|
2021-08-11 18:47:59 +08:00
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// 读取缓存数据
|
2021-09-22 15:08:34 +08:00
|
|
|
|
let data = window.localStorage.getItem("menuData");
|
2021-08-11 18:47:59 +08:00
|
|
|
|
if (!data) {
|
2021-09-22 15:08:34 +08:00
|
|
|
|
vm.$store.commit("setAdded", false);
|
2021-08-11 18:47:59 +08:00
|
|
|
|
return;
|
2021-05-13 10:56:04 +08:00
|
|
|
|
}
|
2021-08-11 18:47:59 +08:00
|
|
|
|
let menuData = JSON.parse(data);
|
|
|
|
|
// 添加菜单路由
|
|
|
|
|
util.initMenuData(vm, menuData);
|
|
|
|
|
}
|
2021-05-13 10:56:04 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 添加所有顶部导航栏下的菜单路由
|
2021-09-22 15:08:34 +08:00
|
|
|
|
util.initAllMenuData = function(constRoutes, data) {
|
2021-08-11 18:47:59 +08:00
|
|
|
|
let allMenuData = [];
|
|
|
|
|
data.forEach(e => {
|
|
|
|
|
if (e.level == 0) {
|
|
|
|
|
e.children.forEach(item => {
|
|
|
|
|
allMenuData.push(item);
|
2021-09-22 15:08:34 +08:00
|
|
|
|
});
|
2021-08-11 18:47:59 +08:00
|
|
|
|
}
|
2021-09-22 15:08:34 +08:00
|
|
|
|
});
|
2021-08-11 18:47:59 +08:00
|
|
|
|
util.initRouterNode(constRoutes, allMenuData);
|
2021-09-22 15:08:34 +08:00
|
|
|
|
};
|
2021-05-13 10:56:04 +08:00
|
|
|
|
|
|
|
|
|
// 生成菜单格式数据
|
2021-09-22 15:08:34 +08:00
|
|
|
|
util.initMenuData = function(vm, data) {
|
2021-08-11 18:47:59 +08:00
|
|
|
|
const menuRoutes = [];
|
|
|
|
|
let menuData = data;
|
|
|
|
|
// 顶部菜单
|
|
|
|
|
let navList = [];
|
|
|
|
|
menuData.forEach(e => {
|
|
|
|
|
let nav = {
|
|
|
|
|
name: e.name,
|
|
|
|
|
title: e.title
|
2021-09-22 15:08:34 +08:00
|
|
|
|
};
|
2021-08-11 18:47:59 +08:00
|
|
|
|
navList.push(nav);
|
2021-09-22 15:08:34 +08:00
|
|
|
|
});
|
2021-08-11 18:47:59 +08:00
|
|
|
|
if (navList.length < 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 存入vuex
|
2021-09-22 15:08:34 +08:00
|
|
|
|
vm.$store.commit("setNavList", navList);
|
|
|
|
|
let currNav = window.localStorage.getItem("currNav");
|
2021-08-11 18:47:59 +08:00
|
|
|
|
if (currNav) {
|
|
|
|
|
// 读取缓存title
|
|
|
|
|
for (var item of navList) {
|
|
|
|
|
if (item.name == currNav) {
|
2021-09-22 15:08:34 +08:00
|
|
|
|
vm.$store.commit("setCurrNavTitle", item.title);
|
2021-08-11 18:47:59 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2021-05-13 10:56:04 +08:00
|
|
|
|
}
|
2021-08-11 18:47:59 +08:00
|
|
|
|
} else {
|
|
|
|
|
// 默认第一个
|
|
|
|
|
currNav = navList[0].name;
|
2021-09-22 15:08:34 +08:00
|
|
|
|
vm.$store.commit("setCurrNavTitle", navList[0].title);
|
2021-08-11 18:47:59 +08:00
|
|
|
|
}
|
2021-09-22 15:08:34 +08:00
|
|
|
|
vm.$store.commit("setCurrNav", currNav);
|
2021-08-11 18:47:59 +08:00
|
|
|
|
for (let item of menuData) {
|
|
|
|
|
if (item.name == currNav) {
|
|
|
|
|
// 过滤
|
|
|
|
|
menuData = item.children;
|
|
|
|
|
break;
|
2021-05-13 10:56:04 +08:00
|
|
|
|
}
|
2021-08-11 18:47:59 +08:00
|
|
|
|
}
|
|
|
|
|
util.initRouterNode(menuRoutes, menuData);
|
|
|
|
|
// 刷新界面菜单
|
2021-09-22 15:08:34 +08:00
|
|
|
|
vm.$store.commit(
|
|
|
|
|
"updateMenulist",
|
|
|
|
|
menuRoutes.filter(item => item.children.length > 0)
|
|
|
|
|
);
|
2021-08-11 18:47:59 +08:00
|
|
|
|
|
|
|
|
|
let tagsList = [];
|
2021-09-22 15:08:34 +08:00
|
|
|
|
vm.$store.state.app.routers.map(item => {
|
2021-08-11 18:47:59 +08:00
|
|
|
|
if (item.children.length <= 1) {
|
|
|
|
|
tagsList.push(item.children[0]);
|
|
|
|
|
} else {
|
|
|
|
|
tagsList.push(...item.children);
|
2021-05-13 10:56:04 +08:00
|
|
|
|
}
|
2021-08-11 18:47:59 +08:00
|
|
|
|
});
|
2021-09-22 15:08:34 +08:00
|
|
|
|
vm.$store.commit("setTagsList", tagsList);
|
2021-05-13 10:56:04 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 生成路由节点
|
2021-09-22 15:08:34 +08:00
|
|
|
|
util.initRouterNode = function(routers, data) {
|
|
|
|
|
// data为所有子菜单数据
|
2021-05-13 10:56:04 +08:00
|
|
|
|
|
2021-08-11 18:47:59 +08:00
|
|
|
|
for (let item of data) {
|
|
|
|
|
let menu = Object.assign({}, item);
|
2021-05-13 10:56:04 +08:00
|
|
|
|
|
2021-08-11 18:47:59 +08:00
|
|
|
|
menu.component = lazyLoading(menu.frontRoute);
|
2021-05-13 10:56:04 +08:00
|
|
|
|
|
2021-08-11 18:47:59 +08:00
|
|
|
|
if (item.children && item.children.length > 0) {
|
|
|
|
|
menu.children = [];
|
|
|
|
|
util.initRouterNode(menu.children, item.children);
|
|
|
|
|
}
|
2021-05-13 10:56:04 +08:00
|
|
|
|
|
2021-08-11 18:47:59 +08:00
|
|
|
|
let meta = {};
|
|
|
|
|
// 给页面添加标题、父级菜单name(方便左侧菜单选中)
|
2021-09-22 15:08:34 +08:00
|
|
|
|
meta.title = menu.title ? menu.title + " - 运营后台" : null;
|
|
|
|
|
meta.firstRouterName = item.firstRouterName;
|
2021-08-11 18:47:59 +08:00
|
|
|
|
menu.meta = meta;
|
2021-05-13 10:56:04 +08:00
|
|
|
|
|
2021-08-11 18:47:59 +08:00
|
|
|
|
routers.push(menu);
|
|
|
|
|
}
|
2021-05-13 10:56:04 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default util;
|