userAvatar.vue 5.4 KB

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