main.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import Vue from "vue";
  2. import axios from "axios";
  3. import App from "./App.vue";
  4. import router from "./router";
  5. import store from "./store";
  6. import GLOBAL from "./config";
  7. import globalVuePlugins from "./plugins/globalVuePlugins";
  8. // https://github.com/RobinCK/vue-ls
  9. import VueLocalStorage from "vue-ls";
  10. import ViewUI from "view-design";
  11. import "view-design/dist/styles/iview.css";
  12. import "./assets/styles/index.less";
  13. // regist v-chart
  14. import "./plugins/VueCharts";
  15. Vue.use(ViewUI, { size: "large" });
  16. ViewUI.Message.config({
  17. duration: 3
  18. });
  19. ViewUI.Notice.config({
  20. duration: 3
  21. });
  22. Vue.use(VueLocalStorage, { storage: "session" });
  23. Vue.use(globalVuePlugins);
  24. Vue.prototype.GLOBAL = GLOBAL;
  25. Vue.config.productionTip = false;
  26. // route interceptor
  27. // router.beforeEach((to, from, next) => {
  28. // const token = Vue.ls.get("token");
  29. // if (to.meta.noRequire) {
  30. // // 不需要登录的页面
  31. // if (token) {
  32. // // 当前状态是已登录
  33. // ViewUI.Notice.info({ title: "您已经登录,已自动跳转到主页", duration: 2 });
  34. // next({ name: "Home" });
  35. // } else {
  36. // next();
  37. // }
  38. // } else {
  39. // // 需要登录的路由
  40. // if (token) {
  41. // next();
  42. // } else {
  43. // // 登录失效的处理
  44. // Vue.ls.clear();
  45. // // Vue.ls.remove("token");
  46. // // Vue.ls.remove("user");
  47. // next({ name: "Login" });
  48. // }
  49. // }
  50. // });
  51. // axios interceptors
  52. var load = "";
  53. // 同一时间有多个请求时,会形成队列。在第一个请求创建loading,在最后一个响应关闭loading
  54. var queue = [];
  55. axios.interceptors.request.use(
  56. config => {
  57. // 显示loading提示
  58. if (!queue.length) {
  59. load = ViewUI.Message.loading({
  60. content: "Loading...",
  61. duration: 0
  62. });
  63. }
  64. queue.push(1);
  65. // 为请求地址添加全局domain
  66. if (config.url.indexOf("http://") < 0) {
  67. config.url = GLOBAL.domain + config.url;
  68. }
  69. // 为请求头添加token信息
  70. let token = Vue.ls.get("token");
  71. if (token) {
  72. config.headers["Authorization"] = token;
  73. }
  74. // 设置延迟时效
  75. config.timeout = GLOBAL.timeout;
  76. return config;
  77. },
  78. error => {
  79. // 关闭loading提示
  80. // 串联并发请求,延时处理是为防止多个loading实例闪屏。
  81. setTimeout(() => {
  82. queue.shift();
  83. if (!queue.length) load();
  84. }, 100);
  85. return Promise.reject(error);
  86. }
  87. );
  88. axios.interceptors.response.use(
  89. response => {
  90. // 关闭loading提示
  91. setTimeout(() => {
  92. queue.shift();
  93. if (!queue.length) load();
  94. }, 100);
  95. return response;
  96. },
  97. error => {
  98. // 关闭loading提示
  99. setTimeout(() => {
  100. queue.shift();
  101. if (!queue.length) load();
  102. }, 100);
  103. return Promise.reject(error);
  104. }
  105. );
  106. new Vue({
  107. router,
  108. store,
  109. render: h => h(App)
  110. }).$mount("#app");