|
@@ -1,25 +1,36 @@
|
|
|
|
|
|
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 () => {
|
|
|
+export const initUserInfoHandler = async (isStore: boolean = false) => {
|
|
|
const userStore = useUserStoreWithOut();
|
|
|
- const userInfoObject = getStoreObject(USERINFO_KEY);
|
|
|
- if (getStoreString(TOKEN_KEY) && userInfoObject) {
|
|
|
- userStore.setUser(userInfoObject);
|
|
|
- return true;
|
|
|
- };
|
|
|
+ if (isStore) {
|
|
|
+ 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 +43,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;
|
|
@@ -80,8 +91,8 @@ const getUserIdByDD = async (data: Required<UserIdParam>) => {
|
|
|
const getUserInfoById = async () => {
|
|
|
return reqest.get("/admin-api/system/auth/get-permission-info");
|
|
|
};
|
|
|
-export const getUserInfoPromise = () => {
|
|
|
+export const getUserInfoPromise = (isStore:boolean = false) => {
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
- resolve(await initUserInfoHandler());
|
|
|
+ resolve(await initUserInfoHandler(isStore));
|
|
|
})
|
|
|
}
|