<template>
<view class="u-upload" v-if="!disabled">
<view v-if="showUploadList" class="u-list-item u-preview-wrap" v-for="(item, index) in lists" :key="index" :style="{
width: width + 'rpx',
height: width + 'rpx',
backgroundImage:'url('+(baseImg)+') no-repeat'
}">
<view v-if="deletable" class="u-delete-icon" @tap.stop="deleteItem(index)" :style="{
background: delBgColor
}">
<u-icon class="u-icon" :name="delIcon" size="10" :color="delColor"></u-icon>
</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 && succeedCloseCircle"
:active-color="activeColor" :percent="percent" :width='progressWidth' class="u-progress" :border-width='borderWidth'
bg-color='initial' :inactive-color='inactivecColor'>
<view class="u-progress-content">
<view class="u-progress-dot">{{`${percent}%`}}</view>
</view>
<view class="uploading">Uploading</view>
</u-circle-progress>
<view @tap.stop="retry(index)" v-if="item.error" class="u-error-btn">点击重试</view>
<image @tap.stop="doPreviewImage(item.url || item.path, index)" class="u-preview-image" v-if="!item.isImage" :src="item.url || item.path"
:mode="imageMode"></image>
</view>
<slot name="file" :file="lists"></slot>
<view style="display: inline-block;" @tap="selectFile" v-if="maxCount > lists.length">
<slot name="addBtn"></slot>
<view v-if="!customBtn" class="u-list-item u-add-wrap" hover-class="u-add-wrap__hover" hover-stay-time="150" :style="{
width: width + 'rpx',
height: width + 'rpx'
}">
<u-icon name="plus" class="u-add-btn" size="20"></u-icon>
<view class="u-add-tips">{{ uploadText }}</view>
</view>
</view>
</view>
</template>
<script>
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 {};
}
},
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: 200
},
delBgColor: {
type: String,
default: '#fa3534'
},
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: '100'
},
borderWidth: {
type: [Number, String],
default: '5'
},
activeColor: {
type: String,
default: '#2979ff',
},
inactivecColor: {
type: String,
default: '#fff'
},
percent: {
type: [Number, String],
default: ''
},
circleOrLine: {
type: Boolean,
default: false
},
succeedCloseCircle: {
type: Boolean,
default: true
},
baseImg: {
type: String,
default: ''
},
},
mounted() {},
data() {
return {
lists: [],
isInCount: true,
uploading: false,
value: '',
type: 'text',
border: true
};
},
watch: {
fileList: {
immediate: true,
handler(val) {
val.map(value => {
let tmp = this.lists.some(val => {
return val.url == value.url;
})
!tmp && this.lists.push({
url: value.url,
error: false,
progress: 100
});
});
}
},
lists(n) {
this.$emit('on-list-change', n);
}
},
methods: {
clear() {
this.lists = [];
},
reUpload() {
this.uploadFile();
},
selectFile() {
if (this.disabled) return;
const {
name = '', maxCount, multiple, maxSize, sizeType, lists, camera, compressed, maxDuration, sourceType
} = this;
let chooseFile = null;
const newMaxCount = maxCount - lists.length;
chooseFile = new Promise((resolve, reject) => {
uni.chooseImage({
count: multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
sourceType: sourceType,
sizeType,
success: resolve,
fail: reject
});
});
chooseFile
.then(res => {
let file = null;
let listOldLength = this.lists.length;
res.tempFiles.map((val, index) => {
if (!multiple && index >= 1) return;
if (val.size > maxSize) {
this.$emit('on-oversize', val, this.lists);
this.showToast('超出允许的文件大小');
} else {
if (maxCount <= lists.length) {
this.$emit('on-exceed', val, this.lists);
this.showToast('超出最大允许的文件个数');
return;
}
lists.push({
url: val.path,
progress: 0,
error: false
});
}
});
this.$emit('on-choose-complete', this.lists);
if (this.autoUpload) this.uploadFile(listOldLength);
})
.catch(error => {
});
},
showToast(message, force = false) {
if (this.showTips || force) {
uni.showToast({
title: message,
icon: 'none'
});
}
},
upload() {
this.uploadFile();
},
retry(index) {
this.lists[index].progress = 0;
this.lists[index].error = false;
this.lists[index].response = null;
uni.showLoading({
title: '重新上传'
});
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;
const task = uni.uploadFile({
url: this.action,
filePath: this.lists[index].url,
name: this.name,
formData: this.formData,
header: this.header,
success: res => {
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].response = data;
this.lists[index].progress = 100;
this.lists[index].error = false;
this.$emit('on-success', data, index, this.lists);
}
},
fail: e => {
this.uploadError(index, e);
},
complete: res => {
uni.hideLoading();
this.uploading = false;
this.uploadFile(index + 1);
this.$emit('on-change', res, index, this.lists);
}
});
task.onProgressUpdate(res => {
if (res.progress > 0) {
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('上传失败,请重试');
},
deleteItem(index) {
uni.showModal({
title: '提示',
content: '您确定要删除此项吗?',
success: res => {
if (res.confirm) {
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.showToast('移除成功');
}
}
});
},
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;
const images = this.lists.map(item => item.url || item.path);
uni.previewImage({
urls: images,
current: url,
success: () => {
this.$emit('on-preview', url, this.lists);
},
fail: () => {
uni.showToast({
title: '预览图片失败',
icon: 'none'
});
}
});
},
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>
@import '../../libs/css/style.components.scss';
.u-upload {
display: flex;
flex-wrap: wrap;
align-items: center;
.upload-img {
width: 109.89rpx;
height: 109.89rpx;
}
}
.u-list-item {
width: 100rpx;
height: 100rpx;
overflow: hidden;
margin: 5rpx;
background: rgb(244, 245, 246);
position: relative;
border-radius: 5rpx;
display: inline-flex;
align-items: center;
justify-content: center;
}
.u-preview-wrap {
border: 1px solid rgb(235, 236, 238);
}
.u-add-wrap {
flex-direction: column;
color: $u-content-color;
font-size: 14rpx;
}
.u-add-tips {
margin-top: 10rpx;
line-height: 20rpx;
}
.u-add-wrap__hover {
background-color: rgb(235, 236, 238);
}
.u-preview-image {
display: block;
width: 100%;
height: 100%;
border-radius: 5rpx;
}
.u-delete-icon {
position: absolute;
top: 5rpx;
right: 5rpx;
z-index: 10;
background-color: $u-type-error;
border-radius: 50rpx;
width: 22rpx;
height: 22rpx;
display: flex;
align-items: center;
justify-content: center;
}
.u-icon {
display: flex;
align-items: center;
justify-content: center;
}
.u-progress {
position: absolute;
// bottom: 5rpx;
// left: 4rpx;
// right: 4rpx;
z-index: 9;
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: #fff;
font-size: 16px;
}
.uploading {
position: absolute;
top: 100%;
font-size: 18px;
color: #fff;
}
}
.u-error-btn {
color: #ffffff;
background-color: $u-type-error;
font-size: 10rpx;
padding: 4px 0;
text-align: center;
position: absolute;
bottom: 0;
left: 0;
right: 0;
z-index: 9;
line-height: 1;
}
</style>