<template>
<view class="u-upload" v-if="!disabled">
<l-file ref="lFile" @up-success="upSuccess"></l-file>
<view v-for="(item, index) in lists" :key="index" class="item-file">
<view v-if="item.type==='file'" class=" file-box" :style="{
width: width / 2 + 'rpx',
height: height + 'rpx'
}"
@click="showPdf(item)">
<image class="file-img" :src='fileImgSrc' />
<view class="file-text"><text>{{item.name}}</text></view>
<view v-if="deletable" class="u-delete-icon" @tap.stop="deleteFile(index)" :style="{
background: delBgColor
}">
<image class="close-img" :src="closeSrc"></image>
</view>
</view>
<view v-else class="u-list-item u-preview-wrap" :style="{
width: width + 'rpx',
height: height + 'rpx'
}">
<view v-if="deletable" class="u-delete-icon" @tap.stop="deleteItem(index)" :style="{
background: delBgColor
}">
<image class="close-img" :src="closeSrc"></image>
</view>
<u-line-progress v-if="showProgress && item.progress > 0 && !item.error && circleOrLine" :show-percent="true"
height="8" class="u-progress" :percent="item.progress"></u-line-progress>
<u-circle-progress v-if="showProgress && item.progress > 0 && !item.error && !circleOrLine&&item.succeedCloseCircle"
:active-color="activeColor" :percent="item.progress" :width='progressWidth' class="u-progress" :border-width='borderWidth'
bg-color='initial' :inactive-color='inactivecColor'>
<view class="u-progress-content">
<view class="u-progress-dot">{{item.progress}}%</view>
</view>
<view class="uploading">Uploading</view>
</u-circle-progress>
<view v-if="item.video && !item.error" @tap.stop='handelPaly(item)' class="u-video-img-box">
<image class="u-video-img" :src="videoPlaySrc" mode=""></image>
</view>
<view v-if="item.error" @tap.stop="retry(index)" class="u-error-btn">
<image class="u-error-btn-img" :src='errorImgSrc' />
<view>{{$i18nMsg.base.uploadModule.clickuploadAgain}}</view>
</view>
<image v-if="item.photo" @tap.stop="doPreviewImage(item.url || item.path, index)" class="u-preview-image" :src="item.url || item.path"
:mode="imageMode"></image>
</view>
</view>
<video ref='video' v-if="videoUrl" id="myVideo" class="u-preview-video" :src="videoUrl" :loop='ifFullScreen'
:autoplay='true' @fullscreenchange='fullscreenchange' :muted='!ifFullScreen'> </video>
<slot name="file" :file="lists"></slot>
<view class="upload__box" style="display: inline-block;" @tap="selectFile" v-if="maxCount > lists.length">
<slot name="addBtn"></slot>
<view v-if="!customBtn&&showAddFile" class="u-list-item u-add-wrap" hover-class="u-add-wrap__hover" hover-stay-time="150" :style="{
width: width + 'rpx',
height: height + 'rpx',
backgroundImage:'url('+(baseImg)+')'
}">
<u-icon v-if="!baseImg" name="plus" class="u-add-btn" size="20"></u-icon>
<view v-if="!baseImg" class="u-add-tips">{{ uploadText }}</view>
</view>
<view v-if="propsShowPanel">
<view v-show="showPanel" class="type__select__panel">
<view class="type__select__panel__item" @tap.stop="handelPanelItem(item.type)" v-for="(item,index) in uploadTypes"
:key="index">
<image class="type__select__panel__item__img" :src="photographSrc" v-if="item.type===0"></image>
<image class="type__select__panel__item__img" :src="shootSrc" v-if="item.type===1"></image>
<image class="type__select__panel__item__img" :src="photoSrc" v-if="item.type===2"></image>
<image class="type__select__panel__item__img" :src="fileSrc" v-if="item.type===3"></image>
<view class="type__select__panel__item__text">{{item.text}}</view>
</view>
</view>
</view>
</view>
</view>
</template>
<script>
import lFile from './l-file/l-file'
export default {
name: 'u-upload',
props: {
showUploadList: {
type: Boolean,
default: true
},
action: {
type: String,
default: ''
},
maxCount: {
type: [String, Number],
default: 52
},
showProgress: {
type: Boolean,
default: true
},
disabled: {
type: Boolean,
default: false
},
imageMode: {
type: String,
default: 'aspectFill'
},
header: {
type: Object,
default () {
return {};
}
},
formData: {
type: Object,
default () {
return {
type: 0
}
}
},
name: {
type: String,
default: 'file'
},
sizeType: {
type: Array,
default () {
return ['original', 'compressed'];
}
},
sourceType: {
type: Array,
default () {
return ['album', 'camera'];
}
},
previewFullImage: {
type: Boolean,
default: true
},
multiple: {
type: Boolean,
default: true
},
deletable: {
type: Boolean,
default: true
},
maxSize: {
type: [String, Number],
default: Number.MAX_VALUE
},
fileList: {
type: Array,
default () {
return [];
}
},
uploadText: {
type: String,
default: '选择图片'
},
autoUpload: {
type: Boolean,
default: true
},
showTips: {
type: Boolean,
default: true
},
customBtn: {
type: Boolean,
default: false
},
width: {
type: [String, Number],
default: 82.41
},
height: {
type: [String, Number],
default: 64.81
},
delBgColor: {
type: String,
default: '#000'
},
delColor: {
type: String,
default: '#ffffff'
},
delIcon: {
type: String,
default: 'close'
},
toJson: {
type: Boolean,
default: true
},
beforeUpload: {
type: Function,
default: null
},
progressWidth: {
type: [Number, String],
default: '30'
},
borderWidth: {
type: [Number, String],
default: '2'
},
activeColor: {
type: String,
default: '#2979ff',
},
inactivecColor: {
type: String,
default: '#fff'
},
percent: {
type: [Number, String],
default: 0
},
circleOrLine: {
type: Boolean,
default: false
},
baseImg: {
type: String,
default: '/static/remote-assist-module/images/fileadd.png'
},
errorImgSrc: {
type: String,
default: ''
},
videoPlaySrc: {
type: String,
default: '/static/remote-assist-module/images/playVideo.png'
},
photographSrc:{
type: String,
default: '/static/remote-assist-module/images/photographSrc.png'
},
closeSrc: {
type: String,
default: '/static/remote-assist-module/images/close1.png'
},
propsShowPanel: {
type: Boolean,
default: true
},
fileImgSrc: {
type: String,
default:'/static/remote-assist-module/images/fileImgSrc.png'
},
shootSrc: {
type: String,
default:'/static/remote-assist-module/images/shootSrc.png'
},
photoSrc: {
type: String,
default: '/static/remote-assist-module/images/photoSrc.png'
},
fileSrc: {
type: String,
default:'/static/remote-assist-module/images/fileSrc.png'
},
value:{
type: Array,
default () {
return []
}
},
showAddFile:{
type:Boolean,
default:true
},
uploadTypes: {
type: Array,
default () {
return [{
type:0,
text: this.$i18nMsg.base.uploadModule.takePictures
},
{
type: 1,
text: this.$i18nMsg.base.uploadModule.shoot
},
{
type:2,
text:this.$i18nMsg.base.uploadModule.album
},
{
type: 3,
text: this.$i18nMsg.base.uploadModule.file
}
];
}
}
},
components: {
lFile
},
data() {
return {
lists:this.value,
fileLists: [],
isInCount: true,
uploading: false,
type: 'text',
border: true,
showPanel: false,
videoSrc: [],
videoUrl: '',
videoContext: '',
ifFullScreen: false,
data: {},
succeedCloseCircle: true
};
},
mounted() {
this.data = JSON.parse(JSON.stringify(this.formData))
},
watch: {
fileList: {
immediate: true,
handler(val) {
}
},
value:{
immediate: true,
handler(val) {
this.lists = val
}
},
lists(n) {
this.$emit('on-list-change', n);
}
},
methods: {
clear() {
this.lists = [];
},
reUpload() {
this.uploadFile();
},
showPdf(item) {
this.$emit("showPdf", item)
},
selectFile(e) {
if (this.disabled) return;
this.showPanel = !this.showPanel
},
handelPanelItem(item) {
for(let item of this.lists){
if(item.succeedCloseCircle){
this.showToast(this.$i18nMsg.base.uploadModule.waitSuccess);
return
}
}
let value="camera"
let type="chooseImage"
if(item===0){
value="camera"
type="chooseImage"
}
if(item===1){
value="camera"
type="chooseVideo"
}
if(item===2){
value="album"
type="pick"
}
if(item===3){
value="album"
type="files"
}
const {
name = '', maxCount, multiple, maxSize, sizeType, lists, camera, compressed, maxDuration, sourceType
} = this;
if (type === 'chooseImage' || type === 'chooseVideo') {
let chooseFile = null;
const newMaxCount = maxCount - lists.length;
chooseFile = new Promise((resolve, reject) => {
uni[type]({
count: multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
sourceType: [value],
sizeType,
success: resolve,
fail: reject
});
});
chooseFile
.then(res => {
let file = null;
let listOldLength = this.lists.length;
if (type === 'chooseVideo') {
this.data.type = 1
res.tempFiles = [{
path: res.tempFilePath,
video: true
}]
}
res.tempFiles.map((val, index) => {
if (!multiple && index >= 1) return;
if (val.size > maxSize) {
this.$emit('on-oversize', val, this.lists);
this.showToast(this.$i18nMsg.base.uploadModule.exceedSize);
} else {
if (maxCount <= lists.length) {
this.$emit('on-exceed', val, this.lists);
this.showToast(this.$i18nMsg.base.uploadModule.exceedFile);
return;
}
lists.push({
url: val.path,
progress: 0,
succeedCloseCircle:false,
task:null,
error: false,
photo:val.video ? false : true,
video: val.video ? true : false
});
}
});
this.$emit('on-choose-complete', this.lists);
this.showPanel = false
if (this.autoUpload) this.uploadFile(listOldLength);
})
.catch(error => {
this.$emit('on-error', error);
});
} else if (type === 'pick') {
plus.gallery.pick(function(e) {
let type2 = 0;
let length = e.length;
for (let i = 0; i < length; i++) {
let url = e[i];
let arr = url.split(".");
let s = arr[arr.length - 1];
switch (s) {
case "png":
type2 = 0;
break;
case "jpg":
type2 = 0;
break;
case "jpeg":
type2 = 0;
break;
case "mp4":
type2 = 1;
break;
case "wma":
type2 = 1;
break;
default:
type2 = 0;
}
}
let listOldLength = this.lists.length;
if (type2 === 0) {
lists.push({
url: e,
progress: 0,
succeedCloseCircle:false,
error: false,
photo:true,
task:null,
video: false
});
this.$emit('on-choose-complete', this.lists);
} else if (type2 === 1) {
this.data.type = 1
lists.push({
url: e,
photo:false,
progress: 0,
task:null,
succeedCloseCircle:false,
error: false,
video: true
});
this.$emit('on-choose-complete', this.lists);
}
if (this.autoUpload) this.uploadFile(listOldLength);
}.bind(this))
} else if (type === 'files') {
this.data.type = 2
this.$refs.lFile.upload({
currentWebview: this.$mp.page.$getAppWebview(),
url: this.action,
name: "file",
header: this.header,
type: 2,
});
}
},
upSuccess(value) {
try {
let fileName = value.fileName;
let fileObj = JSON.parse(value.data.id);
let filePath = fileObj.path;
let attachmentId = fileObj.attachmentId;
let fileSize = fileObj.fileSize;
this.lists.push({
url: fileObj.path,
progress: 100,
succeedCloseCircle:false,
task:null,
error: false,
video: false,
photo:false,
name: value.fileName,
fileSize,
type: "file"
});
this.$emit('on-success', this.lists);
} catch (e) {
console.log("pdf上传异常", e);
}
},
showToast(message, force = false) {
if (this.showTips || force) {
uni.showToast({
title: message,
icon: 'none',
duration:3000
});
}
},
async upVideo() {
},
upload() {
this.uploadFile();
},
retry(index) {
this.lists[index].progress = 0;
this.lists[index].error = false;
this.lists[index].succeedCloseCircle=false;
this.lists[index].response = null;
this.lists[index].task = null;
uni.showLoading({
title: this.$i18nMsg.base.uploadModule.uploadAgain
});
this.uploadFile(index);
},
async uploadFile(index = 0) {
if (this.disabled) return;
if (this.uploading) return;
if (index >= this.lists.length) {
this.$emit('on-uploaded', this.lists);
return;
}
if (!this.action) {
this.showToast('请配置上传地址', true);
return;
}
if (this.lists[index].progress == 100) {
if (this.autoUpload == false) this.uploadFile(index + 1);
return;
}
if (this.beforeUpload && typeof(this.beforeUpload) === 'function') {
let beforeResponse = this.beforeUpload(index, this.lists);
if (!!beforeResponse && typeof beforeResponse.then === 'function') {
await beforeResponse.then(res => {
}).catch(err => {
return this.uploadFile(index + 1);
})
} else if (beforeResponse === false) {
return this.uploadFile(index + 1);
}
}
this.lists[index].error = false;
this.uploading = true;
this.lists[index].task = uni.uploadFile({
url: this.action,
filePath: this.lists[index].url,
name: this.name,
formData: this.data,
header: this.header,
success: res => {
console.log("上传成功啦")
let data = this.toJson && this.checkIsJSON(res.data) ? JSON.parse(res.data) : res.data;
if (![200, 201].includes(res.statusCode)) {
this.uploadError(index, data);
} else {
this.lists[index].url=JSON.parse(res.data).path
this.lists[index].response = data;
this.lists[index].error = false;
this.$emit('on-success', this.lists);
console.log(JSON.stringify(this.lists))
}
},
fail: e => {
this.uploading = false;
this.lists[index].task=null;
this.uploadError(index, e);
},
complete: res => {
uni.hideLoading();
this.lists[index].progress = 100;
this.lists[index].succeedCloseCircle=false;
this.lists[index].task=null;
console.log("上传完成啦")
this.uploading = false;
this.uploadFile(index + 1);
this.$emit('on-change', res, index, this.lists);
}
});
let time = new Date().getTime()
this.lists[index].task.onProgressUpdate(res => {
let nowTime = new Date().getTime()
if(nowTime - time >20){
time = new Date().getTime()
if (res.progress > 0 && res.progress < 100) {
this.lists[index].succeedCloseCircle=true;
this.lists[index].progress=res.progress
}
}
this.$emit('on-progress', res, index, this.lists);
});
},
uploadError(index, err) {
this.lists[index].progress = 0;
this.lists[index].error = true;
this.lists[index].response = null;
this.$emit('on-error', err, index, this.lists);
this.showToast(this.$i18nMsg.base.uploadModule.uploadFailed);
},
deleteItem(index) {
if(this.lists[index].task){
this.lists[index].task.abort();
}
if (this.lists[index].process < 100 && this.lists[index].process > 0) {
typeof this.lists[index].uploadTask != 'undefined' && this.lists[index].uploadTask.abort();
}
this.lists.splice(index, 1);
this.$forceUpdate();
this.$emit('on-remove', index, this.lists);
this.$emit('on-success', this.lists);
this.showToast(this.$i18nMsg.base.uploadModule.removeSuccess);
},
deleteFile(index) {
if(this.lists[index].task){
this.lists[index].task.abort();
}
this.lists.splice(index, 1);
this.$forceUpdate();
this.$emit('on-remove', index, this.lists);
this.$emit('on-success', this.lists);
this.showToast(this.$i18nMsg.base.uploadModule.removeSuccess);
},
closePanel(e){
this.showPanel = false
this.$emit("closePanel")
},
remove(index) {
if (index >= 0 && index < this.lists.length) {
this.lists.splice(index, 1);
this.$emit('on-list-change', this.lists);
}
},
doPreviewImage(url, index) {
if (!this.previewFullImage) return;
let images = []
for(let item of this.lists){
if(item.photo){
images.push(item.url||item.path)
}
}
uni.previewImage({
urls: images,
current: url,
success: () => {
this.$emit('on-preview', url, this.lists);
},
fail: () => {
uni.showToast({
title: this.$i18nMsg.base.uploadModule.failedPicture,
icon: 'none'
});
}
});
},
handelPaly(value) {
let system = uni.getSystemInfoSync().platform
if (system == 'ios') {
uni.requireNativePlugin("autel-common-module").videoPlay(value.url)
} else {
this.videoUrl = value.url
setTimeout(() => {
this.videoContext = uni.createVideoContext('myVideo')
this.videoContext.requestFullScreen()
}, 100)
}
},
fullscreenchange(event) {
if (event.detail.direction === 'vertical') {
this.ifFullScreen = false
this.videoUrl = ''
} else if (event.detail.direction === 'horizontal') {
this.ifFullScreen = true
}
},
checkIsJSON(str) {
if (typeof str == 'string') {
try {
var obj = JSON.parse(str);
if (typeof obj == 'object' && obj) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
return false;
}
}
};
</script>
<style lang="scss" scoped>
.u-upload {
display: flex;
flex-wrap: wrap;
align-items: center;
max-width:420rpx;
.upload__box {
position: relative;
.type__select__panel {
position: absolute;
left: 0;
top: 105%;
z-index: 999;
height: 52.19rpx;
padding: 11rpx;
background: rgba(225, 225, 225, 1);
border-radius: 5px;
border: 1px solid rgba(167, 167, 167, 1);
box-shadow: 0px 3.84rpx 6.59rpx 0px rgba(0, 0, 0, 0.3);
display: flex;
font-size: 9.89rpx;
align-items: center;
justify-content: center;
&::after {
content: '';
position: absolute;
left: 20rpx;
top: -10rpx;
border-right: 8rpx solid transparent;
border-left: 8rpx solid transparent;
border-bottom: 12rpx solid rgba(225, 225, 225, 1);
}
&::before {
content: '';
position: absolute;
left: 20rpx;
top: -12rpx;
border-right: 8rpx solid transparent;
border-left: 8rpx solid transparent;
border-bottom: 11rpx solid rgba(167, 167, 167, 1);
}
&__item {
// width: 46.15rpx;
white-space: nowrap;
padding: 0 6rpx;
overflow: hidden;
text-overflow: ellipsis;
height: 27.47rpx;
background: linear-gradient(180deg, rgba(247, 247, 247, 1) 0%, rgba(184, 184, 184, 1) 100%);
box-shadow: 0px 1px 0px 0px rgba(255, 255, 255, 1);
border-left: 1px solid rgba(177, 177, 177, 1);
border-top: 1px solid rgba(177, 177, 177, 1);
border-bottom: 1px solid rgba(177, 177, 177, 1);
display: flex;
justify-content: center;
align-items: center;
&__img {
width: 13.18rpx;
height: 13.18rpx;
}
&__text {
font-size: 9.89rpx;
margin-left: 3.29rpx;
}
}
&__item:last-child{
border-right: 1px solid rgba(177, 177, 177, 1);
}
&__item:active {
background:linear-gradient(180deg,rgba(162,162,162,1) 0%,rgba(239,239,239,1) 100%);
box-shadow:0px 0.55rpx 0px 0px rgba(255,255,255,1),0px 3.02rpx 4.4rpx 0px rgba(0,0,0,0.35),0px -1.65rpx -1.65rpx 0px rgba(0,0,0,0.17);
border-image:linear-gradient(180deg, rgba(96,96,96,1), rgba(142,142,142,1)) 2 2;
}
}
}
}
.item-file{
margin-right: (80rpx/3.64);
}
.u-list-item {
position: relative;
border-radius: 5rpx;
display: inline-flex;
align-items: center;
justify-content: center;
background-size: 82.41rpx 64.81rpx;
width: (300rpx/3.64);
height: (225rpx/3.64);
}
.file-box {
height: 64.81rpx;
width: 41rpx;
margin: 5rpx;
background: rgb(244, 245, 246);
position: relative;
border-radius: 5rpx;
display: inline-flex;
align-items: center;
justify-content: center;
}
.file-img {
height: 64.81rpx;
width: 41rpx;
}
.file-text {
font-size: 10px;
position: absolute;
bottom: 10px;
left: 5px;
right: 5px;
color: #ffffff;
uni-text {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
word-break: break-all;
}
}
.u-preview-wrap {
border: 1px solid rgb(235, 236, 238);
}
.u-add-wrap {
flex-direction: column;
color: #606266;
font-size: 14rpx;
}
.u-add-tips {
margin-top: 10rpx;
line-height: 20rpx;
font-size: 12px;
}
.u-add-wrap__hover {
background-color: rgb(235, 236, 238);
}
.u-preview-image {
display: block;
width: 100%;
height: 100%;
border-radius: 5rpx;
}
.u-preview-video {
width: 0;
height: 0;
z-index: 0;
display: none;
}
.u-delete-icon {
position: absolute;
top: -4rpx;
right: -4rpx;
z-index: 100;
background-color:#fa3534;
border-radius: 50rpx;
width: 13.73rpx;
height: 13.73rpx;
display: flex;
align-items: center;
justify-content: center;
}
.u-icon {
display: flex;
align-items: center;
justify-content: center;
}
.close-img {
width: 13.73rpx;
height: 13.73rpx;
z-index: 99;
}
.u-progress {
position: absolute;
// bottom: 5rpx;
// left: 4rpx;
// right: 4rpx;
z-index: 99;
width: auto;
.u-progress-content {
height: 90%;
width: 90%;
border-radius: 50%;
background: rgba(255, 255, 255, 0.4);
display: flex;
justify-content: center;
align-items: center;
color: #000000;
font-size: 16px;
z-index: 999;
.u-progress-dot {
font-size: 10px;
}
}
.uploading {
position: absolute;
top: 100%;
font-size: 12px;
color: #fff;
}
}
.u-video-img-box {
width: 100%;
height: 100%;
position: absolute;
// z-index: 1;
.u-video-img {
width:100%;
height:100%;
}
}
.u-error-btn {
color: #ffffff;
font-size: 10rpx;
padding: 4px 0;
text-align: center;
position: absolute;
// top: 50%;
left: 0;
right: 0;
z-index: 9;
font-size: 8.24rpx;
line-height: 1;
.u-error-btn-img {
width: 16.48rpx;
height: 16.48rpx;
margin-bottom: 5.49rpx;
}
}
</style>