userAvatar.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <div>
  3. <div class="user-info-head" @click="editCropper()">
  4. <img v-if="options.img" :src="options.img" class="img-circle img-lg" />
  5. <img v-if="!options.img" src="@/assets/ZDimages/tx_cion.png" class="img-circle img-lg" />
  6. </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. export default {
  62. components: { VueCropper },
  63. props: {
  64. user: {
  65. type: Object
  66. }
  67. },
  68. data() {
  69. return {
  70. // 是否显示弹出层
  71. open: false,
  72. // 是否显示cropper
  73. visible: false,
  74. // 弹出层标题
  75. title: "修改头像",
  76. options: {
  77. img: store.getters.avatar, //裁剪图片的地址
  78. autoCrop: true, // 是否默认生成截图框
  79. autoCropWidth: 200, // 默认生成截图框宽度
  80. autoCropHeight: 200, // 默认生成截图框高度
  81. fixedBox: true // 固定截图框大小 不允许改变
  82. },
  83. previews: {}
  84. };
  85. },
  86. methods: {
  87. // 编辑头像
  88. editCropper() {
  89. this.open = true;
  90. },
  91. // 打开弹出层结束时的回调
  92. modalOpened() {
  93. this.visible = true;
  94. },
  95. // 覆盖默认的上传行为
  96. requestUpload() {
  97. },
  98. // 向左旋转
  99. rotateLeft() {
  100. this.$refs.cropper.rotateLeft();
  101. },
  102. // 向右旋转
  103. rotateRight() {
  104. this.$refs.cropper.rotateRight();
  105. },
  106. // 图片缩放
  107. changeScale(num) {
  108. num = num || 1;
  109. this.$refs.cropper.changeScale(num);
  110. },
  111. // 上传预处理
  112. beforeUpload(file) {
  113. if (file.type.indexOf("image/") == -1) {
  114. this.msgError("文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。");
  115. } else {
  116. const reader = new FileReader();
  117. reader.readAsDataURL(file);
  118. reader.onload = () => {
  119. this.options.img = reader.result;
  120. };
  121. }
  122. },
  123. // 上传图片
  124. uploadImg() {
  125. if(!this.options.img){
  126. this.msgError("请先选择图片")
  127. return
  128. }
  129. this.$refs.cropper.getCropBlob(data => {
  130. const file = new File([data], 'userImg.jpg', { type: 'image/jpeg', lastModified: Date.now() });
  131. let formData = new FormData();
  132. formData.append("avatarfile", file);
  133. uploadAvatar(formData).then(response => {
  134. this.open = false;
  135. this.options.img = response.imgUrl;
  136. store.commit('SET_AVATAR', this.options.img);
  137. this.msgSuccess("修改成功");
  138. this.$parent.getUser();
  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: 130px;
  160. width:130px;
  161. }
  162. .user-info-head img{
  163. width:130px;
  164. height:130px;
  165. }
  166. .user-info-head:hover:after {
  167. content: '请上传证件照片';
  168. position: absolute;
  169. left: 0;
  170. right: 0;
  171. top: 0;
  172. bottom: 0;
  173. color: #eee;
  174. background: rgba(0, 0, 0, 0.5);
  175. font-size: 12px;
  176. text-align: center;
  177. font-style: normal;
  178. -webkit-font-smoothing: antialiased;
  179. -moz-osx-font-smoothing: grayscale;
  180. cursor: pointer;
  181. line-height: 130px;
  182. border-radius: 50%;
  183. }
  184. </style>