CADViewer.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <!-- 更适用于文件共享中心的CAD viewer组件,支持两种使用方法,详见下文 -->
  3. <CADViewer class="w-100% h-100%" :height="height" />
  4. </template>
  5. <script lang="ts" setup>
  6. import { onMounted, onUnmounted } from 'vue';
  7. import { CADViewer, useCADViewer } from '/@/components/CADViewer';
  8. import { downloadById } from '../fileDetail.api';
  9. import { useRoute } from 'vue-router';
  10. import { message } from 'ant-design-vue';
  11. const props = defineProps<{
  12. // 文件共享中心中该文件的ID
  13. id: string;
  14. // 文件名
  15. filename: string;
  16. height: number;
  17. }>();
  18. const { processFile, postMessage, registHook, unregistHook } = useCADViewer();
  19. function openFile(id: string, filename: string) {
  20. // 只触发一次,因为MKY_Open_Mxweb之后会自动触发MKY_Open_File_Complete钩子,导致循环
  21. registHook('MKY_Open_File_Complete', () => {
  22. unregistHook('MKY_Open_File_Complete');
  23. const loading = message.loading('正在下载文件', 0);
  24. downloadById({ id, ifMine: initByRoute }).then((res: Blob) => {
  25. processFile(new File([res], filename))
  26. .then((path) => {
  27. postMessage('MKY_Open_Mxweb', path);
  28. })
  29. .finally(() => {
  30. loading();
  31. });
  32. });
  33. });
  34. }
  35. // watch(
  36. // () => props.id,
  37. // (v) => {
  38. // if (!v) return;
  39. // openFile(v, props.filename);
  40. // }
  41. // );
  42. let initByRoute = false;
  43. onMounted(() => {
  44. const route = useRoute();
  45. if (route.query.id && route.query.filename) {
  46. initByRoute = true;
  47. // 通过 url query 指定文件 ID 的形式使用该组件
  48. openFile(route.query.id as string, route.query.filename as string);
  49. } else {
  50. // 通过 props 指定文件 ID 的形式使用该组件
  51. openFile(props.id, props.filename);
  52. }
  53. });
  54. onUnmounted(() => {
  55. unregistHook('MKY_Open_File_Complete');
  56. });
  57. </script>
  58. <style scoped lang="less">
  59. ::v-deep .suffix {
  60. height: 32px;
  61. line-height: 32px;
  62. margin-left: 5px;
  63. color: #fff;
  64. }
  65. </style>