mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 08:13:44 +08:00 
			
		
		
		
	update 优化 OSS 模块与上传组件 异常处理
This commit is contained in:
		@@ -11,6 +11,7 @@ import com.ruoyi.oss.enumd.CloudServiceEnumd;
 | 
			
		||||
import com.ruoyi.oss.exception.OssException;
 | 
			
		||||
import com.ruoyi.oss.properties.CloudStorageProperties;
 | 
			
		||||
import com.ruoyi.oss.service.ICloudStorageStrategy;
 | 
			
		||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageStrategy;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
@@ -68,7 +69,8 @@ public class OssFactory {
 | 
			
		||||
			return service;
 | 
			
		||||
		}
 | 
			
		||||
		// 获取redis配置信息 创建对象 并缓存
 | 
			
		||||
		service = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type), properties);
 | 
			
		||||
		service = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type));
 | 
			
		||||
		((AbstractCloudStorageStrategy)service).init(properties);
 | 
			
		||||
		SERVICES.put(type, service);
 | 
			
		||||
		SERVICES_UPDATE_TIME.put(type, nowDate);
 | 
			
		||||
		return service;
 | 
			
		||||
 
 | 
			
		||||
@@ -20,6 +20,8 @@ public abstract class AbstractCloudStorageStrategy implements ICloudStorageStrat
 | 
			
		||||
 | 
			
		||||
	protected CloudStorageProperties properties;
 | 
			
		||||
 | 
			
		||||
	public abstract void init(CloudStorageProperties properties);
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public abstract void createBucket();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -24,9 +24,10 @@ import java.io.InputStream;
 | 
			
		||||
 */
 | 
			
		||||
public class AliyunCloudStorageStrategy extends AbstractCloudStorageStrategy {
 | 
			
		||||
 | 
			
		||||
	private final OSSClient client;
 | 
			
		||||
	private OSSClient client;
 | 
			
		||||
 | 
			
		||||
	public AliyunCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) {
 | 
			
		||||
	@Override
 | 
			
		||||
	public void init(CloudStorageProperties cloudStorageProperties) {
 | 
			
		||||
		properties = cloudStorageProperties;
 | 
			
		||||
		try {
 | 
			
		||||
			ClientConfiguration configuration = new ClientConfiguration();
 | 
			
		||||
@@ -35,7 +36,7 @@ public class AliyunCloudStorageStrategy extends AbstractCloudStorageStrategy {
 | 
			
		||||
			client = new OSSClient(properties.getEndpoint(), credentialProvider, configuration);
 | 
			
		||||
			createBucket();
 | 
			
		||||
		} catch (Exception e) {
 | 
			
		||||
			throw new IllegalArgumentException("阿里云存储配置错误! 请检查系统配置!");
 | 
			
		||||
			throw new OssException("阿里云存储配置错误! 请检查系统配置!");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -20,9 +20,10 @@ import java.io.InputStream;
 | 
			
		||||
 */
 | 
			
		||||
public class MinioCloudStorageStrategy extends AbstractCloudStorageStrategy {
 | 
			
		||||
 | 
			
		||||
	private final MinioClient minioClient;
 | 
			
		||||
	private MinioClient minioClient;
 | 
			
		||||
 | 
			
		||||
	public MinioCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) {
 | 
			
		||||
	@Override
 | 
			
		||||
	public void init(CloudStorageProperties cloudStorageProperties) {
 | 
			
		||||
		properties = cloudStorageProperties;
 | 
			
		||||
		try {
 | 
			
		||||
			minioClient = MinioClient.builder()
 | 
			
		||||
@@ -31,7 +32,7 @@ public class MinioCloudStorageStrategy extends AbstractCloudStorageStrategy {
 | 
			
		||||
				.build();
 | 
			
		||||
			createBucket();
 | 
			
		||||
		} catch (Exception e) {
 | 
			
		||||
			throw new IllegalArgumentException("Minio存储配置错误! 请检查系统配置!");
 | 
			
		||||
			throw new OssException("Minio存储配置错误! 请检查系统配置!");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -24,9 +24,10 @@ import java.io.InputStream;
 | 
			
		||||
 */
 | 
			
		||||
public class QcloudCloudStorageStrategy extends AbstractCloudStorageStrategy {
 | 
			
		||||
 | 
			
		||||
	private final COSClient client;
 | 
			
		||||
	private COSClient client;
 | 
			
		||||
 | 
			
		||||
	public QcloudCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) {
 | 
			
		||||
	@Override
 | 
			
		||||
	public void init(CloudStorageProperties cloudStorageProperties) {
 | 
			
		||||
		properties = cloudStorageProperties;
 | 
			
		||||
		try {
 | 
			
		||||
			COSCredentials credentials = new BasicCOSCredentials(
 | 
			
		||||
@@ -43,7 +44,7 @@ public class QcloudCloudStorageStrategy extends AbstractCloudStorageStrategy {
 | 
			
		||||
			client = new COSClient(credentials, clientConfig);
 | 
			
		||||
			createBucket();
 | 
			
		||||
		} catch (Exception e) {
 | 
			
		||||
			throw new IllegalArgumentException("腾讯云存储配置错误! 请检查系统配置!");
 | 
			
		||||
			throw new OssException("腾讯云存储配置错误! 请检查系统配置!");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -22,11 +22,12 @@ import java.io.InputStream;
 | 
			
		||||
 */
 | 
			
		||||
public class QiniuCloudStorageStrategy extends AbstractCloudStorageStrategy {
 | 
			
		||||
 | 
			
		||||
	private final UploadManager uploadManager;
 | 
			
		||||
	private final BucketManager bucketManager;
 | 
			
		||||
	private final String token;
 | 
			
		||||
	private UploadManager uploadManager;
 | 
			
		||||
	private BucketManager bucketManager;
 | 
			
		||||
	private String token;
 | 
			
		||||
 | 
			
		||||
	public QiniuCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) {
 | 
			
		||||
	@Override
 | 
			
		||||
	public void init(CloudStorageProperties cloudStorageProperties) {
 | 
			
		||||
		properties = cloudStorageProperties;
 | 
			
		||||
		try {
 | 
			
		||||
			Configuration config = new Configuration(getRegion(properties.getRegion()));
 | 
			
		||||
@@ -43,7 +44,7 @@ public class QiniuCloudStorageStrategy extends AbstractCloudStorageStrategy {
 | 
			
		||||
				bucketManager.createBucket(bucketName, properties.getRegion());
 | 
			
		||||
			}
 | 
			
		||||
		} catch (Exception e) {
 | 
			
		||||
			throw new IllegalArgumentException("七牛云存储配置错误! 请检查系统配置!");
 | 
			
		||||
			throw new OssException("七牛云存储配置错误! 请检查系统配置!");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -180,7 +180,7 @@ export default {
 | 
			
		||||
        // 调整光标到最后
 | 
			
		||||
        quill.setSelection(length + 1);
 | 
			
		||||
      } else {
 | 
			
		||||
        this.$message.error("图片插入失败");
 | 
			
		||||
        this.$message.error(res.msg);
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    handleUploadError() {
 | 
			
		||||
 
 | 
			
		||||
@@ -146,9 +146,14 @@ export default {
 | 
			
		||||
    },
 | 
			
		||||
    // 上传成功回调
 | 
			
		||||
    handleUploadSuccess(res, file) {
 | 
			
		||||
      this.$message.success("上传成功");
 | 
			
		||||
      this.fileList.push({ name: res.data.fileName, url: res.data.fileName });
 | 
			
		||||
      this.$emit("input", this.listToString(this.fileList));
 | 
			
		||||
      if (res.code === 200) {
 | 
			
		||||
        this.$message.success("上传成功");
 | 
			
		||||
        this.fileList.push({ name: res.data.fileName, url: res.data.fileName });
 | 
			
		||||
        this.$emit("input", this.listToString(this.fileList));
 | 
			
		||||
      } else {
 | 
			
		||||
        this.$message.error(res.msg);
 | 
			
		||||
        this.loading.close();
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    // 删除文件
 | 
			
		||||
    handleDelete(index) {
 | 
			
		||||
@@ -170,9 +175,9 @@ export default {
 | 
			
		||||
      for (let i in list) {
 | 
			
		||||
        strs += list[i].url + separator;
 | 
			
		||||
      }
 | 
			
		||||
      return strs != '' ? strs.substr(0, strs.length - 1) : '';
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
      return strs != "" ? strs.substr(0, strs.length - 1) : "";
 | 
			
		||||
    },
 | 
			
		||||
  },
 | 
			
		||||
};
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -118,9 +118,14 @@ export default {
 | 
			
		||||
    },
 | 
			
		||||
    // 上传成功回调
 | 
			
		||||
    handleUploadSuccess(res) {
 | 
			
		||||
      this.fileList.push({ name: res.data.fileName, url: res.data.url });
 | 
			
		||||
      this.$emit("input", this.listToString(this.fileList));
 | 
			
		||||
      this.loading.close();
 | 
			
		||||
      if (res.code == 200) {
 | 
			
		||||
        this.fileList.push({ name: res.data.fileName, url: res.data.url });
 | 
			
		||||
        this.$emit("input", this.listToString(this.fileList));
 | 
			
		||||
        this.loading.close();
 | 
			
		||||
      } else {
 | 
			
		||||
        this.$message.error(res.msg);
 | 
			
		||||
        this.loading.close();
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    // 上传前loading加载
 | 
			
		||||
    handleBeforeUpload(file) {
 | 
			
		||||
@@ -130,7 +135,7 @@ export default {
 | 
			
		||||
        if (file.name.lastIndexOf(".") > -1) {
 | 
			
		||||
          fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
 | 
			
		||||
        }
 | 
			
		||||
        isImg = this.fileType.some(type => {
 | 
			
		||||
        isImg = this.fileType.some((type) => {
 | 
			
		||||
          if (file.type.indexOf(type) > -1) return true;
 | 
			
		||||
          if (fileExtension && fileExtension.indexOf(type) > -1) return true;
 | 
			
		||||
          return false;
 | 
			
		||||
@@ -163,7 +168,7 @@ export default {
 | 
			
		||||
      this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`);
 | 
			
		||||
    },
 | 
			
		||||
    // 上传失败
 | 
			
		||||
    handleUploadError() {
 | 
			
		||||
    handleUploadError(res) {
 | 
			
		||||
      this.$message({
 | 
			
		||||
        type: "error",
 | 
			
		||||
        message: "上传失败",
 | 
			
		||||
@@ -182,25 +187,26 @@ export default {
 | 
			
		||||
      for (let i in list) {
 | 
			
		||||
        strs += list[i].url + separator;
 | 
			
		||||
      }
 | 
			
		||||
      return strs != '' ? strs.substr(0, strs.length - 1) : '';
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
      return strs != "" ? strs.substr(0, strs.length - 1) : "";
 | 
			
		||||
    },
 | 
			
		||||
  },
 | 
			
		||||
};
 | 
			
		||||
</script>
 | 
			
		||||
<style scoped lang="scss">
 | 
			
		||||
// .el-upload--picture-card 控制加号部分
 | 
			
		||||
::v-deep.hide .el-upload--picture-card {
 | 
			
		||||
    display: none;
 | 
			
		||||
  display: none;
 | 
			
		||||
}
 | 
			
		||||
// 去掉动画效果
 | 
			
		||||
::v-deep .el-list-enter-active,
 | 
			
		||||
::v-deep .el-list-leave-active {
 | 
			
		||||
    transition: all 0s;
 | 
			
		||||
  transition: all 0s;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
::v-deep .el-list-enter, .el-list-leave-active {
 | 
			
		||||
    opacity: 0;
 | 
			
		||||
    transform: translateY(0);
 | 
			
		||||
::v-deep .el-list-enter,
 | 
			
		||||
.el-list-leave-active {
 | 
			
		||||
  opacity: 0;
 | 
			
		||||
  transform: translateY(0);
 | 
			
		||||
}
 | 
			
		||||
</style>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -138,7 +138,7 @@
 | 
			
		||||
          <el-input v-model="form.accessKey" placeholder="请输入accessKey" />
 | 
			
		||||
        </el-form-item>
 | 
			
		||||
        <el-form-item label="secretKey" prop="secretKey">
 | 
			
		||||
          <el-input v-model="form.secretKey" placeholder="请输入秘钥" />
 | 
			
		||||
          <el-input v-model="form.secretKey" placeholder="请输入秘钥" show-password />
 | 
			
		||||
        </el-form-item>
 | 
			
		||||
        <el-form-item label="桶名称" prop="bucketName">
 | 
			
		||||
          <el-input v-model="form.bucketName" placeholder="请输入桶名称" />
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user