UploadAvatar.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <div class="head-img p-rtv c-s-p" @click="editCropper()">
  3. <slot>
  4. <HAvatar :size="82" :src="(options.img as string)" :name="options.name" :defSrc="defSrc" :fit="fit" :shape="shape"></HAvatar>
  5. <div v-if="isIcon" class="icon-edit">
  6. <el-icon><EditPen /></el-icon>
  7. </div>
  8. </slot>
  9. <el-dialog :title="title" v-model="open" width="800px" custom-class="custom-dialog" append-to-body @opened="modalOpened" @close="closeDialog">
  10. <el-row>
  11. <el-col :xs="24" :md="12" :style="{ height: '350px' }">
  12. <vue-cropper ref="cropper" :img="options.img" :info="true" :autoCrop="options.autoCrop" :maxImgSize="4000" :fixedBox="options.fixedBox" :autoCropWidth="300" :autoCropHeight="300" :full="true" :outputType="options.outputType" @realTime="realTime" v-if="visible" />
  13. </el-col>
  14. <el-col :xs="24" :md="12" class="d-flex j-c a-c" :style="{ height: '350px' }">
  15. <div class="avatar-upload-preview" :class="{ 'square': shapeImg === 'square'}">
  16. <img :src="options.previews.url" :style="options.previews.img" />
  17. </div>
  18. </el-col>
  19. </el-row>
  20. <br />
  21. <el-row>
  22. <el-col :lg="2" :md="2">
  23. <el-upload action="#" :http-request="requestUpload" :show-file-list="false" accept=".png,.jpg,.jpeg,.bmp" :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-col>
  32. <el-col :lg="{ span: 1, offset: 2 }" :md="2">
  33. <el-button icon="Plus" @click="changeScale(1)"></el-button>
  34. </el-col>
  35. <el-col :lg="{ span: 1, offset: 1 }" :md="2">
  36. <el-button icon="Minus" @click="changeScale(-1)"></el-button>
  37. </el-col>
  38. <el-col :lg="{ span: 1, offset: 1 }" :md="2">
  39. <el-button icon="RefreshLeft" @click="rotateLeft()"></el-button>
  40. </el-col>
  41. <el-col :lg="{ span: 1, offset: 1 }" :md="2">
  42. <el-button icon="RefreshRight" @click="rotateRight()"></el-button>
  43. </el-col>
  44. <el-col :lg="{ span: 2, offset: 6 }" :md="2">
  45. <el-button type="primary" @click="uploadImg()">提 交</el-button>
  46. </el-col>
  47. </el-row>
  48. </el-dialog>
  49. </div>
  50. </template>
  51. <script setup lang="ts">
  52. import 'vue-cropper/dist/index.css';
  53. import { VueCropper } from 'vue-cropper';
  54. import useUserStore from '@/store/modules/user';
  55. import { propTypes } from '@/utils/propTypes';
  56. import { uploadFile } from '@/api/system/oss';
  57. interface Options {
  58. img: string | ArrayBuffer | null; // 裁剪图片的地址
  59. autoCrop: boolean; // 是否默认生成截图框
  60. name: string; // 是否默认生成截图框
  61. // autoCropWidth: number; // 默认生成截图框宽度
  62. // autoCropHeight: number; // 默认生成截图框高度
  63. fixedBox: boolean; // 固定截图框大小 不允许改变
  64. fileName: string;
  65. previews: any; // 预览数据
  66. outputType: string;
  67. visible: boolean;
  68. }
  69. const props = defineProps({
  70. title: propTypes.string.def('修改头像'),
  71. modelValue: propTypes.any.def(null),
  72. name: propTypes.string.def(''),
  73. defSrc: propTypes.string.def(''),
  74. isIcon: propTypes.bool.def(true),
  75. fit: propTypes.any.def('cover'),
  76. shape: propTypes.any.def('circle'),
  77. isObject: propTypes.bool.def(false),
  78. shapeImg: propTypes.any.def('circle')
  79. });
  80. const userStore = useUserStore();
  81. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  82. const open = ref(false);
  83. const visible = ref(false);
  84. const cropper = ref<any>({});
  85. //图片裁剪数据
  86. const options = reactive<Options>({
  87. img: '',
  88. name: '',
  89. autoCrop: true,
  90. fixedBox: true,
  91. outputType: 'jpeg',
  92. fileName: '',
  93. previews: {},
  94. visible: false
  95. });
  96. /** 编辑头像 */
  97. const editCropper = () => {
  98. open.value = true;
  99. };
  100. /** 打开弹出层结束时的回调 */
  101. const modalOpened = () => {
  102. visible.value = true;
  103. };
  104. /** 覆盖默认上传行为 */
  105. const requestUpload = (): any => {};
  106. /** 向左旋转 */
  107. const rotateLeft = () => {
  108. cropper.value.rotateLeft();
  109. };
  110. /** 向右旋转 */
  111. const rotateRight = () => {
  112. cropper.value.rotateRight();
  113. };
  114. /** 图片缩放 */
  115. const changeScale = (num: number) => {
  116. num = num || 1;
  117. cropper.value.changeScale(num);
  118. };
  119. /** 上传预处理 */
  120. const beforeUpload = (file: any) => {
  121. if (file.type.indexOf('image/') == -1) {
  122. proxy?.$modal.msgError('文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。');
  123. } else {
  124. const reader = new FileReader();
  125. reader.readAsDataURL(file);
  126. reader.onload = () => {
  127. options.img = reader.result;
  128. // 获取图片后缀名
  129. const suffix = file.name.split('.').pop();
  130. if (suffix === 'png') {
  131. options.outputType = 'png';
  132. } else {
  133. options.outputType = 'jpeg';
  134. }
  135. options.fileName = file.name;
  136. };
  137. }
  138. };
  139. const emit = defineEmits(['update:modelValue', 'change']);
  140. /** 上传图片 */
  141. const uploadImg = async () => {
  142. cropper.value.getCropBlob(async (data: any) => {
  143. let formData = new FormData();
  144. formData.append('file', data, options.fileName);
  145. const res = await uploadFile(formData);
  146. if (!res || res.code !== 200) return;
  147. if (props.isObject) {
  148. emit('update:modelValue', { url: res.data.url, fileName: options.fileName });
  149. emit('change', { url: res.data.url, fileName: options.fileName });
  150. } else {
  151. emit('update:modelValue', res.data.url);
  152. emit('change', res.data.url);
  153. }
  154. open.value = false;
  155. });
  156. };
  157. /** 实时预览 */
  158. const realTime = (data: any) => {
  159. options.previews = data;
  160. };
  161. /** 关闭窗口 */
  162. const closeDialog = () => {
  163. options.visible = false;
  164. };
  165. watch(
  166. () => props.modelValue,
  167. async (val) => {
  168. if (val) {
  169. if (props.isObject) {
  170. options.img = val?.url as string;
  171. options.fileName = val?.fileName as string;
  172. // 获取图片后缀名
  173. const suffix = val?.url?.split('.').pop();
  174. if (suffix === 'png') {
  175. options.outputType = 'png';
  176. } else {
  177. options.outputType = 'jpeg';
  178. }
  179. } else {
  180. options.img = val as string;
  181. const suffix = val?.split('.').pop();
  182. if (suffix === 'png') {
  183. options.outputType = 'png';
  184. } else {
  185. options.outputType = 'jpeg';
  186. }
  187. }
  188. }
  189. },
  190. { deep: true, immediate: true }
  191. );
  192. watch(
  193. () => props.name,
  194. async (val) => {
  195. if (val) {
  196. options.name = val as string;
  197. }
  198. },
  199. { deep: true, immediate: true }
  200. );
  201. </script>
  202. <style lang="scss" scoped>
  203. .user-info-head {
  204. position: relative;
  205. display: inline-block;
  206. height: 120px;
  207. }
  208. .user-info-head:hover:after {
  209. content: '+';
  210. position: absolute;
  211. left: 0;
  212. right: 0;
  213. top: 0;
  214. bottom: 0;
  215. color: #eee;
  216. background: rgba(0, 0, 0, 0.5);
  217. font-size: 24px;
  218. font-style: normal;
  219. -webkit-font-smoothing: antialiased;
  220. -moz-osx-font-smoothing: grayscale;
  221. cursor: pointer;
  222. line-height: 110px;
  223. border-radius: 50%;
  224. }
  225. .icon-edit {
  226. position: absolute;
  227. right: 0;
  228. bottom: 0;
  229. width: 20px;
  230. height: 20px;
  231. color: #fff;
  232. line-height: 20px;
  233. text-align: center;
  234. font-size: 14px;
  235. background: radial-gradient(70% 0% at 0% 0%, #0fd2a8 0%, #01e093 100%), #ffffff;
  236. border-radius: 50%;
  237. }
  238. .avatar-upload-preview.square {
  239. border-radius: 2px;
  240. }
  241. .avatar-upload-preview {
  242. width: 300px;
  243. height: 300px;
  244. border-radius: 50%;
  245. margin: auto;
  246. box-shadow: 0 0 4px #ccc;
  247. overflow: hidden;
  248. }
  249. </style>