userAvatar.vue 5.1 KB

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