App.vue 2.35 KB
<template>
  <div id="app">
    <router-view v-if="isRouterAlive" />
  </div>
</template>

<script>
import "@/assets/iconfont/iconfont.css";
import { initDictToLocalstorage } from "@/api/dict-data";
export default {
  name: "App",
  provide() {
    return {
      reload: this.reload
    };
  },
  data() {
    return {
      isRouterAlive: false
    };
  },
  watch: {
    $route(to, form) {
      if (to.path == "/login") {
        this.queryDictName();
      }
    }
  },
  computed: {},
  created() {
    this.queryDictName();
    this.loadTheme();
  },
  methods: {
    queryDictName() {
      // 初始化数据字典到浏览器本地缓存
      initDictToLocalstorage(() => {
        this.isRouterAlive = true;
      });
    },
    loadTheme(){
      const getParams = () => {
        let map = {};
        let query = window.location.search.substring(1);
        if (query == '') {
          query = document.location.href
          let args = query.split('?');
          let arr = [];
          if (args && args.length > 1) {
            arr = args[1].split('&')
          }
          for (let i = 0; i < arr.length; i++) {
            let pair = arr[i].split("=");
            map[pair[0]] = pair[1];
          }
          return map;
        }
        let vars = query.split("&");
        for (let i = 0; i < vars.length; i++) {
          let pair = vars[i].split("=");
          map[pair[0]] = pair[1];
        }
        return map;
      }

      const getVal = (key) => {
        if(key){
          let val = getParams()[key];
          if(!val){
            val = localStorage.getItem(key);
          }

          if(val){
            localStorage.setItem(key,val);
          }
          return val;
        }
        return '';
      }

      getVal('AuthKey');
      getVal('AuthVal');

      let theme = getVal('theme');
      if (!theme) {
        theme = 'simplicity';
      }
      if (!theme) {
        theme = 'simplicity';
      }

      var link = document.createElement("link");
      link.setAttribute("rel", "stylesheet");
      link.setAttribute("type", "text/css");
      link.setAttribute("href", `/mj/public/css/sub-sys/aj-web-${theme}.css`);
      document.body.insertBefore(link, document.body.firstChild);

    },
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function() {
        this.isRouterAlive = true;
      });
    }
  }
};
</script>