|
|
@@ -5,215 +5,216 @@
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
- import Dropzone from 'dropzone'
|
|
|
- import 'dropzone/dist/dropzone.css'
|
|
|
- // import { getToken } from 'api/qiniu';
|
|
|
+import Dropzone from 'dropzone'
|
|
|
+import 'dropzone/dist/dropzone.css'
|
|
|
+// import { getToken } from 'api/qiniu';
|
|
|
|
|
|
- Dropzone.autoDiscover = false
|
|
|
+Dropzone.autoDiscover = false
|
|
|
|
|
|
- export default {
|
|
|
- props: {
|
|
|
- id: {
|
|
|
- type: String,
|
|
|
- required: true
|
|
|
- },
|
|
|
- url: {
|
|
|
- type: String,
|
|
|
- required: false
|
|
|
- },
|
|
|
- clickable: {
|
|
|
- type: Boolean,
|
|
|
- default: true
|
|
|
- },
|
|
|
- defaultMsg: {
|
|
|
- type: String,
|
|
|
- default: '上传图片'
|
|
|
- },
|
|
|
- acceptedFiles: {
|
|
|
- type: String,
|
|
|
- default: 'image/*'
|
|
|
- },
|
|
|
- thumbnailHeight: {
|
|
|
- type: Number,
|
|
|
- default: 200
|
|
|
- },
|
|
|
- thumbnailWidth: {
|
|
|
- type: Number,
|
|
|
- default: 200
|
|
|
- },
|
|
|
- showRemoveLink: {
|
|
|
- type: Boolean,
|
|
|
- default: true
|
|
|
- },
|
|
|
- maxFilesize: {
|
|
|
- type: Number,
|
|
|
- default: 100
|
|
|
- },
|
|
|
- maxFiles: {
|
|
|
- type: Number,
|
|
|
- default: 100
|
|
|
- },
|
|
|
- autoProcessQueue: {
|
|
|
- type: Boolean,
|
|
|
- default: true
|
|
|
- },
|
|
|
- useCustomDropzoneOptions: {
|
|
|
- type: Boolean,
|
|
|
- default: false
|
|
|
- },
|
|
|
- defaultImg: {
|
|
|
- default: '',
|
|
|
- type: [String, Array]
|
|
|
- },
|
|
|
- couldPaste: {
|
|
|
- type: Boolean,
|
|
|
- default: false
|
|
|
- }
|
|
|
+export default {
|
|
|
+ props: {
|
|
|
+ id: {
|
|
|
+ type: String,
|
|
|
+ required: true
|
|
|
},
|
|
|
- created() {
|
|
|
- this.uploadUrl = this.url ? this.url : `${process.env.VUE_APP_BASE_API}/api/files/upload`
|
|
|
+ url: {
|
|
|
+ type: String,
|
|
|
+ required: false,
|
|
|
+ default: ''
|
|
|
},
|
|
|
- data() {
|
|
|
- return {
|
|
|
- dropzone: '',
|
|
|
- uploadUrl: '',
|
|
|
- initOnce: true,
|
|
|
- }
|
|
|
+ clickable: {
|
|
|
+ type: Boolean,
|
|
|
+ default: true
|
|
|
},
|
|
|
- watch: {
|
|
|
- defaultImg(val) {
|
|
|
- if (val.length === 0) {
|
|
|
- this.initOnce = false
|
|
|
- return
|
|
|
- }
|
|
|
- if (!this.initOnce) return
|
|
|
- this.initImages(val)
|
|
|
+ defaultMsg: {
|
|
|
+ type: String,
|
|
|
+ default: '上传图片'
|
|
|
+ },
|
|
|
+ acceptedFiles: {
|
|
|
+ type: String,
|
|
|
+ default: 'image/*'
|
|
|
+ },
|
|
|
+ thumbnailHeight: {
|
|
|
+ type: Number,
|
|
|
+ default: 200
|
|
|
+ },
|
|
|
+ thumbnailWidth: {
|
|
|
+ type: Number,
|
|
|
+ default: 200
|
|
|
+ },
|
|
|
+ showRemoveLink: {
|
|
|
+ type: Boolean,
|
|
|
+ default: true
|
|
|
+ },
|
|
|
+ maxFilesize: {
|
|
|
+ type: Number,
|
|
|
+ default: 100
|
|
|
+ },
|
|
|
+ maxFiles: {
|
|
|
+ type: Number,
|
|
|
+ default: 100
|
|
|
+ },
|
|
|
+ autoProcessQueue: {
|
|
|
+ type: Boolean,
|
|
|
+ default: true
|
|
|
+ },
|
|
|
+ useCustomDropzoneOptions: {
|
|
|
+ type: Boolean,
|
|
|
+ default: false
|
|
|
+ },
|
|
|
+ defaultImg: {
|
|
|
+ default: '',
|
|
|
+ type: [String, Array]
|
|
|
+ },
|
|
|
+ couldPaste: {
|
|
|
+ type: Boolean,
|
|
|
+ default: false
|
|
|
+ }
|
|
|
+ },
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ dropzone: '',
|
|
|
+ uploadUrl: '',
|
|
|
+ initOnce: true
|
|
|
+ }
|
|
|
+ },
|
|
|
+ watch: {
|
|
|
+ defaultImg(val) {
|
|
|
+ if (val.length === 0) {
|
|
|
this.initOnce = false
|
|
|
+ return
|
|
|
}
|
|
|
- },
|
|
|
- mounted() {
|
|
|
- const element = document.getElementById(this.id)
|
|
|
- const vm = this
|
|
|
- this.dropzone = new Dropzone(element, {
|
|
|
- headers: { Authorization: this.$store.getters.token },
|
|
|
- clickable: this.clickable,
|
|
|
- thumbnailWidth: this.thumbnailWidth,
|
|
|
- thumbnailHeight: this.thumbnailHeight,
|
|
|
- maxFiles: this.maxFiles,
|
|
|
- maxFilesize: this.maxFilesize,
|
|
|
- dictRemoveFile: 'Remove',
|
|
|
- addRemoveLinks: this.showRemoveLink,
|
|
|
- acceptedFiles: this.acceptedFiles,
|
|
|
- autoProcessQueue: this.autoProcessQueue,
|
|
|
- dictDefaultMessage: '<i style="margin-top: 3em;display: inline-block" class="material-icons">' + this.defaultMsg + '</i><br>Drop files here to upload',
|
|
|
- dictMaxFilesExceeded: '只能一个图',
|
|
|
- previewTemplate: '<div class="dz-preview dz-file-preview"> <div class="dz-image" style="width:' + this.thumbnailWidth + 'px;height:' + this.thumbnailHeight + 'px" ><img style="width:' + this.thumbnailWidth + 'px;height:' + this.thumbnailHeight + 'px" data-dz-thumbnail /></div> <div class="dz-details"> <div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div> <div class="dz-error-message"><span data-dz-errormessage></span></div> <div class="dz-success-mark"> <i class="material-icons">done</i> </div> <div class="dz-error-mark"><i class="material-icons">error</i></div></div>',
|
|
|
- init() {
|
|
|
- const val = vm.defaultImg
|
|
|
- if (!val) return
|
|
|
- if (Array.isArray(val)) {
|
|
|
- if (val.length === 0) return
|
|
|
- val.map((v, i) => {
|
|
|
- const mockFile = { name: 'name' + i, size: 12345, url: v }
|
|
|
- this.options.addedfile.call(this, mockFile)
|
|
|
- this.options.thumbnail.call(this, mockFile, v)
|
|
|
- mockFile.previewElement.classList.add('dz-success')
|
|
|
- mockFile.previewElement.classList.add('dz-complete')
|
|
|
- vm.initOnce = false
|
|
|
- return true
|
|
|
- })
|
|
|
- } else {
|
|
|
- const mockFile = { name: 'name', size: 12345, url: val }
|
|
|
+ if (!this.initOnce) return
|
|
|
+ this.initImages(val)
|
|
|
+ this.initOnce = false
|
|
|
+ }
|
|
|
+ },
|
|
|
+ created() {
|
|
|
+ this.uploadUrl = this.url ? this.url : `${process.env.VUE_APP_BASE_API}/api/files/upload`
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ const element = document.getElementById(this.id)
|
|
|
+ const vm = this
|
|
|
+ this.dropzone = new Dropzone(element, {
|
|
|
+ headers: { Authorization: this.$store.getters.token },
|
|
|
+ clickable: this.clickable,
|
|
|
+ thumbnailWidth: this.thumbnailWidth,
|
|
|
+ thumbnailHeight: this.thumbnailHeight,
|
|
|
+ maxFiles: this.maxFiles,
|
|
|
+ maxFilesize: this.maxFilesize,
|
|
|
+ dictRemoveFile: 'Remove',
|
|
|
+ addRemoveLinks: this.showRemoveLink,
|
|
|
+ acceptedFiles: this.acceptedFiles,
|
|
|
+ autoProcessQueue: this.autoProcessQueue,
|
|
|
+ dictDefaultMessage: '<i style="margin-top: 3em;display: inline-block" class="material-icons">' + this.defaultMsg + '</i><br>Drop files here to upload',
|
|
|
+ dictMaxFilesExceeded: '只能一个图',
|
|
|
+ previewTemplate: '<div class="dz-preview dz-file-preview"> <div class="dz-image" style="width:' + this.thumbnailWidth + 'px;height:' + this.thumbnailHeight + 'px" ><img style="width:' + this.thumbnailWidth + 'px;height:' + this.thumbnailHeight + 'px" data-dz-thumbnail /></div> <div class="dz-details"> <div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div> <div class="dz-error-message"><span data-dz-errormessage></span></div> <div class="dz-success-mark"> <i class="material-icons">done</i> </div> <div class="dz-error-mark"><i class="material-icons">error</i></div></div>',
|
|
|
+ init() {
|
|
|
+ const val = vm.defaultImg
|
|
|
+ if (!val) return
|
|
|
+ if (Array.isArray(val)) {
|
|
|
+ if (val.length === 0) return
|
|
|
+ val.map((v, i) => {
|
|
|
+ const mockFile = { name: 'name' + i, size: 12345, url: v }
|
|
|
this.options.addedfile.call(this, mockFile)
|
|
|
- this.options.thumbnail.call(this, mockFile, val)
|
|
|
+ this.options.thumbnail.call(this, mockFile, v)
|
|
|
mockFile.previewElement.classList.add('dz-success')
|
|
|
mockFile.previewElement.classList.add('dz-complete')
|
|
|
vm.initOnce = false
|
|
|
- }
|
|
|
- },
|
|
|
- accept: (file, done) => {
|
|
|
- /* 七牛*/
|
|
|
- // const token = this.$store.getters.token;
|
|
|
- // getToken(token).then(response => {
|
|
|
- // file.token = response.data.qiniu_token;
|
|
|
- // file.key = response.data.qiniu_key;
|
|
|
- // file.url = response.data.qiniu_url;
|
|
|
- // done();
|
|
|
- // })
|
|
|
- done()
|
|
|
- },
|
|
|
- sending: (file, xhr, formData) => {
|
|
|
- // formData.append('token', file.token);
|
|
|
- // formData.append('key', file.key);
|
|
|
+ return true
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ const mockFile = { name: 'name', size: 12345, url: val }
|
|
|
+ this.options.addedfile.call(this, mockFile)
|
|
|
+ this.options.thumbnail.call(this, mockFile, val)
|
|
|
+ mockFile.previewElement.classList.add('dz-success')
|
|
|
+ mockFile.previewElement.classList.add('dz-complete')
|
|
|
vm.initOnce = false
|
|
|
}
|
|
|
- })
|
|
|
-
|
|
|
- if (this.couldPaste) {
|
|
|
- document.addEventListener('paste', this.pasteImg)
|
|
|
+ },
|
|
|
+ accept: (file, done) => {
|
|
|
+ /* 七牛*/
|
|
|
+ // const token = this.$store.getters.token;
|
|
|
+ // getToken(token).then(response => {
|
|
|
+ // file.token = response.data.qiniu_token;
|
|
|
+ // file.key = response.data.qiniu_key;
|
|
|
+ // file.url = response.data.qiniu_url;
|
|
|
+ // done();
|
|
|
+ // })
|
|
|
+ done()
|
|
|
+ },
|
|
|
+ sending: (file, xhr, formData) => {
|
|
|
+ // formData.append('token', file.token);
|
|
|
+ // formData.append('key', file.key);
|
|
|
+ vm.initOnce = false
|
|
|
}
|
|
|
+ })
|
|
|
|
|
|
- this.dropzone.on('success', (file, response) => {
|
|
|
- file.fileUrl = response.url
|
|
|
- file.dropzoneId = vm.dropzone.element.id
|
|
|
- vm.$emit('dropzone-success', file, vm.dropzone.element)
|
|
|
- })
|
|
|
- this.dropzone.on('addedfile', file => {
|
|
|
- vm.$emit('dropzone-fileAdded', file)
|
|
|
- })
|
|
|
- this.dropzone.on('removedfile', file => {
|
|
|
- vm.$emit('dropzone-removedFile', file, )
|
|
|
- })
|
|
|
- this.dropzone.on('error', (file, error, xhr) => {
|
|
|
- vm.$emit('dropzone-error', file, error, xhr)
|
|
|
- })
|
|
|
- this.dropzone.on('successmultiple', (file, error, xhr) => {
|
|
|
- vm.$emit('dropzone-successmultiple', file, error, xhr)
|
|
|
- })
|
|
|
+ if (this.couldPaste) {
|
|
|
+ document.addEventListener('paste', this.pasteImg)
|
|
|
+ }
|
|
|
+
|
|
|
+ this.dropzone.on('success', (file, response) => {
|
|
|
+ file.fileUrl = response.url
|
|
|
+ file.dropzoneId = vm.dropzone.element.id
|
|
|
+ vm.$emit('dropzone-success', file, vm.dropzone.element)
|
|
|
+ })
|
|
|
+ this.dropzone.on('addedfile', file => {
|
|
|
+ vm.$emit('dropzone-fileAdded', file)
|
|
|
+ })
|
|
|
+ this.dropzone.on('removedfile', file => {
|
|
|
+ vm.$emit('dropzone-removedFile', file)
|
|
|
+ })
|
|
|
+ this.dropzone.on('error', (file, error, xhr) => {
|
|
|
+ vm.$emit('dropzone-error', file, error, xhr)
|
|
|
+ })
|
|
|
+ this.dropzone.on('successmultiple', (file, error, xhr) => {
|
|
|
+ vm.$emit('dropzone-successmultiple', file, error, xhr)
|
|
|
+ })
|
|
|
+ },
|
|
|
+ destroyed() {
|
|
|
+ document.removeEventListener('paste', this.pasteImg)
|
|
|
+ this.dropzone.destroy()
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ removeAllFiles() {
|
|
|
+ this.dropzone.removeAllFiles(true)
|
|
|
},
|
|
|
- destroyed() {
|
|
|
- document.removeEventListener('paste', this.pasteImg)
|
|
|
- this.dropzone.destroy()
|
|
|
+ processQueue() {
|
|
|
+ this.dropzone.processQueue()
|
|
|
},
|
|
|
- methods: {
|
|
|
- removeAllFiles() {
|
|
|
- this.dropzone.removeAllFiles(true)
|
|
|
- },
|
|
|
- processQueue() {
|
|
|
- this.dropzone.processQueue()
|
|
|
- },
|
|
|
- pasteImg(event) {
|
|
|
- const items = (event.clipboardData || event.originalEvent.clipboardData).items
|
|
|
- if (items[0].kind === 'file') {
|
|
|
- this.dropzone.addFile(items[0].getAsFile())
|
|
|
- }
|
|
|
- },
|
|
|
- initImages(val) {
|
|
|
- if (!val) return
|
|
|
- if (Array.isArray(val)) {
|
|
|
- val.map((v, i) => {
|
|
|
- // const fileUrl = `${process.env.VUE_APP_BASE_API}/api/files/view-file?path=${v}`
|
|
|
- // const mockFile = { url: fileUrl }
|
|
|
- const mockFile = { url: v }
|
|
|
- this.dropzone.options.addedfile.call(this.dropzone, mockFile)
|
|
|
- this.dropzone.options.thumbnail.call(this.dropzone, mockFile, v)
|
|
|
- mockFile.previewElement.classList.add('dz-success')
|
|
|
- mockFile.previewElement.classList.add('dz-complete')
|
|
|
- return true
|
|
|
- })
|
|
|
- } else {
|
|
|
- // const fileUrl = `${process.env.VUE_APP_BASE_API}/api/files/view-file?path=${val}`
|
|
|
+ pasteImg(event) {
|
|
|
+ const items = (event.clipboardData || event.originalEvent.clipboardData).items
|
|
|
+ if (items[0].kind === 'file') {
|
|
|
+ this.dropzone.addFile(items[0].getAsFile())
|
|
|
+ }
|
|
|
+ },
|
|
|
+ initImages(val) {
|
|
|
+ if (!val) return
|
|
|
+ if (Array.isArray(val)) {
|
|
|
+ val.map((v, i) => {
|
|
|
+ // const fileUrl = `${process.env.VUE_APP_BASE_API}/api/files/view-file?path=${v}`
|
|
|
// const mockFile = { url: fileUrl }
|
|
|
- const mockFile = { url: val }
|
|
|
+ const mockFile = { url: v }
|
|
|
this.dropzone.options.addedfile.call(this.dropzone, mockFile)
|
|
|
- this.dropzone.options.thumbnail.call(this.dropzone, mockFile, val)
|
|
|
+ this.dropzone.options.thumbnail.call(this.dropzone, mockFile, v)
|
|
|
mockFile.previewElement.classList.add('dz-success')
|
|
|
mockFile.previewElement.classList.add('dz-complete')
|
|
|
- }
|
|
|
+ return true
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ // const fileUrl = `${process.env.VUE_APP_BASE_API}/api/files/view-file?path=${val}`
|
|
|
+ // const mockFile = { url: fileUrl }
|
|
|
+ const mockFile = { url: val }
|
|
|
+ this.dropzone.options.addedfile.call(this.dropzone, mockFile)
|
|
|
+ this.dropzone.options.thumbnail.call(this.dropzone, mockFile, val)
|
|
|
+ mockFile.previewElement.classList.add('dz-success')
|
|
|
+ mockFile.previewElement.classList.add('dz-complete')
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
+}
|
|
|
</script>
|
|
|
|
|
|
<style scoped>
|