downloadModal.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <BasicModal
  3. v-bind="$attrs"
  4. @register="register"
  5. :title="t('routes.scenes.downloadScene')"
  6. :showCancelBtn="false"
  7. :okText="downloadInfo.isDownloaded ? t('common.okText') : t('routes.scenes.cancelDownload')"
  8. @ok="handleSubmit"
  9. @cancel="cancelDownload"
  10. >
  11. <div class="pt-20px">
  12. <BasicForm @register="registerForm">
  13. <template #label="{ model, field }">
  14. {{ model[field] }}
  15. </template>
  16. <template #process> {{ downloadInfo.process }} % </template>
  17. <template #status> {{ downloadInfo.status }} </template>
  18. </BasicForm>
  19. </div>
  20. <template #centerFooter>
  21. <!-- <a-button>xxxx</a-button> -->
  22. </template>
  23. </BasicModal>
  24. </template>
  25. <script lang="ts">
  26. import { defineComponent, reactive, ref, watch } from 'vue';
  27. import { BasicModal, useModalInner } from '/@/components/Modal';
  28. import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  29. // import { BasicTable, useTable, BasicColumn, FormSchema } from '/@/components/Table';
  30. // import { useMessage } from '/@/hooks/web/useMessage';
  31. // import { checkUserAddAble } from '/@/api/corporation/modal';
  32. import { useI18n } from '/@/hooks/web/useI18n';
  33. import { getDownloadProcessApi } from '/@/api/scene/list';
  34. // import { bindAnchorListParam } from '/@/api/scene/model';
  35. // import { Time } from '/@/components/Time';
  36. // import { useUserStore } from '/@/store/modules/user';
  37. import {
  38. downloadByUrl,
  39. // downloadByData,
  40. // downloadByBase64,
  41. // downloadByOnlineUrl,
  42. } from '/@/utils/file/download';
  43. const schemas: FormSchema[] = [
  44. {
  45. field: 'sceneName',
  46. label: '场景名称:',
  47. component: 'Input',
  48. slot: 'label',
  49. },
  50. {
  51. field: 'process',
  52. label: '下载进度:',
  53. component: 'Input',
  54. slot: 'process',
  55. },
  56. {
  57. field: 'status',
  58. label: '状态:',
  59. component: 'Input',
  60. slot: 'status',
  61. },
  62. ];
  63. export default defineComponent({
  64. components: { BasicModal, BasicForm },
  65. props: {
  66. userData: { type: Object },
  67. },
  68. emits: ['register', 'success'],
  69. setup() {
  70. const { t } = useI18n();
  71. // const { createMessage } = useMessage();
  72. const sceneNum = ref('');
  73. const finishDowloadUrl = ref('');
  74. const downloadInfo = reactive<Recordable>({});
  75. downloadInfo.timer = null;
  76. downloadInfo.process = 0;
  77. downloadInfo.status = '下载中';
  78. downloadInfo.isDownloaded = false;
  79. const [registerForm, { setFieldsValue }] = useForm({
  80. schemas: schemas,
  81. labelWidth: 120,
  82. showActionButtonGroup: false,
  83. actionColOptions: {
  84. span: 24,
  85. },
  86. // submitFunc: handleSubmit,
  87. });
  88. const [register, { closeModal }] = useModalInner((data) => {
  89. data && onDataReceive(data);
  90. });
  91. function onDataReceive(data) {
  92. console.log('Data Received', data, data.num);
  93. setFieldsValue({
  94. ...data,
  95. });
  96. sceneNum.value = data.num;
  97. }
  98. const handleSubmit = async () => {
  99. try {
  100. cancelDownload();
  101. closeModal();
  102. if (downloadInfo.isDownloaded) {
  103. downloadByUrl({
  104. url: finishDowloadUrl.value as any as string,
  105. target: '_self',
  106. });
  107. }
  108. } catch (error) {}
  109. };
  110. async function getDownloadInfo(sceneNum: string) {
  111. downloadInfo.timer = setInterval(async () => {
  112. const res = await getDownloadProcessApi({ sceneNum });
  113. console.log('res', res);
  114. const percent = res.percent && Math.round(res.percent);
  115. downloadInfo.process = percent;
  116. if (res.status === 1000) {
  117. downloadInfo.status = '获取中';
  118. }
  119. if (res.status === 1002 && res.url) {
  120. cancelDownload();
  121. finishDowloadUrl.value = res.url;
  122. downloadInfo.isDownloaded = true;
  123. downloadInfo.status = '获取成功';
  124. handleSubmit();
  125. }
  126. }, 2000);
  127. }
  128. function cancelDownload() {
  129. clearInterval(downloadInfo.timer);
  130. sceneNum.value = '';
  131. }
  132. watch(
  133. () => sceneNum.value,
  134. () => {
  135. console.log('sceneNum', sceneNum.value);
  136. if (sceneNum.value) {
  137. getDownloadInfo(sceneNum.value);
  138. }
  139. },
  140. );
  141. return {
  142. t,
  143. register,
  144. schemas,
  145. handleSubmit,
  146. closeModal,
  147. registerForm,
  148. downloadInfo,
  149. cancelDownload,
  150. };
  151. },
  152. });
  153. </script>