userAvatar.vue 5.4 KB

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