index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div class="ant-upload-preview">
  3. <div style="width: 100%">
  4. <el-upload class="avatar-uploader" :class="[disabled ? 'uploadDisabled' : null]" :disabled="disabled" :accept="accept" :show-file-list="false" action :before-upload="beforeUpload" :http-request="handleChange">
  5. <img v-if="imageUrl" :src="imageUrl" class="avatar" />
  6. <span v-else>
  7. <i v-if="loading" class="el-icon-loading avatar-uploader-icon"></i>
  8. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  9. <span class="upload-desc">添加上传图片</span>
  10. </span>
  11. </el-upload>
  12. </div>
  13. <!-- modal -->
  14. <cropper-modal ref="CropperModal" @cropper-no="handleCropperClose" @cropper-ok="handleCropperSuccess"></cropper-modal>
  15. </div>
  16. </template>
  17. <script>
  18. import CropperModal from './CropperModal'
  19. export default {
  20. name: 'ImageCropper',
  21. components: {
  22. CropperModal
  23. },
  24. props: {
  25. //图片裁切配置
  26. options: {
  27. type: Object,
  28. default: function() {
  29. return {
  30. autoCrop: true, //是否默认生成截图框
  31. autoCropWidth: 200, //默认生成截图框宽度
  32. autoCropHeight: 200, //默认生成截图框高度
  33. fixedBox: false, //是否固定截图框大小 不允许改变
  34. previewsCircle: true, //预览图是否是原圆形
  35. title: '上传图片'
  36. }
  37. }
  38. },
  39. // 上传图片的大小,单位M
  40. imgSize: {
  41. type: Number,
  42. default: 2
  43. },
  44. // 图片地址
  45. imageUrl: {
  46. type: String,
  47. default: ''
  48. },
  49. accept: {
  50. type: String,
  51. default: '.png,.jpg,.jpeg,.gif'
  52. },
  53. acceptArray: {
  54. type: [Array, Object],
  55. default() {
  56. return ['image/jpeg', 'image/png', 'image/jpg', 'image/gif']
  57. }
  58. },
  59. disabled: {
  60. type: Boolean,
  61. default: false
  62. }
  63. },
  64. data() {
  65. return {
  66. loading: false,
  67. isStopRun: false
  68. }
  69. },
  70. methods: {
  71. //从本地选择文件
  72. handleChange(info) {
  73. if (this.isStopRun) {
  74. return
  75. }
  76. this.loading = true
  77. const { options } = this
  78. console.log(info)
  79. this.getBase64(info.file, imageUrl => {
  80. const target = Object.assign({}, options, {
  81. img: imageUrl,
  82. name: info.file.name // 上传文件名
  83. })
  84. this.$refs.CropperModal.edit(target)
  85. })
  86. },
  87. // 上传之前 格式与大小校验
  88. beforeUpload(file) {
  89. this.isStopRun = false
  90. var fileType = file.type
  91. if (fileType.indexOf('image') < 0) {
  92. this.$message.warning('请上传图片')
  93. this.isStopRun = true
  94. return false
  95. }
  96. const isJpgOrPng = this.acceptArray.includes(file.type)
  97. // file.type === 'image/jpeg' ||
  98. // file.type === 'image/png' ||
  99. // file.type === 'image/jpg'
  100. if (!isJpgOrPng) {
  101. this.$message.error('你上传图片格式不正确!')
  102. this.isStopRun = true
  103. }
  104. const isLtSize = file.size < this.imgSize * 1024 * 1024
  105. if (!isLtSize) {
  106. this.$message.error('图片大小不能超过' + this.imgSize + 'MB!')
  107. this.isStopRun = true
  108. }
  109. return isJpgOrPng && isLtSize
  110. },
  111. //获取服务器返回的地址
  112. handleCropperSuccess(data) {
  113. //将返回的数据回显
  114. this.loading = false
  115. this.$emit('crop-upload-success', data)
  116. },
  117. // 取消上传
  118. handleCropperClose() {
  119. this.loading = false
  120. this.$emit('crop-upload-close')
  121. },
  122. getBase64(img, callback) {
  123. const reader = new FileReader()
  124. reader.addEventListener('load', () => callback(reader.result))
  125. reader.readAsDataURL(img)
  126. }
  127. }
  128. }
  129. </script>
  130. <style lang="scss" scoped>
  131. .ant-upload-preview {
  132. background-color: #fff;
  133. .avatar-uploader {
  134. .upload-desc {
  135. position: absolute;
  136. bottom: 0;
  137. left: 0;
  138. color: #ccc;
  139. font-size: 8px;
  140. right: 0;
  141. }
  142. }
  143. }
  144. ::v-deep .avatar-uploader .el-upload--text {
  145. border: 1px dashed #d9d9d9;
  146. border-radius: 6px;
  147. margin-right: 20px;
  148. cursor: pointer;
  149. position: relative;
  150. overflow: hidden;
  151. }
  152. ::v-deep .avatar-uploader.uploadDisabled {
  153. .el-upload--text {
  154. cursor: not-allowed;
  155. background-color: #f7f7f7;
  156. }
  157. .el-upload:hover {
  158. border-color: #d9d9d9;
  159. }
  160. }
  161. ::v-deep .avatar-uploader .el-upload:hover {
  162. border-color: #409eff;
  163. }
  164. ::v-deep .avatar-uploader-icon {
  165. font-size: 22px;
  166. color: #ccc;
  167. width: 108px;
  168. height: 108px;
  169. line-height: 108px;
  170. text-align: center;
  171. }
  172. ::v-deep.avatar {
  173. width: 108px;
  174. height: 108px;
  175. display: block;
  176. }
  177. </style>