UploadPhoto.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <!-- 上传图片组件 -->
  2. <template>
  3. <div class="photo-container">
  4. <el-upload
  5. action="#"
  6. list-type="picture-card"
  7. :auto-upload="false"
  8. accept=".png,.jpg"
  9. class="photo-uploader"
  10. :on-remove="handleRemove"
  11. :on-preview="handlePictureCardPreview"
  12. :on-change="handleChange"
  13. :file-list="defaultArr"
  14. >
  15. <i slot="default" class="el-icon-camera-solid" />
  16. <div slot="file" slot-scope="{file}" style="width:100%;height:100%">
  17. <img
  18. class="el-upload-list__item-thumbnail showImg"
  19. :src="file.url"
  20. alt=""
  21. >
  22. <span class="el-upload-list__item-actions">
  23. <!-- 预览大图 -->
  24. <span
  25. v-if="(photoInfo.url && photoInfo.size != 0)||file.size>0"
  26. class="el-upload-list__item-preview"
  27. @click="handlePictureCardPreview(file)"
  28. >
  29. <i class="el-icon-zoom-in" />
  30. </span>
  31. <!-- 下载
  32. <span
  33. v-if="!disabled"
  34. class="el-upload-list__item-delete"
  35. @click="handleRemove(file)"
  36. >
  37. <i class="el-icon-download" />
  38. </span> -->
  39. <!-- 删除按钮 -->
  40. <span
  41. v-if="(photoInfo.url && photoInfo.size != 0)||file.size>0"
  42. class="el-upload-list__item-delete"
  43. @click="handleRemove(file)"
  44. >
  45. <i class="el-icon-delete" />
  46. </span>
  47. </span>
  48. </div>
  49. </el-upload>
  50. <el-dialog :visible.sync="dialogVisible">
  51. <img width="100%" :src="dialogImageUrl" alt="">
  52. </el-dialog>
  53. </div>
  54. </template>
  55. <script>
  56. export default {
  57. props: {
  58. photoInfo: {
  59. type: Object,
  60. default: function() {
  61. return {}
  62. }
  63. }
  64. },
  65. data() {
  66. return {
  67. dialogImageUrl: '',
  68. dialogVisible: false,
  69. defaultArr: []
  70. }
  71. },
  72. mounted() {
  73. this.$nextTick(() => {
  74. // 网页加载完成后执行
  75. this.defaultArr = [{ name: this.photoInfo.name, url: this.photoInfo.url }]
  76. })
  77. },
  78. methods: {
  79. // 删除图片
  80. handleRemove() {
  81. // console.log('delflie', file)
  82. this.defaultArr = [{ name: this.photoInfo.name, url: undefined }]
  83. // 如果 photoInfo.url && photoInfo.size != 0 表示是后端已经保存过的数据,提交到父组件向后端发起删除请求
  84. if (this.photoInfo.url && this.photoInfo.size !== 0) {
  85. // 向父组件传值
  86. this.$emit('photoRemoved', this.photoInfo.name)
  87. } else {
  88. this.$emit('delphoto', this.photoInfo.name)
  89. }
  90. },
  91. // 预览图片
  92. handlePictureCardPreview(file) {
  93. this.dialogImageUrl = file.url
  94. this.dialogVisible = true
  95. // console.log('handlePictureCardPreview', file)
  96. },
  97. // 图片有改动
  98. handleChange(file, fileList) {
  99. // console.log('file', file)
  100. const fileSize = Number(file.size / 1024 / 1024)
  101. // console.log('fileSize', fileSize)
  102. if (fileSize > 1) {
  103. this.$message({
  104. message: '照片大小不能超过1MB',
  105. type: 'warning',
  106. offset: window.screen.height / 3
  107. })
  108. fileList.pop()
  109. return
  110. }
  111. if (this.photoInfo.url || fileList[0].url) {
  112. this.$message({
  113. message: '覆盖前一个文件',
  114. type: 'warning',
  115. offset: window.screen.height / 3
  116. })
  117. }
  118. fileList.splice(0, 1)
  119. // 向父组件传值
  120. this.$emit('photoChanged', file, this.photoInfo.name)
  121. }
  122. }
  123. }
  124. </script>
  125. <style lang="scss">
  126. $photoSize: 100px;
  127. .photo-uploader {
  128. display: flex;
  129. }
  130. .el-upload-list {
  131. // border: 1px #f00 solid;
  132. display: block;
  133. width: $photoSize;
  134. height: $photoSize;
  135. }
  136. .el-upload--picture-card {
  137. display: block;
  138. width: $photoSize;
  139. height: $photoSize;
  140. line-height: $photoSize;
  141. overflow: hidden;
  142. }
  143. .el-upload-list--picture-card .el-upload-list__item {
  144. width: $photoSize;
  145. height: $photoSize;
  146. overflow: hidden;
  147. }
  148. .showImg{
  149. width: 100%;
  150. height: 100%;
  151. object-position: center;
  152. object-fit:fill;
  153. }
  154. </style>