Selaa lähdekoodia

本地和服务端时间同步

zhangjie 4 vuotta sitten
vanhempi
commit
0c3611d34e

+ 4 - 1
src/main.js

@@ -17,6 +17,7 @@ import "element-ui/lib/theme-chalk/index.css";
 import "./assets/styles/index.scss";
 import "../card/assets/styles/module.scss";
 import { PLATFORM, DEVICE_ID } from "./constants/app";
+import { fetchTime } from "./plugins/syncServerTime";
 
 Vue.use(ElementUI, { size: "medium" });
 
@@ -78,8 +79,10 @@ axios.interceptors.request.use(
 
       // 新版鉴权 to open
       const sessionId = Vue.ls.get("user", { sessionId: "" }).sessionId;
-      const { Authorization, timestamp } = getAuthorization(
+      const timestamp = fetchTime();
+      const Authorization = getAuthorization(
         {
+          timestamp,
           token: token,
           account: sessionId,
           uri: config.url.split("?")[0],

+ 3 - 0
src/modules/login/api.js

@@ -33,3 +33,6 @@ export const getSchoolInfo = code => {
 export const getSysConfig = key => {
   return $postParam("/api/admin/common/sys_config/get_one", { key });
 };
+export const getSysTime = () => {
+  return $postParam("/api/admin/common/get_system_time", {});
+};

+ 13 - 1
src/modules/login/views/Login.vue

@@ -69,10 +69,17 @@
 
 <script>
 import { password, smscode } from "@/plugins/formRules";
-import { login, getSmsCode, getSchoolInfo, getSysConfig } from "../api";
+import {
+  login,
+  getSmsCode,
+  getSchoolInfo,
+  getSysConfig,
+  getSysTime
+} from "../api";
 import { Base64 } from "@/plugins/crypto";
 import ResetPwd from "@/modules/base/components/ResetPwd";
 import { ORG_CODE } from "@/constants/app";
+import { syncTime } from "@/plugins/syncServerTime";
 
 const wstorage = {
   set(key, value, expire = null) {
@@ -138,8 +145,13 @@ export default {
     this.setWaitingTime();
     this.getSchool();
     this.getSmsCodeRequired();
+    this.syncServerTime();
   },
   methods: {
+    async syncServerTime() {
+      const time = await getSysTime();
+      syncTime(time);
+    },
     async getSmsCodeRequired() {
       const data = await getSysConfig("sys.code.enable");
       this.smsCodeRequired = data.configValue === "true";

+ 8 - 15
src/plugins/crypto.js

@@ -24,26 +24,19 @@ export const AES = content => {
  */
 export const getAuthorization = (infos, type) => {
   // {type} {invoker}:base64(sha1(method&uri&timestamp&{secret}))
-  const timestamp = Date.now();
   if (type === "secret") {
     // accessKey | method&uri&timestamp&accessSecret
-    const str = `${infos.method.toLowerCase()}&${infos.uri}&${timestamp}&${
-      infos.accessSecret
-    }`;
+    const str = `${infos.method.toLowerCase()}&${infos.uri}&${
+      infos.timestamp
+    }&${infos.accessSecret}`;
     const sign = CryptoJS.enc.Base64.stringify(CryptoJS.SHA1(str));
-    return {
-      timestamp,
-      Authorization: `Secret ${infos.accessKey}:${sign}`
-    };
+    return `Secret ${infos.accessKey}:${sign}`;
   } else if (type === "token") {
     // userId | method&uri&timestamp&token
-    const str = `${infos.method.toLowerCase()}&${infos.uri}&${timestamp}&${
-      infos.token
-    }`;
+    const str = `${infos.method.toLowerCase()}&${infos.uri}&${
+      infos.timestamp
+    }&${infos.token}`;
     const sign = CryptoJS.enc.Base64.stringify(CryptoJS.SHA1(str));
-    return {
-      timestamp,
-      Authorization: `Token ${infos.account}:${sign}`
-    };
+    return `Token ${infos.account}:${sign}`;
   }
 };

+ 35 - 0
src/plugins/syncServerTime.js

@@ -0,0 +1,35 @@
+let serverTime = getLocalTime();
+let setTs = [];
+
+syncTime(serverTime);
+
+function getLocalTime() {
+  const time = localStorage.getItem("st");
+  const unvalidVals = ["Infinity", "NaN", "null", "undefined"];
+  return unvalidVals.includes(time) ? Date.now() : time * 1;
+}
+
+function addSetTime(action, time = 1 * 1000) {
+  setTs.push(setTimeout(action, time));
+}
+
+function clearSetTs() {
+  if (!setTs.length) return;
+  setTs.forEach(t => clearTimeout(t));
+  setTs = [];
+}
+
+function syncTime(time) {
+  clearSetTs();
+  serverTime = time;
+  localStorage.setItem("st", time);
+  addSetTime(() => {
+    syncTime(serverTime + 1000);
+  });
+}
+
+function fetchTime() {
+  return serverTime;
+}
+
+export { syncTime, fetchTime };