You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xy-frontend/src/main.js

100 lines
3.0 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
//import "./utils/rem"; //设置body文字大小
Vue.config.productionTip = false;
import "../src/assets/css/theme/index.css"; //l绿色主题
import ElementUI from "element-ui";
//样式
import "./assets/css/reset.css"; //默认样式
import "./assets/css/global.less"; //全局定义颜色
import "./assets/css/element.less"; //全局定义颜色
import "./assets/fonts/iconfont.css"; //按钮
//引入Echarts;
import * as echarts from "echarts";
Vue.prototype.$echarts = echarts;
//引入日期// 注册全局 moment
import moment from "moment";
Vue.prototype.$moment = moment;
import { message } from "@/utils/resetMessage";
// import "element-ui/lib/theme-chalk/index.css";
Vue.use(ElementUI, {
size: "small",
});
//图片懒加载
//引入插件
import VueLazyload from "vue-lazyload";
//注册插件
Vue.use(VueLazyload, {
error: require("./assets/img/nodatapic2.jpg"),
listenEvents: [ 'scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove' ]
});
/* 引入公共js*/
import common from "@/utils/common/common.js";
Vue.prototype.common = common;
//挂载弹出信息
Vue.prototype.$message = message;
//防抖
Vue.directive("noMoreClick", {
inserted(el, binding) {
el.addEventListener("click", (e) => {
el.classList.add("is-disabled");
el.disabled = true;
setTimeout(() => {
el.disabled = false;
el.classList.remove("is-disabled");
}, 2000); //我这里设置的是2000毫秒也就是2秒
});
},
});
//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
document.title = `${to.meta.title} | 视频监控可视化平台`;
// 获取用户的角色和token
const role = localStorage.getItem("role");
const token = localStorage.getItem("token");
// 如果没有token并且不是登录页面重定向到登录页面
if (!token && to.path !== "/login") {
return next({ path: "/login" });
}
// 如果用户是超级管理员role == 0则允许访问任何页面
if (role === "0") {
return next();
}
// 如果用户从登录页面跳转到其他页面并且已经拥有token则允许通过
if (from.path === "/login" && token) {
return next();
}
// 获取权限列表这里假设permissions是从后端获取的权限数组
const permissions = JSON.parse(localStorage.getItem("menuPermission")) || [];
// 检查用户是否有访问当前路由的权限
const hasPermission = permissions.some(
(permission) => permission.key === to.path
);
// 如果路由需要权限,但用户没有权限,则重定向到无权限页面
if (to.meta.requiresAuth && !hasPermission) {
return next({ path: "/permission" });
}
// 如果路由在免登录白名单中,则直接通过
if (to.meta.noAuth) {
return next();
}
// 如果以上条件都不满足,正常通过
next();
});
new Vue({
router,
store,
render: (h) => h(App),
}).$mount("#app");