offlineMapInit.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { getMapLocalApi } from '@jeesite/core/api/sys/mapLocal';
  2. import { setServerOfflineMapDir } from './offlineMap';
  3. const normalizeText = (value: unknown) => String(value ?? '').trim();
  4. const isPlainObject = (value: unknown): value is Record<string, unknown> =>
  5. Object.prototype.toString.call(value) === '[object Object]';
  6. const extractMapPath = (payload: any) => {
  7. const data = payload?.data ?? payload?.result ?? payload?.obj ?? payload;
  8. if (typeof data === 'string') return normalizeText(data);
  9. if (!isPlainObject(data)) return '';
  10. const directPath = normalizeText(
  11. data?.path ?? data?.mapPath ?? data?.maplocal ?? data?.mapLocal ?? data?.localPath ?? data?.filePath,
  12. );
  13. if (directPath) return directPath;
  14. const nestedCandidates = [data?.data, data?.result, data?.obj, data?.record, data?.row];
  15. for (const candidate of nestedCandidates) {
  16. if (!isPlainObject(candidate)) continue;
  17. const nestedPath = normalizeText(
  18. candidate.path ??
  19. candidate.mapPath ??
  20. candidate.maplocal ??
  21. candidate.mapLocal ??
  22. candidate.localPath ??
  23. candidate.filePath,
  24. );
  25. if (nestedPath) return nestedPath;
  26. }
  27. return '';
  28. };
  29. export async function initOfflineMapDirFromServer() {
  30. try {
  31. const res = await getMapLocalApi();
  32. const code = Number((res as any)?.code ?? 0);
  33. if (code !== 200) return '';
  34. const path = extractMapPath(res);
  35. setServerOfflineMapDir(path);
  36. return path;
  37. } catch {
  38. return '';
  39. }
  40. }