Keine Beschreibung

localStorageCache.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // GetZTreeListNew,
  2. // 缓存管理对象
  3. const cacheManager = {
  4. // 封装带缓存的AJAX请求
  5. fetchData: function(key, url, params={},forceRefresh = false) {
  6. const self = this;
  7. var storageData = localStorage.getItem(key)
  8. // 否则发起AJAX请求
  9. return new Promise((resolve, reject) => {
  10. if (storageData) {
  11. // 否则发起AJAX请求
  12. const parsedData = JSON.parse(storageData);
  13. resolve(parsedData);
  14. } else {
  15. // 模拟AJAX请求
  16. params.token = $.cookie("token")
  17. $.ajax({
  18. type: "get",
  19. url: huayi.config.callcenter_url + url,
  20. dataType: 'json',
  21. async: false,
  22. data:params ,
  23. success: function (data) {
  24. if (data.state && data.state.toLowerCase() == "success") {
  25. localStorage.setItem(key, JSON.stringify(data));
  26. resolve(data);
  27. }
  28. if (data.rows && data.rows.length >0 ) {
  29. localStorage.setItem(key, JSON.stringify(data));
  30. resolve(data);
  31. }
  32. }
  33. });
  34. }
  35. });
  36. },
  37. // 清除指定缓存
  38. clearCache: function(key) {},
  39. // 检查缓存是否存在
  40. hasCache: function(key) {}
  41. };
  42. //cacheManager.fetchData('GetZTreeListNew',"Dictionary/GetZTreeListNew",{pid: 38 }).then(data => {
  43. // console.log('信息加载完成', 'success',data);
  44. // }).catch(error => {
  45. // console.log('加载失败' + error.message, 'GetZTreeListNew');
  46. // });