tiefen_space_h5/utils/storeInfo.js

54 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2024-07-22 16:07:41 +08:00
"use client";
export function save(key, value) {
// 只在客户端执行
if (
typeof window !== "undefined" &&
typeof window.localStorage !== "undefined"
) {
if (typeof value == "object") {
localStorage.setItem(key, JSON.stringify(value));
} else {
localStorage.setItem(key, value);
2024-07-22 14:38:59 +08:00
}
2024-07-22 16:07:41 +08:00
}
2024-07-03 19:59:39 +08:00
}
2024-07-22 16:07:41 +08:00
export function get(key) {
if (
typeof window !== "undefined" &&
typeof window.localStorage !== "undefined"
) {
2024-07-16 20:20:12 +08:00
let data = localStorage.getItem(key);
2024-07-22 16:41:33 +08:00
// console.log(key, data);
2024-07-22 16:07:41 +08:00
return data?.indexOf("{") != -1 ? JSON.parse(data) : data;
}
2024-07-03 19:59:39 +08:00
}
2024-07-22 16:07:41 +08:00
export function remove(key) {
if (
typeof window !== "undefined" &&
typeof window.localStorage !== "undefined"
) {
localStorage.removeItem(key);
}
2024-07-06 11:05:19 +08:00
}
2024-07-22 16:07:41 +08:00
export function clear() {
if (
typeof window !== "undefined" &&
typeof window.localStorage !== "undefined"
) {
localStorage.clear();
2024-07-06 11:05:19 +08:00
}
2024-07-22 16:07:41 +08:00
}
2024-07-06 11:05:19 +08:00
2024-07-22 16:07:41 +08:00
export function saveUserInfo(data, mobilePhone, regionCode) {
save("token", data.data.token);
save("account", JSON.stringify(data.data.account));
save("mobile_phone", mobilePhone);
save("region_code", regionCode);
}
export function removeUserInfo() {
remove("token");
remove("account");
remove("mobile_phone");
remove("region_code");
}