userAvatar.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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" :close-on-click-modal="false" 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" :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,upload } from "@/api/system/user";
  58. export default {
  59. components: { VueCropper },
  60. props: {
  61. user: {
  62. type: Object
  63. }
  64. },
  65. data() {
  66. return {
  67. // 是否显示弹出层
  68. open: false,
  69. // 是否显示cropper
  70. visible: false,
  71. // 弹出层标题
  72. title: "修改头像",
  73. options: {
  74. img: store.getters.avatar, //裁剪图片的地址
  75. autoCrop: true, // 是否默认生成截图框
  76. autoCropWidth: 200, // 默认生成截图框宽度
  77. autoCropHeight: 200, // 默认生成截图框高度
  78. fixedBox: true // 固定截图框大小 不允许改变
  79. },
  80. previews: {}
  81. };
  82. },
  83. methods: {
  84. // 编辑头像
  85. editCropper() {
  86. this.open = true;
  87. },
  88. // 打开弹出层结束时的回调
  89. modalOpened() {
  90. this.visible = true;
  91. },
  92. // 覆盖默认的上传行为
  93. requestUpload() {
  94. },
  95. // 向左旋转
  96. rotateLeft() {
  97. this.$refs.cropper.rotateLeft();
  98. },
  99. // 向右旋转
  100. rotateRight() {
  101. this.$refs.cropper.rotateRight();
  102. },
  103. // 图片缩放
  104. changeScale(num) {
  105. num = num || 1;
  106. this.$refs.cropper.changeScale(num);
  107. },
  108. // 上传预处理
  109. beforeUpload(file) {
  110. if (file.type.indexOf("image/") == -1) {
  111. this.$modal.msgError("文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。");
  112. } else {
  113. const reader = new FileReader();
  114. reader.readAsDataURL(file);
  115. reader.onload = () => {
  116. this.options.img = reader.result;
  117. };
  118. }
  119. },
  120. // 上传图片
  121. uploadImg() {
  122. this.$refs.cropper.getCropBlob(data => {
  123. // let formData = new FormData();
  124. // formData.append("file", data);
  125. // upload(formData).then(response => {
  126. // this.open = false;
  127. // this.options.img = response.url;
  128. // store.commit('SET_AVATAR', this.options.img);
  129. // this.$modal.msgSuccess("修改成功");
  130. // this.visible = false;
  131. // });
  132. let formData = new FormData();
  133. formData.append("avatarfile", data);
  134. uploadAvatar(formData).then(response => {
  135. this.open = false;
  136. this.options.img = process.env.VUE_APP_BASE_API + response.imgUrl;
  137. store.commit('SET_AVATAR', this.options.img);
  138. this.$modal.msgSuccess("修改成功");
  139. this.visible = false;
  140. });
  141. });
  142. },
  143. // 实时预览
  144. realTime(data) {
  145. this.previews = data;
  146. },
  147. // 关闭窗口
  148. closeDialog() {
  149. this.options.img = store.getters.avatar
  150. this.visible = false;
  151. }
  152. }
  153. };
  154. </script>
  155. <style scoped lang="scss">
  156. .user-info-head {
  157. position: relative;
  158. display: inline-block;
  159. height: 120px;
  160. }
  161. .user-info-head:hover:after {
  162. content: '+';
  163. position: absolute;
  164. left: 0;
  165. right: 0;
  166. top: 0;
  167. bottom: 0;
  168. color: #eee;
  169. background: rgba(0, 0, 0, 0.5);
  170. font-size: 24px;
  171. font-style: normal;
  172. -webkit-font-smoothing: antialiased;
  173. -moz-osx-font-smoothing: grayscale;
  174. cursor: pointer;
  175. line-height: 110px;
  176. border-radius: 50%;
  177. }
  178. </style>