index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div>
  3. <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @opened="modalOpened" @close="closeDialog">
  4. <el-row>
  5. <el-col :xs="24" :md="12" :style="{ height: '350px' }">
  6. <vue-cropper
  7. ref="cropper"
  8. :img="options.img"
  9. :info="true"
  10. :autoCrop="options.autoCrop"
  11. :autoCropWidth="options.autoCropWidth"
  12. :autoCropHeight="options.autoCropHeight"
  13. :fixedBox="options.fixedBox"
  14. @realTime="realTime"
  15. v-if="visible"
  16. />
  17. </el-col>
  18. <el-col :xs="24" :md="12" :style="{ height: '350px' }">
  19. <div class="avatar-upload-preview">
  20. <img :src="previews.url" :style="previews.img" />
  21. </div>
  22. </el-col>
  23. </el-row>
  24. <br />
  25. <el-row>
  26. <el-col :lg="2" :md="2">
  27. <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
  28. <el-button size="small">
  29. 选择
  30. <i class="el-icon-upload el-icon--right"></i>
  31. </el-button>
  32. </el-upload>
  33. </el-col>
  34. <el-col :lg="{ span: 1, offset: 2 }" :md="2">
  35. <el-button icon="el-icon-plus" size="small" @click="changeScale(1)"></el-button>
  36. </el-col>
  37. <el-col :lg="{ span: 1, offset: 1 }" :md="2">
  38. <el-button icon="el-icon-minus" size="small" @click="changeScale(-1)"></el-button>
  39. </el-col>
  40. <el-col :lg="{ span: 1, offset: 1 }" :md="2">
  41. <el-button icon="el-icon-refresh-left" size="small" @click="rotateLeft()"></el-button>
  42. </el-col>
  43. <el-col :lg="{ span: 1, offset: 1 }" :md="2">
  44. <el-button icon="el-icon-refresh-right" size="small" @click="rotateRight()"></el-button>
  45. </el-col>
  46. <el-col :lg="{ span: 2, offset: 6 }" :md="2">
  47. <el-button type="primary" size="small" @click="uploadImg()">提 交</el-button>
  48. </el-col>
  49. </el-row>
  50. </el-dialog>
  51. </div>
  52. </template>
  53. <script>
  54. import upload from '@/utils/oss.js'
  55. import { VueCropper } from 'vue-cropper'
  56. export default {
  57. dicts: ['sys_upload_img'],
  58. components: { VueCropper },
  59. props: {
  60. title: {
  61. type: String,
  62. default: '上传图片'
  63. }
  64. },
  65. data() {
  66. return {
  67. // 是否显示弹出层
  68. open: false,
  69. // 是否显示cropper
  70. visible: false,
  71. options: {
  72. img: null, //裁剪图片的地址
  73. autoCrop: true, // 是否默认生成截图框
  74. autoCropWidth: 200, // 默认生成截图框宽度
  75. autoCropHeight: 200, // 默认生成截图框高度
  76. fixedBox: true // 固定截图框大小 不允许改变
  77. },
  78. previews: {}
  79. }
  80. },
  81. methods: {
  82. // 编辑头像
  83. init(imgUrl) {
  84. this.options.img = this.$store.state.oss.ossBaseUrl + imgUrl
  85. this.open = true
  86. },
  87. // 打开弹出层结束时的回调
  88. modalOpened() {
  89. this.visible = true
  90. },
  91. // 覆盖默认的上传行为
  92. requestUpload() {},
  93. // 向左旋转
  94. rotateLeft() {
  95. this.$refs.cropper.rotateLeft()
  96. },
  97. // 向右旋转
  98. rotateRight() {
  99. this.$refs.cropper.rotateRight()
  100. },
  101. // 图片缩放
  102. changeScale(num) {
  103. num = num || 1
  104. this.$refs.cropper.changeScale(num)
  105. },
  106. // 上传预处理
  107. beforeUpload(file) {
  108. if (file.type.indexOf('image/') == -1) {
  109. this.$modal.msgError('文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。')
  110. } else {
  111. const reader = new FileReader()
  112. reader.readAsDataURL(file)
  113. reader.onload = () => {
  114. this.options.img = reader.result
  115. }
  116. }
  117. },
  118. // 上传图片
  119. uploadImg() {
  120. this.$refs.cropper.getCropBlob(data => {
  121. upload(data, this.dict.type.sys_upload_img.find(item => item.value === 'avatar').label).then(res => {
  122. this.$emit('saveUrl', res.url)
  123. this.open = false
  124. })
  125. })
  126. },
  127. // 实时预览
  128. realTime(data) {
  129. this.previews = data
  130. },
  131. // 关闭窗口
  132. closeDialog() {
  133. this.options.img = null
  134. this.visible = false
  135. }
  136. }
  137. }
  138. </script>
  139. <style scoped lang="scss">
  140. .user-info-head {
  141. position: relative;
  142. display: inline-block;
  143. height: 120px;
  144. }
  145. .user-info-head:hover:after {
  146. content: '+';
  147. position: absolute;
  148. left: 0;
  149. right: 0;
  150. top: 0;
  151. bottom: 0;
  152. color: #eee;
  153. background: rgba(0, 0, 0, 0.5);
  154. font-size: 24px;
  155. font-style: normal;
  156. -webkit-font-smoothing: antialiased;
  157. -moz-osx-font-smoothing: grayscale;
  158. cursor: pointer;
  159. line-height: 110px;
  160. border-radius: 50%;
  161. }
  162. </style>