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 CopyFileDialog from './Dialog.vue'
// 使用基础 Vue 构造器,创建一个“子类”
const CopyFileConstructor = Vue.extend(CopyFileDialog)
 
let copyFileInstance = null
/**
 * 初始化复制文件实例
 * @param {boolean} isBatchOperation 是否为批量
 * @param {object | array} fileInfo 要复制的文件信息
 */
const initInstanceCopyFile = (isBatchOperation, fileInfo) => {
    copyFileInstance = new CopyFileConstructor({
        el: document.createElement('div'),
        data() {
            return {
                isBatchOperation,
                fileInfo
            }
        }
    })
}
/**
 * 复制文件 Promise 函数
 * @returns {Promise} 抛出确认和取消回调函数
 */
const showCopyFileDialog = (obj) => {
    // 非首次调用服务时,在 DOM 中移除上个实例
    if (copyFileInstance !== null) {
        document.body.removeChild(copyFileInstance.$el)
    }
    let { isBatchOperation, fileInfo } = obj
    return new Promise((reslove) => {
        initInstanceCopyFile(isBatchOperation, fileInfo)
        copyFileInstance.callback = (res) => {
            reslove(res)
            // 服务取消时卸载 DOM
            if (res === 'cancel' && copyFileInstance !== null) {
                document.body.removeChild(copyFileInstance.$el)
                copyFileInstance = null
            }
        }
        document.body.appendChild(copyFileInstance.$el) //  挂载 DOM
        Vue.nextTick(() => {
            copyFileInstance.visible = true //  打开对话框
        })
    })
}
 
export default showCopyFileDialog