ImageComponent.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <div class="imgBox">
  3. <div @click="addImg($event)" class="imgCss">
  4. <img src="../../../assets/images/course/imageUpload.png" alt="" />
  5. <input type="file" accept="image/*" style="display: none" @change="beforeUpload($event)" />
  6. </div>
  7. <div class="imgList">
  8. <div v-for="(item, index) in imgList" :key="index" class="imgItem">
  9. <div class="itemImg" @click="previewImg(item)">
  10. <img :src="item" alt="" />
  11. </div>
  12. <div class="deleteImg" @click="deleteItem(item)">
  13. <img src="../../../assets/images/course/delete.png" alt="" />
  14. </div>
  15. </div>
  16. </div>
  17. <div v-if="proVisible" class="mask">
  18. <div class="progressBox">
  19. <div class="lbox"><img :src="require('../../../assets/images/loading.gif')" />上传中,请稍后</div>
  20. <el-progress :text-inside="true" :stroke-width="20" :percentage="progress" style="width: 80%"></el-progress>
  21. </div>
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. export default {
  27. props: {
  28. imgList: {
  29. type: Array,
  30. default: () => {
  31. return []
  32. }
  33. }
  34. },
  35. data() {
  36. return {
  37. // imgList: []
  38. proVisible: false,
  39. progress: 0
  40. }
  41. },
  42. methods: {
  43. previewImg(url) {
  44. this.$hevueImgPreview(url)
  45. },
  46. addImg(e) {
  47. var el = e.currentTarget
  48. el.getElementsByTagName('input')[0].click()
  49. e.target.value = ''
  50. },
  51. deleteItem(i) {
  52. const a = JSON.parse(JSON.stringify(this.imgList))
  53. a.splice(a.indexOf(i), 1)
  54. // this.imgList.splice(this.imgList.indexOf(i), 1)
  55. this.$emit('update:imgList', a)
  56. this.$forceUpdate()
  57. },
  58. beforeUpload(event) {
  59. var file = event.target.files[0]
  60. var credentials = {
  61. accessKeyId: 'AKIATLPEDU37QV5CHLMH',
  62. secretAccessKey: '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'
  63. } // 秘钥形式的登录上传
  64. window.AWS.config.update(credentials)
  65. window.AWS.config.region = 'cn-northwest-1' // 设置区域
  66. var bucket = new window.AWS.S3({ params: { Bucket: 'ccrb' }}) // 选择桶
  67. var _this = this
  68. _this.progress = 0
  69. _this.proVisible = true
  70. if (file) {
  71. var params = {
  72. Key:
  73. file.name.split('.')[0] +
  74. new Date().getTime() +
  75. '.' +
  76. file.name.split('.')[file.name.split('.').length - 1],
  77. ContentType: file.type,
  78. Body: file,
  79. 'Access-Control-Allow-Credentials': '*',
  80. ACL: 'public-read'
  81. } // key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
  82. var options = {
  83. partSize: 2048 * 1024 * 1024,
  84. queueSize: 2,
  85. leavePartsOnError: true
  86. }
  87. bucket
  88. .upload(params, options)
  89. .on('httpUploadProgress', function(evt) {
  90. // 这里可以写进度条
  91. _this.progress = parseInt((evt.loaded * 80) / evt.total)
  92. })
  93. .send(function(err, data) {
  94. _this.progress = 100
  95. _this.mediaLoading = false
  96. setTimeout(() => {
  97. _this.proVisible = false
  98. }, 1000)
  99. if (err) {
  100. // _this.$message.error('上传失败')
  101. } else {
  102. _this.$toast({
  103. message: '上传成功',
  104. type: 'success'
  105. })
  106. const a = JSON.parse(JSON.stringify(_this.imgList))
  107. a.push(data.Location)
  108. _this.$emit('update:imgList', a)
  109. // _this.getImage(_this.imgList)
  110. }
  111. })
  112. }
  113. },
  114. getImage(imageList) {
  115. this.$emit('getImage', imageList)
  116. }
  117. }
  118. }
  119. </script>
  120. <style lang="scss" scoped>
  121. .imgBox {
  122. margin: 10px;
  123. display: flex;
  124. flex-direction: row;
  125. flex-wrap: wrap;
  126. align-items: center;
  127. .imgCss {
  128. width: 50px;
  129. height: 50px;
  130. margin-left: 0 0.3rem;
  131. > img {
  132. width: 100%;
  133. height: 100%;
  134. }
  135. }
  136. .imgList {
  137. display: flex;
  138. flex-direction: row;
  139. flex-wrap: wrap;
  140. align-items: center;
  141. .imgItem {
  142. position: relative;
  143. width: 50px;
  144. margin: 0 10px 10px 0;
  145. .itemImg {
  146. width: 50px;
  147. height: 50px;
  148. z-index: 1;
  149. > img {
  150. width: 100%;
  151. height: 100%;
  152. object-fit: cover;
  153. }
  154. }
  155. .deleteImg {
  156. width: 20px;
  157. height: 20px;
  158. position: absolute;
  159. right: 0;
  160. top: 0;
  161. z-index: 9;
  162. > img {
  163. width: 100%;
  164. height: 100%;
  165. }
  166. }
  167. }
  168. }
  169. .mask {
  170. background-color: rgba(0, 0, 0, 0);
  171. position: fixed;
  172. top: 0;
  173. left: 0;
  174. width: 100%;
  175. height: 100%;
  176. z-index: 20000;
  177. display: flex;
  178. align-items: center;
  179. justify-content: center;
  180. }
  181. .progressBox {
  182. width: 85%;
  183. max-width: 300px;
  184. height: 100px;
  185. background: #fff;
  186. border-radius: 10px;
  187. box-shadow: 0 0 6px 1px #bfbfbf;
  188. display: flex;
  189. align-items: center;
  190. justify-content: center;
  191. flex-direction: column;
  192. }
  193. .progressBox .lbox {
  194. height: 50px;
  195. font-size: 14px;
  196. display: flex;
  197. align-items: center;
  198. }
  199. .progressBox .lbox img {
  200. width: 10px;
  201. margin-right: 10px;
  202. }
  203. /deep/ .progressBox {
  204. .el-progress-bar__outer {
  205. background-color: #d1dfff !important;
  206. height: 20px !important;
  207. }
  208. }
  209. }
  210. </style>