feat(scene): 新增场景状态模块(内存+storage 持久化)

This commit is contained in:
ozh 2026-08-18 17:26:16 +08:00
parent 4809682076
commit a87ad4ea7a
1 changed files with 42 additions and 0 deletions

42
store/scene.js Normal file
View File

@ -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)
}