ow
This commit is contained in:
1 parent
1404640f16
commit
2bbf016cbf
167 files changed
+18941
-589
No files matched your search
@@ -0,0 +1,11 @@
|
||||
//跨页面管理会话状态,页面刷新数据就会清空
|
||||
import { createPinia } from 'pinia';
|
||||
import type { App } from 'vue';
|
||||
|
||||
const store = createPinia();
|
||||
|
||||
export function setupStore(app: App<Element>) {
|
||||
app.use(store);
|
||||
}
|
||||
|
||||
export { store };
|
||||
@@ -0,0 +1,15 @@
|
||||
import { storage } from "@/store/storage";
|
||||
import { SecurityFunctionDisplayData } from "@/data/auto/securityFunctionDisplayData";
|
||||
export const ROUTES = "x-routes";
|
||||
|
||||
export const getRoutes = (): Array<SecurityFunctionDisplayData> => {
|
||||
let str = storage.get(ROUTES);
|
||||
if (str === null) {
|
||||
return [];
|
||||
}
|
||||
return JSON.parse(str) as Array<SecurityFunctionDisplayData>;
|
||||
};
|
||||
|
||||
export const setRoutes = (data: Array<SecurityFunctionDisplayData>) => {
|
||||
storage.set(ROUTES, JSON.stringify(data));
|
||||
};
|
||||
@@ -0,0 +1,111 @@
|
||||
//首字母必须是小写
|
||||
import { defineStore } from "pinia";
|
||||
import { store } from "@/store";
|
||||
import { LoginProfile } from "@/data/auto/loginProfile";
|
||||
import { storage } from "@/store/storage";
|
||||
import { JwtInfo } from "@/data/auto/jwtInfo";
|
||||
|
||||
const JWT_STORAGE_ID = "x-jwt-storage-id";
|
||||
const JWT_REFRESHKEY_STORAGE_ID = "x-jwt-refresh-key-storage-id";
|
||||
const LOGIN_PROFILE_STORAGE_ID = "x-login-profile-storage-id";
|
||||
const LOCALE_ID = "x-locale-id";
|
||||
|
||||
export const useLoginProfileStore = defineStore("loginProfile", {
|
||||
state: (): {
|
||||
loginProfile: LoginProfile;
|
||||
userMobile: string;
|
||||
} => {
|
||||
return {
|
||||
loginProfile: new LoginProfile(),
|
||||
userMobile: "",
|
||||
};
|
||||
},
|
||||
actions: {
|
||||
updateLoginProfile(loginProfile: LoginProfile) {
|
||||
this.loginProfile = loginProfile;
|
||||
storage.set(JWT_STORAGE_ID, "Bearer " + loginProfile.Token.AccessToken);
|
||||
storage.set(JWT_REFRESHKEY_STORAGE_ID, loginProfile.Token.RefreshToken);
|
||||
storage.set(LOGIN_PROFILE_STORAGE_ID, JSON.stringify(this.loginProfile));
|
||||
},
|
||||
updateToken(token: string) {
|
||||
this.loginProfile.Token.AccessToken = token;
|
||||
storage.set(JWT_STORAGE_ID, token);
|
||||
},
|
||||
updateJwtInfo(jwtInfo: JwtInfo) {
|
||||
storage.set(JWT_STORAGE_ID, "Bearer " + jwtInfo.AccessToken);
|
||||
storage.set(JWT_REFRESHKEY_STORAGE_ID, jwtInfo.RefreshToken);
|
||||
},
|
||||
getToken() {
|
||||
return storage.get(JWT_STORAGE_ID);
|
||||
},
|
||||
setLocale(locale: string) {
|
||||
return storage.set(LOCALE_ID, locale);
|
||||
},
|
||||
getLocale() {
|
||||
return storage.get(LOCALE_ID);
|
||||
},
|
||||
getJwtInfo() {
|
||||
let jwt: JwtInfo = new JwtInfo();
|
||||
jwt.AccessToken = storage.get(JWT_STORAGE_ID);
|
||||
jwt.RefreshToken = storage.get(JWT_REFRESHKEY_STORAGE_ID);
|
||||
return jwt;
|
||||
},
|
||||
getRefreshToken() {
|
||||
return storage.get(JWT_REFRESHKEY_STORAGE_ID);
|
||||
},
|
||||
getUserEngName() {
|
||||
let str = storage.get(LOGIN_PROFILE_STORAGE_ID);
|
||||
if (str === null) {
|
||||
return null;
|
||||
}
|
||||
return (JSON.parse(str) as LoginProfile).UserEngName;
|
||||
},
|
||||
getUserLocalName() {
|
||||
let str = storage.get(LOGIN_PROFILE_STORAGE_ID);
|
||||
if (str === null) {
|
||||
return null;
|
||||
}
|
||||
return (JSON.parse(str) as LoginProfile).UserLocalName;
|
||||
},
|
||||
getCreatedTime() {
|
||||
let str = storage.get(LOGIN_PROFILE_STORAGE_ID);
|
||||
if (str === null) {
|
||||
return null;
|
||||
}
|
||||
return (JSON.parse(str) as LoginProfile).Token.CreatedTime.toString();
|
||||
},
|
||||
getUsername() {
|
||||
let str = storage.get(LOGIN_PROFILE_STORAGE_ID);
|
||||
if (str === null) {
|
||||
return null;
|
||||
}
|
||||
return (JSON.parse(str) as LoginProfile).Username;
|
||||
},
|
||||
getUserMobile() {
|
||||
let str = storage.get(LOGIN_PROFILE_STORAGE_ID);
|
||||
if (str === null) {
|
||||
return null;
|
||||
}
|
||||
return (JSON.parse(str) as LoginProfile).UserMobile;
|
||||
},
|
||||
getPermissionInfo() {
|
||||
let str = storage.get(LOGIN_PROFILE_STORAGE_ID);
|
||||
if (str === null) {
|
||||
return null;
|
||||
}
|
||||
return (JSON.parse(str) as LoginProfile).PermissionInfo;
|
||||
},
|
||||
clearStorage() {
|
||||
|
||||
storage.set(JWT_STORAGE_ID, null);
|
||||
storage.set(JWT_REFRESHKEY_STORAGE_ID, null);
|
||||
storage.set(LOGIN_PROFILE_STORAGE_ID, null);
|
||||
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// 在组件setup函数外使用
|
||||
export function useLoginProfileStoreWithOut() {
|
||||
return useLoginProfileStore(store);
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
// 默认缓存期限为1天
|
||||
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 1;
|
||||
|
||||
/**
|
||||
* 创建本地缓存对象
|
||||
* @param {string=} prefixKey -
|
||||
* @param {Object} [storage=localStorage] - sessionStorage | localStorage
|
||||
*/
|
||||
export const createStorage = ({ prefixKey = '', storage = localStorage } = {}) => {
|
||||
/**
|
||||
* 本地缓存类
|
||||
* @class Storage
|
||||
*/
|
||||
const Storage = class {
|
||||
private storage = storage;
|
||||
private prefixKey?: string = prefixKey;
|
||||
|
||||
private getKey(key: string) {
|
||||
return `${this.prefixKey}${key}`.toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 设置缓存
|
||||
* @param {string} key 缓存键
|
||||
* @param {*} value 缓存值
|
||||
* @param expire
|
||||
*/
|
||||
set(key: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
|
||||
const stringData = JSON.stringify({
|
||||
value,
|
||||
expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
|
||||
});
|
||||
this.storage.setItem(this.getKey(key), stringData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @param {string} key 缓存键
|
||||
* @param {*=} def 默认值
|
||||
*/
|
||||
get<T = any>(key: string, def: any = null): T {
|
||||
const item = this.storage.getItem(this.getKey(key));
|
||||
if (item) {
|
||||
try {
|
||||
const data = JSON.parse(item);
|
||||
const { value, expire } = data;
|
||||
// 在有效期内直接返回
|
||||
if (expire === null || expire >= Date.now()) {
|
||||
return value;
|
||||
}
|
||||
this.remove(this.getKey(key));
|
||||
} catch (e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓存删除某项
|
||||
* @param {string} key
|
||||
*/
|
||||
remove(key: string) {
|
||||
this.storage.removeItem(this.getKey(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有缓存
|
||||
* @memberOf Cache
|
||||
*/
|
||||
clear(): void {
|
||||
this.storage.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置cookie
|
||||
* @param {string} name cookie 名称
|
||||
* @param {*} value cookie 值
|
||||
* @param {number=} expire 过期时间
|
||||
* 如果过期时间为设置,默认关闭浏览器自动删除
|
||||
* @example
|
||||
*/
|
||||
setCookie(name: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
|
||||
document.cookie = `${this.getKey(name)}=${value}; Max-Age=${expire}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据名字获取cookie值
|
||||
* @param name
|
||||
*/
|
||||
getCookie(name: string): string {
|
||||
const cookieArr = document.cookie.split('; ');
|
||||
for (let i = 0, length = cookieArr.length; i < length; i++) {
|
||||
const kv = cookieArr[i].split('=');
|
||||
if (kv[0] === this.getKey(name)) {
|
||||
return kv[1];
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据名字删除指定的cookie
|
||||
* @param {string} key
|
||||
*/
|
||||
removeCookie(key: string) {
|
||||
this.setCookie(key, 1, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空cookie,使所有cookie失效
|
||||
*/
|
||||
clearCookie(): void {
|
||||
const keys = document.cookie.match(/[^ =;]+(?==)/g);
|
||||
if (keys) {
|
||||
for (let i = keys.length; i--; ) {
|
||||
document.cookie = `${keys[i]}=0;expire=${new Date(0).toUTCString()}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
return new Storage();
|
||||
};
|
||||
|
||||
export const storage = createStorage();
|
||||
|
||||
export default storage;
|
||||
Reference in new issue
Block a user