|
@@ -1,25 +1,34 @@
|
|
|
|
|
|
import { useUserStoreWithOut, UserInterface } from "@/stores/modules/user";
|
|
|
import { getAuthCode } from "dingtalk-jsapi";
|
|
|
-import { getStoreObject, getStoreString, setStoreObject } from '@/utils/common'
|
|
|
import reqest from "@/utils/request";
|
|
|
import { useDictStoreWithOut } from "@/utils/dict";
|
|
|
|
|
|
const USERINFO_KEY: string = '_userInfo';
|
|
|
-const TOKEN_KEY: string = 'ACCESS_TOKEN'
|
|
|
+const TOKEN_KEY: string = 'ACCESS_TOKEN';
|
|
|
+const EXPIRES_TIME_KEY: string = 'EXPIRES_TIME_KEY';
|
|
|
|
|
|
const initUserInfoHandler = async () => {
|
|
|
const userStore = useUserStoreWithOut();
|
|
|
- const userInfoObject = getStoreObject(USERINFO_KEY);
|
|
|
- if (getStoreString(TOKEN_KEY) && userInfoObject) {
|
|
|
- userStore.setUser(userInfoObject);
|
|
|
- return true;
|
|
|
+ const userInfoString = localStorage.getItem(USERINFO_KEY);
|
|
|
+ if (localStorage.getItem(TOKEN_KEY) && userInfoString) {
|
|
|
+ const expiresTimeString = localStorage.getItem(EXPIRES_TIME_KEY);
|
|
|
+ const date = new Date();
|
|
|
+ const time = date.getTime();
|
|
|
+ if (expiresTimeString && Number(expiresTimeString) > time) {
|
|
|
+ localStorage.clear();
|
|
|
+ } else {
|
|
|
+ const userInfoObject = JSON.parse(userInfoString);
|
|
|
+ userStore.setUser(userInfoObject);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
};
|
|
|
try {
|
|
|
const code: string = (await getDDAuthCode()) as string;
|
|
|
const userIdResult = await getUserIdByDD({ code });
|
|
|
if (userIdResult && userIdResult?.data) {
|
|
|
- setStoreObject(TOKEN_KEY, userIdResult?.data.accessToken);
|
|
|
+ localStorage.setItem(TOKEN_KEY, userIdResult?.data.accessToken);
|
|
|
+ localStorage.setItem(EXPIRES_TIME_KEY, userIdResult?.data.expiresTime)
|
|
|
useDictStoreWithOut();
|
|
|
const userResult = await getUserInfoById();
|
|
|
if (userResult && userResult?.data) {
|
|
@@ -32,7 +41,7 @@ const initUserInfoHandler = async () => {
|
|
|
signatureUrl: userResult?.data.user.signatureUrl,
|
|
|
};
|
|
|
userStore.setUser(userInfo);
|
|
|
- setStoreObject(USERINFO_KEY, userInfo);
|
|
|
+ localStorage.setItem(USERINFO_KEY, JSON.stringify(userInfo));
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|