main.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 $ from "jquery";
  15. import { drag } from "./directives/drag";
  16. import { post, postform } from "./utils/request.js";
  17. import urls from "./api/url";
  18. // 引入dayjs库
  19. import dayjs from 'dayjs';
  20. import * as echarts from "echarts";
  21. Vue.directive("drag", drag);
  22. Vue.prototype.$Post = post;
  23. Vue.prototype.$Echarts = echarts;
  24. Vue.prototype.$dayjs = dayjs;
  25. Vue.prototype.$PostForm = postform
  26. Vue.prototype.urlsCollection = urls;
  27. Vue.config.productionTip = false;
  28. Vue.use(ElementUI);
  29. Vue.prototype.$store = store;
  30. // 根据菜单index,和已选中的菜单返回是否相同
  31. Vue.prototype.$ifMenu = (menuIndex, subMenuIndex) => {
  32. if (
  33. store.state.navSelect !== undefined &&
  34. store.state.navSelect.index == menuIndex &&
  35. (subMenuIndex !== ""
  36. ? store.state.navSelect.subIndex === subMenuIndex
  37. : true)
  38. ) {
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. };
  44. // 根据左侧菜单选中暂存index,与传入的菜单index对比,返回状态(主要使用于MenuCard组件type为imageMenu的元素,《疑点分析》下的子菜单)
  45. Vue.prototype.$ifLeftMenu = (leftMenuTitle) => {
  46. return store.state.leftMenuTitle == leftMenuTitle;
  47. };
  48. // 添加全局路由前置守卫
  49. router.beforeEach((to, from, next) => {
  50. if (to.path === '/' && from.path === '/login') {
  51. // 退出操作成功后
  52. next();
  53. }
  54. if (to.path === '/login' && from.path === '/') {
  55. // 登录操作成功后
  56. next();
  57. }
  58. if (to.path === '/' && from.path === '/') {
  59. // 首页刷新操作
  60. post(urls.getUserInfo).then(
  61. res => {
  62. // 请求成功
  63. if (res.code == 200 && res.content !== "无效token") {
  64. next();
  65. } else {
  66. localStorage.removeItem('TOKEN');
  67. localStorage.removeItem('USER_ID');
  68. ElementUI.Message.error("用户信息获取失败,请尝试重新登录!");
  69. next('/login');
  70. }
  71. },
  72. error => {
  73. // 请求失败!
  74. localStorage.removeItem('TOKEN');
  75. localStorage.removeItem('USER_ID');
  76. ElementUI.Message.error(error)
  77. next('/login');
  78. }
  79. );
  80. }
  81. })
  82. new Vue({
  83. router,
  84. store,
  85. render: (h) => h(App),
  86. // 全局事件总线
  87. beforeCreate() {
  88. Vue.prototype.$bus = this;
  89. },
  90. }).$mount("#app");