index2.vue 9.21 KB
<template>
  <div class="component-upload-image">
    <el-row>
      <draggable v-model="fileList" item-key="index" @end="draggableEnd">
        <template #item="{ element }">
          <div class="fileItem el-upload-list__item is-success animated">
            <img
              :src="element.url"
              alt=""
              class="el-upload-list__item-thumbnail"
            >
            <div class="hover-actions">
              <span @click="handlePictureCardPreview(element)">
                <el-icon><ZoomIn /></el-icon>
              </span>
              <span v-if="!disabled" @click="handleDelete(element)">
                <el-icon><Delete /></el-icon>
              </span>
            </div>
          </div>
        </template>
      </draggable>

      <div v-if="!disabled" v-show="showUpload && fileList.length < limit" 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>

    <!--预览-->
    <el-dialog
      v-model="dialogVisible"
      title="预览"
      width="80%"
      append-to-body
    >
      <img
        :src="dialogImageUrl"
        style="display: block; width: 100%; margin: 0 auto"
      >
    </el-dialog>

    <!-- 上传提示 -->
    <div v-if="showUpload && fileList.length < limit" class="el-upload__tip">
      请上传
      <template v-if="fileSize">
        大小不超过
        <b style="color: #f56c6c">{{ fileSize }}MB</b>
      </template>
      <br>
      <template v-if="fileType">
        格式为
        <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
      </template>
      的文件
    </div>
    <!--裁剪-->
    <el-dialog
      v-model="open"
      title="裁剪图片"
      width="600px"
      append-to-body
      @opened="modalOpened"
      @close="closeDialog"
    >
      <el-row>
        <el-col :style="{ height: '350px' }">
          <vue-cropper
            v-if="visible"
            ref="cropper"
            :img="options.img"
            :info="true"
            :auto-crop="options.autoCrop"
            :auto-crop-width="options.autoCropWidth"
            :auto-crop-height="options.autoCropHeight"
            :fixed-box="options.fixedBox"
            output-type="png"
            @realTime="realTime"
          />
        </el-col>
      </el-row>
      <br>
      <el-row>
        <el-col :lg="2" :md="2">
          <el-upload
            action="#"
            :http-request="requestUpload"
            :show-file-list="false"
            :before-upload="beforeUpload"
            :accept="accept"
          >
            <el-button>
              选择
              <el-icon class="el-icon--right"><Upload /></el-icon>
            </el-button>
          </el-upload>
        </el-col>
        <el-col :lg="{ span: 1, offset: 3 }" :md="2">
          <el-button icon="Plus" @click="changeScale(1)" />
        </el-col>
        <el-col :lg="{ span: 1, offset: 2 }" :md="2">
          <el-button icon="Minus" @click="changeScale(-1)" />
        </el-col>
        <el-col :lg="{ span: 1, offset: 2 }" :md="2">
          <el-button icon="RefreshLeft" @click="rotateLeft()" />
        </el-col>
        <el-col :lg="{ span: 1, offset: 2 }" :md="2">
          <el-button icon="RefreshRight" @click="rotateRight()" />
        </el-col>
        <el-col :lg="{ span: 1, offset: 5 }" :md="2">
          <el-button type="primary" @click="uploadImg()">提 交</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script setup>
import 'vue-cropper/dist/index.css'
import { VueCropper } from 'vue-cropper'
import { computed, getCurrentInstance, ref, watch } from 'vue'
import { reactive } from '@vue/runtime-core'
import request from '@/utils/request'
import _ from 'lodash'

const props = defineProps({
  modelValue: [String, Object, Array],
  // 图片数量限制
  limit: {
    type: Number,
    default: 5
  },
  // 大小限制(MB)
  fileSize: {
    type: Number,
    default: 10
  },
  // 文件类型, 例如['png', 'jpg', 'jpeg']
  fileType: {
    type: Array,
    default: () => ['png', 'jpg', 'jpeg']
  },
  cropWidth: {
    type: Number,
    default: 400
  },
  cropHeight: {
    type: Number,
    default: 300
  },
  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'])

const baseUrl = import.meta.env.VITE_APP_BASE_API

const open = ref(false)
const visible = ref(false)
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const showUpload = ref(true)

// 图片裁剪数据
const options = reactive({
  img: '', // 裁剪图片的地址
  autoCrop: true, // 是否默认生成截图框
  canScale: true, // 图片是否允许滚轮缩放
  autoCropWidth: props.cropWidth, // 默认生成截图框宽度
  autoCropHeight: props.cropHeight, // 默认生成截图框高度
  fixedBox: false, // 固定截图框大小 不允许改变
  previews: {}, // 预览数据,
  visible: false
})

const fileList = ref([])

watch(
  () => props.modelValue,
  (val) => {
    if (val) {
      // 首先将值转为数组
      const list = Array.isArray(val) ? val : props.modelValue.split(',')
      // 然后将数组转为对象数组
      fileList.value = list.map((item) => {
        if (typeof item === 'string') {
          if (item.indexOf('http') === -1) {
            item = { url: baseUrl + item }
          } else {
            item = { url: item }
          }
        }
        return item
      })
    } else {
      fileList.value = []
      return []
    }
  },
  { deep: true, immediate: true }
)

watch(
  () => fileList.value.length,
  (value) => {
    showUpload.value = value !== props.limit
  }, { deep: true, immediate: true }
  
)


/** 裁剪 */
function editCropper() {
  open.value = true
}
/** 打开弹出层结束时的回调 */
function modalOpened() {
  visible.value = true
}
/** 覆盖默认上传行为 */
function requestUpload() {}
/** 向左旋转 */
function rotateLeft() {
  proxy.$refs.cropper.rotateLeft()
}
/** 向右旋转 */
function rotateRight() {
  proxy.$refs.cropper.rotateRight()
}
/** 图片缩放 */
function changeScale(num) {
  num = num || 1
  proxy.$refs.cropper.changeScale(num)
}
/** 上传预处理 */
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 = (aa) => {
      // console.log(aa)
      options.img = reader.result
    }
  }
}
/** 上传图片 */
function uploadImg() {
  proxy.$refs.cropper.getCropBlob((data) => {
    const formData = new FormData()
    formData.append('image', data)

    request({
      url: '/upload/uploadImgToLocalServer',
      method: 'post',
      data: formData
    }).then((res) => {
      open.value = false
      options.img = res.msg
      proxy.$modal.msgSuccess('上传成功')
      visible.value = false
      fileList.value.push({
        url: options.img
      })
      emit('update:modelValue', listToString(fileList.value))
    })
  })
}
/** 实时预览 */
function realTime(data) {
  options.previews = data
}
/** 关闭窗口 */
function closeDialog() {
  options.img = ''
  options.visible = false
}

// 对象转成指定字符串分隔
function listToString(list, separator = ',') {
  let strs = ''
  for (const i in list) {
    if (undefined !== list[i].url && list[i].url.indexOf('blob:') !== 0) {
      strs += list[i].url.replace(baseUrl, '') + separator
    }
  }
  return strs != '' ? strs.substr(0, strs.length - 1) : ''
}

function draggableEnd() {
  emit('update:modelValue', listToString(fileList.value))
}

// 预览
function handlePictureCardPreview(file) {
  dialogImageUrl.value = file.url
  dialogVisible.value = true
}

// 删除图片
function handleDelete(file) {
  const findex = fileList.value.map((f) => f.url).indexOf(file.url)
  fileList.value.splice(findex, 1)
  emit('update:modelValue', listToString(fileList.value))
}
</script>

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

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