wangzhibo
6 天以前 7bd866831780abcf5a59c4cbb7d1be456eea7c99
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
<template>
    <div
        class="cl-image"
        :style="{
            height: style.h
        }"
    >
        <el-image
            :src="url"
            :fit="fit"
            :lazy="lazy"
            :preview-src-list="preview ? urls : undefined"
            :style="{
                height: style.h,
                width: style.w
            }"
            preview-teleported
        >
            <template #error>
                <div class="cl-image__slot">
                    <el-icon :size="18"><picture-filled /></el-icon>
                </div>
            </template>
        </el-image>
    </div>
</template>
 
<script lang="ts" setup>
defineOptions({
    name: 'cl-image'
});
 
import { computed, type PropType } from 'vue';
import { isArray, isNumber, isString } from 'lodash-es';
import { PictureFilled } from '@element-plus/icons-vue';
import { parsePx } from '/@/cool/utils';
 
const props = defineProps({
    modelValue: [String, Array],
    src: [String, Array],
    size: {
        type: [Number, Array],
        default: 100
    },
    radius: {
        type: [Number, String],
        default: 0
    },
    lazy: Boolean,
    fit: {
        type: String as PropType<'' | 'contain' | 'cover' | 'none' | 'fill' | 'scale-down'>,
        default: 'cover'
    },
    compress: String as PropType<'oss' | 'none'>,
    preview: {
        type: Boolean,
        default: true
    }
});
 
const urls = computed(() => {
    const urls: any = props.modelValue || props.src;
 
    if (isArray(urls)) {
        return urls;
    }
 
    if (isString(urls)) {
        if (urls.startsWith('data:image')) {
            return [urls];
        }
 
        return (urls || '').split(',').filter(Boolean);
    }
 
    return [];
});
 
const style = computed(() => {
    const [h, w]: any[] = isNumber(props.size) ? [props.size, props.size] : props.size;
    return {
        h: parsePx(h),
        w: parsePx(w)
    };
});
 
const url = computed(() => {
    const v = urls.value[0];
    return props.compress === 'oss'
        ? `${v}?x-oss-process=image/resize,m_fill,h_${style.value.h},w_${style.value.w}`
        : v;
});
</script>
 
<style lang="scss" scoped>
.cl-image {
    &__slot {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100%;
        border-radius: 6px;
        background-color: var(--el-fill-color-lighter);
        color: var(--el-text-color-regular);
    }
 
    :deep(.el-image__inner) {
        border-radius: v-bind('parsePx(props.radius)');
    }
}
</style>