main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. Vue.use(dataV);
  28. Vue.prototype.$qs = qs;
  29. // Vue.prototype.$tinymce = tinymce;
  30. Vue.use(ElementUI);
  31. // 引入tailwind
  32. // https://www.tailwindcss.cn/docs/installation
  33. Vue.prototype.$BASE_URL = window?._BASE_CONFIG?.API;
  34. Vue.config.productionTip = false;
  35. if (store.state.auth.userInfo.permission !== undefined) {
  36. store.dispatch("auth/setAddRouter", {
  37. resultRouter:
  38. (store.state.auth.userInfo && store.state.auth.userInfo.permission) || [],
  39. router,
  40. });
  41. }
  42. //开始结束时间转换
  43. Vue.prototype.$formatDate = function (date) {
  44. const year = date.getFullYear();
  45. const month = (date.getMonth() + 1).toString().padStart(2, "0"); // Adding 1 to month since it's zero-based
  46. const day = date.getDate().toString().padStart(2, "0");
  47. return `${year}-${month}-${day}`;
  48. };
  49. Vue.directive("lazy-load", lazyLoad);
  50. Vue.directive("hasPermi", permission);
  51. // 时间戳转换
  52. Vue.prototype.$formatDateTWO = function (timestamp) {
  53. const date = new Date(timestamp);
  54. const year = date.getFullYear();
  55. const month = String(date.getMonth() + 1).padStart(2, "0");
  56. const day = String(date.getDate()).padStart(2, "0");
  57. const hours = String(date.getHours()).padStart(2, "0");
  58. const minutes = String(date.getMinutes()).padStart(2, "0");
  59. // const seconds = String(date.getSeconds()).padStart(2, "0");:${seconds}
  60. return `${year}-${month}-${day} ${hours}:${minutes}`;
  61. };
  62. new Vue({
  63. router,
  64. store,
  65. mounted() {
  66. this.applyTheme();
  67. },
  68. watch: {
  69. "$store.state.themes.theme": {
  70. handler(newTheme, oldTheme) {
  71. this.applyTheme(newTheme, oldTheme);
  72. },
  73. immediate: true,
  74. },
  75. },
  76. methods: {
  77. async applyTheme(theme, oldTheme) {
  78. const themeFile = theme || this.$store.state.themes.theme;
  79. // 动态加载新的主题样式
  80. try {
  81. // 动态导入 SCSS 文件
  82. const theme = await import(`@/themes/${themeFile}.scss`);
  83. Vue.prototype.$backgroundColor = getComputedStyle(
  84. document.documentElement
  85. ).getPropertyValue("--background-color");
  86. // 移除之前的主题样式
  87. let oldThemeStyle = document.getElementById("theme-style");
  88. if (oldThemeStyle) {
  89. oldThemeStyle.parentNode.removeChild(oldThemeStyle);
  90. }
  91. // 创建新的样式标签并插入到 head 中
  92. const style = document.createElement("style");
  93. style.id = "theme-style";
  94. style.type = "text/css";
  95. style.innerHTML = theme.default;
  96. document.head.appendChild(style);
  97. if (theme !== oldTheme && oldTheme !== undefined) {
  98. this.$router.go(0);
  99. }
  100. } catch (error) {
  101. console.error("Failed to load theme", error);
  102. }
  103. },
  104. },
  105. render: (h) => h(App),
  106. }).$mount("#app");