editorImage.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div class="upload-container">
  3. <el-button
  4. :disabled="disabled"
  5. icon="el-icon-upload"
  6. size="mini"
  7. :style="{ background: color, borderColor: color }"
  8. @click="dialogVisible = true"
  9. type="primary"
  10. >上传图片
  11. </el-button>
  12. <el-dialog append-to-body :visible.sync="dialogVisible">
  13. <el-upload
  14. :disabled="disabled"
  15. class="editor-slide-upload"
  16. :action="ossUploadUrl"
  17. :data="dataObj"
  18. :multiple="true"
  19. :file-list="fileList"
  20. :show-file-list="true"
  21. list-type="picture-card"
  22. :on-remove="handleRemove"
  23. :on-success="handleSuccess"
  24. :before-upload="beforeUpload"
  25. >
  26. <el-button size="small" type="primary">点击上传</el-button>
  27. </el-upload>
  28. <el-button @click="dialogVisible = false">取 消</el-button>
  29. <el-button type="primary" @click="handleSubmit">确 定</el-button>
  30. </el-dialog>
  31. </div>
  32. </template>
  33. <script>
  34. import { policy } from "@/api/oss";
  35. export default {
  36. name: "editorSlideUpload",
  37. props: {
  38. color: {
  39. type: String,
  40. default: "#1890ff",
  41. },
  42. disabled: {
  43. type: Boolean,
  44. default: false,
  45. },
  46. bucket_name: {
  47. type: String,
  48. default: "mall",
  49. },
  50. },
  51. data() {
  52. return {
  53. dialogVisible: false,
  54. listObj: {},
  55. fileList: [],
  56. dataObj: {
  57. policy: "",
  58. signature: "",
  59. key: "",
  60. KSSAccessKeyId: "",
  61. // dir: "",
  62. acl: "public-read",
  63. name: "",
  64. },
  65. // ossUploadUrl: "https://ks3-cn-beijing.ksyuncs.com/mall",
  66. ossUploadUrl: `https://mall.ks3-cn-beijing.ksyuncs.com`,
  67. };
  68. },
  69. methods: {
  70. checkAllSuccess() {
  71. return Object.keys(this.listObj).every(
  72. (item) => this.listObj[item].hasSuccess
  73. );
  74. },
  75. handleSubmit() {
  76. const arr = Object.keys(this.listObj).map((v) => this.listObj[v]);
  77. if (!this.checkAllSuccess()) {
  78. this.$message(
  79. "请等待所有图片上传成功 或 出现了网络问题,请刷新页面重新上传!"
  80. );
  81. return;
  82. }
  83. console.log(arr);
  84. this.$emit("successCBK", arr);
  85. this.listObj = {};
  86. this.fileList = [];
  87. this.dialogVisible = false;
  88. },
  89. handleSuccess(response, file) {
  90. const uid = file.uid;
  91. const objKeyArr = Object.keys(this.listObj);
  92. console.log(objKeyArr, "objKeyArr", this.listObj);
  93. for (let i = 0, len = objKeyArr.length; i < len; i++) {
  94. if (this.listObj[objKeyArr[i]].uid === uid) {
  95. this.listObj[objKeyArr[i]].url =
  96. this.ossUploadUrl + "/" + this.listObj[objKeyArr[i]].key;
  97. this.listObj[objKeyArr[i]].hasSuccess = true;
  98. return;
  99. }
  100. }
  101. },
  102. handleRemove(file) {
  103. const uid = file.uid;
  104. const objKeyArr = Object.keys(this.listObj);
  105. for (let i = 0, len = objKeyArr.length; i < len; i++) {
  106. if (this.listObj[objKeyArr[i]].uid === uid) {
  107. delete this.listObj[objKeyArr[i]];
  108. return;
  109. }
  110. }
  111. },
  112. beforeUpload(file) {
  113. console.log(file, "试试批量上传");
  114. const _self = this;
  115. const fileName = file.uid;
  116. this.listObj[fileName] = {};
  117. return new Promise((resolve, reject) => {
  118. let key = new Date().getTime() + file.name;
  119. let obj = {
  120. filename: file.name,
  121. bucketName: this.bucket_name,
  122. postData: {
  123. filename: file.name,
  124. acl: "public-read",
  125. key: key,
  126. unknowValueField: [],
  127. },
  128. };
  129. policy(obj)
  130. .then((response) => {
  131. _self.dataObj.policy = response.data.policy;
  132. _self.dataObj.signature = response.data.signature;
  133. _self.dataObj.KSSAccessKeyId = response.data.kssAccessKeyId;
  134. _self.dataObj.key = key;
  135. _self.dataObj.name = file.name;
  136. _self.dataObj.acl = "public-read";
  137. _self.listObj[fileName] = {
  138. hasSuccess: false,
  139. uid: file.uid,
  140. width: this.width,
  141. height: this.height,
  142. name: file.name,
  143. key: key,
  144. };
  145. resolve(true);
  146. })
  147. .catch((err) => {
  148. console.log(err);
  149. reject(false);
  150. });
  151. });
  152. },
  153. },
  154. };
  155. </script>
  156. <style rel="stylesheet/scss" lang="scss" scoped>
  157. .upload-container .editor-slide-upload {
  158. margin-bottom: 20px;
  159. }
  160. </style>