This commit is contained in:
eric
2026-03-10 07:31:37 -05:00
parent 0c34400300
commit a46a31afb5
2 changed files with 25 additions and 13 deletions

View File

@@ -123,23 +123,35 @@ export default function Home() {
const clearAllStorage = useCallback(() => {
try {
if (typeof window !== "undefined" && window.localStorage) {
window.localStorage.clear();
if (typeof window === "undefined") return;
const keys = ["userId", "paidType", "userName"];
keys.forEach((k) => {
try {
localStorage.removeItem(k);
} catch {
/* ignore */
}
});
try {
for (let i = localStorage.length - 1; i >= 0; i--) {
const key = localStorage.key(i);
if (key) localStorage.removeItem(key);
}
} catch {
/* ignore */
}
try {
sessionStorage.clear();
} catch {
/* ignore */
}
removeStorage("userId");
removeStorage("paidType");
removeStorage("userName");
setPaidType(null);
setUserName(null);
setLoading(false);
alert("本地存储已清空");
// 移除 URL 中的 ?type=clean,避免刷新时重复触发
if (typeof window !== "undefined" && window.history.replaceState) {
window.history.replaceState({}, "", window.location.pathname);
}
const path = window.location.pathname || "/";
window.location.replace(path);
} catch (error) {
console.error("清空存储失败:", error);
alert("清空存储失败");
}
}, []);
@@ -174,7 +186,7 @@ export default function Home() {
useEffect(() => {
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get("type") === "clean") {
if (urlParams.get("type")?.trim() === "clean") {
clearAllStorage();
return;
}