main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 globalVuePlugins from "./plugins/globalVuePlugins";
  7. import GLOBAL from "./config";
  8. import { jsonBigNumberToString } from "./plugins/utils";
  9. // import { getAuthorisation } from "./plugins/crypto";
  10. // https://github.com/RobinCK/vue-ls
  11. import VueLocalStorage from "vue-ls";
  12. import ElementUI from "element-ui";
  13. import "element-ui/lib/theme-chalk/index.css";
  14. import "./assets/styles/index.scss";
  15. Vue.use(ElementUI);
  16. Vue.use(VueLocalStorage, { storage: "session" });
  17. Vue.use(globalVuePlugins);
  18. Vue.prototype.GLOBAL = GLOBAL;
  19. Vue.config.productionTip = false;
  20. // route interceptor
  21. router.beforeEach((to, from, next) => {
  22. const token = Vue.ls.get("token");
  23. if (to.meta.noRequire) {
  24. next();
  25. } else {
  26. // 需要登录的路由
  27. if (token) {
  28. next();
  29. } else {
  30. // 登录失效的处理
  31. Vue.ls.clear();
  32. next({ name: "Login" });
  33. }
  34. }
  35. });
  36. // axios interceptors
  37. var load = "";
  38. // 同一时间有多个请求时,会形成队列。在第一个请求创建loading,在最后一个响应关闭loading
  39. var queue = [];
  40. // 解决js处理超过16位number时精度丢失的问题
  41. axios.defaults.transformResponse = [
  42. data => {
  43. return JSON.parse(jsonBigNumberToString(data));
  44. }
  45. ];
  46. // 设置延迟时效
  47. axios.defaults.timeout = GLOBAL.timeout;
  48. axios.interceptors.request.use(
  49. config => {
  50. // 显示loading提示
  51. if (!queue.length) {
  52. load = ElementUI.Message({
  53. iconClass: "el-message__icon el-icon-loading",
  54. message: "Loading...",
  55. duration: 0
  56. });
  57. }
  58. queue.push(1);
  59. // 为请求头添加鉴权信息
  60. let token = Vue.ls.get("token");
  61. if (token) {
  62. // 新版鉴权 to open
  63. // const userId = Vue.ls.get("user").id;
  64. // const { Authorization, timestamp } = getAuthorisation(
  65. // {
  66. // token: token,
  67. // account: userId,
  68. // uri: config.url,
  69. // method: config.method
  70. // },
  71. // "token"
  72. // );
  73. // config.headers["Authorization"] = Authorization;
  74. // config.headers["time"] = timestamp;
  75. // config.headers["deviceId"] = userId;
  76. // config.headers["domain"] = window.location.origin;
  77. // config.headers["platform"] = "print-web";
  78. config.headers["token"] = token;
  79. }
  80. return config;
  81. },
  82. error => {
  83. // 关闭loading提示
  84. // 串联并发请求,延时处理是为防止多个loading实例闪屏。
  85. setTimeout(() => {
  86. queue.shift();
  87. if (!queue.length) load.close();
  88. }, 100);
  89. return Promise.reject(error);
  90. }
  91. );
  92. axios.interceptors.response.use(
  93. response => {
  94. // 关闭loading提示
  95. setTimeout(() => {
  96. queue.shift();
  97. if (!queue.length) load.close();
  98. }, 100);
  99. return response;
  100. },
  101. error => {
  102. // 关闭loading提示
  103. setTimeout(() => {
  104. queue.shift();
  105. if (!queue.length) load.close();
  106. }, 100);
  107. return Promise.reject(error);
  108. }
  109. );
  110. new Vue({
  111. router,
  112. store,
  113. render: h => h(App)
  114. }).$mount("#app");