田源
2025-01-16 39269c81905457378a73dc83050349d7a364a1f8
Source/plt-web/plt-web-ui/src/views/system/userInfo/userAvatar.vue
@@ -61,7 +61,9 @@
<script>
import store from "@/store";
import { VueCropper } from "vue-cropper";
import { uploadAvatar } from "@/api/system/user";
import {getUserPhoto, updateUserPhoto} from "@/api/user";
import {mapGetters} from "vuex";
import {setStore} from '@/util/store'
export default {
  components: { VueCropper },
@@ -74,18 +76,23 @@
      visible: false,
      // 弹出层标题
      title: "修改头像",
      options: {
        img: this.avatar,  //裁剪图片的地址
      previews: {},
      resizeHandler: null
    };
  },
  computed: {
    ...mapGetters(["userInfo"]),
    options() {
      return {
        img: this.userInfo.avatar,  //裁剪图片的地址
        autoCrop: true,             // 是否默认生成截图框
        autoCropWidth: 200,         // 默认生成截图框宽度
        autoCropHeight: 200,        // 默认生成截图框高度
        fixedBox: true,             // 固定截图框大小 不允许改变
        outputType:"png",           // 默认生成截图为PNG格式
        filename: 'avatar'          // 文件名称
      },
      previews: {},
      resizeHandler: null
    };
      }
    }
  },
  methods: {
    // 编辑头像
@@ -95,11 +102,11 @@
    // 打开弹出层结束时的回调
    modalOpened() {
      this.visible = true;
      if (!this.resizeHandler) {
        this.resizeHandler = debounce(() => {
          this.refresh()
        }, 100)
      }
      // if (!this.resizeHandler) {
      //   this.resizeHandler = debounce(() => {
      //     this.refresh()
      //   }, 100)
      // }
      window.addEventListener("resize", this.resizeHandler)
    },
    // 刷新组件
@@ -125,7 +132,7 @@
    // 上传预处理
    beforeUpload(file) {
      if (file.type.indexOf("image/") == -1) {
        this.$modal.msgError("文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。");
        this.$message.error("文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。");
      } else {
        const reader = new FileReader();
        reader.readAsDataURL(file);
@@ -133,18 +140,21 @@
          this.options.img = reader.result;
          this.options.filename = file.name;
        };
        this.$refs.cropper.refresh();
      }
    },
    // 上传图片
    uploadImg() {
      this.$refs.cropper.getCropBlob(data => {
        let formData = new FormData();
        formData.append("avatarfile", data, this.options.filename);
        uploadAvatar(formData).then(response => {
        formData.append("file", data, this.options.filename);
        formData.append("username",this.userInfo.userId);
        updateUserPhoto(formData).then(response => {
          this.open = false;
          this.options.img = process.env.VUE_APP_BASE_API + response.imgUrl;
          store.commit('SET_AVATAR', this.options.img);
          this.$modal.msgSuccess("修改成功");
          this.$message.success('修改成功!');
          this.getUserInfo();
          this.visible = false;
        });
      });
@@ -155,9 +165,21 @@
    },
    // 关闭窗口
    closeDialog() {
      this.options.img = store.getters.avatar
      this.options.img = this.userInfo.avatar; // 关闭窗口更新为缓存中的头像地址 如修改成功更新为最新的 如修改后未保存更新为缓存中的
      this.visible = false;
      window.removeEventListener("resize", this.resizeHandler)
    },
    // 更新缓存内容
    getUserInfo(){
      getUserPhoto().then(res => {
        // 将 Blob 转换为 Base64
        const reader = new FileReader();
        reader.onloadend = (result) => {
          this.userInfo.avatar = reader.result; // 存储 Base64 字符串
          setStore({ name: 'userInfo', content:  this.userInfo });
        };
        reader.readAsDataURL(res.data); // 调用reader方法
      });
    }
  }
};