优化:图片预览

This commit is contained in:
jmal 2021-03-24 13:47:27 +08:00
parent 4b3ce450f9
commit 05afe2c31a
4 changed files with 199 additions and 113 deletions

View File

@ -16,7 +16,7 @@
<script src="https://cdn.bootcss.com/axios/0.21.1/axios.min.js"></script> <script src="https://cdn.bootcss.com/axios/0.21.1/axios.min.js"></script>
<!--vant.js--> <!--vant.js-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@2.12/lib/index.css"/> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@2.12/lib/index.css"/>
<script src="https://cdn.jsdelivr.net/npm/vant@2.12/lib/vant.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vant@2.12/lib/vant.js"></script>
<!--element-ui.js--> <!--element-ui.js-->
<link rel="stylesheet" href="https://unpkg.com/element-ui@2.15.1/lib/theme-chalk/index.css"> <link rel="stylesheet" href="https://unpkg.com/element-ui@2.15.1/lib/theme-chalk/index.css">
<script src="https://unpkg.com/element-ui@2.15.1/lib/index.js"></script> <script src="https://unpkg.com/element-ui@2.15.1/lib/index.js"></script>

View File

@ -1,14 +1,14 @@
<template> <template>
<div> <div>
<viewer v-show="false" <viewer v-show="pc"
:options="options" :options="options"
:images="images" :images="images"
@inited="inited" @inited="inited"
class="viewer" ref="viewer" class="viewer" ref="viewer"
> >
<template slot-scope="scope"> <template slot-scope="scope">
<div v-for="src in scope.images" :key="src"> <div v-for="(image,index) in scope.images" :key="index">
<img :src="src"> <img :src="image.thumbnail" :data-src="image.source">
</div> </div>
</template> </template>
</viewer> </viewer>
@ -17,10 +17,10 @@
<script> <script>
import fileConfig from '@/utils/file-config' import fileConfig from '@/utils/file-config'
import Bus from '@/assets/js/bus' import Bus from '@/assets/js/bus'
export default { export default {
name: "ImageViewer", name: "ImageViewer",
components: { components: {},
},
props: { props: {
file: { file: {
type: Object, type: Object,
@ -45,14 +45,19 @@
} }
} }
}, },
computed: { computed: {},
},
data() { data() {
return { return {
imageThumbnailUrl: process.env.VUE_APP_BASE_API + '/view/thumbnail?jmal-token=' + this.$store.state.user.token + '&id=',
currentSrc: '', currentSrc: '',
images: [], images: [],
imagesFristIndex: -1,
imagesLastIndex: -1,
imageFiles: [],
vantImagePreview: null,
pc: this.$pc,
options: { options: {
url: 'data-src',
initialViewIndex: 3, initialViewIndex: 3,
ready: function (e) { ready: function (e) {
}, },
@ -66,6 +71,7 @@
Bus.$emit('updateImageViewerStatus', false) Bus.$emit('updateImageViewerStatus', false)
}, },
view: function (e) { view: function (e) {
Bus.$emit('imageViewerCurrentIndex', e.detail.index)
}, },
viewed: function (e) { viewed: function (e) {
}, },
@ -78,52 +84,130 @@
}, },
watch: { watch: {
status: function (visible) { status: function (visible) {
this.pc = this.$pc
if (visible) { if (visible) {
this.images = [] this.images = []
let viewIndex = 0 let viewIndex = 0
const findIndex = this.fileList.findIndex(file => file.id === this.file.id) this.imageFiles = this.fileList.filter(element => !element.isFolder && element.contentType.startsWith('image'))
this.fileList.forEach((element,index) => { const findIndex = this.imageFiles.findIndex(file => file.id === this.file.id)
if(index < findIndex + 5 && index > findIndex - 5 ) { this.imageFiles.forEach((element, index) => {
if(!element.isFolder && element.contentType.startsWith('image')){ this.images.push({
let url = fileConfig.previewUrl(this.$store.state.user.name, element, this.$store.getters.token) thumbnail: this.imageThumbnailUrl + element.id,
if(this.shareId){ source: this.getImageUrl(element)
url = fileConfig.publicPreviewUrl(element.id, window.shareId); })
}
this.images.push(url)
if (this.file.id === element.id) { if (this.file.id === element.id) {
viewIndex = this.images.length - 1 viewIndex = this.images.length - 1
} }
} })
}
});
this.show(viewIndex) this.show(viewIndex)
} }
} }
}, },
mounted() { mounted() {
Bus.$on('updateImageViewerStatus', () => { Bus.$on('updateImageViewerStatus', () => {
this.imagesFristIndex = -1
this.imagesLastIndex = -1
this.$emit('update:status', false) this.$emit('update:status', false)
}) })
Bus.$on('imageViewerCurrentIndex', (index) => {
// this.$nextTick(() => {
// let img = document.querySelector('.viewer-container .viewer-canvas > img')
// let url = this.getImageUrlbyThumbnail(this.images[index])
// console.log(img, 'url', url)
// img.src = url
// })
// const findIndex = this.imageFiles.findIndex(file => file.id === this.file.id)
// if(this.imagesFristIndex === -1) {
// const centerIndex = (this.images.length - 1) / 2
// if (findIndex >= centerIndex) {
// this.imagesFristIndex = findIndex - centerIndex
// } else {
// this.imagesFristIndex = 0
// }
// this.imagesLastIndex += this.images.length
// }
// console.log('imagesFristIndex', this.imagesFristIndex, 'imagesLastIndex', this.imagesLastIndex, 'index', index)
// if (index <= 2 && this.imagesFristIndex > 0) {
// let end = this.imagesFristIndex
// let begin = 0
// if (end >= 3) {
// begin = end - 3
// }
// console.log('begin', begin, 'end', end)
// let unshifts = this.imageFiles.slice(begin, end).reverse()
// unshifts.forEach((element, index) => {
// this.images.unshift(this.getImageUrl(element))
// this.imagesFristIndex--
// })
// console.log('index', index , 'unshifts.length', unshifts.length)
// // const that = this
// // setTimeout(function (){
// // that.vantImagePreview.startPosition = index - unshifts.length
// // }, 200)
// }
// if (index >= (this.images.length - 1) && this.imagesLastIndex < (this.imageFiles.length - 1)) {
// let begin = this.imagesLastIndex + 1
// let end = begin + 1
// }
})
}, },
methods: { methods: {
inited(viewer) { inited(viewer) {
this.$viewer = viewer this.$viewer = viewer
}, },
getImageUrl(element) {
let url = fileConfig.previewUrl(this.$store.state.user.name, element, this.$store.getters.token)
if (this.shareId) {
url = fileConfig.publicPreviewUrl(element.id, window.shareId);
}
return url
},
getImageUrlbyThumbnail(url) {
let id = url.split('&id=')[1]
if (id) {
const findIndex = this.imageFiles.findIndex(file => file.id === id)
return this.getImageUrl(this.imageFiles[findIndex])
}
return url
},
show(viewIndex) { show(viewIndex) {
let index = 0 let index = 0
if (viewIndex) { if (viewIndex) {
index = viewIndex index = viewIndex
} }
if (this.$pc) {
this.$viewer.index = index this.$viewer.index = index
this.$viewer.show() this.$viewer.show()
} else {
this.imagePreview(index)
}
}, },
updateStatus() { imagePreview(viewIndex) {
let imageUrls = []
this.images.forEach(image => {
imageUrls.push(image.source)
})
this.vantImagePreview = vant.ImagePreview({
images: imageUrls,
startPosition: viewIndex,
closeable: true,
onClose() {
Bus.$emit('updateImageViewerStatus', false)
},
onChange(index) {
Bus.$emit('imageViewerCurrentIndex', index)
}
})
} }
} }
} }
</script> </script>
<style> <style lang="scss" scoped>
.viewer {
display: none;
}
.viewer-backdrop { .viewer-backdrop {
background-color: rgba(0, 0, 0, 0.7); background-color: rgba(0, 0, 0, 0.7);
} }

View File

@ -32,10 +32,11 @@ Vue.use(Viewer)
Vue.use(VueWechatTitle) Vue.use(VueWechatTitle)
Vue.use(uploader) Vue.use(uploader)
Vue.use(EVueContextmenu) Vue.use(EVueContextmenu)
Vue.use(vant.ImagePreview)
Vue.component('Aplayer', VueAPlayer) Vue.component('Aplayer', VueAPlayer)
VueAPlayer.disableVersionBadge = true VueAPlayer.disableVersionBadge = true

View File

@ -247,7 +247,9 @@
let pinyin = require("pinyin"); let pinyin = require("pinyin");
export default { export default {
components: {ImageViewer, IconFile}, components: {
ImageViewer, IconFile,
},
computed: { computed: {
...mapGetters([ ...mapGetters([
'name', 'name',
@ -962,7 +964,6 @@
this.getFileList() this.getFileList()
} else { } else {
if (row.contentType.startsWith('image')) { if (row.contentType.startsWith('image')) {
// 图片
this.imagePreviewVisible = true this.imagePreviewVisible = true
this.imagePreviewRow = row this.imagePreviewRow = row
return return