1
jhzh
2025-11-12 1bd03f5c2e7b9fa9cc80c4e673e18132da411333
src/page/authentication/index.vue
@@ -89,6 +89,9 @@
        img2key: "",
      },
      imgStatus: false,
      imgStatus2: false,
      previousImg1key: '',
      previousImg2key: '',
      admin: apiUrl.baseURL,
    }
  },
@@ -120,6 +123,15 @@
      } else if (!this.form.img2key) {
        Toast(this.$t("hj209"));
      } else {
        // 验证图片地址是否为有效的网络地址
        if (this.form.img1key && this.form.img1key.startsWith('blob:')) {
          Toast('请等待图片上传完成');
          return;
        }
        if (this.form.img2key && this.form.img2key.startsWith('blob:')) {
          Toast('请等待图片上传完成');
          return;
        }
        // 显示确认弹窗
        this.toAuthentication();
      }
@@ -167,31 +179,92 @@
    },
    handleAvatarSuccess(res, file) {
      this.imgStatus = false;
      this.form.img1key = res.data.url;
      // 确保返回的是有效的网络图片地址,而不是临时 blob URL
      if (res && res.data && res.data.url) {
        // 验证 URL 是否为网络地址(不是 blob URL)
        if (res.data.url.startsWith('http://') || res.data.url.startsWith('https://')) {
          this.form.img1key = res.data.url;
        } else {
          // 如果返回的不是完整 URL,可能是相对路径,需要拼接 baseURL
          this.form.img1key = this.admin.replace(/\/$/, '') + '/' + res.data.url.replace(/^\//, '');
        }
      } else {
        // 如果响应格式不正确,恢复之前的值
        if (this.previousImg1key) {
          this.form.img1key = this.previousImg1key;
        }
        Toast(this.$t("hj209") || '图片上传失败,请重试');
      }
    },
    beforeAvatarUpload(file) {
      this.imgStatus = true;
      // 保存之前的图片地址,如果上传失败可以恢复
      this.previousImg1key = this.form.img1key;
      return true;
    },
    handleError() {
      this.imgStatus = false;
      // 上传失败时,恢复之前的图片地址(如果有)
      if (this.previousImg1key) {
        this.form.img1key = this.previousImg1key;
      } else {
        // 如果没有之前的地址,清空并清理可能的临时 URL
        if (this.form.img1key && this.form.img1key.startsWith('blob:')) {
          URL.revokeObjectURL(this.form.img1key);
          this.form.img1key = '';
        }
      }
      Toast(this.$t("hj209") || '图片上传失败,请重试');
    },
    handleAvatarSuccess2(res, file) {
      this.imgStatus2 = false;
      this.form.img2key = res.data.url; // URL.createObjectURL(file.raw);
      // 确保返回的是有效的网络图片地址,而不是临时 blob URL
      if (res && res.data && res.data.url) {
        // 验证 URL 是否为网络地址(不是 blob URL)
        if (res.data.url.startsWith('http://') || res.data.url.startsWith('https://')) {
          this.form.img2key = res.data.url;
        } else {
          // 如果返回的不是完整 URL,可能是相对路径,需要拼接 baseURL
          this.form.img2key = this.admin.replace(/\/$/, '') + '/' + res.data.url.replace(/^\//, '');
        }
      } else {
        // 如果响应格式不正确,恢复之前的值
        if (this.previousImg2key) {
          this.form.img2key = this.previousImg2key;
        }
        Toast(this.$t("hj209") || '图片上传失败,请重试');
      }
    },
    beforeAvatarUpload2(file) {
      this.imgStatus2 = true;
      const isLt10M = file.size / 1024 / 1024 < 10;
      if (!isLt10M) {
        this.$message.error(this.$t("hj205"));
        this.imgStatus2 = false;
        return false;
      } else {
        this.form.img2key = URL.createObjectURL(file);
        compress(file, function (val) { });
      }
      // 保存之前的图片地址,如果上传失败可以恢复
      this.previousImg2key = this.form.img2key;
      // 不再设置临时 blob URL,等待上传成功后再设置
      compress(file, function (val) { });
      return true;
    },
    handleError2() {
      this.imgStatus2 = false;
      // 上传失败时,恢复之前的图片地址(如果有)
      if (this.previousImg2key) {
        this.form.img2key = this.previousImg2key;
      } else {
        // 如果没有之前的地址,清空并清理可能的临时 URL
        if (this.form.img2key && this.form.img2key.startsWith('blob:')) {
          URL.revokeObjectURL(this.form.img2key);
          this.form.img2key = '';
        }
      }
      Toast(this.$t("hj209") || '图片上传失败,请重试');
    },
    goBack() {
      this.$router.go(-1);
    },
  }
}