feat(scene): 新增场景状态模块(内存+storage 持久化)
This commit is contained in:
parent
4809682076
commit
a87ad4ea7a
|
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* 当前场景全局状态:内存 + uni.storage 持久化。
|
||||
* 项目未引入 Vuex/Pinia,采用轻量模块方案。
|
||||
*/
|
||||
const STORAGE_KEY = 'current_scene'
|
||||
|
||||
let currentScene = null
|
||||
|
||||
// 模块加载时从本地存储恢复上次选中的场景。
|
||||
function initFromStorage() {
|
||||
const cached = uni.getStorageSync(STORAGE_KEY)
|
||||
if (cached && cached.id !== undefined) {
|
||||
currentScene = cached
|
||||
}
|
||||
}
|
||||
|
||||
initFromStorage()
|
||||
|
||||
/**
|
||||
* 获取当前选中场景。
|
||||
* @returns {{ id: *, name: string } | null}
|
||||
*/
|
||||
export function getCurrentScene() {
|
||||
return currentScene
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前场景并持久化。
|
||||
* @param {{ id: *, name: string }} scene
|
||||
*/
|
||||
export function setCurrentScene(scene) {
|
||||
currentScene = scene
|
||||
uni.setStorageSync(STORAGE_KEY, scene)
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除当前场景(本地与持久化均清除)。
|
||||
*/
|
||||
export function clearCurrentScene() {
|
||||
currentScene = null
|
||||
uni.removeStorageSync(STORAGE_KEY)
|
||||
}
|
||||
Loading…
Reference in New Issue