123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- <template>
- <div>
- <div class="file-area">
- <span class="label-text">{{ labelText }}</span>
- <div class="upload-box">
- <el-upload
- ref="upload"
- class="file-uploader"
- action="no"
- :accept="acceptFileType"
- :multiple="limit === null || limit > 1"
- :show-file-list="false"
- :auto-upload="false"
- :file-list="fileList"
- :on-change="onFileChange"
- :on-exceed="handleExceed"
- :limit="limit"
- >
- <el-button>选取{{ labelText }}文件</el-button>
- </el-upload>
- <el-button size="small" type="primary" @click="uploadFiles">上传</el-button>
- </div>
- </div>
- <el-divider />
- <div class="upload-tip">{{ uploadTip }}</div>
- <ul v-if="fileList.length > 0" slot="file-list" class="file-list">
- <li v-for="(file, i) in fileList" :key="i">
- <div class="file-name">
- <span>
- <SvgIcon :icon-class="iconClass" size="12" />
- <span>{{ file.file_name ?? file.name }}</span>
- <!-- <span>({{ file.size }})</span> -->
- </span>
- <span v-if="file.progress > 0 && file.progress < 100"> {{ file.progress }}% </span>
- <span v-else-if="file.file_id"> 完成 </span>
- </div>
- <SvgIcon icon-class="delete-black" size="12" @click="removeFile(file, i)" />
- <SvgIcon
- v-show="type === 'picture' && file.file_id"
- icon-class="mark"
- size="12"
- @click="viewDialog(file.file_id)"
- />
- </li>
- </ul>
- <FillDescribe :file-data="curFile" :visible.sync="visible" @fillDescribeToFile="fillDescribeToFile" />
- </div>
- </template>
- <script>
- import { fileUpload } from '@/api/app';
- import { conversionSize } from '@/utils/common';
- import FillDescribe from '../../common/FillDescribe.vue';
- export default {
- name: 'UploadFile',
- components: {
- FillDescribe,
- },
- inject: ['property'],
- props: {
- // 课件id
- coursewareId: {
- type: String,
- default: '',
- },
- // 组件id
- componentId: {
- type: String,
- default: '',
- },
- // 组件标签
- labelText: {
- type: String,
- default: '',
- },
- // 上传支持的文件格式
- acceptFileType: {
- type: String,
- default: '',
- },
- // 提示语
- uploadTip: {
- type: String,
- default: '',
- },
- // 图标
- iconClass: {
- type: String,
- default: '',
- },
- fileList: {
- type: Array,
- default: () => [],
- },
- fileIdList: {
- type: Array,
- default: () => [],
- },
- fileInfoList: {
- type: Array,
- default: () => [],
- },
- type: {
- type: String,
- default: '',
- },
- singleSize: {
- type: Number,
- default: 100,
- },
- totalSize: {
- type: Number,
- default: 1024,
- },
- limit: {
- type: Number,
- default: null,
- },
- },
- data() {
- return {
- curFile: null,
- conversionSize,
- visible: false,
- content: {
- file_list: this.fileList,
- file_id_list: this.fileIdList,
- file_info_list: this.fileInfoList,
- },
- };
- },
- watch: {
- content: {
- handler(val) {
- this.$emit('updateFileList', val);
- },
- deep: true,
- },
- property: {
- handler(val) {
- if (val.isGetContent) {
- this.content = {
- file_list: this.fileList,
- file_id_list: this.fileIdList,
- file_info_list: this.fileInfoList,
- };
- }
- },
- deep: true,
- immediate: true,
- },
- },
- methods: {
- // 显示自定义样式文件列表
- onFileChange(file, fileList) {
- this.afterSelectFile(file);
- fileList.forEach((file) => {
- if (!file.progress || file.progress <= 0) file.progress = 0;
- });
- const lists = this.$refs.upload.uploadFiles;
- if (lists.length === 0) return;
- const files = lists.filter((item) => {
- let find = this.content.file_list.findIndex((p) => p.uid === item.uid);
- return find === -1;
- });
- if (this.limit !== null && this.content.file_list.length + files.length > this.limit) {
- this.$message.warning(
- `当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + this.content.file_list.length} 个文件`,
- );
- return;
- }
- this.content.file_list = [...this.content.file_list, ...files];
- },
- handleExceed(files, fileList) {
- this.$message.warning(
- `当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`,
- );
- },
- // 删除文件
- removeFile(file, i) {
- this.$confirm('是否删除当前文件?', '提示', {
- confirmButtonText: '确定',
- cancelButtonText: '取消',
- type: 'warning',
- })
- .then(() => {
- this.$refs.upload.handleRemove(file);
- this.content.file_list.splice(i, 1);
- this.content.file_id_list.splice(i, 1);
- })
- .catch(() => {});
- },
- // 文件校验
- afterSelectFile(file) {
- const fileName = file.name;
- let singleSizeTip = `文件[${fileName}]大小超过${conversionSize(this.singleSize * 1024 * 1024)},被移除!`;
- if (file.size > this.singleSize * 1024 * 1024) {
- this.$message.error(singleSizeTip);
- this.$refs.upload.handleRemove(file);
- return false;
- }
- const suffix = fileName.slice(fileName.lastIndexOf('.') + 1, fileName.length).toLowerCase();
- let fileType = [];
- let typeTip = '';
- if (this.type === 'audio') {
- fileType = ['mp3', 'acc', 'wma'];
- typeTip = '音频文件只能是 mp3、acc、wma 格式!';
- } else if (this.type === 'picture') {
- fileType = ['jpg', 'png', 'jpeg'];
- typeTip = '图片文件只能是 jpg、png、jpeg 格式!';
- } else if (this.type === 'video') {
- fileType = ['mp4'];
- typeTip = '视频文件只能是 mp4 格式!';
- } else if (this.type === 'upload_preview') {
- fileType = ['png', 'jpg', 'pdf'];
- typeTip = '文件只能是图片或者pdf';
- }
- const isNeedType = fileType.includes(suffix);
- if (!isNeedType) {
- typeTip += `,[${fileName}]被移除!`;
- this.$message.error(typeTip);
- this.$refs.upload.handleRemove(file);
- return false;
- }
- },
- // 上传文件
- uploadFiles() {
- const files = (this.content.file_list || []).filter((file) => file.uid && file.status !== 'success');
- if (files.length <= 0) {
- this.$message.error('没有需要上传的文件!');
- return false;
- }
- const totalSize = files.reduce((sum, cur) => sum + Number(cur.size || 0), 0);
- if (totalSize > this.totalSize * 1024 * 1024) {
- this.$message.error(`文件总大小不能超过${conversionSize(this.totalSize * 1024 * 1024)}!`);
- return false;
- }
- files
- .filter((p) => {
- let pro = p.progress || -1;
- return pro <= 0;
- })
- .forEach((file) => {
- let form = new FormData();
- form.append(file.name, file.raw, file.name);
- fileUpload('Mid', form, {
- handleUploadProgress: (progressEvent) => {
- // 进度到99等服务器返回文件信息后才算实际完成
- let per = Number((progressEvent.progress * 99).toFixed(2) || 0);
- let en = this.content.file_list.find((p) => p.uid === file.uid);
- if (en) {
- en.progress = per;
- this.$forceUpdate();
- }
- },
- }).then(({ file_info_list }) => {
- let file_index = this.content.file_list.findIndex((p) => p.uid === file.uid);
- if (file_index > -1) {
- if (this.type === 'picture') {
- this.content.file_info_list[file_index] = {
- file_id: file_info_list[0].file_id,
- file_name: file_info_list[0].file_name,
- title: '',
- intro: '',
- };
- }
- this.content.file_list[file_index] = {
- file_id: file_info_list[0].file_id,
- file_name: file_info_list[0].file_name,
- file_url: file_info_list[0].file_url,
- };
- this.content.file_id_list.push(file_info_list[0].file_id);
- this.$refs.upload.uploadFiles = [];
- this.$forceUpdate();
- }
- });
- });
- },
- // 显示弹窗
- viewDialog(file_id) {
- if (file_id) this.visible = true;
- this.curFile = this.content.file_info_list.find((file) => file.file_id === file_id);
- },
- // 给文件加介绍
- fillDescribeToFile(file) {
- let en = this.content.file_info_list.find((p) => p.file_id === file.file_id);
- if (en) {
- Object.assign(en, file);
- }
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- .module-content {
- .file-area {
- display: flex;
- column-gap: 16px;
- align-items: center;
- .label-text {
- font-size: 14px;
- color: $font-light-color;
- }
- div {
- flex: 1;
- }
- }
- .el-divider {
- margin: 16px 0;
- }
- .upload-tip {
- margin-bottom: 16px;
- font-size: 12px;
- color: #86909c;
- }
- .upload-box {
- display: flex;
- justify-content: space-between;
- .file-uploader {
- flex: 1;
- :deep .el-upload {
- &--text {
- width: 100%;
- background-color: $fill-color;
- border-radius: 2px 0 0 2px;
- .el-button {
- width: 100%;
- color: #86909c;
- text-align: left;
- }
- }
- }
- }
- .el-button {
- border-radius: 0 2px 2px 0;
- }
- }
- .old_file_list {
- margin-top: 16px;
- }
- .file-list {
- display: flex;
- flex-direction: column;
- row-gap: 16px;
- li {
- display: flex;
- column-gap: 12px;
- align-items: center;
- .file-name {
- display: flex;
- column-gap: 14px;
- align-items: center;
- justify-content: space-between;
- max-width: 360px;
- padding: 8px 12px;
- font-size: 14px;
- color: #1d2129;
- background-color: #f7f8fa;
- span {
- display: flex;
- column-gap: 14px;
- align-items: center;
- }
- }
- .svg-icon {
- cursor: pointer;
- }
- }
- }
- }
- </style>
|