storage.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. function set(key, data, sync = true) {
  2. try {
  3. if (sync) {
  4. return uni.setStorageSync(key, data);
  5. } else {
  6. uni.setStorage({
  7. key,
  8. data,
  9. success: function(res) {
  10. }
  11. });
  12. }
  13. } catch (e) {
  14. return false;
  15. }
  16. }
  17. function get(key, sync = true) {
  18. try {
  19. if (sync) {
  20. return uni.getStorageSync(key);
  21. } else {
  22. let val = '';
  23. uni.getStorage({
  24. key,
  25. success: function(res) {
  26. val = res.data;
  27. }
  28. });
  29. return val;
  30. }
  31. } catch (e) {
  32. return false;
  33. }
  34. }
  35. function info(sync = false) {
  36. try {
  37. if (sync) {
  38. return uni.getStorageInfoSync();
  39. } else {
  40. let val = '';
  41. uni.getStorageInfo({
  42. success: function(res) {
  43. val = res;
  44. }
  45. });
  46. return val
  47. }
  48. } catch (e) {
  49. return false;
  50. }
  51. }
  52. function remove(key, sync = true) {
  53. try {
  54. if (sync) {
  55. return uni.removeStorageSync(key);
  56. } else {
  57. uni.removeStorage({
  58. key: key
  59. });
  60. }
  61. } catch (e) {
  62. return false;
  63. }
  64. }
  65. function clear(sync = true) {
  66. try {
  67. if (sync) {
  68. return uni.clearStorageSync();
  69. } else {
  70. uni.clearStorage();
  71. }
  72. } catch (e) {
  73. return false;
  74. }
  75. }
  76. function has(key, sync = true) {
  77. try {
  78. if (sync) {
  79. return uni.getStorageSync(key) !== null;
  80. } else {
  81. let val = false;
  82. uni.getStorage({
  83. key: key,
  84. success: function(res) {
  85. val = res.data !== null;
  86. }
  87. });
  88. return val;
  89. }
  90. } catch (e) {
  91. return false;
  92. }
  93. }
  94. const storage = {
  95. get,
  96. set,
  97. info,
  98. remove,
  99. clear,
  100. has
  101. }
  102. export default storage;