main.ts 5.67 KB
import App from '@/App.vue';
import router from "@/router";
import Antd, {Form} from 'ant-design-vue';
import * as Vue from 'vue';
import axios, {AxiosInstance, AxiosStatic} from "axios";
import VueAxios from 'vue-axios';
import AutumnV from "./index";
// import "autumn-antv/dist/autumn-antv.css";
import store, {SecurityDirectiveA} from './store'
import './assets/style/main.less';
import 'echarts/lib/chart/bar';
import 'echarts/lib/chart/line';
import 'echarts/lib/chart/pie';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/legend';
import 'echarts/lib/component/markPoint';
import 'echarts/lib/component/markLine';
import 'echarts/lib/component/graphic';
import 'echarts/lib/component/visualMap';
import {AuthService} from "@/common/AuthService";
import {CustomTokenInterceptor} from "@/common/CustomTokenInterceptor";
import filters from "@/filters/filters";
import {createI18n, useI18n} from "vue-i18n";
import messages from "./assets/i18n/index";
import ECharts from 'vue-echarts';
import {use} from 'echarts/core';
import {CanvasRenderer} from 'echarts/renderers';
import {BarChart, GaugeChart, GraphChart, LineChart, SankeyChart, MapChart, FunnelChart} from 'echarts/charts';

import {GridComponent, LegendComponent, TitleComponent, ToolboxComponent, TooltipComponent, VisualMapComponent} from 'echarts/components';
import Authenticity from "lib/src/core/security/Authenticity";
import Vue3ColorPicker from "vue3-colorpicker";
import "vue3-colorpicker/style.css";
import isoWeek from "dayjs/plugin/isoWeek";
import weekOfYear from "dayjs/plugin/weekOfYear";
import quarterOfYear from 'dayjs/plugin/quarterOfYear';
import weekday from "dayjs/plugin/weekday";
import weekYear from "dayjs/plugin/weekYear";
import localeData from "dayjs/plugin/localeData";
import customParseFormat from "dayjs/plugin/customParseFormat";
import advancedFormat from "dayjs/plugin/advancedFormat";
import dayjs from "dayjs";
import "dayjs/locale/zh-cn";
import updateLocale from "dayjs/plugin/updateLocale";
import tinymce from 'tinymce/tinymce';
import {SecurityController} from "@/api/SecurityController";
import * as echarts from 'echarts/core';
import {WALDEN_THEME} from "@/assets/style/walden";
import SecurityDirective from "lib/src/core/security/SecurityDirective";

dayjs.locale("zh-cn");
dayjs.extend(customParseFormat);
dayjs.extend(advancedFormat);
dayjs.extend(localeData);
dayjs.extend(weekday);
dayjs.extend(weekYear);
dayjs.extend(weekOfYear);
dayjs.extend(isoWeek);
dayjs.extend(updateLocale);
dayjs.extend(quarterOfYear);
dayjs.updateLocale('zh-cn', {
    weekStart: 1,
});
/**
 * 自定义类型申明
 */
declare module '@vue/runtime-core' {

    // export interface GlobalComponents {
    //     FontIcon:typeof fontIcon
    // }
    interface ComponentCustomProperties {
        $auth: AuthService,
        $filters: any,
        $route: any,
        $router: any,
        $apiUrl: string,
        $previewUrl: string,
        $goBack: () => void
    }
}

function goBack() {
    router.go(-1);
}

use([
    CanvasRenderer,
    BarChart,
    MapChart,
    LineChart,
    GaugeChart,
    SankeyChart,
    FunnelChart,
    GraphChart,
    GridComponent,
    LegendComponent,
    TooltipComponent,
    TitleComponent,
    VisualMapComponent,
    ToolboxComponent

]);

const i18n = createI18n({
    locale: 'zh_CN',
    legacy: false,
    globalInjection: true,
    fallbackLocale: 'zh_CN',
    messages: messages
});

const systemI18n = {
    setup() {
        const {locale, t} = useI18n();
        return {locale, t};
    }
};
// const obj = JSON.parse(data)
echarts.registerTheme('walden', WALDEN_THEME)
const apiUrl = import.meta.env.VITE_API_BASE_URL;

// 👇 最关键一步:挂到全局
(window as any).tinymce = tinymce
const axiosInstance: AxiosInstance = axios.create({timeout: 1000 * 60, baseURL: apiUrl, headers: {}});

export const authService: AuthService = new AuthService();

if (import.meta.env.MODE === 'development') {
    authService.updateToken(import.meta.env.VITE_TOKEN);
} else if (import.meta.env.MODE === 'electron') {
    authService.updateToken(import.meta.env.VITE_TOKEN);
} else {
    const token: any = document.getElementById('token');
    if (token) {
        authService.updateToken(token.value);
    } else {
        window.location.href = 'login';
    }
}

const app = Vue.createApp(App, systemI18n).use(store).use(router).use(Antd).use(Form).use(AutumnV).use(i18n).use(Vue3ColorPicker).use(VueAxios, axios);
// 全局注册组件(也可以使用局部注册)
app.component('v-chart', ECharts);
//注册自定义权限控制指令
app.directive('sec', SecurityDirectiveA);

app.config.globalProperties.axios = <AxiosStatic>axiosInstance;
//全局属性
const tokenInterceptor: CustomTokenInterceptor = new CustomTokenInterceptor(axiosInstance, authService);
app.config.globalProperties.$auth = authService;
app.config.globalProperties.axios = <AxiosStatic>axiosInstance;
app.config.globalProperties.$store = store;
app.config.globalProperties.$filters = filters;
app.config.globalProperties.$apiUrl = apiUrl;
app.config.globalProperties.$goBack = goBack;
app.provide('axios', app.config.globalProperties.axios);
//注册自定义权限控制指令


SecurityController.getUserAuthentication(axiosInstance as AxiosStatic, null).then((res) => {
    const authenticity: Authenticity = res.data;
    authenticity.token = authService.getToken();
    authService.saveAuthentication(authenticity);
    authService.setAuthentication(authenticity);
    //将当前用户信息写入vuex的store中
    store.commit('security/changeAuthenticity', authenticity);
    //将用户的权限树写入到vuex的store中
    store.commit('security/changePrivileges', authenticity.privileges ?? []);
    app.mount('#app');
});

// app.mount('#app');