import AsyncStorage from '@react-native-async-storage/async-storage' const cacheKey = '__chushu_app_uuid' async function setUuid(id: string) { return await AsyncStorage.setItem(cacheKey, id) } function generateUuid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { const r = (Math.random() * 16) | 0 const v = c === 'x' ? r : (r & 0x3) | 0x8 return v.toString(16) }) } /** * 获取本地缓存的uuid 如果没有 则生成一个 * @returns */ export async function getAppUuid() { let uuid = await AsyncStorage.getItem(cacheKey) if (!uuid) { uuid = generateUuid() setUuid(uuid) } return uuid }