main.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. //5.x 引用方式
  18. import * as 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. /*if(sessionStorage.getItem('li_code') != '所有') {
  70. config.params['condition'] = config.params['condition'] + " and V_LICODE='" + sessionStorage.getItem('li_code') + "'";
  71. }*/
  72. }
  73. return config;
  74. });
  75. Vue.prototype.$httpImg = axios.create({
  76. //设置20秒超时时间
  77. timeout: 20000,
  78. baseURL: '/mes', //这里写后端地址http://localhost:8099/uas/ https://mes.ubtob.net:8099/mes/
  79. responseType: 'blob'
  80. });
  81. Vue.prototype.$httpImg.interceptors.request.use(config => {
  82. //为请求头添加x-access-token 字段为服务端返回的token
  83. if(sessionStorage.getItem('x-access-token')) {
  84. config.headers['x-access-token'] = sessionStorage.getItem('x-access-token');
  85. }
  86. return config;
  87. });
  88. // response interceptor 响应拦截
  89. //token,一般是在登录完成之后,将用户的token通过localStorage或者cookie存在本地,
  90. // 然后用户每次在进入页面的时候(即在main.js中),会首先从本地存储中读取token,如果token存在说明用户已经登陆过,
  91. // 则更新vuex中的token状态。然后,在每次请求接口的时候,都会在请求的header中携带token,
  92. // 后台人员就可以根据你携带的token来判断你的登录是否过期,如果没有携带,则说明没有登录过。
  93. // 添加响应拦截器
  94. /*Vue.prototype.$http.interceptors.response.use(function (response) {
  95. if (response.status === 200) {
  96. return Promise.resolve(response);
  97. } else {
  98. return Promise.reject(response);
  99. }
  100. }, function (error) {
  101. // 对响应错误做点什么
  102. if (error.response.status) {
  103. switch (error.response.status) {
  104. // 401: 未登录
  105. // 未登录则跳转登录页面,并携带当前页面的路径
  106. // 在登录成功后返回当前页面,这一步需要在登录页操作。
  107. case 500:
  108. router.replace({
  109. path: '/login',
  110. query: {
  111. redirect: router.currentRoute.fullPath
  112. }
  113. });
  114. break;
  115. // 403 token过期
  116. // 登录过期对用户进行提示
  117. // 清除本地token和清空vuex中token对象
  118. // 跳转登录页面
  119. case 403:
  120. this.$message({
  121. message: '登录过期,请重新登录',
  122. duration: 1000,
  123. type: 'success'
  124. });
  125. // 清除token
  126. localStorage.removeItem('token');
  127. store.commit('loginSuccess', null);
  128. // 跳转登录页面,并将要浏览的页面fullPath传过去,登录成功后跳转需要访问的页面
  129. setTimeout(() => {
  130. router.replace({
  131. path: '/login',
  132. query: {
  133. redirect: router.currentRoute.fullPath
  134. }
  135. });
  136. }, 1000);
  137. break;
  138. // 404请求不存在
  139. case 404:
  140. this.$message({
  141. message: '网络请求不存在',
  142. duration: 1500,
  143. type: 'success'
  144. });
  145. break;
  146. // 其他错误,直接抛出错误提示
  147. default:
  148. this.$message({
  149. message: error.response.data.message,
  150. duration: 1500,
  151. type: 'success'
  152. });
  153. }
  154. return Promise.reject(error.response);
  155. }
  156. });*/