mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-01-24 18:40:22 +08:00
feat: pinia persist plugin custom serializer (#3244)
This commit is contained in:
parent
dccc8f625d
commit
ea51c492c2
@ -10,4 +10,4 @@ export const cacheCipher = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Whether the system cache is encrypted using aes
|
// Whether the system cache is encrypted using aes
|
||||||
export const enableStorageEncryption = !isDevMode();
|
export const SHOULD_ENABLE_STORAGE_ENCRYPTION = !isDevMode();
|
||||||
|
@ -5,13 +5,49 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
import type { Pinia } from 'pinia';
|
import type { Pinia } from 'pinia';
|
||||||
import { createPersistedState } from 'pinia-plugin-persistedstate';
|
import { createPersistedState, Serializer } from 'pinia-plugin-persistedstate';
|
||||||
import type { PersistedStateFactoryOptions } from 'pinia-plugin-persistedstate';
|
import type { PersistedStateFactoryOptions } from 'pinia-plugin-persistedstate';
|
||||||
import { getCommonStoragePrefix } from '@/utils/env';
|
import { getCommonStoragePrefix } from '@/utils/env';
|
||||||
|
import { Encryption, EncryptionFactory } from '@/utils/cipher';
|
||||||
|
import { cacheCipher, SHOULD_ENABLE_STORAGE_ENCRYPTION } from '@/settings/encryptionSetting';
|
||||||
|
|
||||||
export const PERSIST_KEY_PREFIX = getCommonStoragePrefix();
|
export const PERSIST_KEY_PREFIX = getCommonStoragePrefix();
|
||||||
|
|
||||||
// TODO customSerializer
|
const persistEncryption: Encryption = EncryptionFactory.createAesEncryption({
|
||||||
|
key: cacheCipher.key,
|
||||||
|
iv: cacheCipher.iv,
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom serializer for serialization and deserialization of storage data
|
||||||
|
* 自定义序列化器,用于序列化和反序列化存储数据
|
||||||
|
*
|
||||||
|
* @param shouldEnableEncryption whether to enable encryption for storage data 是否启用存储数据加密
|
||||||
|
* @returns serializer
|
||||||
|
*/
|
||||||
|
function customSerializer(shouldEnableEncryption: boolean): Serializer {
|
||||||
|
if (shouldEnableEncryption) {
|
||||||
|
return {
|
||||||
|
deserialize: (value) => {
|
||||||
|
const decrypted = persistEncryption.decrypt(value);
|
||||||
|
return JSON.parse(decrypted);
|
||||||
|
},
|
||||||
|
serialize: (value) => {
|
||||||
|
const serialized = JSON.stringify(value);
|
||||||
|
return persistEncryption.encrypt(serialized);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
deserialize: (value) => {
|
||||||
|
return JSON.parse(value);
|
||||||
|
},
|
||||||
|
serialize: (value) => {
|
||||||
|
return JSON.stringify(value);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register Pinia Persist Plugin
|
* Register Pinia Persist Plugin
|
||||||
@ -34,5 +70,6 @@ export function createPersistedStateOptions(keyPrefix: string): PersistedStateFa
|
|||||||
return {
|
return {
|
||||||
storage: localStorage,
|
storage: localStorage,
|
||||||
key: (id) => `${keyPrefix}__${id}`,
|
key: (id) => `${keyPrefix}__${id}`,
|
||||||
|
serializer: customSerializer(SHOULD_ENABLE_STORAGE_ENCRYPTION),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
7
src/utils/cache/index.ts
vendored
7
src/utils/cache/index.ts
vendored
@ -1,13 +1,16 @@
|
|||||||
import { getStorageShortName } from '/@/utils/env';
|
import { getStorageShortName } from '/@/utils/env';
|
||||||
import { createStorage as create, CreateStorageParams } from './storageCache';
|
import { createStorage as create, CreateStorageParams } from './storageCache';
|
||||||
import { enableStorageEncryption, DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
|
import {
|
||||||
|
SHOULD_ENABLE_STORAGE_ENCRYPTION,
|
||||||
|
DEFAULT_CACHE_TIME,
|
||||||
|
} from '/@/settings/encryptionSetting';
|
||||||
|
|
||||||
export type Options = Partial<CreateStorageParams>;
|
export type Options = Partial<CreateStorageParams>;
|
||||||
|
|
||||||
const createOptions = (storage: Storage, options: Options = {}): Options => {
|
const createOptions = (storage: Storage, options: Options = {}): Options => {
|
||||||
return {
|
return {
|
||||||
// No encryption in debug mode
|
// No encryption in debug mode
|
||||||
hasEncrypt: enableStorageEncryption,
|
hasEncrypt: SHOULD_ENABLE_STORAGE_ENCRYPTION,
|
||||||
storage,
|
storage,
|
||||||
prefixKey: getStorageShortName(),
|
prefixKey: getStorageShortName(),
|
||||||
...options,
|
...options,
|
||||||
|
1
src/utils/cache/storageCache.ts
vendored
1
src/utils/cache/storageCache.ts
vendored
@ -9,6 +9,7 @@ export interface CreateStorageParams extends EncryptionParams {
|
|||||||
hasEncrypt: boolean;
|
hasEncrypt: boolean;
|
||||||
timeout?: Nullable<number>;
|
timeout?: Nullable<number>;
|
||||||
}
|
}
|
||||||
|
// TODO 移除此文件夹下全部代码
|
||||||
export const createStorage = ({
|
export const createStorage = ({
|
||||||
prefixKey = '',
|
prefixKey = '',
|
||||||
storage = sessionStorage,
|
storage = sessionStorage,
|
||||||
|
Loading…
Reference in New Issue
Block a user