master
2022-04-07 a28f8de41ab2d99457875db7cdd0cc5bf9325f32
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
<template>
    <cl-upload
        :accept="accept"
        list-type
        :before-upload="onBeforeUpload"
        :on-progress="onUploadProgress"
        :on-success="onUploadSuccess"
    >
        <slot></slot>
    </cl-upload>
</template>
 
<script lang="ts">
import { defineComponent, inject } from "vue";
import { useCool } from "/@/cool";
 
export default defineComponent({
    props: {
        name: String,
        accept: String
    },
 
    emits: ["before-upload", "success"],
 
    setup(props, { emit }) {
        const { store } = useCool();
 
        // 聊天信息
        const chat = inject<any>("chat");
 
        // 上传前
        function onBeforeUpload(file: any) {
            // 先添加到列表中,等待上传
            function next(options = {}) {
                const data = {
                    content: {
                        [`${props.name}Url`]: ""
                    },
                    type: 0,
                    uid: file.uid,
                    loading: true,
                    progress: "0%",
                    contentType: chat.modes.indexOf(props.name),
                    ...options
                };
 
                emit("before-upload", data);
            }
 
            // 图片预览
            if (props.name == "image") {
                const fileReader = new FileReader();
 
                fileReader.onload = (e: any) => {
                    const imageUrl = e.target.result;
                    const image = new Image();
 
                    image.onload = () => {
                        let height = 0;
                        let width = 0;
 
                        if (image.width > 200) {
                            width = 200;
                            height = (image.height * 200) / image.width;
                        }
 
                        next({
                            content: {
                                imageUrl
                            },
                            style: {
                                height: height + "px",
                                width: width + "px"
                            }
                        });
                    };
 
                    image.src = imageUrl;
                };
 
                fileReader.readAsDataURL(file);
            } else {
                next();
            }
        }
 
        // 上传中
        function onUploadProgress(e: any, file: any) {
            store.commit("UPDATE_MESSAGE", {
                file,
                data: {
                    progress: e.percent + "%"
                }
            });
        }
 
        // 上传成功
        function onUploadSuccess(res: any, file: any) {
            store.commit("UPDATE_MESSAGE", {
                file,
                data: {
                    loading: false,
                    content: {
                        [`${props.name}Url`]: res.data
                    }
                },
                callback: (data: any) => {
                    emit("success", data);
                }
            });
        }
 
        return {
            onBeforeUpload,
            onUploadProgress,
            onUploadSuccess
        };
    }
});
</script>