userAvatar.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. interface Options {
  63. img: string | any; // 裁剪图片的地址
  64. autoCrop: boolean; // 是否默认生成截图框
  65. autoCropWidth: number; // 默认生成截图框宽度
  66. autoCropHeight: number; // 默认生成截图框高度
  67. fixedBox: boolean; // 固定截图框大小 不允许改变
  68. fileName: string;
  69. previews: any; // 预览数据
  70. outputType: string;
  71. visible: boolean;
  72. }
  73. const userStore = useUserStore();
  74. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  75. const open = ref(false);
  76. const visible = ref(false);
  77. const title = ref('修改头像');
  78. const cropper = ref<any>({});
  79. //图片裁剪数据
  80. const options = reactive<Options>({
  81. img: userStore.avatar,
  82. autoCrop: true,
  83. autoCropWidth: 200,
  84. autoCropHeight: 200,
  85. fixedBox: true,
  86. outputType: 'png',
  87. fileName: '',
  88. previews: {},
  89. visible: false
  90. });
  91. /** 编辑头像 */
  92. const editCropper = () => {
  93. open.value = true;
  94. };
  95. /** 打开弹出层结束时的回调 */
  96. const modalOpened = () => {
  97. visible.value = true;
  98. };
  99. /** 覆盖默认上传行为 */
  100. const requestUpload = (): any => {};
  101. /** 向左旋转 */
  102. const rotateLeft = () => {
  103. cropper.value.rotateLeft();
  104. };
  105. /** 向右旋转 */
  106. const rotateRight = () => {
  107. cropper.value.rotateRight();
  108. };
  109. /** 图片缩放 */
  110. const changeScale = (num: number) => {
  111. num = num || 1;
  112. cropper.value.changeScale(num);
  113. };
  114. /** 上传预处理 */
  115. const beforeUpload = (file: any) => {
  116. if (file.type.indexOf('image/') == -1) {
  117. proxy?.$modal.msgError('文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。');
  118. } else {
  119. const reader = new FileReader();
  120. reader.readAsDataURL(file);
  121. reader.onload = () => {
  122. options.img = reader.result;
  123. options.fileName = file.name;
  124. };
  125. }
  126. };
  127. /** 上传图片 */
  128. const uploadImg = async () => {
  129. cropper.value.getCropBlob(async (data: any) => {
  130. let formData = new FormData();
  131. formData.append('avatarfile', data, options.fileName);
  132. const res = await uploadAvatar(formData);
  133. open.value = false;
  134. options.img = res.data.imgUrl;
  135. userStore.setAvatar(options.img as string);
  136. proxy?.$modal.msgSuccess('修改成功');
  137. visible.value = false;
  138. });
  139. };
  140. /** 实时预览 */
  141. const realTime = (data: any) => {
  142. options.previews = data;
  143. };
  144. /** 关闭窗口 */
  145. const closeDialog = () => {
  146. options.img = userStore.avatar;
  147. options.visible = false;
  148. };
  149. </script>
  150. <style lang="scss" scoped>
  151. .user-info-head {
  152. position: relative;
  153. display: inline-block;
  154. height: 120px;
  155. }
  156. .user-info-head:hover:after {
  157. content: '+';
  158. position: absolute;
  159. left: 0;
  160. right: 0;
  161. top: 0;
  162. bottom: 0;
  163. color: #eee;
  164. background: rgba(0, 0, 0, 0.5);
  165. font-size: 24px;
  166. font-style: normal;
  167. -webkit-font-smoothing: antialiased;
  168. -moz-osx-font-smoothing: grayscale;
  169. cursor: pointer;
  170. line-height: 110px;
  171. border-radius: 50%;
  172. }
  173. </style>