123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import { CAD_VIEWER_IFRAME_ID } from '../viewer.data';
- import { message } from 'ant-design-vue';
- import { transformCadFile } from '../viewer.api';
- import { SupportedOperationName } from '../types';
- import { useGlobSetting } from '/@/hooks/setting';
- const globSetting = useGlobSetting();
- const baseApiUrl = globSetting.domainUrl;
- export default function useCADViewer() {
- /** 向CADViewer发送指令 */
- function postMessage(cmd: SupportedOperationName, param?: unknown) {
- const dom = document.getElementById(CAD_VIEWER_IFRAME_ID) as any;
- if (!dom) message.info('CAD预览器尚未准备好,请稍后再试');
- dom.contentWindow.postMessage(
- {
- cmd: cmd,
- type: 'sendStringToExecute',
- param: param,
- },
- '*'
- );
- }
- /** 调用 api 转换文件格式,并返回转换后文件的网络地址 */
- function processFile(file: Blob) {
- const data = new FormData();
- data.append('file', file);
- const close = message.loading('正在转换文件格式,请稍等', 0);
- return transformCadFile(data)
- .then((result) => {
- const filepath = result.replace('/data/file/', '');
- if (import.meta.env.PROD) {
- return `${baseApiUrl}/sys/common/static/${filepath}`.replace(/\/+/g, '/');
- } else {
- return import.meta.env.VITE_GLOB_DOMAIN_URL + `/sys/common/static/${filepath}`.replace(/\/+/g, '/');
- }
- })
- .finally(() => {
- close();
- });
- }
- return {
- /** 向CADViewer发送指令 */
- postMessage,
- /** 调用 api 转换文件格式,并返回转换后文件的网络地址 */
- processFile,
- };
- }
|