vue webapp项目中图片上传组件

<template>
  <div class="upload-img common-padding">
    <ul class="business-type no-margin" v-if="showTitle">
      <li class="list ">
        <label>{{title}}</label>
      </li>
    </ul>
    <div class="box">
      <template v-if="imageListValue instanceof Array && imageListValue.length > 0">
        <div class="upload-text"  v-for="(item, index) in imageListValue" :key="index"  @click="removeImg(item)">
          <img :src="publicHost + item.attachmentPath" class="result-img"/>
        </div>
      </template>
      <div class="receive-img"  v-if="(Object.prototype.toString.call(imageListValue) === '[object Object]') && Object.keys(imageListValue).length !== 0" @click="removeImg()">
        <img :src="publicHost + imageListValue.attachmentPath" class="result-img"/>
      </div>

      <label>
        <div class="upload-text" v-if="showAdd">
          <img src="./../../../static/img/common/add.png" class="add-plus"/>
        </div>
        <slot name="upload-img" v-if="!(imageListValue instanceof Array) && !imageListValue"></slot>
        <input type="file"  capture="camera" accept="image/png,image/gif,image/jpeg" class="hidden" @change="getBaseUrl($event)"/>
      </label>
    </div>
  </div>
</template>
<script>
import { MessageBox } from 'mint-ui'
import {uploadFileByBase64Str} from '@/api/file'
export default{
  data () {
    return {
      image: null,
      temp: []
    }
  },
  props: {
    title: {
      default: '标题',
      type: String
    },
    imageList: {},
    showAdd: {
      type: Boolean,
      default: true
    },
    showTitle: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    imageListValue: {
      get: function () {
        return this.imageList
      },
      set: function (newValue) {
        this.$emit('update:imageList', newValue)
      }
    },
    publicHost () {
      return this.$store.state.dictionary.publicHost + '/'
    }
  },
  components: {},
  methods: {
    removeImg (url) {
      MessageBox.confirm('是否删除该图片?').then(action => {
        if (this.imageListValue instanceof Array) {
          this.imageListValue.map(val => {
            if (val.attachmentPath === url.attachmentPath) {
              this.imageListValue.splice(this.imageListValue.indexOf(val), 1)
              this.$emit('deleteImg', val)
            }
          })
        } else {
          this.imageListValue = {}
        }
      })
    },
    getBaseUrl (e) {
      const file = e.target.files[0]
      const reader = new FileReader()
      reader.readAsDataURL(file)// 读出 base64
      let _this = this
      reader.onloadend = function () {
        const dataURL = reader.result
        const data = {
          fileName: file.name,
          fileStr: dataURL
        }
        uploadFileByBase64Str(data).then(res => {
          if (res.code === 200) {
            const attachmentUrl = res.result[0]
            if (_this.imageListValue instanceof Array) {
              _this.imageListValue.push(attachmentUrl)
            } else {
              _this.imageListValue = attachmentUrl
            }
          }
        })
      }
    }
  }
}
</script>
<style lang="less">
  #fileInput {
    display: none;
  }
  .box{
    .img-preview{
      display: none;
    }
  }
</style>
<style lang='less' scoped>
  @import "./../../public/comment";
  .hidden{
    display: none;
  }
  .upload-img{
    padding-bottom: 10px/2;
    margin-bottom: 10px/2;
    background-color: #fff;
    .business-type{
      margin: 20px auto;
    }
    .no-margin{
      margin: 0;
    }
    .upload-text{
      background: #F2F2F2;
      width: 160px/2;
      height: 160px/2;
      border-radius: 2px;
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 0 10px/2 20px/2;
      .result-img{
        width: 100%;
        height: 100%;
      }
      .add-plus{
        width: 60%;
      }
    }
    .receive-img{
      height: 160px/2;
      img{
        height: 100%;
      }
    }
  }
  .box{
    display: flex;
    flex-wrap: wrap;
  }
  .list{
    width: 100%;
    height: 104px/2;
    display: flex;
    align-items: center;
    font-size: 32px/2;
    color: #333333;
    background-color: #fff;
    margin-bottom: 5px/2;
    label{
      width: 260px/2;
    }
    .text-1{
      font-size: 28px/2;
      color: #999999;
    }
    .text-2{
      color: @lightBlue;
    }
  }

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值