Files
local_alert/src/main.ts
2024-11-26 16:25:47 +08:00

89 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { createApp,ref, onBeforeUnmount} from 'vue'
import App from './App.vue'
import router from './router';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import zhCn from 'element-plus/es/locale/lang/zh-cn';
// import axiosInstance from '@/utils/axios-config';
import axios from 'axios';
import '@/assets/global.css'
import { useGlobalWebSocket } from './utils/useGlobalWebSocket';
import DataVVue3 from '@kjgl77/datav-vue3';
import '@kjgl77/datav-vue3/dist/style.css';
import mitt from 'mitt';
import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/reset.css';
import { createPinia } from 'pinia';
const app = createApp(App)
const globalWebSocket = useGlobalWebSocket();
const pinia = createPinia();
app.provide('globalWebSocket', globalWebSocket);
// app.provide('axios', axiosInstance);
app.use(ElementPlus, { locale: zhCn });
app.use(router);
app.use(DataVVue3);
app.use(Antd);
app.use(pinia)
// 导航守卫,检查登录状态
router.beforeEach((to, from, next) => {
const token = localStorage.getItem('alertToken'); // 检查 token 是否存在,作为是否登录的依据
if (to.matched.some(record => record.meta.requiresAuth)) {
// 该路由需要认证
if (!token) {
// 如果没有 token重定向到登录页面
next({
path: '/login',
query: { redirect: to.fullPath } // 将当前路径传递给登录页面,登录后可以重定向回来
});
} else {
// 已登录,继续访问
next();
}
} else {
// 不需要认证,继续访问
next();
}
});
// 定义转换函数
app.config.globalProperties.xToh = (px: number): string => {
const vh = (px / window.innerHeight) * 100;
return `${vh}vh`;
};
app.config.globalProperties.xTow = (px: number): string => {
const vw = (px / window.innerWidth) * 100;
return `${vw}vw`;
};
app.config.globalProperties.vhToPx = (vh: number): number => {
return (vh / 100) * window.innerHeight;
};
app.config.globalProperties.vwToPx = (vw: number): number => {
return (vw / 100) * window.innerWidth;
};
// 响应式处理
const updateDimensions = () => {
app.config.globalProperties.windowHeight = window.innerHeight;
app.config.globalProperties.windowWidth = window.innerWidth;
};
window.addEventListener('resize', updateDimensions);
updateDimensions(); // 初始化
// 清理事件监听器
onBeforeUnmount(() => {
window.removeEventListener('resize', updateDimensions);
});
const eventBus = mitt();
app.config.globalProperties.$eventBus = eventBus;
app.mount('#app')