editorImage.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div class="upload-container">
  3. <!--<el-button :style="{background:color,borderColor:color}" icon="el-icon-upload" size="mini" type="primary" @click="opUpload('img')">上传图片-->
  4. <!--</el-button>-->
  5. <!--<el-button :style="{background:color,borderColor:color}" icon="el-icon-upload" size="mini" type="primary" @click="opUpload('video')">上传视频-->
  6. <!--</el-button>-->
  7. <el-dialog :visible.sync="dialogVisible" :modal="false">
  8. <el-upload v-if="uploadType === 'file'"
  9. :multiple="true"
  10. :file-list="fileList"
  11. :show-file-list="true"
  12. :on-remove="handleRemove"
  13. :on-success="handleSuccess"
  14. :before-upload="beforeUpload"
  15. :accept="'.pdf,.ppt,.pptx,.xls,.xml,.wps,.xlsx,.doc,.docx,.txt,.rtf,.md'"
  16. class="editor-slide-upload"
  17. :on-error="onError"
  18. :list-type="'text'"
  19. :action="uploadFileUrl">
  20. <el-button size="small" type="primary">点击上传附件</el-button>
  21. </el-upload>
  22. <el-upload v-else
  23. :multiple="true"
  24. :file-list="fileList"
  25. :show-file-list="true"
  26. :on-remove="handleRemove"
  27. :on-error="onError"
  28. :on-success="handleSuccess"
  29. :before-upload="beforeUpload"
  30. :accept="uploadType === 'img'? 'image/*':'.mp4,.mov,.avi'"
  31. class="editor-slide-upload"
  32. :list-type="uploadType === 'img'? 'picture-card': 'text'"
  33. :action="uploadType === 'img'? uploadUrl:uploadVideoUrl">
  34. <el-button v-if="uploadType === 'img'" size="small" type="primary">点击上传图片</el-button>
  35. <el-button v-else size="small" type="primary">点击上传视频</el-button>
  36. </el-upload>
  37. <el-button @click="dialogVisible = false">取 消</el-button>
  38. <el-button type="primary" @click="handleSubmit">确 定</el-button>
  39. </el-dialog>
  40. </div>
  41. </template>
  42. <script>
  43. // import { getToken } from 'api/qiniu'
  44. export default {
  45. name: 'EditorSlideUpload',
  46. props: {
  47. color: {
  48. type: String,
  49. default: '#2973fd'
  50. }
  51. },
  52. data() {
  53. return {
  54. uploadUrl: "",
  55. uploadFileUrl: "",
  56. uploadVideoUrl: "",
  57. dialogVisible: false,
  58. listObj: {},
  59. fileList: [],
  60. uploadType: 'img',
  61. }
  62. },
  63. mounted() {
  64. this.uploadUrl = this.fun.getRealUrl("upload.uploadPic", {});
  65. this.uploadFileUrl = this.fun.getRealUrl("upload.uploadPic", {upload_type: 'file'});
  66. this.uploadVideoUrl = this.fun.getRealUrl("upload.uploadPic", {upload_type: 'videos'});
  67. },
  68. methods: {
  69. opUpload(str) {
  70. this.uploadType = str;
  71. this.dialogVisible=true
  72. },
  73. checkAllSuccess() {
  74. return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
  75. },
  76. handleSubmit() {
  77. console.log(this.listObj)
  78. const arr = Object.keys(this.listObj).map(v => this.listObj[v])
  79. if (!this.checkAllSuccess()) {
  80. this.$message('请等待所有图片上传成功 或 出现了网络问题,请刷新页面重新上传!')
  81. return
  82. }
  83. this.$emit('successCBK', {uploadType: this.uploadType, arr: arr})
  84. this.listObj = {}
  85. this.fileList = []
  86. this.dialogVisible = false
  87. },
  88. handleSuccess(response, file) {
  89. if(response.result == 0) {
  90. this.$message.error(response.msg);
  91. return;
  92. }
  93. const uid = file.uid
  94. const objKeyArr = Object.keys(this.listObj)
  95. for (let i = 0, len = objKeyArr.length; i < len; i++) {
  96. if (this.listObj[objKeyArr[i]].uid === uid) {
  97. this.listObj[objKeyArr[i]].url = response.data.img_url
  98. this.listObj[objKeyArr[i]].hasSuccess = true
  99. return
  100. }
  101. }
  102. },
  103. handleRemove(file) {
  104. const uid = file.uid
  105. const objKeyArr = Object.keys(this.listObj)
  106. for (let i = 0, len = objKeyArr.length; i < len; i++) {
  107. if (this.listObj[objKeyArr[i]].uid === uid) {
  108. delete this.listObj[objKeyArr[i]]
  109. return
  110. }
  111. }
  112. },
  113. beforeUpload(file) {
  114. const _self = this
  115. const _URL = window.URL || window.webkitURL
  116. const fileName = file.uid
  117. this.listObj[fileName] = {}
  118. return new Promise((resolve, reject) => {
  119. if(this.uploadType ==='img') {
  120. const img = new Image()
  121. img.src = _URL.createObjectURL(file)
  122. img.onload = function() {
  123. _self.listObj[fileName] = { hasSuccess: false, uid: file.uid, width: this.width, height: this.height }
  124. }
  125. }else {
  126. _self.listObj[fileName] = { hasSuccess: false, uid: file.uid, width: 0, height: 0, name: file.name }
  127. }
  128. resolve(true)
  129. })
  130. },
  131. onError(err) {
  132. console.log(err)
  133. }
  134. }
  135. }
  136. </script>
  137. <style rel="stylesheet/scss" lang="scss" scoped>
  138. .editor-slide-upload {
  139. margin-bottom: 20px;
  140. text-align: center;
  141. :deep(.el-upload--picture-card) {
  142. width: 100%;
  143. }
  144. }
  145. </style>
  146. <style>
  147. .tinymce-box .el-upload-list{ list-style: none;}
  148. .tinymce-box .el-upload-list li {list-style:none}
  149. </style>