doum
2025-09-26 9057e04efad1b7d61c77a72e5c37a504d0aee935
admin/src/components/common/UploadFaceImg.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,206 @@
<template>
  <div>
    <el-upload
        class="avatar-uploader"
        accept=".png,.jpg,.jpeg"
        :style="customStyle"
        action=""
        :auto-upload="false"
        :show-file-list="false"
        :on-change='openUpdateIcon'>
      <img v-if=" file.imgurlfull" style="width: 100%;" :src="file.imgurlfull" :style="customStyle" class="avatar">
      <div v-else :style="customStyle">
        <i class="el-icon-plus avatar-uploader-icon"></i>
        <div class="tips-style">{{ tipsLabel }}</div>
      </div>
    </el-upload>
    <el-dialog
        append-to-body
        :close-on-click-modal="false"
        title="上传图片"
        :visible.sync="updateImg"
        width="1000px"
        class="icon-dialog-wrapper dialong-com-style">
      <ImageCropper ref="iconShot" v-if="updateImg" :imgSrc="img" >
      </ImageCropper>
      <span slot="footer" class="dialog-footer">
            <el-button v-if="loading">取 æ¶ˆ</el-button>
            <el-button v-else @click="updateImg = false">取 æ¶ˆ</el-button>
            <el-button :loading="loading" type="primary" @click="uploadIcon">ç¡® å®š</el-button>
          </span>
    </el-dialog>
  </div>
</template>
<script>
import ImageCropper from '@/components/common/ImageCropper'
import { upload } from '@/api/system/common'
export default {
  components: { ImageCropper },
  props: {
    file: {
      type: Object,
      default: () => {}
    },
    tipsLabel: '',
    customStyle: {
      type: String,
      default: 'width: 90px; height: 90px;'
    },
    uploadData: Object
  },
  data () {
    return {
      loading: false,
      fileInfo:{},
      img: null,
      updateImg: false,
      imageSrc: null,
      uploadImgUrl: process.env.VUE_APP_API_PREFIX + '/visitsAdmin/cloudService/public/upload'
    }
  },
  methods: {
    uploadIcon () {
      // èŽ·å–è£å‰ªåŽçš„å›¾ç‰‡
      this.$refs.iconShot.getImagecropper().getCropBlob((fileData) => { // èŽ·å–å½“å‰è£å‰ªå¥½çš„æ•°æ®
        // æ³¨æ­¤æ—¶çš„data是一个Blob数据,部分接口接收的是File转化的FormData数据
        console.log(fileData)
        const formData = new FormData()
        formData.append('folder', 'member')
        if(this.uploadData.isFace || this.uploadData.isFace == 0){
          formData.append('isFace', 0)
        }
        formData.append(
            'file',
            new File(
                [fileData], // å°†Blob类型转化成File类型
                this.fileInfo.name, // è®¾ç½®File类型的文件名称
                { type: this.fileInfo.type } // è®¾ç½®File类型的文件类型
            )
        )
        this.loading = true
        upload(formData).then(res => {
          this.loading = false
          console.log(res)
          this.file.imgurl = res.imgaddr
          this.file.imgurlfull = res.url
          this.$message.success('上传成功')
          this.imageSrc = res.url
          this.updateImg = false
          this.$emit('uploadSuccess', { imgurl: res.imgaddr, imgurlfull: res.url, name: res.originname })
          this.$emit('uploadEnd')
        }, () => {
          this.loading = false
        })
      })
    },
    // ä¸Šä¼ å›¾ç‰‡
    openUpdateIcon (file, fileList) {
      const isJPG = file.raw.type === 'image/jpeg' || file.raw.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 5
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG/PNG æ ¼å¼!')
        return false
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
        return false
      }
      // ä¸Šä¼ æˆåŠŸåŽå°†å›¾ç‰‡åœ°å€èµ‹å€¼ç»™è£å‰ªæ¡†æ˜¾ç¤ºå›¾ç‰‡
      this.$nextTick(async () => {
        // base64方式
        // this.option.img = await fileByBase64(file.raw)
        this.fileInfo.name = file.name
        this.fileInfo.type = file.type
        console.log(file, fileList)
        this.img = URL.createObjectURL(file.raw)
        // this.loading = false
        this.updateImg = true
      })
    },
    handleAvatarSuccess (res, file) {
      if (res.code == 200) {
        const { data } = res
        this.file.imgurl = data.imgaddr
        this.file.imgurlfull = data.url
        this.$message.success('上传成功')
        this.imageSrc = data.url
        this.updateImg = true
        // this.$emit('uploadSuccess', { imgurl: data.imgaddr, imgurlfull: data.url, name: data.originname })
      } else {
        this.$message.error('上传失败')
      }
      this.$emit('uploadEnd')
    },
    uploadError () {
      this.$message.error('上传失败')
      this.$emit('uploadEnd')
    },
    // // æ‹¦æˆª
    beforeAvatarUpload (file) {
      this.$emit('uploadBegin')
      return true
    }
  }
}
</script>
<style lang="scss" scoped>
$image-width: 90px;
.avatar-uploader {
  width: $image-width;
  height: $image-width;
}
::v-deep .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  line-height: 90px;
  font-size: 28px;
  color: #8c939d;
  width: $image-width;
  height: $image-width;
  text-align: center;
}
.avatar {
  width: 100% !important;
  height: auto !important;
  display: block;
}
.tips-style {
  height: 13px;
  font-size: 13px;
  font-weight: 400;
  color: #999999;
  line-height: 13px;
}
</style>
<style lang="scss" scoped>
::v-deep .el-upload--picture-card{
  width: 90px !important;
  height: 90px !important;
}
::v-deep .el-upload-list__item {
  width: 90px !important;
  height: 90px !important;
}
.icon {
  -webkit-transform: translate(-50%,-50%);
  -ms-transform: translate(-50%,-50%);
  transform: translate(0%, -85%);
}
::v-deep .el-upload-list__item {
  width: 90px !important;
  height: 90px !important;
}
</style>