Browse Source

🚀 优化头像上传

master
wangxiang 2 years ago
parent
commit
aa22dd6091
  1. 34
      src/views/system/user/userInfo/index.vue

34
src/views/system/user/userInfo/index.vue

@ -79,8 +79,7 @@
<BasicForm v-show="state.currentCardKey === 'baseInfo'" @register="registerForm"/> <BasicForm v-show="state.currentCardKey === 'baseInfo'" @register="registerForm"/>
<AForm v-show="state.currentCardKey === 'uploadAvatar'" <AForm v-show="state.currentCardKey === 'uploadAvatar'"
ref="formElRef" ref="formElRef"
:labelCol="{ style: { width: '120px' } }" class="upload-avatar-form"
:wrapperCol="{ style: { width: '100%' } }"
:model="state.userInfo" :model="state.userInfo"
:scrollToFirstError="true" :scrollToFirstError="true"
@keypress.enter="handleSubmit" @keypress.enter="handleSubmit"
@ -90,8 +89,9 @@
class="avatar-uploader" class="avatar-uploader"
accept="image/*" accept="image/*"
:multiple="false" :multiple="false"
:customRequest="handleUploadAvatar"
:show-upload-list="false" :show-upload-list="false"
:before-upload="beforeUpload" :before-upload="handleBeforeUpload"
> >
<img v-if="state.imageUrl" :src="state.imageUrl"> <img v-if="state.imageUrl" :src="state.imageUrl">
<div v-else> <div v-else>
@ -101,8 +101,11 @@
</div> </div>
</AUpload> </AUpload>
</AFormItem> </AFormItem>
<AFormItem> <AFormItem :wrapperCol="{ style: { 'text-align': 'left' } }">
<a-button type="primary">保存</a-button> <a-button preIcon="fa-regular:save"
:loading="state.uploadAvatarBtnLoading"
type="primary"
>保存</a-button>
</AFormItem> </AFormItem>
</AForm> </AForm>
</ACard> </ACard>
@ -130,6 +133,7 @@
interface InfoState { interface InfoState {
currentCardKey: string; currentCardKey: string;
baseInfoBtnLoading: boolean; baseInfoBtnLoading: boolean;
uploadAvatarBtnLoading: boolean;
uploadAvatarLoading: boolean; uploadAvatarLoading: boolean;
userInfo: User | any; userInfo: User | any;
imageUrl?: string; imageUrl?: string;
@ -152,6 +156,7 @@
currentCardKey: 'baseInfo', currentCardKey: 'baseInfo',
baseInfoBtnLoading: false, baseInfoBtnLoading: false,
uploadAvatarLoading: false, uploadAvatarLoading: false,
uploadAvatarBtnLoading: false,
userInfo: undefined, userInfo: undefined,
imageUrl: undefined, imageUrl: undefined,
tabList: [ tabList: [
@ -217,19 +222,20 @@
'font-size': '14px' 'font-size': '14px'
})); }));
const beforeUpload = async (file: File) => { function handleBeforeUpload (file: File) {
const isJpgOrPng = file.type?.includes('image/'), const isJpgOrPng = file.type?.includes('image/'),
isLt2M = file.size / 1024 / 1024 < 10; isLt2M = file.size / 1024 / 1024 < 10;
!isJpgOrPng && createMessage.error('您只能上传 image/* 格式的文件!'); !isJpgOrPng && createMessage.error('您只能上传 image/* 格式的文件!');
!isLt2M && createMessage.error('图片必须小于 10MB!'); !isLt2M && createMessage.error('图片必须小于 10MB!');
return isJpgOrPng && isLt2M;
}
// async function handleUploadAvatar(file) {
if (isJpgOrPng && isLt2M) {
try { try {
state.uploadAvatarLoading = true; state.uploadAvatarLoading = true;
const { data } = await commonUpload({ const { data } = await commonUpload({
file: file, file: file.file,
name: 'file', name: 'file',
}, (progressEvent: ProgressEvent) => { }, (progressEvent: ProgressEvent) => {
const complete = ((progressEvent.loaded / progressEvent.total) * 100) | 0; const complete = ((progressEvent.loaded / progressEvent.total) * 100) | 0;
@ -240,8 +246,6 @@
state.uploadAvatarLoading = false; state.uploadAvatarLoading = false;
} }
} }
return false;
};
</script> </script>
@ -284,14 +288,18 @@
margin: 10px 10px 10px 0px; margin: 10px 10px 10px 0px;
::v-deep(.avatar-uploader) > .ant-upload { ::v-deep(.avatar-uploader) > .ant-upload {
width: 128px; width: 180px;
height: 128px; height: 180px;
} }
::v-deep(.ant-upload-select-picture-card .ant-upload-text) { ::v-deep(.ant-upload-select-picture-card .ant-upload-text) {
margin-top: 8px; margin-top: 8px;
color: #666; color: #666;
} }
::v-deep(.upload-avatar-form) > .ant-row {
margin-bottom: 0px;
}
} }
</style> </style>

Loading…
Cancel
Save