userAvatar.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <div>
  3. <div class="user-info-head" @click="editCropper()">
  4. <img v-bind:src="imgRequest + options.img" title="点击上传头像" class="img-circle img-lg" />
  5. </div>
  6. <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @opened="modalOpened">
  7. <el-row>
  8. <el-col :xs="24" :md="12" :style="{height: '350px'}">
  9. <vue-cropper
  10. ref="cropper"
  11. :img="options.img"
  12. :info="true"
  13. :autoCrop="options.autoCrop"
  14. :autoCropWidth="options.autoCropWidth"
  15. :autoCropHeight="options.autoCropHeight"
  16. :fixedBox="options.fixedBox"
  17. @realTime="realTime"
  18. v-if="visible"
  19. />
  20. </el-col>
  21. <el-col :xs="24" :md="12" :style="{height: '350px'}">
  22. <div class="avatar-upload-preview">
  23. <img :src="previews.url" :style="previews.img" />
  24. </div>
  25. </el-col>
  26. </el-row>
  27. <br />
  28. <el-row>
  29. <el-col :lg="2" :md="2">
  30. <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
  31. <el-button size="small">
  32. 上传
  33. <i class="el-icon-upload el-icon--right"></i>
  34. </el-button>
  35. </el-upload>
  36. </el-col>
  37. <el-col :lg="{span: 1, offset: 2}" :md="2">
  38. <el-button icon="el-icon-plus" 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-minus" size="small" @click="changeScale(-1)"></el-button>
  42. </el-col>
  43. <el-col :lg="{span: 1, offset: 1}" :md="2">
  44. <el-button icon="el-icon-refresh-left" size="small" @click="rotateLeft()"></el-button>
  45. </el-col>
  46. <el-col :lg="{span: 1, offset: 1}" :md="2">
  47. <el-button icon="el-icon-refresh-right" size="small" @click="rotateRight()"></el-button>
  48. </el-col>
  49. <el-col :lg="{span: 2, offset: 6}" :md="2">
  50. <el-button type="primary" size="small" @click="uploadImg()">提 交</el-button>
  51. </el-col>
  52. </el-row>
  53. </el-dialog>
  54. </div>
  55. </template>
  56. <script>
  57. import store from "@/store";
  58. import { VueCropper } from "vue-cropper";
  59. import { uploadAvatar } from "@/api/system/user";
  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. },
  82. previews: {}
  83. };
  84. },
  85. methods: {
  86. // 编辑头像
  87. editCropper() {
  88. this.open = true;
  89. },
  90. // 打开弹出层结束时的回调
  91. modalOpened() {
  92. this.visible = true;
  93. },
  94. // 覆盖默认的上传行为
  95. requestUpload() {
  96. },
  97. // 向左旋转
  98. rotateLeft() {
  99. this.$refs.cropper.rotateLeft();
  100. },
  101. // 向右旋转
  102. rotateRight() {
  103. this.$refs.cropper.rotateRight();
  104. },
  105. // 图片缩放
  106. changeScale(num) {
  107. num = num || 1;
  108. this.$refs.cropper.changeScale(num);
  109. },
  110. // 上传预处理
  111. beforeUpload(file) {
  112. if (file.type.indexOf("image/") == -1) {
  113. this.msgError("文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。");
  114. } else {
  115. const reader = new FileReader();
  116. reader.readAsDataURL(file);
  117. reader.onload = () => {
  118. this.options.img = reader.result;
  119. };
  120. }
  121. },
  122. // 上传图片
  123. uploadImg() {
  124. this.$refs.cropper.getCropBlob(data => {
  125. let formData = new FormData();
  126. formData.append("avatarfile", data);
  127. uploadAvatar(formData).then(response => {
  128. this.open = false;
  129. this.options.img = response.imgUrl;
  130. store.commit('SET_AVATAR', this.options.img);
  131. this.msgSuccess("修改成功");
  132. this.visible = false;
  133. });
  134. });
  135. },
  136. // 实时预览
  137. realTime(data) {
  138. this.previews = data;
  139. }
  140. }
  141. };
  142. </script>
  143. <style scoped lang="scss">
  144. .user-info-head {
  145. position: relative;
  146. display: inline-block;
  147. }
  148. .user-info-head:hover:after {
  149. content: '+';
  150. position: absolute;
  151. left: 0;
  152. right: 0;
  153. top: 0;
  154. bottom: 0;
  155. color: #eee;
  156. background: rgba(0, 0, 0, 0.5);
  157. font-size: 24px;
  158. font-style: normal;
  159. -webkit-font-smoothing: antialiased;
  160. -moz-osx-font-smoothing: grayscale;
  161. cursor: pointer;
  162. line-height: 110px;
  163. border-radius: 50%;
  164. }
  165. </style>