import Vue from "vue"; import axios from "axios"; import App from "./App.vue"; import router from "./router"; import store from "./store"; import globalVuePlugins from "./plugins/globalVuePlugins"; import GLOBAL from "./config"; import { jsonBigNumberToString } from "./plugins/utils"; // import { getAuthorisation } from "./plugins/crypto"; // https://github.com/RobinCK/vue-ls import VueLocalStorage from "vue-ls"; import ElementUI from "element-ui"; import "element-ui/lib/theme-chalk/index.css"; import "./assets/styles/index.scss"; Vue.use(ElementUI); Vue.use(VueLocalStorage, { storage: "session" }); Vue.use(globalVuePlugins); Vue.prototype.GLOBAL = GLOBAL; Vue.config.productionTip = false; // route interceptor router.beforeEach((to, from, next) => { const token = Vue.ls.get("token"); if (to.meta.noRequire) { next(); } else { // 需要登录的路由 if (token) { next(); } else { // 登录失效的处理 Vue.ls.clear(); next({ name: "Login" }); } } }); // axios interceptors var load = ""; // 同一时间有多个请求时,会形成队列。在第一个请求创建loading,在最后一个响应关闭loading var queue = []; // 解决js处理超过16位number时精度丢失的问题 axios.defaults.transformResponse = [ data => { return JSON.parse(jsonBigNumberToString(data)); } ]; // 设置延迟时效 axios.defaults.timeout = GLOBAL.timeout; axios.interceptors.request.use( config => { // 显示loading提示 if (!queue.length) { load = ElementUI.Message({ iconClass: "el-message__icon el-icon-loading", message: "Loading...", duration: 0 }); } queue.push(1); // 为请求头添加鉴权信息 let token = Vue.ls.get("token"); if (token) { // 新版鉴权 to open // const userId = Vue.ls.get("user").id; // const { Authorization, timestamp } = getAuthorisation( // { // token: token, // account: userId, // uri: config.url, // method: config.method // }, // "token" // ); // config.headers["Authorization"] = Authorization; // config.headers["time"] = timestamp; // config.headers["deviceId"] = userId; // config.headers["domain"] = window.location.origin; // config.headers["platform"] = "print-web"; config.headers["token"] = token; } return config; }, error => { // 关闭loading提示 // 串联并发请求,延时处理是为防止多个loading实例闪屏。 setTimeout(() => { queue.shift(); if (!queue.length) load.close(); }, 100); return Promise.reject(error); } ); axios.interceptors.response.use( response => { // 关闭loading提示 setTimeout(() => { queue.shift(); if (!queue.length) load.close(); }, 100); return response; }, error => { // 关闭loading提示 setTimeout(() => { queue.shift(); if (!queue.length) load.close(); }, 100); return Promise.reject(error); } ); new Vue({ router, store, render: h => h(App) }).$mount("#app");