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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<template>
    <slot></slot>
 
    <!-- 按钮 -->
    <el-button @click="open" v-if="showBtn">{{ btnText }}</el-button>
 
    <!-- 列表 -->
    <div class="cl-upload-space__list" v-if="urls.length > 0 && showList">
        <cl-upload v-model="urls" disabled deletable draggable :multiple="multiple" />
    </div>
 
    <!-- 弹框 -->
    <cl-dialog
        v-model="visible"
        :title="config.title"
        height="650px"
        width="1070px"
        padding="0"
        keep-alive
        :scrollbar="false"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
    >
        <space-inner :ref="setRefs('inner')" v-bind="config" @confirm="confirm" />
 
        <template #footer>
            <el-button @click="close">{{ $t('取消') }}</el-button>
            <el-button :disabled="selection.length == 0" type="success" @click="confirm()">
                {{
                    $t('选择 {count}/{limit} 个', { count: selection.length, limit: config.limit })
                }}
            </el-button>
        </template>
    </cl-dialog>
</template>
 
<script lang="ts" setup>
defineOptions({
    name: 'cl-upload-space'
});
 
import { computed, nextTick, onMounted, ref, watch } from 'vue';
import { useCool } from '/@/cool';
import SpaceInner from './space-inner.vue';
import { assign, isString } from 'lodash-es';
import { useI18n } from 'vue-i18n';
 
const { t } = useI18n();
 
const props = defineProps({
    modelValue: [String, Array],
    // 标题
    title: String,
    // 按钮文本
    text: String,
    // 是否多选
    multiple: {
        type: Boolean,
        default: true
    },
    // 可选数量
    limit: {
        type: Number,
        default: 9
    },
    // 类型
    accept: String,
    // 显示按钮
    showBtn: {
        type: Boolean,
        default: true
    },
    // 显示列表
    showList: {
        type: Boolean,
        default: true
    }
});
 
const emit = defineEmits(['update:modelValue', 'change', 'confirm']);
 
const { refs, setRefs } = useCool();
 
// 是否可见
const visible = ref(false);
 
// 配置
const config = ref({
    title: '',
    limit: 9
});
 
// 展示列表
const urls = ref<any[]>([]);
 
// 选中列表
const selection = computed<Eps.SpaceInfoEntity[]>(() => refs.inner?.selection || []);
 
// 按钮文案
const btnText = computed(() => {
    return props.text || t('选择文件');
});
 
// 打开
function open(options?: any) {
    visible.value = true;
 
    // 合并配置
    config.value = assign(
        { ...props, title: props.title || t('文件空间'), text: props.text || t('点击上传') },
        options
    );
 
    // 非多选情况
    if (!props.multiple) {
        config.value.limit = 1;
    }
 
    nextTick(() => {
        refs.inner?.clear();
    });
}
 
// 关闭
function close() {
    visible.value = false;
}
 
// 确认
function confirm(arr?: Eps.SpaceInfoEntity[]) {
    const list = arr || selection.value;
 
    // 读取文件地址
    urls.value = list.map(e => e.url);
 
    // 返回值
    const v = props.multiple ? urls.value : urls.value[0];
 
    // 事件
    emit('update:modelValue', v);
    emit('change', v);
    emit('confirm', list);
 
    // 关闭
    close();
}
 
onMounted(() => {
    watch(
        () => props.modelValue,
        val => {
            if (val) {
                urls.value = isString(val) ? [val] : val;
            }
        },
        {
            immediate: true
        }
    );
});
 
defineExpose({
    open,
    close
});
</script>
 
<style lang="scss" scoped>
.cl-upload-space__list {
    margin-top: 10px;
}
</style>