123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <template>
- <view class="file-upload-container">
- <view class="file-upload-view" v-for="(item, index) in files" :key="index">
- <view class="file-upload-ckick" @click="imgView(index)">
- <image v-if="item.url" mode="aspectFit" :src="imageUrl + item.url" style="width: 100%; height: 100%"> </image>
- <template v-else>
- <image mode="aspectFit" :src="item.loUrl" style="width: 100%; height: 100%"> </image>
- <view class="mack" @tap.stop="() => {}"></view>
- <image @tap.stop="() => {}" class="uploadImg" mode="aspectFit" src="/static/images/upload/loading.png">
- </image>
- </template>
- <template v-if="!disabled">
- <text class="iconfont icon-guanbi1 delImg" @tap.stop="del(index)"></text>
- </template>
- </view>
- </view>
- <template v-if="!disabled">
- <view v-if="count === 0 ? true : count - files.length" class="file-upload-view" @click="upload">
- <view class="file-upload-ckick">
- <text class="iconfont icon-paizhao uploadImg"></text>
- </view>
- </view>
- </template>
- </view>
- </template>
- <script>
- import { uploadImgFull } from '@/common/utils/util.js'
- import { b64_md5 } from '@/utils/md5.js'
- import loadingImg from './loading.png'
- export default {
- props: {
- disabled: {
- type: Boolean,
- default: false
- },
- count: {
- type: Number,
- default: 0
- },
- fileList: {
- type: Array,
- default: () => []
- }
- },
- data() {
- return {
- imageUrl: this.$imageUrl,
- files: this.setFileVal(this.fileList)
- }
- },
- watch: {
- fileList() {
- this.files = this.setFileVal(this.fileList)
- },
- files: {
- handler(newName, oldName) {
- if (newName.every(item => (item.url ? true : false))) {
- this.$emit(
- 'getFiles',
- newName.map(item => item.url)
- )
- }
- },
- deep: true
- }
- },
- methods: {
- setFileVal(arr) {
- return arr.map(item => {
- if (typeof item == 'string') {
- return {
- url: item,
- key: b64_md5(item)
- }
- } else {
- return item
- }
- })
- },
- upload() {
- uni.chooseImage({
- count: this.count === 0 ? 0 : this.count - this.files.length,
- sizeType: ['original'],
- success: async res => {
- const leng = res.tempFiles.length
- for (var file of res.tempFiles) {
- this.files.push({
- url: false,
- key: b64_md5(file.path),
- loUrl: file.path
- })
- }
- for (var i = 0; i < leng; i++) {
- var data = await uploadImgFull(res.tempFiles[i])
- var obj = this.files.find(item => item.key === b64_md5(res.tempFiles[i].path))
- if (obj) {
- obj.url = data.url
- obj.key = b64_md5(data.url)
- delete obj.loUrl
- }
- }
- },
- fail: err => {
- console.log(err)
- }
- })
- },
- del(index) {
- this.files.splice(index, 1)
- },
- imgView(index) {
- var list = this.files.map(item => {
- if (item.url) {
- return this.imageUrl + item.url
- } else {
- return loadingImg
- }
- })
- uni.previewImage({
- current: index,
- urls: list
- })
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .file-upload-container {
- width: 100%;
- height: 100%;
- display: flex;
- flex-wrap: wrap;
- justify-content: space-between;
- .file-upload-view {
- display: inline-block;
- margin-bottom: 20rpx;
- &:nth-last-of-type(-n + 2) {
- margin-bottom: 0;
- }
- .file-upload-ckick {
- width: 300rpx;
- height: 190rpx;
- background: #ffffff;
- border-radius: 20rpx;
- opacity: 1;
- border: 1rpx solid #e4e4e4;
- box-sizing: border-box;
- padding: 10rpx;
- position: relative;
- display: flex;
- justify-content: center;
- align-items: center;
- .mack {
- width: 100%;
- height: 100%;
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- z-index: 2;
- background: rgba(0, 0, 0, 0.2);
- }
- .icon-paizhao {
- background: $theme-color;
- color: #ffffff;
- font-size: 40rpx;
- border-radius: 50%;
- text-align: center;
- line-height: 80rpx;
- }
- .uploadImg {
- width: 80rpx;
- height: 80rpx;
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- z-index: 3;
- }
- .delImg {
- position: absolute;
- top: -16rpx;
- right: -16rpx;
- z-index: 2;
- width: 40rpx;
- height: 40rpx;
- background: $minor-color;
- color: #ffffff;
- border-radius: 50%;
- text-align: center;
- line-height: 40rpx;
- font-size: 28rpx;
- }
- }
- }
- }
- </style>
|