From a87ad4ea7a09658b4f8097c6615bf639c5c53dc3 Mon Sep 17 00:00:00 2001 From: ozh Date: Tue, 18 Aug 2026 17:26:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(scene):=20=E6=96=B0=E5=A2=9E=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E7=8A=B6=E6=80=81=E6=A8=A1=E5=9D=97=EF=BC=88=E5=86=85?= =?UTF-8?q?=E5=AD=98+storage=20=E6=8C=81=E4=B9=85=E5=8C=96=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- store/scene.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 store/scene.js diff --git a/store/scene.js b/store/scene.js new file mode 100644 index 0000000..82ada65 --- /dev/null +++ b/store/scene.js @@ -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) +}