const GLOBAL_HIDDEN_KEY = "tb_qiaobao_hidden_all"; const PAGE_HIDDEN_KEY = "tb_qiaobao_hidden_pages"; const DOCK_EDGE_KEY = "tb_qiaobao_dock_edge"; const POSITION_KEY = "tb_qiaobao_position"; export function getQiaobaoDockEdge() { return uni.getStorageSync(DOCK_EDGE_KEY) === "left" ? "left" : "right"; } export function setQiaobaoDockEdge(edge) { uni.setStorageSync(DOCK_EDGE_KEY, edge === "left" ? "left" : "right"); } export function getQiaobaoPosition() { const position = uni.getStorageSync(POSITION_KEY); if (!position || typeof position !== "object") return null; const x = Number(position.x); const y = Number(position.y); return Number.isFinite(x) && Number.isFinite(y) ? { x, y } : null; } export function saveQiaobaoPosition(position) { if (!position) return; const x = Number(position.x); const y = Number(position.y); if (Number.isFinite(x) && Number.isFinite(y)) { uni.setStorageSync(POSITION_KEY, { x, y }); } } export function getCurrentPagePath() { try { const pages = getCurrentPages(); const current = pages[pages.length - 1]; return current?.route || current?.$page?.fullPath?.split("?")[0] || "unknown"; } catch (error) { return "unknown"; } } export function isQiaobaoHidden(pagePath = getCurrentPagePath()) { const hiddenAll = uni.getStorageSync(GLOBAL_HIDDEN_KEY); const hiddenPages = uni.getStorageSync(PAGE_HIDDEN_KEY); return hiddenAll === true || (Array.isArray(hiddenPages) && hiddenPages.includes(pagePath)); } export function hideQiaobaoForPage(pagePath = getCurrentPagePath(), edge = "right") { const hiddenPages = uni.getStorageSync(PAGE_HIDDEN_KEY); const nextPages = Array.isArray(hiddenPages) ? [...hiddenPages] : []; if (!nextPages.includes(pagePath)) nextPages.push(pagePath); uni.setStorageSync(PAGE_HIDDEN_KEY, nextPages); setQiaobaoDockEdge(edge); uni.$emit("qiaobao-visibility-change", { scope: "page", pagePath, edge }); } export function hideQiaobaoGlobally(edge = "right") { uni.setStorageSync(GLOBAL_HIDDEN_KEY, true); setQiaobaoDockEdge(edge); uni.$emit("qiaobao-visibility-change", { scope: "all", edge }); } export function showQiaobao(pagePath = getCurrentPagePath()) { uni.removeStorageSync(GLOBAL_HIDDEN_KEY); const hiddenPages = uni.getStorageSync(PAGE_HIDDEN_KEY); if (Array.isArray(hiddenPages)) { uni.setStorageSync( PAGE_HIDDEN_KEY, hiddenPages.filter((item) => item !== pagePath), ); } uni.$emit("qiaobao-visibility-change", { scope: "show", pagePath }); } export function resetQiaobaoVisibility() { uni.removeStorageSync(GLOBAL_HIDDEN_KEY); uni.removeStorageSync(PAGE_HIDDEN_KEY); uni.$emit("qiaobao-visibility-change", { scope: "reset" }); }