|
@@ -1,8 +1,12 @@
|
|
|
import Vue from "vue";
|
|
|
+import Store from "@/store";
|
|
|
import axios from "axios";
|
|
|
import { loadProgressBar } from "axios-progress-bar";
|
|
|
import cachingGet from "./axiosCache";
|
|
|
-import { getKeyToken, removeKeyToken } from "../auth/auth";
|
|
|
+import { notifyInvalidTokenThrottled } from "./axiosNotice";
|
|
|
+import { getToken, removeToken } from "../auth/auth";
|
|
|
+
|
|
|
+const PLATFORM = "Wap";
|
|
|
|
|
|
// Full config: https://github.com/axios/axios#request-config
|
|
|
// axios.defaults.baseURL = process.env.BASE_URL || process.env.apiUrl || '';
|
|
@@ -17,23 +21,31 @@ const cacheGetUrls = [];
|
|
|
|
|
|
const _axiosApp = axios.create(config);
|
|
|
|
|
|
-/**
|
|
|
- * B. new token lifecycle
|
|
|
- * 1. 任何接口地址都尝试加上 key & token (第三方请求,单独做新的$http)
|
|
|
- * 2. 如果没有 key & token,就当做这是无需认证的api
|
|
|
- * 3. 做好token失效的处理,在interceptors.response 统一删除
|
|
|
- * 4. key & token 由外部管理,不在此插件管理。一般是在登录成功后设置。
|
|
|
- * xx. 额外:通过js-cookie将key等存入cookie
|
|
|
- */
|
|
|
+function gToken(uri, token) {
|
|
|
+ const now = Date.now();
|
|
|
+ // console.log(`${uri}&${now}&${token}`);
|
|
|
+ const a = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
+ let randomStr = "";
|
|
|
+ for (let i = 0; i < 6; i++) {
|
|
|
+ const idx = Math.round(Math.random() * 100) % a.length;
|
|
|
+ randomStr += a[idx];
|
|
|
+ }
|
|
|
+
|
|
|
+ const Authorization = `Token ${randomStr}${btoa(
|
|
|
+ uri + "&" + now + "&" + token
|
|
|
+ )}`;
|
|
|
+
|
|
|
+ return Authorization;
|
|
|
+}
|
|
|
|
|
|
_axiosApp.interceptors.request.use(
|
|
|
function(config) {
|
|
|
- if (config.url.endsWith("Login") === false) {
|
|
|
- const [wk_token, wk_key] = getKeyToken();
|
|
|
- if (wk_token) {
|
|
|
- config.headers.common["token"] = wk_token;
|
|
|
- config.headers.common["key"] = wk_key;
|
|
|
- }
|
|
|
+ const wk_token = getToken();
|
|
|
+ if (wk_token) {
|
|
|
+ config.headers.common["Authorization"] = gToken(config.url, wk_token);
|
|
|
+ config.headers.common["deviceId"] = Store.state.user.deviceId;
|
|
|
+ config.headers.common["domain"] = Store.state.user.domain;
|
|
|
+ config.headers.common["platform"] = PLATFORM;
|
|
|
}
|
|
|
return config;
|
|
|
},
|
|
@@ -69,18 +81,8 @@ _axiosApp.interceptors.response.use(
|
|
|
|
|
|
// 登录失效 跳转登录页面
|
|
|
if (status == 403 || status == 401) {
|
|
|
- if (
|
|
|
- window.___lastInvalidDate === undefined ||
|
|
|
- window.___lastInvalidDate < Date.now() - 300
|
|
|
- ) {
|
|
|
- Vue.prototype.$notify({
|
|
|
- showClose: true,
|
|
|
- message: "登录失效,请重新登录!",
|
|
|
- type: "error",
|
|
|
- });
|
|
|
- window.___lastInvalidDate = Date.now();
|
|
|
- }
|
|
|
- removeKeyToken();
|
|
|
+ notifyInvalidTokenThrottled();
|
|
|
+ removeToken();
|
|
|
return Promise.reject(error);
|
|
|
} else if (status == 405) {
|
|
|
Vue.prototype.$notify({
|