Dialog copy.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <a-modal
  3. title="解压缩文件"
  4. v-model:visible="visible"
  5. :maskClosable="false"
  6. :closable="false"
  7. @afterVisibleChange="handleVisibleChange"
  8. @cancel="handleDialogClose"
  9. >
  10. <div class="unzip-tree-wrapper" v-if="unzipMode === 2">
  11. <div class="target-path">
  12. <span class="label">目标路径:</span>
  13. <a-input v-model:value="targetPath" readonly size="small" />
  14. </div>
  15. <a-spin :spinning="loading">
  16. <a-tree
  17. :tree-data="fileTree"
  18. :field-names="{ children: 'children', title: 'label', key: 'id' }"
  19. highlight-current
  20. :expandedKeys="defaultExpandedKeys"
  21. @update:expandedKeys="(val) => (defaultExpandedKeys = val)"
  22. node-key="id"
  23. @select="handleNodeClick"
  24. :showLine="true"
  25. >
  26. <template #title="{ dataRef }">
  27. <span class="custom-tree-node">
  28. <span class="label">{{ dataRef.label }}</span>
  29. <a-button type="link" size="small" class="add-folder-btn" @click.stop="handleAddFolderBtnClick(dataRef)">
  30. 新建文件夹
  31. </a-button>
  32. </span>
  33. </template>
  34. </a-tree>
  35. </a-spin>
  36. </div>
  37. <div class="unzip-text" v-else>
  38. <a-spin />
  39. 正在解压缩,请稍等片刻...
  40. </div>
  41. <template #footer>
  42. <template v-if="unzipMode === 2">
  43. <a-button @click="handleDialogClose">取 消</a-button>
  44. <a-button type="primary" :loading="sureBtnLoading" @click="handleDialogSure"> 确 定 </a-button>
  45. </template>
  46. </template>
  47. </a-modal>
  48. </template>
  49. <script setup>
  50. import { ref } from 'vue'
  51. import { message } from 'ant-design-vue'
  52. import { getFoldTree, unzipFile } from '@/api/myResource/file'
  53. const { proxy } = getCurrentInstance()
  54. const props = defineProps({
  55. unzipMode: Number,
  56. userFileId: String,
  57. callback: Function
  58. })
  59. const visible = ref(false)
  60. const targetPath = ref('/')
  61. const fileTree = ref([])
  62. const loading = ref(false)
  63. const defaultExpandedKeys = ref([])
  64. const sureBtnLoading = ref(false)
  65. const handleDialogClose = () => {
  66. visible.value = false
  67. props.callback('cancel')
  68. }
  69. const handleVisibleChange = (val) => {
  70. if (val) {
  71. if (props.unzipMode === 2) {
  72. initFileTree()
  73. } else {
  74. handleUnzipFile()
  75. }
  76. }
  77. }
  78. const initFileTree = async (id) => {
  79. try {
  80. loading.value = true
  81. const res = await getFoldTree()
  82. if (res.success) {
  83. fileTree.value = [res.data]
  84. defaultExpandedKeys.value = id ? [id] : [fileTree.value[0].id]
  85. } else {
  86. message.error(res.message)
  87. }
  88. } finally {
  89. loading.value = false
  90. }
  91. }
  92. const handleNodeClick = (selectedKeys, { node }) => {
  93. targetPath.value = node.filePath || '/'
  94. }
  95. const handleAddFolderBtnClick = async (data) => {
  96. const result = await proxy.$openDialog.addFolder({
  97. filePath: data.filePath || '/'
  98. })
  99. if (result === 'confirm') {
  100. initFileTree(data.id)
  101. }
  102. }
  103. const handleUnzipFile = async () => {
  104. try {
  105. sureBtnLoading.value = true
  106. const reqData = {
  107. unzipMode: props.unzipMode,
  108. userFileId: props.userFileId,
  109. ...(props.unzipMode === 2 ? { filePath: targetPath.value } : {})
  110. }
  111. const res = await unzipFile(reqData)
  112. if (res.success) {
  113. message.success('解压成功')
  114. visible.value = false
  115. props.callback('confirm')
  116. } else {
  117. message.error(res.message)
  118. }
  119. } finally {
  120. sureBtnLoading.value = false
  121. }
  122. }
  123. const handleDialogSure = () => {
  124. handleUnzipFile()
  125. }
  126. defineExpose({
  127. visible
  128. })
  129. </script>
  130. <style lang="less" scoped>
  131. @import '@/style/myResource/varibles.less';
  132. .unzip-tree-wrapper {
  133. height: 300px;
  134. overflow: auto;
  135. .target-path {
  136. display: flex;
  137. align-items: center;
  138. margin-bottom: 16px;
  139. .label {
  140. width: 80px;
  141. }
  142. .ant-input {
  143. flex: 1;
  144. }
  145. }
  146. .custom-tree-node {
  147. width: 100%;
  148. display: flex;
  149. align-items: center;
  150. justify-content: space-between;
  151. .add-folder-btn {
  152. display: none;
  153. }
  154. &:hover .add-folder-btn {
  155. display: inline-block;
  156. }
  157. }
  158. }
  159. .unzip-text {
  160. padding: 40px 0 64px 0;
  161. text-align: center;
  162. .ant-spin {
  163. margin-right: 8px;
  164. }
  165. }
  166. .ant-btn-primary {
  167. &,
  168. &:hover,
  169. &:focus {
  170. background-color: @primary-color;
  171. border-color: @primary-color;
  172. }
  173. }
  174. .ant-tree-node-selected {
  175. background-color: @primary-color;
  176. }
  177. </style>