| | |
| | | <template> |
| | | <el-dialog |
| | | :model-value="dialogVisible" |
| | | @opened="$emit('update:dialogVisible', true)" |
| | | @closed="$emit('update:dialogVisible', false)" |
| | | width="66%" |
| | | @opened="handleOpen" |
| | | @closed="handleClose" |
| | | top="5vh" |
| | | width="68%" |
| | | destroy-on-close |
| | | :close-on-press-escape="false" |
| | | > |
| | | <div class="main"> |
| | | <el-row justify="end" v-if="!readonly"> |
| | | <el-row justify="end"> |
| | | <el-text v-if="onContextMenu != undefined" size="small" type="info">{{ |
| | | `(${contextMenuStr})` |
| | | }}</el-text> |
| | | <div v-if="!readonly"> |
| | | <el-text size="small" type="info" class="m-r-8" |
| | | >最多选择{{ maxSelect }}张图片</el-text |
| | | > |
| | |
| | | <el-button size="small" type="primary" @click="handleCancel" |
| | | >取消</el-button |
| | | > |
| | | </el-row> |
| | | </div> |
| | | </el-row> |
| | | |
| | | <div class="center"> |
| | | <el-tabs v-if="typeList.length > 0" v-model="activeId" type="card"> |
| | | <el-tab-pane |
| | | v-for="item in typeList" |
| | | :key="item.typeId" |
| | | :label=" |
| | | item.typeName + ' (' + typeImgMap.get(item.typeId).length + ')' |
| | | " |
| | | :name="item.typeId" |
| | | > |
| | | </el-tab-pane> |
| | | </el-tabs> |
| | | <el-scrollbar |
| | | <div class="center"> |
| | | <el-tabs v-if="typeList.length > 0" v-model="activeId" type="card"> |
| | | <el-tab-pane |
| | | v-for="item in typeList" |
| | | :key="item.typeId" |
| | | :label=" |
| | | item.typeName + ' (' + typeImgMap.get(item.typeId).length + ')' |
| | | " |
| | | :name="item.typeId" |
| | | > |
| | | </el-tab-pane> |
| | | </el-tabs> |
| | | <el-scrollbar height="70vh"> |
| | | <div |
| | | v-if="typeImgMap.get(activeId) && typeImgMap.get(activeId).length > 0" |
| | | class="imgs" |
| | | > |
| | | <el-image |
| | | v-loading="img.loading" |
| | | v-for="(img, i) in typeImgMap.get(activeId)" |
| | | :key="i" |
| | | :class="[img.isSelect ? 'selected' : 'noActive', 'image']" |
| | | fit="cover" |
| | | :src="img.url" |
| | | lazy |
| | | :preview-src-list=" |
| | | readonly ? typeImgMap.get(activeId).map((v) => v.url) : [] |
| | | " |
| | | :initial-index="i" |
| | | @contextmenu="(e) => showContextMenu(e, i)" |
| | | @click="onSelect(img, i)" |
| | | @load="onOneImgLoadSuccess(img)" |
| | | @error="onOneImgLoadError(img)" |
| | | /> |
| | | </el-scrollbar> |
| | | </div> |
| | | <el-row v-else justify="space-between"> |
| | | <el-empty description="暂无记录" /> |
| | | </el-row> |
| | | </div> |
| | | </el-scrollbar> |
| | | </div> |
| | | </el-dialog> |
| | | </template> |
| | | <script setup> |
| | | import { ref, watch } from 'vue'; |
| | | import { ref, watch, computed, onMounted, onUnmounted } from 'vue'; |
| | | |
| | | const props = defineProps({ |
| | | dialogVisible: Boolean, |
| | |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | defaultFile: { |
| | | type: Array, |
| | | default: () => [] |
| | | }, |
| | | // 图片可选数量,当传入数字时,代表图片数量 |
| | | maxSelect: { |
| | | type: Number, |
| | | default: 3 |
| | | }, |
| | | // 图片右键点击事件 |
| | | onContextMenu: { |
| | | type: Function |
| | | }, |
| | | contextMenuStr: { |
| | | type: String, |
| | | default: '右键点击图片触发额外操作' |
| | | } |
| | | }); |
| | | |
| | | const emit = defineEmits(['submit', 'cancel', 'update:dialogVisible']); |
| | | |
| | | const activeId = ref(''); |
| | | // const typeImgMap = ref(new Map()); |
| | | |
| | | const selectedImgUrlList = ref([]); |
| | | |
| | | let loadedImgCount = ref(0); |
| | | // 加载状态 |
| | | const loading = computed(() => { |
| | | if (activeId.value == '') { |
| | | return false; |
| | | } |
| | | // 保证最开始是加载状态,三分之一加载之后停止展示加载状态 |
| | | return !( |
| | | props.typeImgMap.get(activeId.value).length / 3 <= |
| | | loadedImgCount.value |
| | | ); |
| | | }); |
| | | function onOneImgLoadError(img) { |
| | | img.loading = false; |
| | | loadedImgCount.value++; |
| | | } |
| | | function onOneImgLoadSuccess(img) { |
| | | img.loading = false; |
| | | loadedImgCount.value++; |
| | | } |
| | | watch( |
| | | () => activeId.value, |
| | | (nV, oV) => { |
| | | loadedImgCount.value = 0; |
| | | }, |
| | | { immediate: true } |
| | | ); |
| | | |
| | | function onSelect(img, i) { |
| | | if (props.readonly) { |
| | |
| | | img.isSelect = false; |
| | | } |
| | | } |
| | | |
| | | function handleOpen() { |
| | | emit('update:dialogVisible', true); |
| | | } |
| | | function handleClose() { |
| | | selectedImgUrlList.value.forEach((item) => (item.isSelect = false)); |
| | | selectedImgUrlList.value = []; |
| | | emit('update:dialogVisible', false); |
| | | } |
| | | function handleSubmit() { |
| | | emit('submit', selectedImgUrlList.value); |
| | | emit('update:dialogVisible', false); |
| | |
| | | emit('update:dialogVisible', false); |
| | | } |
| | | |
| | | // 图片右键点击时间 |
| | | function showContextMenu(event, index) { |
| | | if (props.onContextMenu) { |
| | | event.preventDefault(); |
| | | props.onContextMenu(event, activeId.value, index); |
| | | } |
| | | } |
| | | |
| | | watch( |
| | | () => props.typeList, |
| | | (nV, oV) => { |
| | | if (nV != oV && nV.length > 0) { |
| | | activeId.value = nV[0].typeId; |
| | | } |
| | | }, |
| | | { immediate: true } |
| | | ); |
| | | |
| | | watch( |
| | | () => props.typeImgMap, |
| | | (newMap, oldMap) => { |
| | | if (newMap.get(activeId.value) == undefined) { |
| | | return; |
| | | } |
| | | newMap.get(activeId.value).forEach((i) => { |
| | | if (i.isSelect == true) { |
| | | return; |
| | | } |
| | | props.defaultFile.forEach((imgItem) => { |
| | | if (imgItem.url == i.url) { |
| | | i.isSelect = true; |
| | | selectedImgUrlList.value.push(i); |
| | | } |
| | | }); |
| | | }); |
| | | }, |
| | | { immediate: true } |
| | | ); |
| | |
| | | } |
| | | |
| | | .main { |
| | | margin: 0 auto; /* 使父元素居中 */ |
| | | height: 100%; |
| | | width: 100%; |
| | | /* 使父元素居中 */ |
| | | /* margin: 0 auto; */ |
| | | /* width: 100%; */ |
| | | } |
| | | |
| | | .imgs { |
| | | height: 50vh; |
| | | width: 100%; |
| | | min-height: 100px !important; |
| | | /* border-style:solid; |
| | | border-radius: 1px; */ |
| | | /* height: 100%; */ |
| | |
| | | |
| | | .image { |
| | | margin: 5px; |
| | | height: 210px; |
| | | width: 200px; |
| | | height: 250px; |
| | | width: 240px; |
| | | border-radius: 4px; |
| | | } |
| | | |
| | | .active { |
| | | padding: 5px; |
| | | width: 20%; |
| | | height: 200px; |
| | | border: 0.5rem outset rgb(52, 155, 4); |
| | | } |
| | | |
| | | .selected { |
| | |
| | | } |
| | | |
| | | ::v-deep .el-dialog__body { |
| | | height: 60vh; |
| | | padding: 10px calc(var(--el-dialog-padding-primary) + 10px) !important; |
| | | } |
| | | </style> |