Michael Wang 4 жил өмнө
parent
commit
1c2432baca

+ 14 - 0
src/auth/auth.js

@@ -17,3 +17,17 @@ export function setToken(token) {
 export function removeToken() {
   return Cookies.remove(TokenKey);
 }
+
+const SessionKey = "SessionKey";
+
+export function getSessionId() {
+  return Cookies.get(SessionKey);
+}
+
+export function setSessionId(sessionId) {
+  return Cookies.set(SessionKey, sessionId);
+}
+
+export function removeSessionId() {
+  return Cookies.remove(SessionKey);
+}

+ 34 - 18
src/plugins/axiosApp.js

@@ -4,11 +4,19 @@ import axios from "axios";
 import { loadProgressBar } from "axios-progress-bar";
 import cachingGet from "./axiosCache";
 import { notifyInvalidTokenThrottled } from "./axiosNotice";
-import { getToken, removeToken } from "../auth/auth";
+import { getToken, removeToken, getSessionId } from "../auth/auth";
 import axiosRetry from "axios-retry";
 import { PLATFORM, DEVICE_ID } from "@/constant/constants";
 import { Notification } from "element-ui";
+import CryptoJS from "crypto-js";
 
+// console.log(btoa(CryptoJS.SHA1("pWWQ0qyaXL8QHni4ig9YiWYTKr8UVQd4")));
+// console.log(
+//   CryptoJS.enc.Base64.stringify(
+//     CryptoJS.SHA1("pWWQ0qyaXL8QHni4ig9YiWYTKr8UVQd4")
+//   )
+// );
+//QpVbMSbQrMVCxQEKqks8+E34+W8=
 // Full config:  https://github.com/axios/axios#request-config
 // axios.defaults.baseURL = process.env.BASE_URL || process.env.apiUrl || '';
 // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
@@ -23,33 +31,41 @@ const cacheGetUrls = [];
 const _axiosApp = axios.create(config);
 axiosRetry(_axiosApp);
 
-// 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];
-//   }
+function gToken(method, url, token, now) {
+  // const now = Date.now();
+  console.log(`${getSessionId()} ${method}&${url}&${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
-//   )}`;
+  const Authorization = `Token ${getSessionId()}:${CryptoJS.enc.Base64.stringify(
+    CryptoJS.SHA1("post&" + url + "&" + now + "&" + token)
+  )}`;
 
-//   return Authorization;
-// }
+  return Authorization;
+}
 
 _axiosApp.interceptors.request.use(
   function (config) {
     const wk_token = getToken();
+    const now = Date.now();
     if (wk_token) {
-      // config.headers.common["Authorization"] = gToken(config.url, wk_token);
-      config.headers.common["Authorization"] = wk_token;
+      const completeURL = new URL("http://nasty.com" + config.url);
+      const path = completeURL.pathname;
+      config.headers.common["Authorization"] = gToken(
+        config.method,
+        path,
+        wk_token,
+        now
+      );
+      // config.headers.common["Authorization"] = wk_token;
     }
     config.headers.common["platform"] = PLATFORM;
     config.headers.common["deviceId"] = DEVICE_ID;
-    config.headers.common["time"] = Date.now();
+    config.headers.common["time"] = now;
     return config;
   },
   function (error) {

+ 2 - 4
src/store/modules/user.js

@@ -2,7 +2,7 @@ import { loginByUsername, logout } from "@/api/login";
 // import { removeKeyToken, setKeyToken } from "@/auth/auth";
 // import { omit } from "lodash-es";
 import { LOGIN_BY_USERNAME, LOG_OUT, FED_LOG_OUT } from "../action-types";
-import { setToken } from "@/auth/auth";
+import { setToken, setSessionId } from "@/auth/auth";
 
 const user = {
   state: {
@@ -15,9 +15,6 @@ const user = {
       // state.user = user;
       state = Object.assign(state, user);
     },
-    SET_SMS_SEND_DATE: (state, date) => {
-      state = Object.assign(state, { smsSendDate: date });
-    },
   },
 
   actions: {
@@ -27,6 +24,7 @@ const user = {
         const data = response.data.data;
         commit("SET_USER", { ...data.account, roleCodes: data.roleCodes });
         setToken(data.accessToken);
+        setSessionId(data.sessionId);
       });
     },