storage.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import AsyncStorage from '@react-native-community/async-storage';
  2. //登陆信息进行本地存储
  3. //参数 json 登陆信息json
  4. export const StorageData = async (json) => {
  5. try {
  6. const {account, usertype, userName, authorities, token} = json.data;
  7. await AsyncStorage.setItem('account', JSON.stringify(account));
  8. await AsyncStorage.setItem('userName', JSON.stringify(userName));
  9. await AsyncStorage.setItem('usertype', JSON.stringify(usertype));
  10. await AsyncStorage.setItem('token', JSON.stringify(token));
  11. await AsyncStorage.setItem('authority', JSON.stringify(authorities));
  12. } catch (e) {
  13. console.log(e);
  14. }
  15. };
  16. export const StorageState = async (json) => {
  17. try {
  18. const {initRealm} = json;
  19. await AsyncStorage.setItem('initRealmFlag', JSON.stringify(initRealm));
  20. } catch (e) {
  21. console.log(e);
  22. }
  23. };
  24. //单独数据存储
  25. //参数 key:字段名称
  26. // value: 存储值
  27. export const IndividualStorageData = async (key, value) => {
  28. try {
  29. await AsyncStorage.setItem(key, value);
  30. } catch (e) {
  31. console.log(e);
  32. }
  33. }
  34. //读取本地数据存储信息
  35. //参数 key:取值字段名称
  36. export const RetrieveData = async (key) => {
  37. try {
  38. const value = await AsyncStorage.getItem(key)
  39. if (value !== null) {
  40. return value;
  41. }
  42. } catch (e) {
  43. console.log(e);
  44. }
  45. };
  46. //清空本地存储数据
  47. export const ClearAll = async () => {
  48. try {
  49. await AsyncStorage.clear();
  50. } catch (e) {
  51. console.log(e);
  52. }
  53. };