Files
huachuang/nl-ui/nl-ui-admin-vben/yudao-ui-admin-vben/packages/stores/src/setup.ts

82 lines
1.9 KiB
TypeScript
Raw Normal View History

2026-07-08 15:08:41 +08:00
import type { Pinia } from 'pinia';
import type { App } from 'vue';
import { createPinia } from 'pinia';
import SecureLS from 'secure-ls';
let pinia: Pinia;
type SecureLSStorage = {
get(key: string): any;
set(key: string, value: unknown): void;
};
type SecureLSCtor = new (config?: {
encodingType?: string;
encryptionSecret?: string;
isCompression?: boolean;
metaKey?: string;
}) => SecureLSStorage;
const secureLSModule = SecureLS as unknown as {
default?: SecureLSCtor;
SecureLS?: SecureLSCtor;
};
const SecureLSConstructor =
secureLSModule.default ??
secureLSModule.SecureLS ??
(SecureLS as unknown as SecureLSCtor);
export interface InitStoreOptions {
/**
* @zh_CN , @vben/stores appapp缓存冲突,
*/
namespace: string;
}
/**
* @zh_CN pinia
*/
export async function initStores(app: App, options: InitStoreOptions) {
const { createPersistedState } = await import('pinia-plugin-persistedstate');
pinia = createPinia();
const { namespace } = options;
const ls = new SecureLSConstructor({
encodingType: 'aes',
encryptionSecret: import.meta.env.VITE_APP_STORE_SECURE_KEY,
isCompression: true,
metaKey: `${namespace}-secure-meta`,
});
pinia.use(
createPersistedState({
// key $appName-$store.id
key: (storeKey) => `${namespace}-${storeKey}`,
storage: import.meta.env.DEV
? localStorage
: {
getItem(key) {
return ls.get(key);
},
setItem(key, value) {
ls.set(key, value);
},
},
}),
);
app.use(pinia);
return pinia;
}
export function resetAllStores() {
if (!pinia) {
console.error('Pinia is not installed');
return;
}
const allStores = (pinia as any)._s;
for (const [_key, store] of allStores) {
store.$reset();
}
}