cellLocalInit.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { getCellLocalApi } from '@jeesite/core/api/sys/cellLocal';
  2. import { setServerCellLocalPath } from './cellLocal';
  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 extractCellLocalPath = (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 ??
  12. data?.cellPath ??
  13. data?.cellLocal ??
  14. data?.cellLocalPath ??
  15. data?.baseStationPath ??
  16. data?.localPath ??
  17. data?.filePath,
  18. );
  19. if (directPath) return directPath;
  20. const nestedCandidates = [data?.data, data?.result, data?.obj, data?.record, data?.row];
  21. for (const candidate of nestedCandidates) {
  22. if (!isPlainObject(candidate)) continue;
  23. const nestedPath = normalizeText(
  24. candidate.path ??
  25. candidate.cellPath ??
  26. candidate.cellLocal ??
  27. candidate.cellLocalPath ??
  28. candidate.baseStationPath ??
  29. candidate.localPath ??
  30. candidate.filePath,
  31. );
  32. if (nestedPath) return nestedPath;
  33. }
  34. return '';
  35. };
  36. export async function initCellLocalPathFromServer() {
  37. try {
  38. const res = await getCellLocalApi();
  39. const code = Number((res as any)?.code ?? 0);
  40. if (code !== 200) return '';
  41. const path = extractCellLocalPath(res);
  42. setServerCellLocalPath(path);
  43. return path;
  44. } catch {
  45. return '';
  46. }
  47. }