mallConfig.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import request from './request.js';
  2. import api from './appOnLaunch.js';
  3. import Vue from "vue";
  4. let isFirstAppRun = true;
  5. let isGettingFromService = false;
  6. let mallConfig = null;
  7. let cacheKey = '_APP_CONFIG';
  8. let resolveList = [];
  9. let rejectList = [];
  10. const getConfigFormServer = (resolve, reject) => {
  11. if (resolve) {
  12. resolveList.push(resolve);
  13. }
  14. if (reject) {
  15. rejectList.push(reject);
  16. }
  17. if (isGettingFromService) {
  18. return;
  19. }
  20. isGettingFromService = true;
  21. request({
  22. url: api.index.config,
  23. }).then(response => {
  24. isGettingFromService = false;
  25. if (response.code === 0) {
  26. mallConfig = response.data;
  27. Vue.prototype.$storage.setStorageSync(cacheKey, mallConfig);
  28. for (let i in resolveList) {
  29. resolveList[i](mallConfig);
  30. }
  31. resolveList = [];
  32. } else {
  33. for (let i in rejectList) {
  34. rejectList[i](response.msg);
  35. }
  36. rejectList = [];
  37. }
  38. }).catch(error => {
  39. isGettingFromService = false;
  40. for (let i in rejectList) {
  41. rejectList[i](error.msg);
  42. }
  43. rejectList = [];
  44. });
  45. };
  46. export default {
  47. getConfig(options) {
  48. return new Promise((resolve, reject) => {
  49. if (mallConfig) {
  50. return resolve(mallConfig);
  51. }
  52. mallConfig = Vue.prototype.$storage.getStorageSync(cacheKey);
  53. if (mallConfig) {
  54. if (isFirstAppRun) {
  55. isFirstAppRun = false;
  56. getConfigFormServer();
  57. }
  58. return resolve(mallConfig);
  59. } else {
  60. getConfigFormServer(resolve, reject);
  61. }
  62. });
  63. },
  64. resetConfig() {
  65. mallConfig = null;
  66. Vue.prototype.$storage.removeStorageSync(cacheKey);
  67. isGettingFromService = false;
  68. }
  69. };