editorImage.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <div class="upload-container">
  3. <el-button icon='el-icon-upload' size="mini" :style="{background:color,borderColor:color}"
  4. @click=" dialogVisible=true" type="primary">上传图片
  5. </el-button>
  6. <el-dialog append-to-body :visible.sync="dialogVisible">
  7. <el-upload class="editor-slide-upload"
  8. :action="useOss?ossUploadUrl:minioUploadUrl"
  9. :data="useOss?dataObj:null"
  10. :multiple="true"
  11. :file-list="fileList"
  12. :show-file-list="true"
  13. list-type="picture-card"
  14. :on-remove="handleRemove"
  15. :on-success="handleSuccess"
  16. :before-upload="beforeUpload">
  17. <el-button size="small" type="primary">点击上传</el-button>
  18. </el-upload>
  19. <el-button @click="dialogVisible = false">取 消</el-button>
  20. <el-button type="primary" @click="handleSubmit">确 定</el-button>
  21. </el-dialog>
  22. </div>
  23. </template>
  24. <script>
  25. import {policy} from '@/api/oss'
  26. export default {
  27. name: 'editorSlideUpload',
  28. props: {
  29. color: {
  30. type: String,
  31. default: '#1890ff'
  32. }
  33. },
  34. data() {
  35. return {
  36. dialogVisible: false,
  37. listObj: {},
  38. fileList: [],
  39. dataObj: {
  40. policy: '',
  41. signature: '',
  42. key: '',
  43. ossaccessKeyId: '',
  44. dir: '',
  45. host: ''
  46. },
  47. useOss:false, //使用oss->true;使用MinIO->false
  48. ossUploadUrl:'http://macro-oss.oss-cn-shenzhen.aliyuncs.com',
  49. minioUploadUrl:'http://localhost:8080/minio/upload',
  50. }
  51. },
  52. methods: {
  53. checkAllSuccess() {
  54. return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
  55. },
  56. handleSubmit() {
  57. const arr = Object.keys(this.listObj).map(v => this.listObj[v])
  58. if (!this.checkAllSuccess()) {
  59. this.$message('请等待所有图片上传成功 或 出现了网络问题,请刷新页面重新上传!')
  60. return
  61. }
  62. console.log(arr);
  63. this.$emit('successCBK', arr);
  64. this.listObj = {};
  65. this.fileList = [];
  66. this.dialogVisible = false;
  67. },
  68. handleSuccess(response, file) {
  69. const uid = file.uid;
  70. const objKeyArr = Object.keys(this.listObj)
  71. for (let i = 0, len = objKeyArr.length; i < len; i++) {
  72. if (this.listObj[objKeyArr[i]].uid === uid) {
  73. this.listObj[objKeyArr[i]].url = this.dataObj.host + '/' + this.dataObj.dir + '/' + file.name;
  74. if(!this.useOss){
  75. //不使用oss直接获取图片路径
  76. this.listObj[objKeyArr[i]].url = response.data.url;
  77. }
  78. this.listObj[objKeyArr[i]].hasSuccess = true;
  79. return
  80. }
  81. }
  82. },
  83. handleRemove(file) {
  84. const uid = file.uid;
  85. const objKeyArr = Object.keys(this.listObj);
  86. for (let i = 0, len = objKeyArr.length; i < len; i++) {
  87. if (this.listObj[objKeyArr[i]].uid === uid) {
  88. delete this.listObj[objKeyArr[i]];
  89. return
  90. }
  91. }
  92. },
  93. beforeUpload(file) {
  94. const _self = this
  95. const fileName = file.uid;
  96. this.listObj[fileName] = {};
  97. if(!this.useOss){
  98. //不使用oss不需要获取策略
  99. this.listObj[fileName] = {hasSuccess: false, uid: file.uid, width: this.width, height: this.height};
  100. return true;
  101. }
  102. return new Promise((resolve, reject) => {
  103. policy().then(response => {
  104. _self.dataObj.policy = response.data.policy;
  105. _self.dataObj.signature = response.data.signature;
  106. _self.dataObj.ossaccessKeyId = response.data.accessKeyId;
  107. _self.dataObj.key = response.data.dir + '/${filename}';
  108. _self.dataObj.dir = response.data.dir;
  109. _self.dataObj.host = response.data.host;
  110. _self.listObj[fileName] = {hasSuccess: false, uid: file.uid, width: this.width, height: this.height};
  111. resolve(true)
  112. }).catch(err => {
  113. console.log(err)
  114. reject(false)
  115. })
  116. })
  117. }
  118. }
  119. }
  120. </script>
  121. <style rel="stylesheet/scss" lang="scss" scoped>
  122. .upload-container .editor-slide-upload{
  123. margin-bottom: 20px;
  124. }
  125. </style>