userAvatar.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div class="user-info-head" @click="editCropper()"><img :src="options.img" title="点击上传头像" class="img-circle img-lg" /></div>
  3. <el-dialog :title="title" v-model="open" width="800px" append-to-body @opened="modalOpened" @close="closeDialog">
  4. <el-row>
  5. <el-col :xs="24" :md="12" :style="{height: '350px'}">
  6. <vue-cropper
  7. ref="cropper"
  8. :img="options.img"
  9. :info="true"
  10. :autoCrop="options.autoCrop"
  11. :autoCropWidth="options.autoCropWidth"
  12. :autoCropHeight="options.autoCropHeight"
  13. :fixedBox="options.fixedBox"
  14. :outputType="options.outputType"
  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="options.previews.url" :style="options.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>
  30. 选择
  31. <el-icon class="el-icon--right"><Upload /></el-icon>
  32. </el-button>
  33. </el-upload>
  34. </el-col>
  35. <el-col :lg="{span: 1, offset: 2}" :md="2">
  36. <el-button icon="Plus" @click="changeScale(1)"></el-button>
  37. </el-col>
  38. <el-col :lg="{span: 1, offset: 1}" :md="2">
  39. <el-button icon="Minus" @click="changeScale(-1)"></el-button>
  40. </el-col>
  41. <el-col :lg="{span: 1, offset: 1}" :md="2">
  42. <el-button icon="RefreshLeft" @click="rotateLeft()"></el-button>
  43. </el-col>
  44. <el-col :lg="{span: 1, offset: 1}" :md="2">
  45. <el-button icon="RefreshRight" @click="rotateRight()"></el-button>
  46. </el-col>
  47. <el-col :lg="{span: 2, offset: 6}" :md="2">
  48. <el-button type="primary" @click="uploadImg()">提 交</el-button>
  49. </el-col>
  50. </el-row>
  51. </el-dialog>
  52. </template>
  53. <script setup>
  54. import "vue-cropper/dist/index.css";
  55. import { VueCropper } from "vue-cropper";
  56. import { uploadAvatar } from "@/api/system/user";
  57. import useUserStore from '@/store/modules/user'
  58. const userStore = useUserStore()
  59. const { proxy } = getCurrentInstance();
  60. const open = ref(false);
  61. const visible = ref(false);
  62. const title = ref("修改头像");
  63. //图片裁剪数据
  64. const options = reactive({
  65. img: userStore.avatar, // 裁剪图片的地址
  66. autoCrop: true, // 是否默认生成截图框
  67. autoCropWidth: 200, // 默认生成截图框宽度
  68. autoCropHeight: 200, // 默认生成截图框高度
  69. fixedBox: true, // 固定截图框大小 不允许改变
  70. outputType:"png", // 默认生成截图为PNG格式
  71. previews: {} //预览数据
  72. });
  73. /** 编辑头像 */
  74. function editCropper() {
  75. open.value = true;
  76. };
  77. /** 打开弹出层结束时的回调 */
  78. function modalOpened() {
  79. visible.value = true;
  80. };
  81. /** 覆盖默认上传行为 */
  82. function requestUpload() {
  83. };
  84. /** 向左旋转 */
  85. function rotateLeft() {
  86. proxy.$refs.cropper.rotateLeft();
  87. };
  88. /** 向右旋转 */
  89. function rotateRight() {
  90. proxy.$refs.cropper.rotateRight();
  91. };
  92. /** 图片缩放 */
  93. function changeScale(num) {
  94. num = num || 1;
  95. proxy.$refs.cropper.changeScale(num);
  96. };
  97. /** 上传预处理 */
  98. function beforeUpload(file) {
  99. if (file.type.indexOf("image/") == -1) {
  100. proxy.$modal.msgError("文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。");
  101. } else {
  102. const reader = new FileReader();
  103. reader.readAsDataURL(file);
  104. reader.onload = () => {
  105. options.img = reader.result;
  106. };
  107. }
  108. };
  109. /** 上传图片 */
  110. function uploadImg() {
  111. proxy.$refs.cropper.getCropBlob(data => {
  112. let formData = new FormData();
  113. formData.append("avatarfile", data);
  114. uploadAvatar(formData).then(response => {
  115. open.value = false;
  116. options.img = import.meta.env.VITE_APP_BASE_API + response.imgUrl;
  117. userStore.avatar = options.img;
  118. proxy.$modal.msgSuccess("修改成功");
  119. visible.value = false;
  120. });
  121. });
  122. };
  123. /** 实时预览 */
  124. function realTime(data) {
  125. options.previews = data;
  126. };
  127. /** 关闭窗口 */
  128. function closeDialog() {
  129. options.img = userStore.avatar;
  130. options.visible = false;
  131. };
  132. </script>
  133. <style lang='scss' scoped>
  134. .user-info-head {
  135. position: relative;
  136. display: inline-block;
  137. height: 120px;
  138. }
  139. .user-info-head:hover:after {
  140. content: "+";
  141. position: absolute;
  142. left: 0;
  143. right: 0;
  144. top: 0;
  145. bottom: 0;
  146. color: #eee;
  147. background: rgba(0, 0, 0, 0.5);
  148. font-size: 24px;
  149. font-style: normal;
  150. -webkit-font-smoothing: antialiased;
  151. -moz-osx-font-smoothing: grayscale;
  152. cursor: pointer;
  153. line-height: 110px;
  154. border-radius: 50%;
  155. }
  156. </style>