userAvatar.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div>
  3. <img v-bind:src="options.img" @click="editCropper()" title="点击上传头像" class="img-circle img-lg" />
  4. <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
  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. />
  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 store from "@/store";
  55. import { VueCropper } from "vue-cropper";
  56. import { uploadAvatar } from "@/api/system/user";
  57. export default {
  58. components: { VueCropper },
  59. props: {
  60. user: {
  61. type: Object
  62. }
  63. },
  64. data() {
  65. return {
  66. // 是否显示弹出层
  67. open: false,
  68. // 弹出层标题
  69. title: "修改头像",
  70. options: {
  71. img: store.getters.avatar, //裁剪图片的地址
  72. autoCrop: true, // 是否默认生成截图框
  73. autoCropWidth: 200, // 默认生成截图框宽度
  74. autoCropHeight: 200, // 默认生成截图框高度
  75. fixedBox: true // 固定截图框大小 不允许改变
  76. },
  77. previews: {}
  78. };
  79. },
  80. methods: {
  81. // 编辑头像
  82. editCropper() {
  83. this.open = true;
  84. },
  85. // 覆盖默认的上传行为
  86. requestUpload() {
  87. },
  88. // 向左旋转
  89. rotateLeft() {
  90. this.$refs.cropper.rotateLeft();
  91. },
  92. // 向右旋转
  93. rotateRight() {
  94. this.$refs.cropper.rotateRight();
  95. },
  96. // 图片缩放
  97. changeScale(num) {
  98. num = num || 1;
  99. this.$refs.cropper.changeScale(num);
  100. },
  101. // 上传预处理
  102. beforeUpload(file) {
  103. if (file.type.indexOf("image/") == -1) {
  104. this.msgError("文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。");
  105. } else {
  106. const reader = new FileReader();
  107. reader.readAsDataURL(file);
  108. reader.onload = () => {
  109. this.options.img = reader.result;
  110. };
  111. }
  112. },
  113. // 上传图片
  114. uploadImg() {
  115. this.$refs.cropper.getCropBlob(data => {
  116. let formData = new FormData();
  117. formData.append("avatarfile", data);
  118. uploadAvatar(formData).then(response => {
  119. if (response.code === 200) {
  120. this.open = false;
  121. this.options.img = process.env.VUE_APP_BASE_API + response.imgUrl;
  122. this.msgSuccess("修改成功");
  123. }
  124. this.$refs.cropper.clearCrop();
  125. });
  126. });
  127. },
  128. // 实时预览
  129. realTime(data) {
  130. this.previews = data;
  131. }
  132. }
  133. };
  134. </script>