123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <template>
- <view style="width: 100%;height: 100%;">
- <view class="upload-wrap">
- <view class="btn-click mgb-16 upload-btn" @click="handleUploadClick()" v-show="isUpload === false">
- <!-- <image :src="icons.upload" mode="aspectFill" class="upload-icon" /> -->
- <i class="el-icon-upload2"></i>
- <text class="upload-text">上传附件</text>
- <text class="upload-text1">{{ `只能上传不超过${this.limit}个文件` }}</text>
- </view>
- <view class="mgb-16 file-wrap" v-for="(item, index) in fileList" :key="index">
- <view class="btn-click file-line" @click="handlePreview(item)">
- <!-- <view class="btn-click file-line" @click="handleUploadFile(item)"> -->
- <view class="file-info">
- <i :class="`el-icon-${icons[getFileType(item.name) || 'file']}`"></i>
- <text class="file-name">{{ item.name || title[type] }}</text>
- </view>
- <i :class='`el-icon-${icons.close}`' v-if="isDelete === false"
- @click.stop="handleDeleteFile(index)"></i>
- </view>
- </view>
- <view class="mgb-16 file-wrap" v-if="fileList.length === 0">
- <view class="file-line">
- <text class="file-empty">暂无数据</text>
- </view>
- </view>
- </view>
- <xe-upload ref="XeUpload" :options="{ url: uploadUrl }" @callback="handleUploadCallback"></xe-upload>
- <u-action-sheet @close="uploadShow = false" cancelText="取消" :actions="uploadList"
- @select="handleChangeUploadType" :title="uploadTitle" :show="uploadShow"></u-action-sheet>
- <mumu-previewOffce :fileType="prviewFileType" :fileUrl='prviewFileUrl'
- v-model='showPreview'></mumu-previewOffce>
- <u-popup v-if="videoShow" @close="handleCloseVideo" mode="top" :overlay="true" :show="videoShow"
- :closeOnClickOverlay="true">
- <view v-if="videoShow" style="width: 100%;height: 100%;">
- <video v-if="videoShow" style="width: 100%;" :src="videoUrl" controls preload="auto" autoPlay={true} />
- </view>
- </u-popup>
- </view>
- </template>
- <script>
- import MumuPreviewOffce from '@/uni_modules/mumu-previewOffce/components/mumu-previewOffce/mumu-previewOffce.vue'
- import { mapState, mapMutations, mapActions } from 'vuex'
- import OSSService from "@/api/sys/OSSService"
- export default {
- components: {
- MumuPreviewOffce
- },
- computed: mapState({
- userInfo: (state) => state.user.userInfo,
- }),
- created() {
- this.ossService = new OSSService()
- },
- props: {
- //上传地址
- uploadUrl: {
- type: String,
- default: ''
- },
- // 文件列表
- fileList: {
- type: Array,
- default: () => []
- },
- // 最大上传个数
- limit: {
- type: Number,
- default: 9
- },
- // 是否可以删除
- isDelete: {
- type: Boolean,
- default: false
- },
- // 是否可以上传
- isUpload: {
- type: Boolean,
- default: false
- },
- },
- ossService: null,
- data() {
- return {
- uploadType: '',
- videoShow: false,
- videoUrl: '',
- prviewFileType: '',
- prviewFileUrl: '',
- showPreview: false,
- icons: {
- close: 'circle-close',
- image: 'picture-outline',
- video: 'video-camera',
- file: 'document',
- audio: 'bell',
- unknown: 'document'
- },
- uploadFile: '',
- uploadShow: false,
- uploadTitle: '附件上传',
- uploadList: [
- {
- name: '视频上传',
- },
- {
- name: '图片上传',
- },
- {
- name: '文件上传',
- }
- ],
- }
- },
- methods: {
- handleCloseVideo() {
- this.videoUrl = ""
- this.videoShow = false
- },
- // 预览
- handlePreview(row) {
- const fileType = this.getFileType(row.name);
- const flieArr = row.name.split('.');
- let suffix = flieArr[flieArr.length - 1];
- this.ossService.getTemporaryUrl("/" + row.url).then((data) => {
- if (fileType == "image") {
- return uni.previewImage({
- current: data,
- urls: [data],
- });
- }
- if (fileType == "video") {
- this.videoUrl = data
- this.videoShow = true
- return
- }
- if (fileType == "file") {
- this.prviewFileUrl = data
- this.prviewFileType = suffix
- this.showPreview = true
- return
- }
- uni.showModal({
- title: '提示',
- content: data,
- showCancel: false,
- });
- })
- },
- handleDeleteFile(index) {
- console.log();
- const deletedFile = this.fileList[index]; // 获取被删除的文件
- // this.fileList.splice(index, 1); // 删除文件
- this.$emit("onRemove", deletedFile, this.fileList, index); // 传递剩余文件列表和被删除的文件
- },
- getFileType(fileName = '') {
- const flieArr = fileName.split('.');
- let suffix = flieArr[flieArr.length - 1];
- if (!suffix) return '';
- suffix = suffix.toLocaleLowerCase();
- const image = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp'];
- if (image.includes(suffix)) return 'image';
- const video = ['mp4', 'm4v'];
- if (video.includes(suffix)) return 'video';
- const audio = ['mp3', 'm4a', 'wav', 'aac'];
- if (audio.includes(suffix)) return 'audio';
- const office = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'plain'];
- if (office.includes(suffix)) return 'file';
- return 'unknown';
- },
- handleUploadClick() {
- if (this.fileList.length >= this.limit) {
- uni.showModal({
- title: '提示',
- content: `只能上传不超过${this.limit}个文件`,
- showCancel: false,
- });
- return
- }
- this.uploadShow = true
- this.uploadType = 'type'
- },
- handleChangeUploadType(index) {
- if (index.name == "视频上传") {
- this.$refs.XeUpload.upload('video', {});
- }
- if (index.name == "图片上传") {
- this.$refs.XeUpload.upload('image', {});
- }
- if (index.name == "文件上传") {
- this.$refs.XeUpload.upload('file', {});
- }
- },
- handleUploadCallback(e) {
- this.uploadShow = false
- const file = e.data[0]
- this.fileList.push({
- name: file.name,
- size: file.size,
- url: file.response.url,
- lsUrl: file.response.lsUrl,
- createBy: this.userInfo,
- by: this.userInfo.id,
- createTime: this.formatDateNew(new Date())
- })
- console.log(this.fileList);
- this.$emit("onSuccess", file, this.fileList)
- },
- formatDateNew(date) {
- const year = date.getFullYear();
- const month = (date.getMonth() + 1).toString().padStart(2, '0');
- const day = date.getDate().toString().padStart(2, '0');
- const hours = date.getHours().toString().padStart(2, '0');
- const minutes = date.getMinutes().toString().padStart(2, '0');
- const seconds = date.getSeconds().toString().padStart(2, '0');
- return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
- },
- }
- }
- </script>
- <style scoped lang="scss">
- .btn-click {
- transition: all 0.3s;
- opacity: 1;
- }
- .btn-click:active {
- opacity: 0.5;
- }
- .mgb-16 {
- margin-bottom: 16rpx;
- &:last-child {
- margin-bottom: 0;
- }
- }
- .upload-wrap {
- width: 100%;
- border-radius: 16rpx;
- background: white;
- .upload-btn {
- width: 100%;
- height: 176rpx;
- border: 2rpx dashed;
- color: #409EFF;
- border-color: #b3d8ff;
- background: #ecf5ff;
- border-radius: 16rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- flex-direction: column;
- .upload-icon {
- width: 48rpx;
- height: 48rpx;
- margin-bottom: 8rpx;
- }
- .upload-text {
- font-size: 26rpx;
- line-height: 40rpx;
- }
- .upload-text1 {
- font-size: 20rpx;
- line-height: 40rpx;
- }
- }
- .file-wrap {
- .file-line {
- width: 100%;
- border-color: #b3d8ff;
- background: #ecf5ff;
- border-radius: 8rpx;
- padding: 16rpx;
- font-size: 26rpx;
- color: #409EFF;
- line-height: 40rpx;
- display: flex;
- align-items: center;
- justify-content: space-between;
- .file-info {
- width: 90%;
- display: flex;
- align-items: center;
- .file-name {
- max-width: 80%;
- padding-left: 16rpx;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- }
- .file-icon {
- width: 40rpx;
- height: 40rpx;
- flex-shrink: 0;
- }
- .file-empty {
- color: #999999;
- }
- }
- }
- }
- </style>
|