hyb
2025-05-14 87453ffd761425b9f363a09a0f8fe07d770cb325
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* jshint esversion: 6 */
import Vue from "vue";
import ElementUI from "element-ui";
import VJsoneditor from "v-jsoneditor";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import VueApexCharts from "vue-apexcharts";
import Skeleton from "vue-loading-skeleton";
import VueClipboard from "vue-clipboard2";
import "element-ui/lib/theme-chalk/index.css";
import "styles/iconfont.css";
import "styles/swagger.css";
import "styles/tree.css";
import "styles/home.css";
import "styles/reports.css";
import "styles/iconfont.js";
import * as api from "./restful/api";
import { datetimeObj2str, timestamp2time } from "@/util/format";
 
Vue.use(ElementUI);
Vue.use(VueApexCharts);
Vue.use(VJsoneditor);
Vue.use(Skeleton);
Vue.use(VueClipboard);
 
Vue.component("ApexCharts", VueApexCharts);
 
Vue.config.productionTip = false;
Vue.prototype.$api = api
 
Vue.filter("datetimeFormat", datetimeObj2str);
Vue.filter("timestampToTime", timestamp2time);
 
Vue.prototype.setLocalValue = function(name, value) {
    if (window.localStorage) {
        localStorage.setItem(name, value);
    } else {
        alert("This browser does not support localStorage");
    }
};
 
Vue.prototype.getLocalValue = function(name) {
    const value = localStorage.getItem(name);
    if (value) {
        // localStorage只能存字符串,布尔类型需要转换
        if (value === "false" || value === "true") {
            return eval(value);
        }
        return value;
    } else {
        return "";
    }
};
 
router.beforeEach((to, from, next) => {
    // 延迟确保在路由变化后再更新状态,避免潜在的时序问题
    setTimeout(() => {
        // 修改页面title
        if (to.meta.title) {
            document.title = to.meta.title;
        }
 
        // 更新侧边栏状态
        if (to.name) {
            store.commit("setRouterName", to.name);
            // 如果需要,也可以更新LocalStorage
            Vue.prototype.setLocalValue("routerName", to.name);
        }
 
        // 鉴权
        if (to.meta.requireAuth) {
            if (store.state.token !== "") {
                next();
            } else {
                next({
                    name: "Login"
                });
            }
        } else {
            next();
        }
    });
});
 
new Vue({
    el: "#app",
    router,
    store,
    components: { App },
    template: "<App/>",
    created() {
        if (this.getLocalValue("token") === null) {
            this.setLocalValue("token", "");
        }
 
        if (this.getLocalValue("user") === null) {
            this.setLocalValue("user", "");
        }
 
        if (this.getLocalValue("name") === null) {
            this.setLocalValue("name", "");
        }
 
        if (this.getLocalValue("id") === null) {
            this.setLocalValue("id", "");
        }
 
        if (this.getLocalValue("routerName") === null) {
            this.setLocalValue("routerName", "ProjectList");
        }
 
        if (this.getLocalValue("is_superuser") === null) {
            this.setLocalValue("is_superuser", false);
        }
 
        if (this.getLocalValue("show_hosts") === null) {
            this.setLocalValue("show_hosts", false);
        }
 
        this.$store.commit("isLogin", this.getLocalValue("token"));
        this.$store.commit("setUser", this.getLocalValue("user"));
        this.$store.commit("setName", this.getLocalValue("name"));
        this.$store.commit("setId", parseInt(this.getLocalValue("id"), 10));
        this.$store.commit("setRouterName", this.getLocalValue("routerName"));
        this.$store.commit(
            "setIsSuperuser",
            this.getLocalValue("is_superuser")
        );
        this.$store.commit("setShowHosts", this.getLocalValue("show_hosts"));
        this.$store.dispatch("initStore").then(() => {});
    }
});