-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
125 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// This constant sets the default cache time for the system, measured in seconds | ||
export const DEFAULT_STORAGE_TIME = 60 * 60 * 24 * 7; | ||
|
||
// This constant defines the key used for AES encryption of storage | ||
export const STORAGE_CIPHER_KEY = "boot-vant@"; | ||
|
||
// This constant defines the initialization vector used for AES encryption of storage | ||
export const STORAGE_CIPHER_IV = "@boot-vant"; | ||
|
||
// This constant determines whether storage encryption using AES should be enabled in the system | ||
// It is set to true in production mode and false in development mode | ||
export const SHOULD_ENABLE_STORAGE_ENCRYPTION = !import.meta.env.DEV; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { counter } from "./counter"; | ||
import type { App } from "vue"; | ||
import { createPinia } from "pinia"; | ||
import { registerPiniaPersistPlugin } from "~/store/plugin/persist"; | ||
|
||
const appStore: any = {}; | ||
const store = createPinia(); | ||
registerPiniaPersistPlugin(store); | ||
|
||
/** | ||
* 注册app状态库 | ||
*/ | ||
export const registerStore = () => { | ||
appStore.counter = counter(); | ||
}; | ||
export function setupStore(app: App<Element>) { | ||
app.use(store); | ||
} | ||
|
||
export default appStore; | ||
export { store }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Pinia Persist Plugin | ||
* Pinia 持久化插件 | ||
* @link https://prazdevs.github.io/pinia-plugin-persistedstate/zh/guide/ | ||
* | ||
*/ | ||
import type { Pinia } from "pinia"; | ||
import destr from "destr"; | ||
import { createPersistedState } from "pinia-plugin-persistedstate"; | ||
import type { PersistedStateFactoryOptions, Serializer } from "pinia-plugin-persistedstate"; | ||
import { | ||
SHOULD_ENABLE_STORAGE_ENCRYPTION, | ||
STORAGE_CIPHER_IV, | ||
STORAGE_CIPHER_KEY, | ||
} from "~/setting/encryptionSetting"; | ||
import type { Encryption } from "~/utils"; | ||
import { EncryptionFactory, createStorageName } from "~/utils"; | ||
import type { GlobEnvConfig } from "#/config"; | ||
|
||
const persistEncryption: Encryption = EncryptionFactory.createAesEncryption({ key: STORAGE_CIPHER_KEY, iv: STORAGE_CIPHER_IV }); | ||
|
||
export const PERSIST_KEY_PREFIX = createStorageName(<GlobEnvConfig>import.meta.env); | ||
|
||
/** | ||
* Custom serializer for serialization and deserialization of storage data | ||
* 自定义序列化器,用于序列化和反序列化存储数据 | ||
* | ||
* @param shouldEnableEncryption whether to enable encryption for storage data 是否启用存储数据加密 | ||
* @returns serializer | ||
*/ | ||
const customSerializer = (shouldEnableEncryption: boolean): Serializer => { | ||
if (shouldEnableEncryption) { | ||
return { | ||
deserialize: (value) => { | ||
const decrypted = persistEncryption.decrypt(value); | ||
return destr(decrypted); | ||
}, | ||
serialize: (value) => { | ||
const serialized = JSON.stringify(value); | ||
return persistEncryption.encrypt(serialized); | ||
}, | ||
}; | ||
} else { | ||
return { | ||
deserialize: (value) => { | ||
return destr(value); | ||
}, | ||
serialize: (value) => { | ||
return JSON.stringify(value); | ||
}, | ||
}; | ||
} | ||
}; | ||
|
||
/** | ||
* Register Pinia Persist Plugin | ||
* 注册 Pinia 持久化插件 | ||
* | ||
* @param pinia Pinia instance Pinia 实例 | ||
*/ | ||
export function registerPiniaPersistPlugin(pinia: Pinia) { | ||
pinia.use(createPersistedState(createPersistedStateOptions(PERSIST_KEY_PREFIX))); | ||
} | ||
|
||
/** | ||
* Create Persisted State Options | ||
* 创建持久化状态选项 | ||
* | ||
* @param keyPrefix prefix for storage key 储存键前缀 | ||
* @returns persisted state factory options | ||
*/ | ||
export function createPersistedStateOptions(keyPrefix: string): PersistedStateFactoryOptions { | ||
return { | ||
storage: localStorage, | ||
key: id => `${keyPrefix}__${id}`, | ||
serializer: customSerializer(SHOULD_ENABLE_STORAGE_ENCRYPTION), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters