|
@@ -0,0 +1,129 @@
|
|
|
+<template>
|
|
|
+ <div>
|
|
|
+ <el-upload
|
|
|
+ :action="oss_url"
|
|
|
+ :data="dataObj"
|
|
|
+ :multiple="false"
|
|
|
+ :show-file-list="isShowFileList"
|
|
|
+ :file-list="fileList"
|
|
|
+ :before-upload="beforeUpload"
|
|
|
+ :on-remove="handleRemove"
|
|
|
+ :on-success="handleUploadSuccess"
|
|
|
+ >
|
|
|
+ <el-button size="small" type="primary">{{fileList.length == 0 ? '点击上传':'重新上传'}}</el-button>
|
|
|
+ </el-upload>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+<script>
|
|
|
+import { getOssConfig } from '@/api/common';
|
|
|
+
|
|
|
+export default {
|
|
|
+ name: 'fileUpload',
|
|
|
+ props: {
|
|
|
+ fileList: Array,
|
|
|
+ fileType: {
|
|
|
+ type: Array,
|
|
|
+ default: () => ['image', 'video', 'word', 'excel', 'ppt', 'pdf']
|
|
|
+ }
|
|
|
+ },
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ oss_url: '',
|
|
|
+ dataObj: {},
|
|
|
+ aliosstoken: '',
|
|
|
+ };
|
|
|
+ },
|
|
|
+ created() {
|
|
|
+ getOssConfig().then(res => {
|
|
|
+ this.oss_url = res.data.host;
|
|
|
+ this.dataObj = res.data;
|
|
|
+ })
|
|
|
+ },
|
|
|
+ computed: {
|
|
|
+ isShowFileList: {
|
|
|
+ get: function() {
|
|
|
+ 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) {
|
|
|
+ const date = Date.now();
|
|
|
+ const uuid = this.getUUID();
|
|
|
+ const fileSuffix = name.substring(name.lastIndexOf(".") + 1);
|
|
|
+ return `${date}${uuid}.${fileSuffix}`;
|
|
|
+ },
|
|
|
+ handleRemove(file, fileList) {
|
|
|
+ // this.fileList = [{ name: '', url: '' }];
|
|
|
+ this.fileList.pop();
|
|
|
+ },
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (whiteList.indexOf(fileSuffix) === -1) {
|
|
|
+ return this.$errorMsg('只支持上传' + this.fileType.join(',') + '文件!');
|
|
|
+ }
|
|
|
+ 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)'
|
|
|
+ });
|
|
|
+ this.fileList.pop();
|
|
|
+ this.fileList.push({
|
|
|
+ name: file.name,
|
|
|
+ url: this.dataObj.key
|
|
|
+ });
|
|
|
+ this.showFileList = true;
|
|
|
+ loading.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+</script>
|