MrShi
2025-04-11 3a45da15c947c2d478a44a51bd0f926647b1b841
company/src/components/common/UploadAvatarImage.vue
@@ -3,16 +3,18 @@
    <el-upload
      class="avatar-uploader"
      accept=".png,.jpg"
      :style="customStyle"
      :action="uploadImgUrl"
      :data="uploadData"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :on-error="uploadError"
      :before-upload="beforeAvatarUpload">
      <template v-if="file.imgurlfull">
        <img :src="file.imgurlfull" class="avatar">
      </template>
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      <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>
  </div>
@@ -25,16 +27,16 @@
      type: Object,
      default: () => {}
    },
    tipsLabel: '',
    customStyle: {
      type: String,
      default: 'width: 90px; height: 90px;'
    },
    uploadData: Object
  },
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_API_PREFIX + '/public/uploadLocal',
      // uploadData: {
      //   folder: 'upload',
      //   type: 'image'
      // },
      imgurlfull: ''
      uploadImgUrl: process.env.VUE_APP_API_PREFIX + '/public/upload'
    }
  },
@@ -42,10 +44,11 @@
    // 上传图片
    handleAvatarSuccess(res, file) {
      if (res.code == 200) {
        this.file.imgurl = res.data.imgaddr;
        this.file.imgurlfull = res.data.url;
        this.$tip.apiSuccess('上传成功')
        this.$emit('uploadSuccess', { imgurl: res.data.imgaddr, imgurlfull: res.data.url })
        let { data } = res
        this.file.imgurl = data.imgaddr;
        this.file.imgurlfull = data.url;
        this.$message.success('上传成功')
        this.$emit('uploadSuccess', { imgurl: data.imgaddr, imgurlfull: data.url, name: data.originname })
      } else {
        this.$tip.apiFailed('上传失败')
      }
@@ -65,7 +68,7 @@
</script>
<style lang="scss" scoped>
$image-width: 100px;
$image-width: 90px;
.avatar-uploader {
  width: $image-width;
  height: $image-width;
@@ -75,26 +78,31 @@
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  width: $image-width;
  height: $image-width;
  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;
  line-height: $image-width;
  text-align: center;
}
.avatar {
  width: $image-width;
  height: $image-width;
  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{