123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <template>
- <div>
- <el-upload
- :action="oss_url"
- :data="dataObj"
- :multiple="multiple"
- :show-file-list="isShowFileList"
- :file-list="fileList"
- :before-upload="beforeUpload"
- :on-remove="handleRemove"
- :on-success="handleUploadSuccess"
- >
- <el-button size="small" type="primary">{{
- multiple ? '点击上传' : fileList.length == 0 ? '点击上传' : '重新上传'
- }}</el-button>
- </el-upload>
- </div>
- </template>
- <script>
- import { getOssConfig } from '@/api/common'
- export default {
- name: 'fileUpload',
- props: {
- fileList: Array,
- multiple: {
- type: Boolean,
- default: false
- },
- fileType: {
- type: Array,
- default: () => ['image', 'video', 'word', 'excel', 'ppt', 'pdf']
- }
- },
- data() {
- return {
- oss_url: '',
- dataObj: {},
- aliosstoken: '',
- flag: false
- }
- },
- created() {
- getOssConfig().then(res => {
- this.oss_url = res.data.host
- this.dataObj = res.data
- })
- },
- computed: {
- isShowFileList: {
- get: function () {
- if (!this.multiple) {
- if (this.fileList.length > 0 && this.fileList[0].url) {
- return true
- } else {
- return false
- }
- }
- },
- set: function (newValue) {}
- }
- },
- methods: {
- getUUID() {
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
- return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16)
- })
- },
- createName(name) {
- console.log(444)
- const date = Date.now()
- const uuid = this.getUUID()
- const fileSuffix = name.substring(name.lastIndexOf('.') + 1)
- return `${date}${uuid}.${fileSuffix}`
- },
- handleRemove(file, fileList) {
- if (!this.flag) {
- fileList.pop()
- this.fileList.pop()
- }
- this.flag = false
- // this.fileList = [{ name: '', url: '' }];
- },
- beforeUpload(file) {
- const fileSuffix = file.name.substring(file.name.lastIndexOf('.') + 1)
- const imgList = ['jpg', 'jpeg', 'png']
- const videoList = ['mp4']
- const wordList = ['doc', 'docx', 'dot', 'wps', 'wpt']
- const excelList = ['xls', 'xlsx', 'xlt', 'et', 'ett']
- const pptList = ['ppt', 'pptx', 'dps', 'dpt', 'pot', 'pps']
- const pdfList = ['pdf']
- let whiteList = []
- if (this.fileType.includes('image')) {
- whiteList = whiteList.concat(imgList)
- }
- if (this.fileType.includes('video')) {
- whiteList = whiteList.concat(videoList)
- }
- if (this.fileType.includes('word')) {
- whiteList = whiteList.concat(wordList)
- }
- if (this.fileType.includes('excel')) {
- whiteList = whiteList.concat(excelList)
- }
- if (this.fileType.includes('ppt')) {
- whiteList = whiteList.concat(pptList)
- }
- if (this.fileType.includes('pdf')) {
- whiteList = whiteList.concat(pdfList)
- }
- console.log(whiteList.indexOf(fileSuffix))
- if (whiteList.indexOf(fileSuffix) === -1) {
- this.$errorMsg('只支持上传' + this.fileType.join(',') + '文件!')
- this.flag = true
- return false
- } else {
- const fileName = this.createName(file.name)
- const loading = this.$loading({
- lock: true,
- text: 'Loading',
- spinner: 'el-icon-loading',
- background: 'rgba(0, 0, 0, 0.7)'
- })
- this.dataObj.key = this.dataObj.dir + fileName
- }
- },
- handleUploadSuccess(res, file, fileList) {
- const loading = this.$loading({
- lock: true,
- text: 'Loading',
- spinner: 'el-icon-loading',
- background: 'rgba(0, 0, 0, 0.7)'
- })
- if (!this.multiple) {
- this.fileList.pop()
- }
- console.log(4747)
- this.fileList.push({
- name: file.name,
- status: 'success',
- url: this.dataObj.key
- })
- this.showFileList = true
- loading.close()
- }
- }
- }
- </script>
|