feat: profile avatar

pull/5/MERGE
xingyu 2023-04-27 15:05:14 +08:00
parent dc98ffb224
commit 513a6052aa
3 changed files with 21 additions and 11 deletions

View File

@ -1,3 +1,4 @@
import { ContentTypeEnum } from '@/enums/httpEnum'
import { defHttp } from '@/utils/http/axios'
export interface ProfileDept {
@ -86,7 +87,15 @@ export function updateUserPwdApi(oldPassword: string, newPassword: string) {
// 用户头像上传
export function uploadAvatarApi(data) {
return defHttp.put({ url: Api.uploadAvatarApi, data: { file: data } })
return defHttp.put({
url: Api.uploadAvatarApi,
headers: {
'Content-type': ContentTypeEnum.FORM_DATA,
// @ts-ignore
ignoreCancelToken: true
},
data
})
}
// 社交绑定,使用 code 授权码

View File

@ -158,16 +158,18 @@ function handlerToolbar(event: string, arg?: number) {
async function handleOk() {
const uploadApi = props.uploadApi
if (uploadApi && isFunction(uploadApi)) {
try {
setModalProps({ confirmLoading: true })
const blob = dataURLtoBlob(previewSource.value)
try {
setModalProps({ confirmLoading: true })
if (uploadApi && isFunction(uploadApi)) {
const result = await uploadApi({ name: 'file', file: blob, filename })
emit('uploadSuccess', { source: previewSource.value, data: result.url })
closeModal()
} finally {
setModalProps({ confirmLoading: false })
} else {
emit('uploadSuccess', { source: previewSource.value, data: blob, filename: filename })
}
closeModal()
} finally {
setModalProps({ confirmLoading: false })
}
}
</script>

View File

@ -9,7 +9,6 @@
<div class="mb-2">头像</div>
<CropperAvatar
:value="avatar"
:uploadApi="uploadAvatarApi as any"
btnText="更换头像"
:btnProps="{ preIcon: 'ant-design:cloud-upload-outlined' }"
@change="updateAvatar"
@ -52,10 +51,10 @@ const avatar = computed(() => {
return avatar || headerImg
})
async function updateAvatar({ src, data }) {
await uploadAvatarApi({ avatarFile: data })
async function updateAvatar({ data }) {
const res = await uploadAvatarApi({ avatarFile: data })
const userinfo = userStore.getUserInfo
userinfo.user.avatar = src
userinfo.user.avatar = res
userStore.setUserInfo(userinfo)
}