zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import Vue from 'vue'
// 导入组件
import UnzipFileDialog from './Dialog.vue'
// 使用基础 Vue 构造器,创建一个“子类”
const UnzipFileConstructor = Vue.extend(UnzipFileDialog)
 
let unzipFileInstance = null
/**
 * 初始化解压缩文件实例
 * @param {string} unzipMode 解压模式 0-解压到当前文件夹, 1-自动创建该文件名目录,并解压到目录里, 2-手动选择解压目录
 * @param {string} userFileId 文件用户 ID
 */
const initInstanceUnzipFile = (unzipMode, userFileId) => {
    unzipFileInstance = new UnzipFileConstructor({
        el: document.createElement('div'),
        data() {
            return {
                unzipMode,
                userFileId
            }
        }
    })
}
/**
 * 解压缩文件 Promise 函数
 * @returns {Promise} 抛出确认和取消回调函数
 */
const showUnzipFileDialog = (obj) => {
    // 非首次调用服务时,在 DOM 中移除上个实例
    if (unzipFileInstance !== null) {
        document.body.removeChild(unzipFileInstance.$el)
    }
    let { unzipMode, userFileId } = obj
    return new Promise((reslove) => {
        initInstanceUnzipFile(unzipMode, userFileId)
        unzipFileInstance.callback = (res) => {
            reslove(res)
            // 服务取消时卸载 DOM
            if (res === 'cancel' && unzipFileInstance !== null) {
                document.body.removeChild(unzipFileInstance.$el)
                unzipFileInstance = null
            }
        }
        document.body.appendChild(unzipFileInstance.$el) //  挂载 DOM
        Vue.nextTick(() => {
            unzipFileInstance.visible = true //  打开对话框
        })
    })
}
 
export default showUnzipFileDialog