caseConfigGuide.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { Modal } from 'ant-design-vue';
  2. import { initCellLocalPathFromServer } from '@jeesite/core/utils/cell/cellLocalInit';
  3. import { getCellLocalPath } from '@jeesite/core/utils/cell/cellLocal';
  4. import { hydratePersistedCaseGlobalConfig } from '@jeesite/core/utils/caseGlobalConfig';
  5. import { initOfflineMapDirFromServer } from '@jeesite/core/utils/map/offlineMapInit';
  6. import { getOfflineMapDir } from '@jeesite/core/utils/map/offlineMap';
  7. export const OPEN_SETTING_DRAWER_EVENT = 'layout-setting:open-drawer';
  8. export const OPEN_OFFLINE_MAP_MODAL_EVENT = 'layout-setting:open-offline-map-modal';
  9. export const OPEN_CELL_LOCAL_MODAL_EVENT = 'layout-setting:open-cell-local-modal';
  10. const GUIDE_MODAL_DELAY = 180;
  11. let globalCaseConfigInitPromise: Promise<void> | null = null;
  12. let hasPromptedGlobalCaseConfigGuide = false;
  13. function dispatchGuideEvent(eventName: string) {
  14. if (typeof window === 'undefined') return;
  15. window.dispatchEvent(new CustomEvent(eventName));
  16. }
  17. export function openSettingDrawerByGuide() {
  18. dispatchGuideEvent(OPEN_SETTING_DRAWER_EVENT);
  19. }
  20. export function openOfflineMapModalByGuide() {
  21. openSettingDrawerByGuide();
  22. window.setTimeout(() => {
  23. dispatchGuideEvent(OPEN_OFFLINE_MAP_MODAL_EVENT);
  24. }, GUIDE_MODAL_DELAY);
  25. }
  26. export function openCellLocalModalByGuide() {
  27. openSettingDrawerByGuide();
  28. window.setTimeout(() => {
  29. dispatchGuideEvent(OPEN_CELL_LOCAL_MODAL_EVENT);
  30. }, GUIDE_MODAL_DELAY);
  31. }
  32. export function initGlobalCaseConfig() {
  33. if (!globalCaseConfigInitPromise) {
  34. globalCaseConfigInitPromise = hydratePersistedCaseGlobalConfig()
  35. .catch(() => undefined)
  36. .then(() => Promise.all([initOfflineMapDirFromServer(), initCellLocalPathFromServer()]))
  37. .then(() => undefined)
  38. .catch(() => undefined);
  39. }
  40. return globalCaseConfigInitPromise;
  41. }
  42. export function resetCaseConfigGuideState() {
  43. globalCaseConfigInitPromise = null;
  44. hasPromptedGlobalCaseConfigGuide = false;
  45. }
  46. export function promptCaseConfigGuide() {
  47. if (typeof window === 'undefined') return false;
  48. const missingOfflineMap = !getOfflineMapDir();
  49. const missingCellLocal = !getCellLocalPath();
  50. if (!missingOfflineMap && !missingCellLocal) {
  51. return false;
  52. }
  53. const missingItems = [missingOfflineMap ? '离线地图文件' : '', missingCellLocal ? '基站包文件' : ''].filter(Boolean);
  54. const content = `当前系统还未配置${missingItems.join('、')}。`;
  55. Modal.confirm({
  56. title: '全局配置提醒',
  57. content,
  58. centered: true,
  59. okText: '去配置',
  60. cancelText: '稍后',
  61. onOk: () => {
  62. if (missingOfflineMap) {
  63. openOfflineMapModalByGuide();
  64. return;
  65. }
  66. openCellLocalModalByGuide();
  67. },
  68. });
  69. return true;
  70. }
  71. export async function promptCaseConfigGuideOnce() {
  72. if (typeof window === 'undefined' || hasPromptedGlobalCaseConfigGuide) {
  73. return false;
  74. }
  75. hasPromptedGlobalCaseConfigGuide = true;
  76. await initGlobalCaseConfig();
  77. return promptCaseConfigGuide();
  78. }