tiefen_space_app/utils/storeInfo.js

131 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//存储数据
import AsyncStorage from "@react-native-async-storage/async-storage";
//查看客户端版本号
// import * as Application from "expo-application";
//查看系统版本、机型、设备类型
// import * as Device from "expo-device";
// import { Platform } from "react-native";
// {
// "b_mid": 111, // 用户id
// "b_did": "xxx", // 设备id
// "b_ver": "1.0.0", // 版本
// "b_osver": "4.0.0", // 系统版本
// "b_dt": 0, // 设备类型
// "b_ch": "xiaomi", //渠道
// "b_model": "xiaomi 13", // 机型
// "b_nt": 0, // 网络类型 // 0: wifi, 1: 流量
// "b_ts": 1, // 请求时间戳,毫秒级
// "b_token": "xxxx", // 请求令牌
// "b_loc": {
// "b_lon": 1.1, // 经度
// "b_lat": 1.1 // 纬度
// }
// }
//进入app时存储基本信息b_did、b_ver、b_osver、b_dt、b_ch、b_model
// export async function storeAppInfo() {
// async function getDid() {
// if (Platform.OS === "android") {
// return Application.androidId;
// } else {
// return await Application.getIosIdForVendorAsync();
// }
// }
// const b_did = await getDid();
// const b_ver = Application.nativeApplicationVersion;
// const b_dt = Device.osName === "Android" ? 0 : 1;
// const b_ch = "production";
// const b_model = Device.modelName;
// const b_osver = Device.osVersion;
// const appInfo = {
// b_did: b_did,
// b_ver: b_ver,
// b_dt: b_dt,
// b_ch: b_ch,
// b_model: b_model,
// b_osver: b_osver,
// };
// try {
// await AsyncStorage.setItem("app_info", JSON.stringify(appInfo));
// } catch (e) {
// console.log(e);
// }
// }
//保存数据
export async function save(key, value) {
try {
await AsyncStorage.setItem(key, JSON.stringify(value));
} catch (e) {
console.log(e);
}
}
//获取数据
export async function get(key) {
try {
return await AsyncStorage.getItem(key).then((value) => {
const jsonValue = JSON.parse(value);
return jsonValue;
});
} catch (e) {
console.log(e);
}
}
//更新数据(不适用于数组)
export async function update(key, newValue) {
try {
await AsyncStorage.getItem(key).then(async (oldValue) => {
newValue =
typeof newValue === "object"
? Object.assign({}, JSON.parse(oldValue), newValue)
: newValue;
await AsyncStorage.setItem(key, JSON.stringify(newValue));
});
} catch (e) {
console.log(e);
}
}
//移除数据
export async function remove(key) {
try {
await AsyncStorage.removeItem(key);
} catch (e) {
console.log(e);
}
}
//向数组类型数据添加值
export async function addArr(key, newValue) {
try {
await AsyncStorage.getItem(key).then(async (oldValue) => {
if (oldValue) {
newValue = [...newValue, ...JSON.parse(oldValue)];
} else {
newValue = [...newValue];
}
await AsyncStorage.setItem(key, JSON.stringify(newValue));
});
} catch (e) {
console.log(e);
}
}
//清除当前账号的全部聊天记录
export async function clearMessagesCache(userMid) {
try {
const keys = await AsyncStorage.getAllKeys();
const userChatKeys = keys.filter((key) => key.startsWith(`${userMid}_to_`));
await AsyncStorage.multiRemove(userChatKeys);
console.log(`用户 ${userMid} 的聊天记录已清除`);
} catch (error) {
console.error(error);
}
}