main.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import Vue from 'vue';
  2. import App from './App.vue';
  3. import router from './router';
  4. import store from './store';
  5. import dataV from '@jiaminghi/data-view';
  6. // 引入全局css
  7. import './assets/scss/style.scss';
  8. import 'element-ui/lib/theme-chalk/index.css';
  9. // 按需引入vue-awesome图标
  10. import Icon from 'vue-awesome/components/Icon';
  11. import 'vue-awesome/icons/chart-bar.js';
  12. import 'vue-awesome/icons/chart-area.js';
  13. import 'vue-awesome/icons/chart-pie.js';
  14. import 'vue-awesome/icons/chart-line.js';
  15. import 'vue-awesome/icons/align-left.js';
  16. //引入echart
  17. //4.x 引用方式
  18. import echarts from 'echarts'
  19. import ElementUI from 'element-ui';
  20. import Cookies from 'js-cookie'
  21. //5.x 引用方式为按需引用
  22. //希望使用5.x版本的话,需要在package.json中更新版本号,并切换引用方式
  23. //import * as echarts from 'echarts'
  24. Vue.prototype.$echarts = echarts
  25. Vue.config.productionTip = false;
  26. Vue.prototype.$cookie = Cookies;
  27. // 全局注册
  28. Vue.component('icon', Icon);
  29. Vue.use(dataV);
  30. Vue.use(ElementUI);
  31. router.beforeEach((to, from, next) => {
  32. /**
  33. * 未登录则跳转到登录页
  34. * 未登录跳转到登录页,也可以通过axios的响应拦截器去实现,但是会先在当前页面渲染一下,再跳转到登录页,会有个闪动的现象
  35. * 这里通过路由守卫的方式,不会在当前页闪现一下,但是需要在每个路由组件添加一个是否需要登录的标识位,如本项目中的requireAuth字段
  36. */
  37. if (to.matched.some((auth) => auth.meta.requireAuth)) {
  38. let token = sessionStorage.getItem("x-access-token");
  39. if (token && token != undefined) {
  40. next();
  41. } else {
  42. next({
  43. path: "/login"
  44. });
  45. }
  46. } else {
  47. next();
  48. }
  49. });
  50. new Vue({
  51. router,
  52. store,
  53. render: (h) => h(App),
  54. }).$mount('#app');
  55. import axios from 'axios';
  56. //把方法放到vue的原型上,这样就可以全局使用了
  57. Vue.prototype.$http = axios.create({
  58. //设置20秒超时时间
  59. timeout: 20000,
  60. baseURL: '/mes', //这里写后端地址http://localhost:8099/uas/ https://mes.ubtob.net:8099/mes/
  61. });
  62. Vue.prototype.$http.interceptors.request.use(config => {
  63. //为请求头添加x-access-token 字段为服务端返回的token
  64. if(sessionStorage.getItem('x-access-token')) {
  65. config.headers['x-access-token'] = sessionStorage.getItem('x-access-token');
  66. }
  67. if(config.params && config.params['condition']) {
  68. //console.log(sessionStorage.getItem('li_code'));
  69. }
  70. return config;
  71. });
  72. // response interceptor 响应拦截
  73. //token,一般是在登录完成之后,将用户的token通过localStorage或者cookie存在本地,
  74. // 然后用户每次在进入页面的时候(即在main.js中),会首先从本地存储中读取token,如果token存在说明用户已经登陆过,
  75. // 则更新vuex中的token状态。然后,在每次请求接口的时候,都会在请求的header中携带token,
  76. // 后台人员就可以根据你携带的token来判断你的登录是否过期,如果没有携带,则说明没有登录过。
  77. // 添加响应拦截器
  78. /*Vue.prototype.$http.interceptors.response.use(function (response) {
  79. if (response.status === 200) {
  80. return Promise.resolve(response);
  81. } else {
  82. return Promise.reject(response);
  83. }
  84. }, function (error) {
  85. // 对响应错误做点什么
  86. if (error.response.status) {
  87. switch (error.response.status) {
  88. // 401: 未登录
  89. // 未登录则跳转登录页面,并携带当前页面的路径
  90. // 在登录成功后返回当前页面,这一步需要在登录页操作。
  91. case 500:
  92. router.replace({
  93. path: '/login',
  94. query: {
  95. redirect: router.currentRoute.fullPath
  96. }
  97. });
  98. break;
  99. // 403 token过期
  100. // 登录过期对用户进行提示
  101. // 清除本地token和清空vuex中token对象
  102. // 跳转登录页面
  103. case 403:
  104. this.$message({
  105. message: '登录过期,请重新登录',
  106. duration: 1000,
  107. type: 'success'
  108. });
  109. // 清除token
  110. localStorage.removeItem('token');
  111. store.commit('loginSuccess', null);
  112. // 跳转登录页面,并将要浏览的页面fullPath传过去,登录成功后跳转需要访问的页面
  113. setTimeout(() => {
  114. router.replace({
  115. path: '/login',
  116. query: {
  117. redirect: router.currentRoute.fullPath
  118. }
  119. });
  120. }, 1000);
  121. break;
  122. // 404请求不存在
  123. case 404:
  124. this.$message({
  125. message: '网络请求不存在',
  126. duration: 1500,
  127. type: 'success'
  128. });
  129. break;
  130. // 其他错误,直接抛出错误提示
  131. default:
  132. this.$message({
  133. message: error.response.data.message,
  134. duration: 1500,
  135. type: 'success'
  136. });
  137. }
  138. return Promise.reject(error.response);
  139. }
  140. });*/