上传图片
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { hdHttp, HdResponse,BasicConstant,ExpertData, authStore } from '@itcast/basic'
|
||||
import { hdHttp, HdResponse,BasicConstant,ExpertData, authStore, ChangeUtil } from '@itcast/basic'
|
||||
import HashMap from '@ohos.util.HashMap'
|
||||
import { BusinessError } from '@kit.BasicServicesKit';
|
||||
import { promptAction, router } from '@kit.ArkUI'
|
||||
@@ -123,28 +123,29 @@ export struct EditUserDataComp {
|
||||
this.photoSheetDialog = new CustomDialogController({
|
||||
builder: PhotoActionSheet({
|
||||
controller: this.photoSheetDialog,
|
||||
onPhotoSelected: async (url: string) => {
|
||||
this.photoPath = url;
|
||||
console.log('Selected image URI:', url);
|
||||
try {
|
||||
promptAction.showToast({ message: '正在上传图片...', duration: 2000 });
|
||||
// 调用上传方法,使用明确的返回类型
|
||||
const response = await hdHttp.uploadImage<UploadAvatarResponse>(
|
||||
this.updateDataUrl,
|
||||
url
|
||||
);
|
||||
if (response.code === 1) {
|
||||
promptAction.showToast({ message: '图片上传成功' });
|
||||
console.log('上传成功,返回数据:', response.data);
|
||||
// 处理上传成功后的逻辑
|
||||
// this.currentUser.avatar = response.data.avatarUrl;
|
||||
onPhotoSelected: async (uri: string) => {
|
||||
this.photoPath = uri;
|
||||
console.info('Selected image URI:', uri);
|
||||
const base64String = await ChangeUtil.imageToBase64(uri);
|
||||
const updateDataUrl:string = BasicConstant.urlExpert + 'modify';
|
||||
hdHttp.post<string>(updateDataUrl, {
|
||||
uuid: authStore.getUser().uuid,
|
||||
userName: authStore.getUser().userName,
|
||||
photo: base64String,
|
||||
type:'2'
|
||||
} as updateExtraData).then(async (res: HdResponse<string>) => {
|
||||
let json:callBackData = JSON.parse(res+'') as callBackData;
|
||||
if(json.code == 1 && json.data && typeof json.data === 'object') {
|
||||
authStore.updateUser(json.data)
|
||||
console.log('更新图片成功:', authStore.getUser().email);
|
||||
promptAction.showToast({message:'头像修改成功', duration: 1000})
|
||||
} else {
|
||||
promptAction.showToast({ message: response.message || '图片上传失败' });
|
||||
console.error('更新图片失败:'+json.message)
|
||||
promptAction.showToast({ message: json.message, duration: 1000 })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('图片上传出错:', error);
|
||||
promptAction.showToast({ message: '图片上传出错,请重试' });
|
||||
}
|
||||
}).catch((err: BusinessError) => {
|
||||
console.error(`更新图片请求失败: ${err}`);
|
||||
})
|
||||
}
|
||||
}),
|
||||
alignment: DialogAlignment.Bottom,
|
||||
|
||||
Reference in New Issue
Block a user