main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import Vue from "vue";
  2. // 进入svg自定义icon插件
  3. import "./assets/icons/index.js";
  4. import App from "./App.vue";
  5. import router from "./router";
  6. import store from "./store";
  7. import ElementUI from "element-ui";
  8. import "element-ui/lib/theme-chalk/index.css";
  9. // 添加字体(优设标题黑)
  10. import "./assets/font/font.css";
  11. import "./assets/global.css";
  12. import SvgIcon from "@/components/SvgIcon";
  13. Vue.component("svg-icon", SvgIcon);
  14. import { drag } from "./directives/drag";
  15. import { get, post, postform, getFile } from "./utils/request.js";
  16. import urls from "./api/url";
  17. import CryptoJS from "./utils/publicFunction.js";
  18. // 引入dayjs库
  19. import dayjs from "dayjs";
  20. import * as echarts from "echarts";
  21. import proj4 from "proj4";
  22. // 滚动插件
  23. import scroll from "vue-seamless-scroll/src";
  24. Vue.directive("drag", drag);
  25. Vue.prototype.$CryptoJS = CryptoJS;
  26. Vue.prototype.$Post = post;
  27. Vue.prototype.$Get = get;
  28. Vue.prototype.$GetFile = getFile;
  29. Vue.prototype.$Echarts = echarts;
  30. Vue.prototype.$dayjs = dayjs;
  31. Vue.prototype.$proj4 = proj4;
  32. Vue.prototype.$PostForm = postform;
  33. Vue.prototype.urlsCollection = urls;
  34. Vue.config.productionTip = false;
  35. Vue.use(ElementUI);
  36. Vue.use(scroll);
  37. Vue.prototype.$store = store;
  38. // 根据菜单index,和已选中的菜单返回是否相同
  39. Vue.prototype.$ifMenu = (menuIndex, subMenuIndex) => {
  40. if (
  41. store.state.navSelect !== undefined &&
  42. store.state.navSelect.index == menuIndex &&
  43. (subMenuIndex !== ""
  44. ? store.state.navSelect.subIndex === subMenuIndex
  45. : true)
  46. ) {
  47. return true;
  48. } else {
  49. return false;
  50. }
  51. };
  52. // 根据左侧菜单选中暂存index,与传入的菜单index对比,返回状态(主要使用于MenuCard组件type为imageMenu的元素,《疑点分析》下的子菜单)
  53. Vue.prototype.$ifLeftMenu = (leftMenuTitle) => {
  54. return store.state.leftMenuTitle == leftMenuTitle;
  55. };
  56. // 添加全局路由前置守卫
  57. router.beforeEach((to, from, next) => {
  58. if (to.meta.title) {
  59. document.title = to.meta.title;
  60. }
  61. if (to.path === "/" && from.path === "/login") {
  62. // 退出操作成功后
  63. next();
  64. }
  65. if (to.path === "/login" && from.path === "/") {
  66. // 登录操作成功后
  67. next();
  68. }
  69. if (to.path === "/" && from.path === "/") {
  70. // 首页刷新操作
  71. post(urls.getUserInfo).then(
  72. (res) => {
  73. // 请求成功
  74. if (res.code == 200 && res.content !== "无效token") {
  75. next();
  76. } else {
  77. localStorage.removeItem("TOKEN");
  78. localStorage.removeItem("USER_ID");
  79. localStorage.removeItem("USER_NAME");
  80. ElementUI.Message.error("用户信息获取失败,请尝试重新登录!");
  81. next("/login");
  82. }
  83. },
  84. (error) => {
  85. // 请求失败!
  86. localStorage.removeItem("TOKEN");
  87. localStorage.removeItem("USER_ID");
  88. localStorage.removeItem("USER_NAME");
  89. ElementUI.Message.error(error);
  90. next("/login");
  91. }
  92. );
  93. }
  94. });
  95. new Vue({
  96. router,
  97. store,
  98. render: (h) => h(App),
  99. // 全局事件总线
  100. beforeCreate() {
  101. Vue.prototype.$bus = this;
  102. },
  103. }).$mount("#app");