wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { ElMessage, ElMessageBox } from 'element-plus';
import { mitt, router, service } from '/@/cool';
import { t } from '/@/plugins/i18n';
 
export const usePlugin = () => {
    function getName(file: File) {
        return file.name.replace('.cool', '');
    }
 
    // 注册
    function register() {
        document.body.addEventListener('dragover', e => {
            e.preventDefault();
        });
        document.body.addEventListener('drop', e => {
            e.preventDefault();
 
            if (e.dataTransfer) {
                const file = e.dataTransfer.files[0];
 
                if (file.name.endsWith('.cool')) {
                    ElMessageBox.confirm(
                        t('检测到插件「{name}」,是否安装?', { name: getName(file) }),
                        t('提示'),
                        {
                            type: 'warning',
                            confirmButtonText: t('安装')
                        }
                    )
                        .then(() => {
                            install(file);
                        })
                        .catch(() => null);
                }
            }
        });
    }
 
    // 安装
    function install(file: File) {
        const next = (force: boolean) => {
            const data = new FormData();
 
            data.append('files', file);
            data.append('force', String(force));
 
            service.plugin.info
                .request({
                    url: '/install',
                    method: 'POST',
                    data,
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                })
                .then(res => {
                    if (!res) {
                        // 发送事件
                        mitt.emit('plugin.refresh');
 
                        // 标题
                        const title = t('插件「{name}」安装成功', { name: getName(file) });
 
                        // 是否插件页面
                        if (router.currentRoute.value.path == '/helper/plugins') {
                            ElMessage.success(title);
                        } else {
                            ElMessageBox.alert(title, t('提示'), {
                                type: 'success',
                                confirmButtonText: t('点击查看'),
                                showCancelButton: true
                            })
                                .then(() => {
                                    router.push('/helper/plugins');
                                })
                                .catch(() => null);
                        }
 
                        return;
                    }
 
                    if (res.type == 0) {
                        ElMessageBox.confirm(res.message, t('提示'), {
                            type: 'error',
                            showConfirmButton: false
                        })
                            .then(() => {
                                next(true);
                            })
                            .catch(() => null);
                    }
 
                    if (res.type == 1 || res.type == 2) {
                        ElMessageBox.confirm(res.message, t('提示'), {
                            type: 'warning',
                            confirmButtonText: t('继续')
                        })
                            .then(() => {
                                next(true);
                            })
                            .catch(() => null);
                    }
 
                    if (res.type == 3) {
                        next(true);
                    }
                })
                .catch(err => {
                    ElMessage.error(err.message);
                });
        };
 
        next(false);
    }
 
    return {
        register,
        install
    };
};