main.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Vue from "vue";
  2. import App from "./App.vue";
  3. import router from "./router";
  4. import { store } from "./store/index.js";
  5. // import "./assets/style/element-variables.module.scss";
  6. import "./icons/index"; // icon
  7. import VueRulerTool from "vue-ruler-tool";
  8. // 引入element ui
  9. import ElementUI from "element-ui";
  10. import "element-ui/lib/theme-chalk/index.css";
  11. import "@wangeditor/editor/dist/css/style.css";
  12. // 引入css
  13. import "./styles/index.scss";
  14. // 注册svg组件
  15. import SvgIcons from "@/components/SvgIcons/index.vue";
  16. import VueDraggableResizable from "vue-draggable-resizable-gorkys";
  17. import "vue-draggable-resizable-gorkys/dist/VueDraggableResizable.css";
  18. Vue.component("VueDraggableResizable", VueDraggableResizable);
  19. Vue.component("SvgIcons", SvgIcons);
  20. Vue.component("vue-ruler-tool", VueRulerTool);
  21. // 序列化post方法
  22. import qs from "qs";
  23. // 将自动注册所有组件为全局组件
  24. import dataV from "@jiaminghi/data-view";
  25. import permission from "./directives/permission"; // 导入自定义指令
  26. import lazyLoad from "./directives/lazyLoad";
  27. import "echarts-gl";
  28. Vue.use(dataV);
  29. Vue.prototype.$qs = qs;
  30. // Vue.prototype.$tinymce = tinymce;
  31. Vue.use(ElementUI);
  32. // 引入tailwind
  33. // https://www.tailwindcss.cn/docs/installation
  34. Vue.prototype.$BASE_URL = window?._BASE_CONFIG?.API;
  35. Vue.config.productionTip = false;
  36. if (store.state.auth.userInfo.permission !== undefined) {
  37. store.dispatch("auth/setAddRouter", {
  38. resultRouter:
  39. (store.state.auth.userInfo && store.state.auth.userInfo.permission) || [],
  40. router,
  41. });
  42. }
  43. //开始结束时间转换
  44. Vue.prototype.$formatDate = function (date) {
  45. const year = date.getFullYear();
  46. const month = (date.getMonth() + 1).toString().padStart(2, "0"); // Adding 1 to month since it's zero-based
  47. const day = date.getDate().toString().padStart(2, "0");
  48. return `${year}-${month}-${day}`;
  49. };
  50. Vue.directive("lazy-load", lazyLoad);
  51. Vue.directive("hasPermi", permission);
  52. // 时间戳转换
  53. Vue.prototype.$formatDateTWO = function (timestamp) {
  54. const date = new Date(timestamp);
  55. const year = date.getFullYear();
  56. const month = String(date.getMonth() + 1).padStart(2, "0");
  57. const day = String(date.getDate()).padStart(2, "0");
  58. const hours = String(date.getHours()).padStart(2, "0");
  59. const minutes = String(date.getMinutes()).padStart(2, "0");
  60. // const seconds = String(date.getSeconds()).padStart(2, "0");:${seconds}
  61. return `${year}-${month}-${day} ${hours}:${minutes}`;
  62. };
  63. // 监听页面刷新或关闭事件
  64. window.addEventListener("beforeunload", () => {
  65. // 调用清空 Vuex 仓库的 Action
  66. store.commit("dragChart/clearchart");
  67. });
  68. new Vue({
  69. router,
  70. store,
  71. mounted() {
  72. this.applyTheme();
  73. },
  74. watch: {
  75. "$store.state.themes.theme": {
  76. handler(newTheme, oldTheme) {
  77. this.applyTheme(newTheme, oldTheme);
  78. },
  79. immediate: true,
  80. },
  81. },
  82. methods: {
  83. async applyTheme(theme, oldTheme) {
  84. const themeFile = theme || this.$store.state.themes.theme;
  85. // 动态加载新的主题样式
  86. try {
  87. // 动态导入 SCSS 文件
  88. const theme = await import(`@/themes/${themeFile}.scss`);
  89. Vue.prototype.$backgroundColor = getComputedStyle(
  90. document.documentElement
  91. ).getPropertyValue("--background-color");
  92. // 移除之前的主题样式
  93. let oldThemeStyle = document.getElementById("theme-style");
  94. if (oldThemeStyle) {
  95. oldThemeStyle.parentNode.removeChild(oldThemeStyle);
  96. }
  97. // 创建新的样式标签并插入到 head 中
  98. const style = document.createElement("style");
  99. style.id = "theme-style";
  100. style.type = "text/css";
  101. style.innerHTML = theme.default;
  102. document.head.appendChild(style);
  103. if (theme !== oldTheme && oldTheme !== undefined) {
  104. this.$router.go(0);
  105. }
  106. } catch (error) {
  107. console.error("Failed to load theme", error);
  108. }
  109. },
  110. },
  111. render: (h) => h(App),
  112. }).$mount("#app");