userAvatar.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 justify="center">
  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-row>
  21. <br />
  22. <div class="avatar-actions">
  23. <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
  24. <el-button>
  25. 选择
  26. <el-icon class="el-icon--right">
  27. <Upload />
  28. </el-icon>
  29. </el-button>
  30. </el-upload>
  31. <el-button icon="Plus" @click="changeScale(1)"></el-button>
  32. <el-button icon="Minus" @click="changeScale(-1)"></el-button>
  33. <el-button icon="RefreshLeft" @click="rotateLeft()"></el-button>
  34. <el-button icon="RefreshRight" @click="rotateRight()"></el-button>
  35. <el-button type="primary" @click="uploadImg()">提 交</el-button>
  36. </div>
  37. </el-dialog>
  38. </div>
  39. </template>
  40. <script setup lang="ts">
  41. import 'vue-cropper/dist/index.css';
  42. import { VueCropper } from 'vue-cropper';
  43. import { uploadAvatar } from '@/api/system/user';
  44. import { useUserStore } from '@/store/modules/user';
  45. import { UploadRawFile } from 'element-plus';
  46. interface Options {
  47. img: string | any; // 裁剪图片的地址
  48. autoCrop: boolean; // 是否默认生成截图框
  49. autoCropWidth: number; // 默认生成截图框宽度
  50. autoCropHeight: number; // 默认生成截图框高度
  51. fixedBox: boolean; // 固定截图框大小 不允许改变
  52. fileName: string;
  53. previews: any; // 预览数据
  54. outputType: string;
  55. visible: boolean;
  56. }
  57. const userStore = useUserStore();
  58. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  59. const open = ref(false);
  60. const visible = ref(false);
  61. const title = ref('修改头像');
  62. const cropper = ref<any>({});
  63. //图片裁剪数据
  64. const options = reactive<Options>({
  65. img: userStore.avatarUrl || userStore.avatar,
  66. autoCrop: true,
  67. autoCropWidth: 200,
  68. autoCropHeight: 200,
  69. fixedBox: true,
  70. outputType: 'png',
  71. fileName: 'avatar.png',
  72. previews: {},
  73. visible: false
  74. });
  75. /** 编辑头像 */
  76. const editCropper = () => {
  77. open.value = true;
  78. };
  79. /** 打开弹出层结束时的回调 */
  80. const modalOpened = () => {
  81. visible.value = true;
  82. };
  83. /** 覆盖默认上传行为 */
  84. const requestUpload = (): any => {};
  85. /** 向左旋转 */
  86. const rotateLeft = () => {
  87. cropper.value.rotateLeft();
  88. };
  89. /** 向右旋转 */
  90. const rotateRight = () => {
  91. cropper.value.rotateRight();
  92. };
  93. /** 图片缩放 */
  94. const changeScale = (num: number) => {
  95. num = num || 1;
  96. cropper.value.changeScale(num);
  97. };
  98. /** 上传预处理 */
  99. const beforeUpload = (file: UploadRawFile): any => {
  100. if (file.type.indexOf('image/') == -1) {
  101. proxy?.$modal.msgError('文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。');
  102. } else {
  103. const reader = new FileReader();
  104. reader.readAsDataURL(file);
  105. reader.onload = () => {
  106. options.img = reader.result;
  107. options.fileName = file.name;
  108. };
  109. }
  110. };
  111. /** 上传图片 */
  112. const uploadImg = async () => {
  113. cropper.value.getCropBlob(async (data: any) => {
  114. try {
  115. const formData = new FormData();
  116. const fileName = options.fileName || `avatar.${options.outputType || 'png'}`;
  117. formData.append('avatarfile', data, fileName);
  118. const res = await uploadAvatar(formData);
  119. await userStore.getInfo(); // 刷新用户信息以获取最新的oss_id
  120. options.img = res?.data?.imgUrl || userStore.avatarUrl || userStore.avatar;
  121. open.value = false;
  122. proxy?.$modal.msgSuccess('修改成功');
  123. } catch (error) {
  124. proxy?.$modal.msgError('上传失败,请重试');
  125. } finally {
  126. visible.value = false;
  127. }
  128. });
  129. };
  130. /** 实时预览 */
  131. const realTime = (data: any) => {
  132. options.previews = data;
  133. };
  134. /** 关闭窗口 */
  135. const closeDialog = () => {
  136. options.img = userStore.avatarUrl || userStore.avatar;
  137. options.visible = false;
  138. };
  139. </script>
  140. <style lang="scss" scoped>
  141. .user-info-head {
  142. position: relative;
  143. display: inline-block;
  144. height: 120px;
  145. }
  146. .user-info-head:hover:after {
  147. content: '+';
  148. position: absolute;
  149. left: 0;
  150. right: 0;
  151. top: 0;
  152. bottom: 0;
  153. color: #eee;
  154. background: rgba(0, 0, 0, 0.5);
  155. font-size: 24px;
  156. font-style: normal;
  157. -webkit-font-smoothing: antialiased;
  158. -moz-osx-font-smoothing: grayscale;
  159. cursor: pointer;
  160. line-height: 110px;
  161. border-radius: 50%;
  162. }
  163. .avatar-actions {
  164. display: flex;
  165. flex-wrap: wrap;
  166. justify-content: center;
  167. align-items: center;
  168. gap: 12px;
  169. }
  170. </style>