cache.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import siteInfo from 'siteInfo';
  2. const clearStorage = function() {
  3. uni.clearStorageSync();
  4. };
  5. const setStorageSync = function(key, data) {
  6. // #ifdef H5
  7. key += `_${siteInfo.id}` + `_${siteInfo.platform}`;
  8. // #endif
  9. uni.setStorageSync(key, data);
  10. };
  11. const getStorageSync = function(key) {
  12. // #ifdef H5
  13. key += `_${siteInfo.id}` + `_${siteInfo.platform}`;
  14. // #endif
  15. return uni.getStorageSync(key);
  16. };
  17. const getStorage = function({key, success, fail}) {
  18. // #ifdef H5
  19. key += `_${siteInfo.id}` + `_${siteInfo.platform}`;
  20. // #endif
  21. uni.getStorage({
  22. key: key,
  23. success: function(res) {
  24. success && success(res);
  25. },
  26. fail: function () {
  27. fail && fail();
  28. }
  29. });
  30. }
  31. const setStorage = function({key, data, success, fail}) {
  32. // #ifdef H5
  33. key += `_${siteInfo.id}` + `_${siteInfo.platform}`;
  34. // #endif
  35. uni.setStorage({
  36. key: key,
  37. data: data,
  38. success: function(res) {
  39. success && success(res);
  40. },
  41. fail: function () {
  42. fail && fail();
  43. }
  44. });
  45. }
  46. const removeStorageSync = function(key) {
  47. // #ifdef H5
  48. key += `_${siteInfo.id}` + `_${siteInfo.platform}`;
  49. // #endif
  50. uni.removeStorageSync(key);
  51. };
  52. const removeStorage = function({key, success, fail}) {
  53. // #ifdef H5
  54. key += `_${siteInfo.id}` + `_${siteInfo.platform}`;
  55. // #endif
  56. uni.removeStorage({
  57. key: key,
  58. success: function(res) {
  59. success && success(res);
  60. },
  61. fail: function () {
  62. fail && fail();
  63. }
  64. })
  65. }
  66. export { clearStorage, setStorageSync, getStorageSync, removeStorageSync, getStorage, setStorage, removeStorage }