123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645 |
- <template>
- <view v-if="!disabled" class="tn-image-upload-class tn-image-upload">
- <block v-if="showUploadList">
- <view
- v-for="(item, index) in lists"
- :key="index"
- class="tn-image-upload__item tn-image-upload__item-preview"
- :style="{
- width: $t.string.getLengthUnitValue(width),
- height: $t.string.getLengthUnitValue(height)
- }"
- >
-
- <view
- v-if="deleteable"
- class="tn-image-upload__item-preview__delete"
- @tap.stop="deleteItem(index)"
- :style="{
- borderTopColor: deleteBackgroundColor
- }"
- >
- <view
- class="tn-image-upload__item-preview__delete--icon"
- :class="[`tn-icon-${deleteIcon}`]"
- :style="{
- color: deleteColor
- }"
- ></view>
- </view>
-
- <tn-line-progress
- v-if="showProgress && item.progress > 0 && !item.error"
- class="tn-image-upload__item-preview__progress"
- :percent="item.progress"
- :showPercent="false"
- :round="false"
- :height="8"
- ></tn-line-progress>
-
- <view v-if="item.error" class="tn-image-upload__item-preview__error-btn" @tap.stop="retry(index)">点击重试</view>
-
- <image
- class="tn-image-upload__item-preview__image"
- :src="item.url || item.path"
- :mode="imageMode"
- @tap.stop="doPreviewImage(item.url || item.path, index)"
- ></image>
- </view>
- </block>
-
-
- <slot name="file" :file="lists"></slot>
-
-
- <view v-if="maxCount > lists.length" class="tn-image-upload__add" :class="{'tn-image-upload__add--custom': customBtn}" @tap="selectFile">
-
- <view
- v-if="!customBtn"
- class="tn-image-upload__item tn-image-upload__item-add"
- hover-class="tn-hover-class"
- hover-stay-time="150"
- :style="{
- width: $t.string.getLengthUnitValue(width),
- height: $t.string.getLengthUnitValue(height)
- }"
- >
- <view class="tn-image-upload__item-add--icon tn-icon-add"></view>
- <view class="tn-image-upload__item-add__tips">{{ uploadText }}</view>
- </view>
-
- <view>
- <slot name="addBtn"></slot>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- name: 'tn-image-upload',
- props: {
-
- fileList: {
- type: Array,
- default() {
- return []
- }
- },
-
- action: {
- type: String,
- default: ''
- },
-
- name: {
- type: String,
- default: 'file'
- },
-
- header: {
- type: Object,
- default() {
- return {}
- }
- },
-
- formData: {
- type: Object,
- default() {
- return {}
- }
- },
-
- disabled: {
- type: Boolean,
- default: false
- },
-
- autoUpload: {
- type: Boolean,
- default: true
- },
-
- maxCount: {
- type: Number,
- default: 9
- },
-
- showUploadList: {
- type: Boolean,
- default: true
- },
-
- imageMode: {
- type: String,
- default: 'aspectFill'
- },
-
- previewFullImage: {
- type: Boolean,
- default: true
- },
-
- showProgress: {
- type: Boolean,
- default: true
- },
-
- deleteable: {
- type: Boolean,
- default: true
- },
-
- deleteIcon: {
- type: String,
- default: 'close'
- },
-
- deleteBackgroundColor: {
- type: String,
- default: ''
- },
-
- deleteColor: {
- type: String,
- default: ''
- },
-
- uploadText: {
- type: String,
- default: '选择图片'
- },
-
- showTips: {
- type: Boolean,
- default: true
- },
-
- customBtn: {
- type: Boolean,
- default: false
- },
-
- width: {
- type: Number,
- default: 200
- },
-
- height: {
- type: Number,
- default: 200
- },
-
-
- sizeType: {
- type: Array,
- default() {
- return ['original', 'compressed']
- }
- },
-
- sourceType: {
- type: Array,
- default() {
- return ['album', 'camera']
- }
- },
-
- multiple: {
- type: Boolean,
- default: true
- },
-
- maxSize: {
- type: Number,
- default: 10 * 1024 * 1024
- },
-
- limitType: {
- type: Array,
- default() {
- return ['png','jpg','jpeg','webp','gif','image']
- }
- },
-
- toJson: {
- type: Boolean,
- default: true
- },
-
- beforeUpload: {
- type: Function,
- default: null
- },
-
- beforeRemove: {
- type: Function,
- default: null
- },
- index: {
- type: [Number, String],
- default: ''
- }
- },
- computed: {
-
- },
- data() {
- return {
- lists: [],
- uploading: false
- }
- },
- watch: {
- fileList: {
- handler(val) {
- val.map(value => {
-
-
-
- let tmp = this.lists.some(listVal => {
- return listVal.url === value.url
- })
-
- !tmp && this.lists.push({ url: value.url, error: false, progress: 100 })
- })
- },
- immediate: true
- },
- lists(val) {
- this.$emit('on-list-change', val, this.index)
- }
- },
- methods: {
-
- clear() {
- this.lists = []
- },
-
- reUpload() {
- this.uploadFile()
- },
-
- selectFile() {
- if (this.disabled) return
- const {
- name = '',
- maxCount,
- multiple,
- maxSize,
- sizeType,
- lists,
- camera,
- compressed,
- 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,
- sizeType,
- success: resolve,
- fail: reject
- })
- })
- chooseFile.then(res => {
- let file = null
- let listOldLength = lists.length
- res.tempFiles.map((val, index) => {
- if (!this.checkFileExt(val)) return
-
-
- if (!multiple && index >= 1) return
- if (val.size > maxSize) {
- this.$emit('on-oversize', val, lists, this.index)
- this.showToast('超出可允许文件大小')
- } else {
- if (maxCount <= lists.length) {
- this.$emit('on-exceed', val, lists, this.index)
- this.showToast('超出最大允许的文件数')
- return
- }
- lists.push({
- url: val.path,
- progress: 0,
- error: false,
- file: val
- })
- }
- })
- this.$emit('on-choose-complete', this.lists, this.index)
- if (this.autoUpload) this.uploadFile(listOldLength)
- }).catch(err => {
- this.$emit('on-choose-fail', err)
- })
- },
-
- showToast(message, force = false) {
- if (this.showTips || force) {
- this.$t.message.toast(message)
- }
- },
-
- upload() {
- this.uploadFile()
- },
-
- retry(index) {
- this.lists[index].progress = 0
- this.lists[index].error = false
- this.lists[index].response = null
- this.$t.message.loading('重新上传')
- 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, this.index)
- return
- }
-
- if (this.lists[index].progress === 100) {
- this.lists[index].uploadTask = null
- if (this.autoUpload) this.uploadFile(index + 1)
- return
- }
-
- if (this.beforeUpload && typeof(this.beforeUpload) === 'function') {
-
-
-
-
- let beforeResponse = this.beforeUpload.bind(this.$t.$parent.call(this))(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)
- } else {
-
- }
- }
-
- if (!this.action) {
- this.showToast('请配置上传地址', true)
- return
- }
- 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.$t.test.jsonString(res.data) ? JSON.parse(res.data) : res.data
- if (![200, 201, 204].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, this.index)
- }
- },
- fail: err => {
- this.uploadError(index, err)
- },
- complete: res => {
- this.$t.message.closeLoading()
- this.uploading = false
- this.uploadFile(index + 1)
- this.$emit('on-change', res, index, this.lists, this.index)
- }
- })
- this.lists[index].uploadTask = task
- task.onProgressUpdate(res => {
- if (res.progress > 0) {
- this.lists[index].progress = res.progress
- this.$emit('on-progress', res, index, this.lists, this.index)
- }
- })
- },
-
- uploadError(index, err) {
- this.lists[index].progress = 0
- this.lists[index].error = true
- this.lists[index].response = null
- this.showToast('上传失败,请重试')
- this.$emit('on-error', err, index, this.lists, this.index)
- },
-
- deleteItem(index) {
- if (!this.deleteable) return
- this.$t.message.modal(
- '提示',
- '您确定要删除吗?',
- async () => {
-
-
- if (this.beforeRemove && typeof(this.beforeRemove) === 'function') {
- let beforeResponse = this.beforeRemove.bind(this.$t.$parent.call(this))(index, this.lists)
-
- if (!!beforeResponse && typeof beforeResponse.then === 'function') {
- await beforeResponse.then(res => {
-
- this.handlerDeleteItem(index)
- }).catch(err => {
- this.showToast('删除操作被中断')
- })
- } else if (beforeResponse === false) {
- this.showToast('删除操作被中断')
- } else {
- this.handlerDeleteItem(index)
- }
- } else {
- this.handlerDeleteItem(index)
- }
- }, true)
- },
-
- handlerDeleteItem(index) {
-
- if (this.lists[index].progress < 100 && this.lists[index].progress > 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.index)
- this.showToast('删除成功')
- },
-
- remove(index) {
- if (!this.deleteable) return
-
- if (index >= 0 && index < this.lists.length) {
- this.lists.splice(index, 1)
- }
- },
-
- 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, this.index)
- },
- fail: () => {
- this.showToast('预览图片失败')
- }
- })
- },
-
- checkFileExt(file) {
-
- let noArrowExt = false
-
- let fileExt = ''
- const reg = /.+\./
-
-
- fileExt = file.name.replace(reg, '').toLowerCase()
-
-
- fileExt = file.path.replace(reg, '').toLowerCase()
-
- noArrowExt = this.limitType.some(ext => {
- return ext.toLowerCase() === fileExt
- })
- if (!noArrowExt) this.showToast(`不支持${fileExt}格式的文件`)
- return noArrowExt
- }
- }
- }
- </script>
- <style lang="scss" scoped>
-
- .tn-image-upload {
- display: flex;
- flex-direction: row;
- flex-wrap: wrap;
- align-items: center;
-
- &__item {
- /* #ifndef APP-NVUE */
- display: flex;
- /* #endif */
- align-items: center;
- justify-content: center;
- width: 200rpx;
- height: 200rpx;
- overflow: hidden;
- margin: 12rpx;
- margin-left: 0;
- background-color: $tn-font-holder-color;
- position: relative;
- border-radius: 10rpx;
-
- &-preview {
- border: 1rpx solid $tn-border-solid-color;
-
- &__delete {
- display: flex;
- align-items: center;
- justify-content: center;
- position: absolute;
- top: 0;
- right: 0;
- z-index: 10;
- border-top: 60rpx solid;
- border-left: 60rpx solid transparent;
- border-top-color: $tn-color-red;
- width: 0rpx;
- height: 0rpx;
-
- &--icon {
- position: absolute;
- top: -50rpx;
- right: 6rpx;
- color: #FFFFFF;
- font-size: 24rpx;
- line-height: 1;
- }
- }
-
- &__progress {
- position: absolute;
- width: auto;
- bottom: 0rpx;
- left: 0rpx;
- right: 0rpx;
- z-index: 9;
- /* #ifdef MP-WEIXIN */
- display: inline-flex;
- /* #endif */
- }
-
- &__error-btn {
- position: absolute;
- bottom: 0;
- left: 0;
- right: 0;
- background-color: $tn-color-red;
- color: #FFFFFF;
- font-size: 20rpx;
- padding: 8rpx 0;
- text-align: center;
- z-index: 9;
- line-height: 1;
- }
-
- &__image {
- display: block;
- width: 100%;
- height: 100%;
- border-radius: 10rpx;
- }
- }
-
- &-add {
- flex-direction: column;
- color: $tn-content-color;
- font-size: 26rpx;
-
- &--icon {
- font-size: 40rpx;
- }
-
- &__tips {
- margin-top: 20rpx;
- line-height: 40rpx;
- }
- }
- }
-
- &__add {
- width: auto;
- display: inline-block;
-
- &--custom {
- width: 100%;
- }
- }
- }
- </style>
|