userAvatar.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div class="user-info-head" @click="editCropper()">
  3. <img :src="options.img" title="点击上传头像" class="img-circle img-lg" />
  4. <el-dialog v-model="open" :title="title" 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. v-if="visible"
  9. ref="cropper"
  10. :img="options.img"
  11. :info="true"
  12. :auto-crop="options.autoCrop"
  13. :auto-crop-width="options.autoCropWidth"
  14. :auto-crop-height="options.autoCropHeight"
  15. :fixed-box="options.fixedBox"
  16. :output-type="options.outputType"
  17. @real-time="realTime"
  18. />
  19. </el-col>
  20. <el-col :xs="24" :md="12" :style="{ height: '350px' }">
  21. <div class="avatar-upload-preview">
  22. <img :src="options.previews.url" :style="options.previews.img" />
  23. </div>
  24. </el-col>
  25. </el-row>
  26. <br />
  27. <el-row>
  28. <el-col :lg="2" :md="2">
  29. <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
  30. <el-button>
  31. 选择
  32. <el-icon class="el-icon--right">
  33. <Upload />
  34. </el-icon>
  35. </el-button>
  36. </el-upload>
  37. </el-col>
  38. <el-col :lg="{ span: 1, offset: 2 }" :md="2">
  39. <el-button icon="Plus" @click="changeScale(1)"></el-button>
  40. </el-col>
  41. <el-col :lg="{ span: 1, offset: 1 }" :md="2">
  42. <el-button icon="Minus" @click="changeScale(-1)"></el-button>
  43. </el-col>
  44. <el-col :lg="{ span: 1, offset: 1 }" :md="2">
  45. <el-button icon="RefreshLeft" @click="rotateLeft()"></el-button>
  46. </el-col>
  47. <el-col :lg="{ span: 1, offset: 1 }" :md="2">
  48. <el-button icon="RefreshRight" @click="rotateRight()"></el-button>
  49. </el-col>
  50. <el-col :lg="{ span: 2, offset: 6 }" :md="2">
  51. <el-button type="primary" @click="uploadImg()">提 交</el-button>
  52. </el-col>
  53. </el-row>
  54. </el-dialog>
  55. </div>
  56. </template>
  57. <script setup lang="ts">
  58. import 'vue-cropper/dist/index.css';
  59. import { VueCropper } from 'vue-cropper';
  60. import { uploadAvatar } from '@/api/system/user';
  61. import useUserStore from '@/store/modules/user';
  62. import { UploadRawFile } from 'element-plus';
  63. interface Options {
  64. img: string | any; // 裁剪图片的地址
  65. autoCrop: boolean; // 是否默认生成截图框
  66. autoCropWidth: number; // 默认生成截图框宽度
  67. autoCropHeight: number; // 默认生成截图框高度
  68. fixedBox: boolean; // 固定截图框大小 不允许改变
  69. fileName: string;
  70. previews: any; // 预览数据
  71. outputType: string;
  72. visible: boolean;
  73. }
  74. const userStore = useUserStore();
  75. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  76. const open = ref(false);
  77. const visible = ref(false);
  78. const title = ref('修改头像');
  79. const cropper = ref<any>({});
  80. //图片裁剪数据
  81. const options = reactive<Options>({
  82. img: userStore.avatar,
  83. autoCrop: true,
  84. autoCropWidth: 200,
  85. autoCropHeight: 200,
  86. fixedBox: true,
  87. outputType: 'png',
  88. fileName: '',
  89. previews: {},
  90. visible: false
  91. });
  92. /** 编辑头像 */
  93. const editCropper = () => {
  94. open.value = true;
  95. };
  96. /** 打开弹出层结束时的回调 */
  97. const modalOpened = () => {
  98. visible.value = true;
  99. };
  100. /** 覆盖默认上传行为 */
  101. const requestUpload = (): any => {};
  102. /** 向左旋转 */
  103. const rotateLeft = () => {
  104. cropper.value.rotateLeft();
  105. };
  106. /** 向右旋转 */
  107. const rotateRight = () => {
  108. cropper.value.rotateRight();
  109. };
  110. /** 图片缩放 */
  111. const changeScale = (num: number) => {
  112. num = num || 1;
  113. cropper.value.changeScale(num);
  114. };
  115. /** 上传预处理 */
  116. const beforeUpload = (file: UploadRawFile): any => {
  117. if (file.type.indexOf('image/') == -1) {
  118. proxy?.$modal.msgError('文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。');
  119. } else {
  120. const reader = new FileReader();
  121. reader.readAsDataURL(file);
  122. reader.onload = () => {
  123. options.img = reader.result;
  124. options.fileName = file.name;
  125. };
  126. }
  127. };
  128. /** 上传图片 */
  129. const uploadImg = async () => {
  130. cropper.value.getCropBlob(async (data: any) => {
  131. let formData = new FormData();
  132. formData.append('avatarfile', data, options.fileName);
  133. const res = await uploadAvatar(formData);
  134. open.value = false;
  135. options.img = res.data.imgUrl;
  136. userStore.setAvatar(options.img);
  137. proxy?.$modal.msgSuccess('修改成功');
  138. visible.value = false;
  139. });
  140. };
  141. /** 实时预览 */
  142. const realTime = (data: any) => {
  143. options.previews = data;
  144. };
  145. /** 关闭窗口 */
  146. const closeDialog = () => {
  147. options.img = userStore.avatar;
  148. options.visible = false;
  149. };
  150. </script>
  151. <style lang="scss" scoped>
  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>