avatar.vue 7.29 KB
<template>
  <div class="component-upload-image" style="min-width: 230px">
    <el-row justify="center">
      <div v-if="modelValue" class="fileItem el-upload-list__item is-success animated">
        <img
          :src="fillImgUrl(modelValue)"
          class="el-upload-list__item-thumbnail"
        >
        <div class="hover-actions">
          <span @click="handlePictureCardPreview(modelValue)">
            <el-icon><ZoomIn /></el-icon>
          </span>
          <span v-if="!disabled" @click="handleDelete(element)">
            <el-icon><Delete /></el-icon>
          </span>
        </div>
      </div>
      <div v-if="round" class="fileItem el-upload-list__item is-success animated">
        <img
          :src="fillImgUrl(round)"
          class="el-upload-list__item-thumbnail img2"
        >
        <div class="hover-actions">
          <span @click="handlePictureCardPreview(round)">
            <el-icon><ZoomIn /></el-icon>
          </span>
          <span v-if="!disabled" @click="handleDelete(element)">
            <el-icon><Delete /></el-icon>
          </span>
        </div>
      </div>
      <div v-if="!disabled" v-show="!modelValue" class="el-upload-list--picture-card">
        <div class="el-upload--picture-card" @click="editCropper()">
          <el-icon class="avatar-uploader-icon"><plus /></el-icon>
        </div>
      </div>
    </el-row>
    <!-- 上传提示 -->
    <div v-if="!modelValue" class="el-upload__tip" style="text-align: left">
      请上传
      <template v-if="fileSize">
        <div>
          大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
        </div>
      </template>
      <template v-if="fileType">
        <div>
          格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
        </div>
      </template>
      的文件
    </div>
    <!--裁剪-->
    <el-dialog
      v-model="open"
      append-to-body
      title="裁剪图片"
      width="800px"
      @close="closeDialog"
    >
      <el-row :gutter="20" style="margin-bottom: 10px">
        <el-col :span="12">
          <div style="text-align: center">级位证书照片</div>
        </el-col>
        <el-col :span="12">
          <div style="text-align: center">个人会员证照片</div>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :style="{ height: '350px' }">
          <rectangle-avatar ref="avatar1Ref" v-model="options.img1" />
        </el-col>
        <el-col :span="12" :style="{ height: '350px' }">
          <round-avatar ref="avatar2Ref" v-model="options.img2" />
        </el-col>
      </el-row>
      <br>
      <el-row justify="center">
        <el-col :span="12" class="text-center">
          <el-upload
            ref="uploadRef"
            :accept="accept"
            :before-upload="beforeUpload"
            :http-request="()=>{}"
            :limit="1"
            :show-file-list="false"
            action="#"
          >
            <el-button>
              选择
              <el-icon class="el-icon--right"><Upload /></el-icon>
            </el-button>
          </el-upload>
        </el-col>
        <el-col :span="12" class="text-center">
          <el-button type="primary" @click="uploadImg()">提 交</el-button>
        </el-col>
      </el-row>
    </el-dialog>

    <!--预览-->
    <el-dialog
      v-model="dialogVisible"
      append-to-body
      title="预览"
      width="50%"
    >
      <img
        :src="fillImgUrl(imgVal)"
        style="display: block; width: 100%; margin: 0 auto"
      >
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, getCurrentInstance, ref } from 'vue'
import { reactive } from '@vue/runtime-core'
import request from '@/utils/request'
import _ from 'lodash'
import RoundAvatar from './components/round'
import RectangleAvatar from './components/rectangle2'
const imgVal = ref()

const props = defineProps({
  modelValue: String,
  round: String,
  // 大小限制(MB)
  fileSize: {
    type: Number,
    default: 10
  },
  fileType: {
    type: Array,
    default: () => ['png', 'jpg', 'jpeg']
  },
  disabled: {
    type: Boolean,
    default: false
  }
})
const accept = computed(() => {
  return _.map(props.fileType, (t) => {
    if (t.indexOf('.') === 0) {
      return t
    } else {
      return '.' + t
    }
  }).join(',')
})

const { proxy } = getCurrentInstance()
const emit = defineEmits(['update:modelValue', 'update:round'])

const open = ref(false)
const dialogVisible = ref(false)


// 图片裁剪数据
const options = reactive({
  img1: '',
  img2: ''
})

/** 裁剪 */
function editCropper() {
  open.value = true
}

/** 上传预处理 */
function beforeUpload(file) {
  if (props.fileSize) {
    const isLt = file.size / 1024 / 1024 < props.fileSize
    if (!isLt) {
      proxy.$modal.msgError(`上传图片大小不能超过 ${props.fileSize} MB!`)
      return false
    }
  }
  if (file.type.indexOf('image/') == -1) {
    proxy.$modal.msgError(
      '文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。'
    )
  } else {
    const reader = new FileReader()
    reader.readAsDataURL(file)
    reader.onload = () => {
      options.img1 = reader.result
      options.img2 = reader.result
    }

    proxy.$refs['uploadRef'].clearFiles()
  }
}
/** 上传图片 */
function uploadImg() {
  Promise.all([
    proxy.$refs['avatar1Ref'].getBlob(),
    proxy.$refs['avatar2Ref'].getBlob()
  ]).then(async(blobArr) => {
    const img1Path = await sendImg(blobArr[0])
    const img2Path = await sendImg(blobArr[1])

    emit('update:modelValue', img1Path)
    emit('update:round', img2Path)

    open.value = false
    proxy.$modal.msgSuccess('上传成功')
  })
}

function sendImg(imgData) {
  const formData = new FormData()
  formData.append('image', imgData)

  return request({
    // url: '/upload/uploadImgToLocalServer',
    url: '/fileServer/uploadImg',
    method: 'post',
    headers: { repeatSubmit: false },
    data: formData
  }).then((res) => res.data)
}

/** 关闭窗口 */
function closeDialog() {
  proxy.$refs['avatar1Ref'].clear()
  proxy.$refs['avatar2Ref'].clear()

  options.img1 = ''
  options.img2 = URL.createObjectURL(new Blob())
}

// 预览
function handlePictureCardPreview(row) {
  imgVal.value = row
  dialogVisible.value = true
}

// 删除图片
function handleDelete() {
  emit('update:round', '')
  emit('update:modelValue', '')
}
</script>

<style lang="scss" scoped>
// .el-upload--picture-card 控制加号部分
:deep(.hide .el-upload--picture-card) {
  display: none;
}
.fileItem {
  position: relative;
  width: 100px;
  height: 125px;
  overflow: hidden;
  margin-bottom: 10px;
  margin-right: 10px;
}
.fileItem img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.fileItem .img2 {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  object-fit: cover;
  margin-top: 12px;
}
.fileItem .hover-actions {
  background: rgba(0, 0, 0, 0.4);
  width: 100%;
  height: 100%;
  position: absolute;
  top: 100%;
  left: 0;
  color: #fff;
  transition: top 0.2s;
  text-align: center;
  span {
    cursor: pointer;
    font-size: 26px;
    padding: 10px;
    margin: 50px 5px;
    display: inline-block;
  }
  span:hover {
    background: rgba(255, 255, 255, 0.4);
  }
}
.el-upload--picture-card{
  width: 100px;
  height: 125px !important;
}
.fileItem:hover .hover-actions {
  top: 0;
}

.component-upload-image > div > div {
  display: flex;
}
</style>