's'
This commit is contained in:
36
app/page.tsx
36
app/page.tsx
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user