2023-12-29 00:27:44 +08:00
|
|
|
|
//存储数据
|
|
|
|
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
|
|
|
//查看系统版本、机型、设备类型
|
2024-01-25 03:43:07 +08:00
|
|
|
|
import * as Device from "expo-device";
|
|
|
|
|
import { Platform } from "react-native";
|
2023-12-29 00:27:44 +08:00
|
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
// "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)
|
2024-01-25 03:43:07 +08:00
|
|
|
|
export async function storeAppInfo() {
|
2024-03-21 18:32:38 +08:00
|
|
|
|
const Application = require("expo-application");
|
2024-01-25 03:43:07 +08:00
|
|
|
|
async function getDid() {
|
|
|
|
|
if (Platform.OS === "android") {
|
2024-03-21 18:32:38 +08:00
|
|
|
|
return Application.getAndroidId();
|
2024-01-25 03:43:07 +08:00
|
|
|
|
} else {
|
|
|
|
|
return await Application.getIosIdForVendorAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-29 00:27:44 +08:00
|
|
|
|
|
2024-01-25 03:43:07 +08:00
|
|
|
|
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;
|
2023-12-29 00:27:44 +08:00
|
|
|
|
|
2024-01-25 03:43:07 +08:00
|
|
|
|
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,
|
|
|
|
|
};
|
2023-12-29 00:27:44 +08:00
|
|
|
|
|
2024-01-25 03:43:07 +08:00
|
|
|
|
try {
|
|
|
|
|
await AsyncStorage.setItem("app_info", JSON.stringify(appInfo));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-29 00:27:44 +08:00
|
|
|
|
|
|
|
|
|
//保存数据
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|