useCADViewer.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { CAD_VIEWER_IFRAME_ID } from '../viewer.data';
  2. import { message } from 'ant-design-vue';
  3. import { transformCadFile } from '../viewer.api';
  4. import { SupportedOperationName } from '../types';
  5. import { useGlobSetting } from '/@/hooks/setting';
  6. const globSetting = useGlobSetting();
  7. const baseApiUrl = globSetting.domainUrl;
  8. export default function useCADViewer() {
  9. /** 向CADViewer发送指令 */
  10. function postMessage(cmd: SupportedOperationName, param?: unknown) {
  11. const dom = document.getElementById(CAD_VIEWER_IFRAME_ID) as any;
  12. if (!dom) message.info('CAD预览器尚未准备好,请稍后再试');
  13. dom.contentWindow.postMessage(
  14. {
  15. cmd: cmd,
  16. type: 'sendStringToExecute',
  17. param: param,
  18. },
  19. '*'
  20. );
  21. }
  22. /** 调用 api 转换文件格式,并返回转换后文件的网络地址 */
  23. function processFile(file: Blob) {
  24. const data = new FormData();
  25. data.append('file', file);
  26. const close = message.loading('正在转换文件格式,请稍等', 0);
  27. return transformCadFile(data)
  28. .then((result) => {
  29. const filepath = result.replace('/data/file/', '');
  30. if (import.meta.env.PROD) {
  31. return `${baseApiUrl}/sys/common/static/${filepath}`.replace(/\/+/g, '/');
  32. } else {
  33. return import.meta.env.VITE_GLOB_DOMAIN_URL + `/sys/common/static/${filepath}`.replace(/\/+/g, '/');
  34. }
  35. })
  36. .finally(() => {
  37. close();
  38. });
  39. }
  40. return {
  41. /** 向CADViewer发送指令 */
  42. postMessage,
  43. /** 调用 api 转换文件格式,并返回转换后文件的网络地址 */
  44. processFile,
  45. };
  46. }