appUserAvatar.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <div>
  3. <img v-bind:src="baseUrl+options.img" slot="reference" @click="editCropper()" title="点击上传头像" class="img-lg" />
  4. <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @opened="modalOpened">
  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" :md="2">
  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}" :md="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}" :md="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}" :md="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}" :md="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}" :md="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/appUser";
  58. import { uploadFile } from "@/api/common/common";
  59. export default {
  60. components: { VueCropper },
  61. props: {
  62. personal: {
  63. type: Object
  64. }
  65. },
  66. data() {
  67. return {
  68. baseUrl: null,
  69. // 是否显示弹出层
  70. open: false,
  71. // 是否显示cropper
  72. visible: false,
  73. // 弹出层标题
  74. title: "修改头像",
  75. options: {
  76. img: this.personal.avatar, //裁剪图片的地址
  77. autoCrop: true, // 是否默认生成截图框
  78. autoCropWidth: 200, // 默认生成截图框宽度
  79. autoCropHeight: 200, // 默认生成截图框高度
  80. fixedBox: true // 固定截图框大小 不允许改变
  81. },
  82. previews: {}
  83. };
  84. },
  85. mounted() {
  86. this.baseUrl = process.env.VUE_APP_BASE_API;
  87. // console.log(this.baseUrl)
  88. },
  89. methods: {
  90. // 编辑头像
  91. editCropper() {
  92. this.open = true;
  93. },
  94. // 打开弹出层结束时的回调
  95. modalOpened() {
  96. this.visible = true;
  97. },
  98. // 覆盖默认的上传行为
  99. requestUpload() {
  100. },
  101. // 向左旋转
  102. rotateLeft() {
  103. this.$refs.cropper.rotateLeft();
  104. },
  105. // 向右旋转
  106. rotateRight() {
  107. this.$refs.cropper.rotateRight();
  108. },
  109. // 图片缩放
  110. changeScale(num) {
  111. num = num || 1;
  112. this.$refs.cropper.changeScale(num);
  113. },
  114. // 上传预处理
  115. beforeUpload(file) {
  116. if (file.type.indexOf("image/") == -1) {
  117. this.msgError("文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。");
  118. } else {
  119. const reader = new FileReader();
  120. reader.readAsDataURL(file);
  121. reader.onload = () => {
  122. this.options.img = reader.result;
  123. };
  124. }
  125. },
  126. // 上传图片
  127. uploadImg() {
  128. this.$refs.cropper.getCropBlob(data => {
  129. let formData = new FormData();
  130. formData.append("file", data);
  131. uploadFile(formData).then(response => {
  132. if (response.code === 200) {
  133. this.open = false;
  134. this.options.img = response.fileName;
  135. this.personal.avatar = this.options.img;
  136. this.msgSuccess("上传成功");
  137. }
  138. this.visible = false;
  139. });
  140. });
  141. },
  142. // 实时预览
  143. realTime(data) {
  144. this.previews = data;
  145. }
  146. }
  147. };
  148. </script>
  149. <style scoped>
  150. .avatar-upload-preview {
  151. width: 200px;
  152. height: 200px;
  153. border-radius: 0%
  154. }
  155. </style>